查看文件源代码功能实现

原创作品,允许转载,转载时请务必以超链接形式标明文章  原始出处 、作者信息和本声明。否则将追究法律责任。 http://dba10g.blog.51cto.com/764602/855193
我们经常遇到,查看文件源代码的功能。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; 
        } 



 

本文出自 “简单” 博客,请务必保留此出处http://dba10g.blog.51cto.com/764602/855193

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PinXin工作室,PinXinToolV1.05.12版 (总说明) 版权所有(C) 2010.05.12 pinxin 保留所有权利 本程序为cad应用软件,您可以任意复制传播本软件,但必须保证它的完整性。 作者对本软件不提供任何保证,不对因使用本软件引起的损害事件承担任何责任。 本软件结合了"迷你工具"、"燕秀工具箱"、"常青藤、及"源泉建筑"及一些的lsp程序。 是在迷你建筑工具的基础上融合了燕秀工具箱、常青藤、cad杀毒及本人改写的一些其它lsp文件,本文件不用在用到繁琐安装步骤。 安装步骤: 首先: 下载本程序安装包“PinXinToolV1.05.12.exe”在双击打开安装,点 下一步,下一步,最后完成本辅助工具的安装, 然后: 桌面上会出现一个快捷方式“导入PinXinTool.exe”;在cad没有打开的情况下双击此“快捷方式”按照说明选择相应的选项导入到cad内,此快捷方式在开始菜单的程序组也有相应的“快捷图标”本人建议保留。以后重装cad后点此“快捷方式”就可直接安装本辅助工具了。 在用到软件某命令时,有时会跳出要求注册窗口,这时可先关掉提示窗口,然后键入快捷键“cqt”会出现相应的注册机,再使用刚刚提示注册的那命令,复制“申请码"粘贴在注册机相应的位置获取“注册码”然后复制“注册码”到相应位置注册,关闭对话框,不能用的命令就可以用了。 插件简介: 1、本插件多为lsp编译主导插件(其包括“老迈的迷你工具”、“燕秀工具箱”、等一些小的lsp插件,对外来病毒(恶意修改、及其他破坏)的攻击没有防御能力,所以本工具加入了cad启动查杀病毒功能。经本人的测试有很高的防御能力,大大减低了cad的中毒率。 2、cad常用命令不能用。属于cad中毒现象,解决方法:用cad杀毒软件杀毒。cad杀毒软件有两种一种是直接杀毒需要安装,一种是关闭 CAD杀毒。 3、cad杀毒后还是有命令不能用,还有可能是文件有问题,可以试用pu命令清理文件。然后用命令ap加载app文件夹里的文件 件夹(a.14-pinxin子文件里)里所有文件(注意要添加到内容和历史文件里)。 3、上述办法还不能解决问题,(我也无能无力了~v~!!)只有重装软件了 关于cad绘图习惯本人,给出的几点个人意见: 1、关于块编辑,也就是我们在组块时,名称输入对应的表现内容但是基点一定得选好。选在中心或是有关系的点(对称点、中点等等)。 所组的块(单一块:块最终表现的结果为一个图层内容。混合块:最终表现的内容为多个层的内容。)单一块在块编辑器将所有内容 归为0层。混合块在组块选基点后,将该块归0层。 2、关于图层的管理:图层本公司有相应的图层管理,表现内容要与层相对应,颜色应该随层(有特殊表现的内容可作相应调整)。 3、本人自己汇总的cad快捷键及快捷键表也已经放在(《PinXinToolV1.05.12程序》子目录里的 “pinxin文件夹内”的 CAD命令表.xls)习惯为个人使用习惯. 各插件拥有者简介: 1. 燕秀工具箱: 姓名: 赖心秀 籍贯: 江西大余 出生: 1984 性别: 男 电话: 15970010188 , 13058182801 QQ: 446174853 E-Mail: yanxiucad@163.com Blog: http://yanxiucad.bokee.com 燕秀CAX模具技术论坛: www.yxcax.com “ (我很佩服的,虽然我们专业不同,模具界的传奇人物。搞模具不认识他,我觉得可惜。搞模具的可以去单独下“载燕秀工具箱”用) 2. 源泉建筑: YQArch (源泉建筑) 版权属于: Walter Lam & Mai.c.y Email: bigd99◎126.com, h2o99◎21cn.com QQ: 854607353, 575825448 软件下载更新: http://princess.32o.cn/mysoftware http://bigd.5d6d.com (论坛) “源泉建筑”为免费软件,您可以使用、复制、传播本软件,但请勿修改或反编译其核心运行程序文件 yqarch.vlx,以维护知识产权。未经作者同意不能将本软件用于其他商业用途。作者对使用本程序所引起的一切后果不承担任何责任。 (功能强大的专业软件) 3.免费迷你建筑工具 版权属于Mai.C.Y, 请到下列地址下载免费的迷你建筑工具 网络 U盘:http://szmaicy.ys168.com QQ号:509157557 迷你建筑工具QQ群:38158839 AutoCAD 是AUTODESK公司的产品,详http://www.autodesk.com/ ◆ 声明:任何人不得修改 mytool15.vlx 文件及版权声明。每个人均可复制、使用及传播此 工具。本人及工具未声明或隐含任何保证,使用《迷你建筑工具》程序,纯属用户个人的选择, 作者不对此工具程序造成的任何损失负责。 ◆ 本人职业是建筑师,利用空闲的时间编写了这个工具,目的是为了提高自己制图速度。工 具的开发在1993年从 AutoCAD R10平台开始,期间很多人提供了宝贵的意见。在2000年,我将 工具发到的网上,给人免费使用。 ◆ 主要特点:这是一款完全免费、无须注册、没有任何功能限制、也没有时间的限制、功能 类似于天正建筑 3.0版的工具集式AutoCAD 建筑插件,它命令精简、功能强大、使用简单,集 成了同类建筑软件的大部分常用功能,符合一般建筑师的设计和绘图习惯。强大的定制功能, 除了不影响用户的绘图习惯外,还可以很好支持在其他工具集类建筑插件下绘制的图纸文件。 1. 核心程序用 AutoLISP 语言编写,经 Visual Lisp 编译为 VLX文件,支持AutoCAD 2004+ 的中文版和英文版。 2. 绿色软件,不往系统添加任何文件。 3. 除了专门为建筑设计编写了大量功能外,本工具还针对AutoCAD 的弱项,如文字处理、 图库编辑管理、图层管理等,为用户提供了一个非常好的解决方案。 4. 因每个人的缩写命令习惯不一样,本工具不修改缩写命令文件acad.pgp。 5. 操作简单,方便新手入门。本工具的设计出发点是: “屏幕菜单配合命令使用,以点 击菜单为主 ”。点击菜单的优点是不用记住繁琐的命令。ACAD本身的一些功能强大的命令, 如过滤器 filter 和更改文字的Chtext,由于开关繁多和使用复杂,以至用者寥寥。有鉴 于此,本工具的命令尽量简化令人头疼的开关,必不可少的均能在屏幕菜单上找到选项。 方便快捷的屏幕菜单正是本工具的精华之一。充分利用屏幕菜单, 能节省大量画图的时间。 命令mini_menu 或快捷键 CIRL+F1可开或关屏幕菜单。 4.常青藤 是一款收费软件,适合各专业使用,功能强大,值得收纳的软件,(在安装目录里有相应的解密工具) 5.cad杀毒程序等其他小程序,(收集归纳) px05.12版: 插件资源分享站点,大家可以常常光顾本站点,并给一定的意见, 大家有好的建议和资料建议编辑以后上传到本站并加以说明、 大家不要乱改密码及乱删东西, 我们的目标是:共同研究及开发新的程序。 我们的宗旨是:留其精华,去其糟粕。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值