查看文件源代码功能实现

简介:
我们经常遇到,查看文件源代码的功能。Struts 自带的例子是这样的。
 
其中思路是这样的:将文件流转换为List 输出出来。
用到了
第一种:读取Class
InputStream in = getClass().getResourceAsStream(className);
第二种:一般文件
InputStream in = ClassLoaderUtil.getResourceAsStream(page.substring(indexOf+1), getClass()); 

//or 

    in = servletContext.getResourceAsStream(page);
 
第三种:配置文件
new URL(config).openStream()
 
 
/* 
* $Id: ViewSourceAction.java 570518 2007-08-28 18:26:48Z jholmes $ 

* Licensed to the Apache Software Foundation (ASF) under one 
* or more contributor license agreements.    See the NOTICE file 
* distributed with this work for additional information 
* regarding copyright ownership.    The ASF licenses this file 
* to you under the Apache License, Version 2.0 (the 
* "License"); you may not use this file except in compliance 
* with the License.    You may obtain a copy of the License at 

*    http://www.apache.org/licenses/LICENSE-2.0 

* Unless required by applicable law or agreed to in writing, 
* software distributed under the License is distributed on an 
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
* KIND, either express or implied.    See the License for the 
* specific language governing permissions and limitations 
* under the License. 
*/
 
package org.apache.struts2.showcase.source; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.ArrayList; 
import java.util.List; 

import javax.servlet.ServletContext; 
import javax.servlet.http.HttpServletRequest; 

import org.apache.struts2.ServletActionContext; 
import org.apache.struts2.util.ServletContextAware; 

import com.opensymphony.xwork2.ActionSupport; 
import com.opensymphony.xwork2.util.ClassLoaderUtil; 

/** 
* Processes configuration, page, and action class paths to create snippets 
* of the files for display. 
*/
 
public  class ViewSourceAction  extends ActionSupport  implements ServletContextAware { 

         private String page; 
         private String className; 
         private String config; 

         /** 
         * 数据行列表 
         */
 
         private List pageLines; 
         private List classLines; 
         private List configLines; 

         private  int configLine; 
         private  int padding = 10; 

         private ServletContext servletContext; 

         public String execute()  throws MalformedURLException, IOException { 

                 if (page !=  null && page.trim().length() > 0) { 

                         int indexOf = page.indexOf( "//"); 
      InputStream in = ClassLoaderUtil.getResourceAsStream(page.substring(indexOf+1), getClass()); 
                        page = page.replace("//", "/"); 

                        if (in == null) { 
                                in = servletContext.getResourceAsStream(page); 
                                while (in == null && page.indexOf('/', 1) > 0) { 
                                        page = page.substring(page.indexOf('/', 1)); 
                                        in = servletContext.getResourceAsStream(page); 
                                } 
                        } 
                        pageLines = read(in, -1); 

                        if (in != null) { 
                                in.close(); 
                        } 
                } 

                if (className != null && className.trim().length() > 0) { 
                        className = "/"+className.replace('.', '/') + ".java"
                        InputStream in = getClass().getResourceAsStream(className); 
                        if (in == null) { 
                                in = servletContext.getResourceAsStream("/WEB-INF/src"+className); 
                        } 
                        classLines = read(in, -1); 

                        if (in != null) { 
                                in.close(); 
                        } 
                } 

                String rootPath = ServletActionContext.getServletContext().getRealPath("/"); 
                                 
                if (config != null && config.trim().length() > 0 && (rootPath == null || config.startsWith(rootPath))) { 
                        int pos = config.lastIndexOf(':'); 
                        configLine = Integer.parseInt(config.substring(pos+1)); 
                        config = config.substring(0, pos).replace("//", "/"); 
                        configLines = read(new URL(config).openStream(), configLine); 
                } 
                return SUCCESS; 
        } 


        /** 
         * @param className the className to set 
         */
 
        public void setClassName(String className) { 
                this.className = className; 
        } 

        /** 
         * @param config the config to set 
         */
 
        public void setConfig(String config) { 
                this.config = config; 
        } 

        /** 
         * @param page the page to set 
         */
 
        public void setPage(String page) { 
                this.page = page; 
        } 

        /** 
         * @param padding the padding to set 
         */
 
        public void setPadding(int padding) { 
                this.padding = padding; 
        } 



        /** 
         * @return the classLines 
         */
 
        public List getClassLines() { 
                return classLines; 
        } 

        /** 
         * @return the configLines 
         */
 
        public List getConfigLines() { 
                return configLines; 
        } 

        /** 
         * @return the pageLines 
         */
 
        public List getPageLines() { 
                return pageLines; 
        } 

        /** 
         * @return the className 
         */
 
        public String getClassName() { 
                return className; 
        } 

        /** 
         * @return the config 
         */
 
        public String getConfig() { 
                return config; 
        } 

        /** 
         * @return the page 
         */
 
        public String getPage() { 
                return page; 
        } 

        /** 
         * @return the configLine 
         */
 
        public int getConfigLine() { 
                return configLine; 
        } 

        /** 
         * @return the padding 
         */
 
        public int getPadding() { 
                return padding; 
        } 

        /** 
         * Reads in a strea, optionally only including the target line number 
         * and its padding 
         * 
         * @param in The input stream 
         * @param targetLineNumber The target line number, negative to read all 
         * @return A list of lines 
         */
 
        private List read(InputStream in, int targetLineNumber) { 
                List snippet = null
                if (in != null) { 
                        snippet = new ArrayList(); 
                        int startLine = 0; 
                        int endLine = Integer.MAX_VALUE; 
                        if (targetLineNumber > 0) { 
                                startLine = targetLineNumber - padding; 
                                endLine = targetLineNumber + padding; 
                        } 
                        try { 
                                BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 

                                int lineno = 0; 
                                String line; 
                                while ((line = reader.readLine()) != null) { 
                                        lineno++; 
                                        if (lineno >= startLine && lineno <= endLine) { 
                                                snippet.add(line); 
                                        } 
                                } 
                        } catch (Exception ex) { 
                                // ignoring as snippet not available isn't a big deal 
                        } 
                } 
                return snippet; 
        } 

        public void setServletContext(ServletContext arg0) { 
                this.servletContext = arg0; 
        } 



 


本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/855193,如需转载请自行联系原作者
相关文章
|
3月前
|
数据挖掘 开发工具 Python
基于Python开发的企业编码生成系统(源码+可执行程序+程序配置说明书+程序使用说明书)
基于Python开发的企业编码生成系统(源码+可执行程序+程序配置说明书+程序使用说明书)
|
Java 数据安全/隐私保护
在线预览项目kkFileView(二)代码讲解与修改
在线预览项目kkFileView(二)代码讲解与修改
785 0
在线预览项目kkFileView(二)代码讲解与修改
|
3月前
基于Airtest陀螺世界辅助源代码
基于Airtest陀螺世界辅助源代码
20 0
基于Airtest陀螺世界辅助源代码
|
3月前
|
开发工具 数据安全/隐私保护 Python
基于Python开发的图片批量处理器(源码+可执行程序+程序配置说明书+程序使用说明书)
基于Python开发的图片批量处理器(源码+可执行程序+程序配置说明书+程序使用说明书)
|
3月前
|
NoSQL 开发工具 数据库
基于Python开发的学生信息管理系统控制台程序(源码+可执行程序exe文件+程序配置说明书+程序使用说明书)
基于Python开发的学生信息管理系统控制台程序(源码+可执行程序exe文件+程序配置说明书+程序使用说明书)
|
3月前
|
NoSQL 开发工具 数据库
基于Python开发的DIY字符画程序(源码+可执行程序exe文件+程序配置说明书+程序使用说明书)
基于Python开发的DIY字符画程序(源码+可执行程序exe文件+程序配置说明书+程序使用说明书)
|
5月前
|
小程序 前端开发 JavaScript
小程序组件库ColorUI的使用,一键复制粘贴
小程序组件库ColorUI的使用,一键复制粘贴
102 0
|
10月前
tkintertools 模块实战一:任务清单小工具
tkintertools 模块实战一:任务清单小工具
77 0
|
10月前
代码对比工具
代码对比工具
|
11月前
|
Linux 编译器 C语言
Linux驱动开发笔记(一):helloworld驱动源码编写、makefile编写以及驱动编译基本流程
基于linux的驱动开发学习笔记,本篇是描述了一个字符驱动的基础开发流程,以便做嵌入式开发多年的应用或者系统学习驱动开发。