001 //$HeadURL$
002 /*----------------------------------------------------------------------------
003 This file is part of deegree, http://deegree.org/
004 Copyright (C) 2001-2009 by:
005 Department of Geography, University of Bonn
006 and
007 lat/lon GmbH
008
009 This library is free software; you can redistribute it and/or modify it under
010 the terms of the GNU Lesser General Public License as published by the Free
011 Software Foundation; either version 2.1 of the License, or (at your option)
012 any later version.
013 This library is distributed in the hope that it will be useful, but WITHOUT
014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016 details.
017 You should have received a copy of the GNU Lesser General Public License
018 along with this library; if not, write to the Free Software Foundation, Inc.,
019 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020
021 Contact information:
022
023 lat/lon GmbH
024 Aennchenstr. 19, 53177 Bonn
025 Germany
026 http://lat-lon.de/
027
028 Department of Geography, University of Bonn
029 Prof. Dr. Klaus Greve
030 Postfach 1147, 53001 Bonn
031 Germany
032 http://www.geographie.uni-bonn.de/deegree/
033
034 e-mail: info@deegree.org
035 ----------------------------------------------------------------------------*/
036
037 package org.deegree.i18n;
038
039 import java.io.File;
040 import java.io.FileInputStream;
041 import java.io.FileOutputStream;
042 import java.io.IOException;
043 import java.util.ArrayList;
044 import java.util.List;
045 import java.util.Properties;
046
047 import org.deegree.framework.util.ConvenienceFileFilter;
048 import org.deegree.framework.util.FileUtils;
049 import org.deegree.framework.util.StringTools;
050
051 /**
052 * tool for creating a properties file mapping each property key of a defined source properties file to the class where
053 * it is used
054 *
055 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
056 * @author last edited by: $Author: poth $
057 *
058 * @version $Revision: 6251 $, $Date: 2007-03-19 16:59:28 +0100 (Mo, 19 Mrz 2007) $
059 */
060 public class ClassMessagesCreator {
061
062 private Properties descProperties;
063
064 private Properties classesProperties;
065
066 private String[] roots;
067
068 private String outputFile;
069
070 /**
071 *
072 * @param propertiesFileName
073 * @param outputFile
074 * @param rootDirs
075 * @throws Exception
076 */
077 public ClassMessagesCreator( String propertiesFileName, String outputFile, String... rootDirs ) throws Exception {
078 File file = new File( propertiesFileName );
079 descProperties = new Properties();
080 FileInputStream fis = new FileInputStream( file );
081 descProperties.load( fis );
082 System.out.println( "defined properties: " + descProperties.size() );
083 fis.close();
084 classesProperties = new Properties();
085 this.roots = rootDirs;
086 this.outputFile = outputFile;
087 }
088
089 /**
090 *
091 * @throws IOException
092 */
093 public void run()
094 throws IOException {
095 List<File> fileList = new ArrayList<File>( 100 );
096 for ( int i = 0; i < roots.length; i++ ) {
097 // '/' as path seperator
098 roots[i] = StringTools.replace( roots[i], "\\", "/", true );
099 File root = new File( roots[i] );
100 File[] files = root.listFiles( new ConvenienceFileFilter( true, "JAVA", "JSP" ) );
101 for ( File file : files ) {
102 fileList.add( file );
103 }
104 // remove disk char (windows)
105 if ( roots[i].indexOf( ':' ) > -1 ) {
106 int p = roots[i].indexOf( ':' );
107 roots[i] = roots[i].substring( p + 1 );
108 }
109 }
110
111 collect( fileList.toArray( new File[fileList.size()] ) );
112
113 FileOutputStream fos = new FileOutputStream( outputFile );
114 // Iterator iterator = descProperties.keySet().iterator();
115 for ( Object k : descProperties.keySet() ) {
116 String key = k.toString();
117 classesProperties.put( key, "------- N O T U S E D --------" );
118 }
119 classesProperties.store( fos, "" );
120 fos.close();
121 }
122
123 private void collect( File[] files )
124 throws IOException {
125 for ( File file : files ) {
126 if ( file.isDirectory() ) {
127 System.out.println( file );
128 collect( file.listFiles( new ConvenienceFileFilter( true, "JAVA", "JSP" ) ) );
129 } else {
130 String s = FileUtils.readTextFile( file ).toString();
131 List<String> list = new ArrayList<String>( 100 );
132 for ( Object k : descProperties.keySet() ) {
133 String key = k.toString();
134 if ( s.indexOf( "\"" + key + "\"" ) > -1 ) {
135 String tmp = file.getAbsolutePath();
136 tmp = StringTools.replace( tmp, "\\", "/", true );
137 if ( !tmp.toUpperCase().endsWith( ".JSP" ) ) {
138 int p = tmp.indexOf( "org\\deegree" );
139 if ( p < 0 ) {
140 p = tmp.indexOf( "org/deegree" );
141 }
142 if ( p < 0 ) {
143 p = 0;
144 }
145 tmp = tmp.substring( p, tmp.length() - 5 );
146 tmp = StringTools.replace( tmp, "/", ".", true );
147 } else {
148 for ( int i = 0; i < roots.length; i++ ) {
149 int p = tmp.indexOf( roots[i] );
150 if ( p > -1 ) {
151 // cut root dir name from file path
152 tmp = tmp.substring( p + roots[i].length() );
153 break;
154 }
155 }
156 }
157 classesProperties.put( key, tmp );
158 list.add( key );
159 }
160 }
161 for ( String key : list ) {
162 descProperties.remove( key );
163 }
164 }
165 }
166 }
167
168 /**
169 * @param args
170 * @throws Exception
171 */
172 public static void main( String[] args )
173 throws Exception {
174
175 Properties map = new Properties();
176 for ( int i = 0; i < args.length; i += 2 ) {
177 System.out.println( args[i + 1] );
178 map.put( args[i], args[i + 1] );
179 }
180 try {
181 validate( map );
182 } catch ( Exception e ) {
183 System.out.println( e.getMessage() );
184 System.out.println();
185 printHelp();
186 return;
187 }
188 String[] rootDirs = StringTools.toArray( map.getProperty( "-rootDirs" ), ",;", true );
189
190 ClassMessagesCreator cmc = new ClassMessagesCreator( map.getProperty( "-properties" ),
191 map.getProperty( "-outFile" ), rootDirs );
192 cmc.run();
193 }
194
195 private static void printHelp() {
196 System.out.println( "------------------------------------------------" );
197 System.out.println( "usage/parameters:" );
198 System.out.println();
199 System.out.println( "-properties : name/path of the properties file to analyse." );
200 System.out.println( " example: -properties /src/org/deegree/i18n/messages.properties" );
201 System.out.println();
202 System.out.println( "-outFile : name/path of the file to store the results" );
203 System.out.println( " example: -outFile /src/org/deegree/i18n/messages_classes.properties" );
204 System.out.println();
205 System.out.println( "-rootDirs name/path of directories from where to start searching for used properties" );
206 System.out.println( " example: -rootDirs /src/org/deegree,/src/igeoportal" );
207 }
208
209 private static void validate( Properties map )
210 throws Exception {
211 if ( map.get( "-properties" ) == null ) {
212 throw new Exception( "-properties (name/path of the properties file to analyse) must be set" );
213 }
214 if ( map.get( "-outFile" ) == null ) {
215 throw new Exception( "-outFile (name/path of the file to store the results) must be set" );
216 }
217 if ( map.get( "-rootDirs" ) == null ) {
218 throw new Exception( "-rootDirs (name/path of directories from where to start searching for "
219 + "used properties) must be set" );
220 }
221
222 }
223
224 }