使用正则表达式将JSP中文本标签抽取出来填到PropertyFile中

在java前台程序的开发过程中,经常要把大量JSP中的文本key抽取出来,放到Property文件中,非常麻烦,于是写了这么一个工具。 

这个程序在JSP文件中查找"jsp.xxxxxx"这样的字符串(一行上如果有多个这样的字符串也能找出来),然后填充到一个HashMap中(利用这个字符串做key,这样避免我们过滤出来的字符串有duplicated的问题),最后写到Property文件中,接下来只需要到Property文件中填写value就可以了。 

同样的,对于java文件,也会查找action.xxx, form.xxx, bean.xxx等这样的字符串,然后抽取出来。 

所以说,本工具最关键的地方就是那两个正则表达式。:) 

代码使用了Jakarta的ORO正则表达式库,参考了之前发的一篇来自IBM DeveloperWorks的文章。 

目前已经修正了不少bug,这个工具已经可以用了,实用效果很不错。 


/*
* GenerateProperties.java
*/

package  cn.com.jointforce.common;

import  org.apache.oro.text.regex. * ;
import  java.io. * ;
import  java.util. * ;

/**
*
@author  Eric
*/
public   class  GenerateProperties {
    
/**
     * Creates a new instance of GenerateProperties
     
*/
    
public  GenerateProperties() {
    }
    
    
// ============================================================== Definition BEGIN =================================================================================================== //
    
    
//  jsp file reside directory path
     private  String jsp_directory  =   " D:\\JointForce\\S.EasyCluster-2.0\\InterfaceSrc\\easycluster\\web\\admin\\jsp " ;
    
    
//  java file reside directory path
     private  String java_directory  =   " D:\\JointForce\\S.EasyCluster-2.0\\InterfaceSrc\\easycluster\\src\\cn\\com\\jointforce\\admin " ;
    
    
//  root module property file
     private  String root_property_file  =   " D:\\JointForce\\S.EasyCluster-2.0\\InterfaceSrc\\easycluster\\src\\cn\\com\\jointforce\\root\\resource\\ApplicationResource.properties " ;
    
    
//  portal module property file
     private  String portal_property_file  =   " D:\\JointForce\\S.EasyCluster-2.0\\InterfaceSrc\\easycluster\\src\\cn\\com\\jointforce\\portal\\resource\\ApplicationResource.properties " ;
    
    
//  admin module property file
     private  String admin_property_file  =   " D:\\JointForce\\S.EasyCluster-2.0\\InterfaceSrc\\easycluster\\src\\cn\\com\\jointforce\\admin\\resource\\ApplicationResource.properties " ;
    
    
//  distill resources from jsp, default output property file
     private  String default_property_file  =   " portal " ;   //  portal or admin or root
    
    
//  recursive jsp directory?
     private   boolean  recursive_jsp_directory  =   true ;
    
    
//  recursive java directory
     private   boolean  recursive_java_directory  =   true ;
    
    
//  jsp filter Regular String
    String jsp_restring  =   " ['\ " ](jsp | error)\\.[ ^ ' \"]+[ ' \ " ] " ;
    
    
//  java filter Regular String
    String java_restring  =   " ['\ " ](action | bean | error | form)\\.[ ^ ' \"]+[ ' \ " ] " ;
    
    
//  All filenames container
     private  ArrayList fileContainer  =   new  ArrayList();
    
    
//  root properties container
     private  Properties root_props  =   new  Properties();
    
    
//  portal properties container
     private  Properties portal_props  =   new  Properties();
    
    
//  admin properties container
     private  Properties admin_props  =   new  Properties();
            
    
// ============================================================== Definition END =================================================================================================== //
    
    
/**
     * Set default property files
     
*/
    
public   void  SetDefaultOutputProperty(String value) {
        default_property_file 
=  value;
    }
    
    
/**
     * List files function, using recursion
     
*/
    
private   void  searchFiles(String dir, String jsp_or_java) {
                
        File root 
=   new  File(dir);
        File[] file_array 
=  root.listFiles();
        
        
for  ( int  i = 0 ; i < file_array.length; i ++ ) {
            
if  (file_array[i].isDirectory()) {
                
if  (jsp_or_java.equalsIgnoreCase( " jsp " )) {
                    
if  (recursive_jsp_directory) {
                        searchFiles(file_array[i].getAbsolutePath(), jsp_or_java);
                    }
                } 
else   if  (jsp_or_java.equalsIgnoreCase( " java " )) {
                    
if  (recursive_java_directory) {
                        searchFiles(file_array[i].getAbsolutePath(), jsp_or_java);
                    }
                }
            } 
else  {
                fileContainer.add(file_array[i].getAbsolutePath());
            }
        }
    }
    
    
/**
     * Load root, portal, admin current properties
     
*/
    
private   void  LoadProperties() {
        InputStream root_is 
=   null ;
        InputStream portal_is 
=   null ;
        InputStream admin_is 
=   null ;
        
        
try  {
            
//  Looking for properties file
            root_is  =   new  FileInputStream(root_property_file);
            portal_is 
=   new  FileInputStream(portal_property_file);
            admin_is 
=   new  FileInputStream(admin_property_file);
        
            
if  (root_is  !=   null ) {
                root_props.load(root_is);
            }
            
if  (portal_is  !=   null ) {
                portal_props.load(portal_is);
            }
            
if  (admin_is  !=   null ) {
                admin_props.load(admin_is);
            }
        } 
catch  (IOException e) {
            e.printStackTrace();
        } 
finally  {
            
try  {
                
if  (root_is  !=   null )
                    root_is.close();
                
if  (admin_is  !=   null )
                    admin_is.close();
                
if  (portal_is  !=   null )
                    portal_is.close();
            } 
catch  (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    
/**
     * Process JSP files
     
*/
    
public   void  DistillJSP() {
        fileContainer.clear();
        
        
//  Get all filenames under JSP directory
        searchFiles(jsp_directory,  " jsp " );
        
        
//  Load root, portal, admin current properties
        LoadProperties();
        
        
try  {
            
//  Prepare the Writers
            FileWriter root_props_writer  =   new  FileWriter(root_property_file,  true );
            BufferedWriter root_props_bufwriter 
=   new  BufferedWriter(root_props_writer);
            FileWriter portal_props_writer 
=   new  FileWriter(portal_property_file,  true );
            BufferedWriter portal_props_bufwriter 
=   new  BufferedWriter(portal_props_writer);
            FileWriter admin_props_writer 
=   new  FileWriter(admin_property_file,  true );
            BufferedWriter admin_props_bufwriter 
=   new  BufferedWriter(admin_props_writer);
        
            
//  Iterate the files
             int  file_number  =  fileContainer.size();
            
//  The result string container
            HashMap result_container  =   new  HashMap();
            
            
for  ( int  i = 0 ; i < file_number; i ++ ) {
                
//  Ignore files don't end with `jsp'
                String filename  =  (String)fileContainer.get(i);
                
if  (filename  ==   null   ||  filename.trim().length()  <   1   ||
                    
! filename.substring(filename.length()  -   3 , filename.length()).equalsIgnoreCase( " jsp " )) {
                    
continue ;
                }
                
                
//  Open the JSP File and read out the resources
                FileReader jsp_file_reader  =   new  FileReader(filename);
                BufferedReader buffer_reader 
=   new  BufferedReader(jsp_file_reader);
                String input_line 
=   null ;
                
                PatternCompiler orocom 
=   new  Perl5Compiler();
                Pattern pattern 
=  orocom.compile(jsp_restring);
                PatternMatcher matcher 
=   new  Perl5Matcher();
                
                
while ((input_line  =  buffer_reader.readLine())  !=   null ) {
                    
//  System.out.println("Got one line from jsp file: " + input_line);
                    
                    
//  Begin filter
                    
//  Use PatternMatcherInput class, we can find the next string in one line string
                    
//  Otherwise ORO will find the first match string in one line then ignore the rest string of this line
                    PatternMatcherInput reinput  =   new  PatternMatcherInput(input_line);
                    
while  (matcher.contains(reinput, pattern)) {
                        MatchResult result 
=  matcher.getMatch();
                        String result_string 
=  result.group( 0 );
                        
                        
//  Get rid of the quotations
                        result_string  =  result_string.substring( 1 , result_string.length()  -   1 );
                        
//  if we got the duplicated result_string, HashMap will overcome this issue
                        result_container.put(result_string,  " dummy object " );
                    }
                }
                
                buffer_reader.close();
                jsp_file_reader.close();
            }
            
            
//  Iterate the HashMap and write the result
            Iterator it  =  result_container.keySet().iterator();
            
while  (it.hasNext()) {
                String key 
=  (String)it.next();
                
                
if  (default_property_file.equalsIgnoreCase( " portal " )) {
                    
//  check whether this key has been added?
                     if  (portal_props.getProperty(key)  ==   null ) {
                        System.out.println(
" Found one resource:  "   +  key);
                        portal_props_bufwriter.newLine();
                        portal_props_bufwriter.write(key 
+   " =dummy " );
                        portal_props_bufwriter.newLine();
                    }
                } 
else   if  (default_property_file.equalsIgnoreCase( " admin " )) {
                    
//  check whether this key has been added?
                     if  (admin_props.getProperty(key)  ==   null ) {
                        System.out.println(
" Found one resource:  "   +  key);
                        admin_props_bufwriter.newLine();
                        admin_props_bufwriter.write(key 
+   " =dummy " );
                        admin_props_bufwriter.newLine();
                    }
                } 
else   if  (default_property_file.equalsIgnoreCase( " root " )) {
                    
//  check whether this key has been added?
                     if  (root_props.getProperty(key)  ==   null ) {
                        System.out.println(
" Found one resource:  "   +  key);
                        root_props_bufwriter.newLine();
                        root_props_bufwriter.write(key 
+   " =dummy " );
                        root_props_bufwriter.newLine();
                    }
                }
            }
    
            root_props_bufwriter.close();
            root_props_writer.close();
            admin_props_bufwriter.close();
            admin_props_writer.close();
            portal_props_bufwriter.close();
            portal_props_writer.close();
        } 
catch  (Exception e) {
            e.printStackTrace();
        }
    }
    
    
/**
     * Process JAVA Files
     
*/
    
public   void  DistillJava() {
        fileContainer.clear();
        
        
//  Get all filenames under Java directory
        searchFiles(java_directory,  " java " );
        
        
//  Load root, portal, admin current properties
        LoadProperties();
        
        
try  {
            
//  Prepare the Writers
            FileWriter root_props_writer  =   new  FileWriter(root_property_file,  true );
            BufferedWriter root_props_bufwriter 
=   new  BufferedWriter(root_props_writer);
            FileWriter portal_props_writer 
=   new  FileWriter(portal_property_file,  true );
            BufferedWriter portal_props_bufwriter 
=   new  BufferedWriter(portal_props_writer);
            FileWriter admin_props_writer 
=   new  FileWriter(admin_property_file,  true );
            BufferedWriter admin_props_bufwriter 
=   new  BufferedWriter(admin_props_writer);
        
            
//  Iterate the files
             int  file_number  =  fileContainer.size();
            
//  The result string container
            HashMap result_container  =   new  HashMap();
            
            
for  ( int  i = 0 ; i < file_number; i ++ ) {
                
//  Ignore files don't end with `java'
                String filename  =  (String)fileContainer.get(i);
                
if  (filename  ==   null   ||  filename.trim().length()  <   1   ||
                    
! filename.substring(filename.length()  -   4 , filename.length()).equalsIgnoreCase( " java " )) {
                    
continue ;
                }
                
                
//  Open the Java File and read out the resources
                FileReader java_file_reader  =   new  FileReader(filename);
                BufferedReader buffer_reader 
=   new  BufferedReader(java_file_reader);
                String input_line 
=   null ;
                
                PatternCompiler orocom 
=   new  Perl5Compiler();
                Pattern pattern 
=  orocom.compile(java_restring);
                PatternMatcher matcher 
=   new  Perl5Matcher();
                
                
while ((input_line  =  buffer_reader.readLine())  !=   null ) {
                    
//  System.out.println("Got one line from java file: " + input_line);
                    
                    
//  Begin filter
                    
//  Use PatternMatcherInput class, we can find the next string in one line string
                    
//  Otherwise ORO will find the first match string in one line then ignore the rest string of this line
                    PatternMatcherInput reinput  =   new  PatternMatcherInput(input_line);
                    
while  (matcher.contains(reinput, pattern)) {
                        MatchResult result 
=  matcher.getMatch();
                        String result_string 
=  result.group( 0 );
                        
                        
//  Get rid of the quotations
                        result_string  =  result_string.substring( 1 , result_string.length()  -   1 );
                        
//  if we got the duplicated result_string, HashMap will overcome this issue
                        result_container.put(result_string,  " dummy object " );
                    }
                }
                
                buffer_reader.close();
                java_file_reader.close();
            }
                
            
//  Iterate the HashMap and write the result
            Iterator it  =  result_container.keySet().iterator();
            
while  (it.hasNext()) {
                String key 
=  (String)it.next();
                
                
if  (default_property_file.equalsIgnoreCase( " portal " )) {
                    
//  check whether this key has been added?
                     if  (portal_props.getProperty(key)  ==   null ) {
                        System.out.println(
" Found one resource:  "   +  key);
                        portal_props_bufwriter.newLine();
                        portal_props_bufwriter.write(key 
+   " =dummy " );
                        portal_props_bufwriter.newLine();
                    }
                } 
else   if  (default_property_file.equalsIgnoreCase( " admin " )) {
                    
//  check whether this key has been added?
                     if  (admin_props.getProperty(key)  ==   null ) {
                        System.out.println(
" Found one resource:  "   +  key);
                        admin_props_bufwriter.newLine();
                        admin_props_bufwriter.write(key 
+   " =dummy " );
                        admin_props_bufwriter.newLine();
                    }
                } 
else   if  (default_property_file.equalsIgnoreCase( " root " )) {
                    
//  check whether this key has been added?
                     if  (root_props.getProperty(key)  ==   null ) {
                        System.out.println(
" Found one resource:  "   +  key);
                        root_props_bufwriter.newLine();
                        root_props_bufwriter.write(key 
+   " =dummy " );
                        root_props_bufwriter.newLine();
                    }
                }
            }
                            
            root_props_bufwriter.close();
            root_props_writer.close();
            admin_props_bufwriter.close();
            admin_props_writer.close();
            portal_props_bufwriter.close();
            portal_props_writer.close();
        } 
catch  (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    
/**
     * 
@param  args the command line arguments
     
*/
    
public   static   void  main(String[] args) {
        GenerateProperties handle 
=   new  GenerateProperties();
        handle.SetDefaultOutputProperty(
" admin " );
        handle.DistillJSP();
        handle.DistillJava();
    }
    
}

 

 

转载于:https://www.cnblogs.com/super119/archive/2011/01/03/1924540.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值