KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > util > MessageTemplateFinder


1 /**
2  * Copyright (C) 2003 Manfred Andres
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  * Created on 11.05.2004
19  */

20
21 package freecs.util;
22
23 import java.io.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileReader JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.TreeSet JavaDoc;
30
31 /**
32  * @author Manfred Andres
33  *
34  * freecs.util
35  */

36 public class MessageTemplateFinder {
37     static TreeSet JavaDoc v = new TreeSet JavaDoc();
38     static Properties JavaDoc oldprops = new Properties JavaDoc();
39
40     public static void main(String JavaDoc[] args) {
41         if (args == null || args.length<1) {
42             System.out.println ("please specify the path to the sourcefolder");
43             System.exit(1);
44         }
45         String JavaDoc base = args[0];
46         if (args.length > 1 && args[1]!=null && args[1].length()>0) {
47             try {
48                 FileInputStream JavaDoc fis = new FileInputStream JavaDoc (args[1]);
49                 oldprops.load(fis);
50                 fis.close();
51             } catch (Exception JavaDoc e) {
52                 System.out.println ("old properties-file wasn't found");
53                 e.printStackTrace();
54             }
55         }
56         File JavaDoc basef = new File JavaDoc (base);
57         if (!basef.exists() || !basef.isDirectory()) {
58             System.out.println ("The path specified is not correct");
59             System.exit (1);
60         }
61         System.out.println ("Searching...");
62         search (basef);
63         
64         // add messagetemplates which are set via MessageParser.setMessageTemplate();
65
v.add("message.user.join.server");
66         v.add("message.q");
67         v.add("message.kh.personal");
68         v.add("message.user.leaving.server");
69         v.add("message.user.leaving.server.kicked");
70
71         System.out.println("Found templates:------------------------------------\r\n");
72         for (Iterator JavaDoc e = v.iterator(); e.hasNext(); ) {
73             String JavaDoc cs = (String JavaDoc) e.next();
74             System.out.print(cs);
75             String JavaDoc propval = oldprops.getProperty(cs);
76             if (propval!=null) {
77                 for (int i = 0; i < (36 - cs.length()); i++)
78                     System.out.print (" ");
79                 System.out.print("= ");
80                 System.out.print(propval);
81                 oldprops.remove(cs);
82             }
83             System.out.print("\r\n");
84         }
85         System.out.println("----------------------- Values from old-properties-file, which have not been found in source\r\n");
86         for (Enumeration JavaDoc e = oldprops.keys(); e.hasMoreElements(); ) {
87             String JavaDoc k = (String JavaDoc) e.nextElement();
88             System.out.print(k);
89             for (int i = 0; i < (36 - k.length()); i++)
90                 System.out.print (" ");
91             System.out.print ("= ");
92             System.out.println (oldprops.getProperty(k));
93         }
94     }
95     
96     public static void search(File JavaDoc f) {
97         System.out.print("searching ");
98         if (f.isDirectory()) {
99             System.out.print("DIRECTORY: ");
100             System.out.println (f.getName());
101             File JavaDoc[] farr = f.listFiles();
102             for (int i = 0; i<farr.length; i++)
103                 search (farr[i]);
104         } else if (f.isFile()) {
105             System.out.print("file: ");
106             System.out.println (f.getName());
107             String JavaDoc cntnt = null;
108             try {
109                 FileReader JavaDoc fr = new FileReader JavaDoc(f);
110                 char fcnt[] = new char[(int) f.length()];
111                 int read = fr.read(fcnt);
112                 cntnt=String.copyValueOf(fcnt);
113             } catch (Exception JavaDoc e) {
114                 e.printStackTrace();
115             }
116             int idx;
117             while ((idx = cntnt.indexOf(".msgTemplate")) != -1) {
118                 cntnt = cntnt.substring(idx+12).trim();
119                 if (!cntnt.startsWith("="))
120                     continue;
121                 cntnt = cntnt.substring(1).trim();
122                 if (!cntnt.startsWith("\""))
123                     continue;
124                 cntnt = cntnt.substring(1);
125                 int idx2 = cntnt.indexOf("\"");
126                 if (idx2 == -1)
127                     continue;
128                 String JavaDoc cs = cntnt.substring(0, idx2);
129                 if (!v.contains(cs))
130                     v.add(cs);
131             }
132         }
133     }
134 }
135
Popular Tags