KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > commandLine > command > locbundlecheck


1 /*****************************************************************************
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14
15  * The Original Software is the CVS Client Library.
16  * The Initial Developer of the Original Software is Milos Kleint.
17  * Portions created by Milos Kleint are Copyright (C) 2000.
18  * All Rights Reserved.
19
20  * Contributor(s): Milos Kleint.
21  *****************************************************************************/

22 package org.netbeans.lib.cvsclient.commandLine.command;
23
24 import java.io.*;
25 import java.io.PrintStream JavaDoc;
26 import java.text.MessageFormat JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.ResourceBundle JavaDoc;
33
34 import org.netbeans.lib.cvsclient.command.*;
35 import org.netbeans.lib.cvsclient.command.FileInfoContainer;
36 import org.netbeans.lib.cvsclient.command.annotate.*;
37 import org.netbeans.lib.cvsclient.commandLine.*;
38 import org.netbeans.lib.cvsclient.commandLine.ListenerProvider;
39 import org.netbeans.lib.cvsclient.event.CVSAdapter;
40 import org.netbeans.lib.cvsclient.event.CVSListener;
41
42 /**
43  * The locbundlecheck command
44  * @author Milos Kleint
45  */

46 public class locbundlecheck extends CVSAdapter implements CommandProvider {
47     
48     /**
49      * A constructor that is used to create the CommandProvider.
50      */

51     public locbundlecheck() {
52     }
53     
54     public String JavaDoc getName() {
55         return "locbundlecheck"; // NOI18N
56
}
57     
58     public String JavaDoc[] getSynonyms() {
59         return new String JavaDoc[] { "lbch", "lbcheck" }; // NOI18N
60
}
61     
62     public String JavaDoc getUsage() {
63         return ResourceBundle.getBundle(CommandProvider.class.getPackage().getName()+".Bundle").getString("locbundlecheck.usage"); // NOI18N
64
}
65     
66     public void printShortDescription(PrintStream JavaDoc out) {
67         String JavaDoc msg = ResourceBundle.getBundle(CommandProvider.class.getPackage().getName()+".Bundle").getString("locbundlecheck.shortDescription"); // NOI18N
68
out.print(msg);
69     }
70     
71     public void printLongDescription(PrintStream JavaDoc out) {
72         String JavaDoc msg = ResourceBundle.getBundle(CommandProvider.class.getPackage().getName()+".Bundle").getString("locbundlecheck.longDescription"); // NOI18N
73
out.println(msg);
74     }
75     
76     public Command createCommand(String JavaDoc[] args, int index, GlobalOptions gopt, String JavaDoc workDir) {
77         LocBundleAnnotateCommand command = new LocBundleAnnotateCommand();
78         final String JavaDoc getOptString = command.getOptString();
79         GetOpt go = new GetOpt(args, getOptString + "i:");
80         int ch = -1;
81         go.optIndexSet(index);
82         boolean usagePrint = false;
83         String JavaDoc arg;
84         String JavaDoc localization = null;
85         while ((ch = go.getopt()) != go.optEOF) {
86             if (ch == 'i') {
87                 localization = go.optArgGet();
88                 command.setLocalization(localization);
89             } else {
90                 boolean ok = command.setCVSCommand((char)ch, go.optArgGet());
91                 if (!ok) {
92                     usagePrint = true;
93                 }
94             }
95         }
96         if (usagePrint || localization == null) {
97             throw new IllegalArgumentException JavaDoc(getUsage());
98         }
99         int fileArgsIndex = go.optIndexGet();
100         // test if we have been passed any file arguments
101
if (fileArgsIndex < args.length) {
102             Collection JavaDoc fls = new ArrayList JavaDoc();
103             
104             // send the arguments as absolute paths
105
if (workDir == null) {
106                 workDir = System.getProperty("user.dir");
107             }
108             command.setWorkDir(workDir);
109             File workingDir = new File(workDir);
110             for (int i = fileArgsIndex; i < args.length; i++) {
111                 File fl = new File(workingDir, args[i]);
112 // System.out.println("file=" + fl);
113
if (fl.exists() && fl.isDirectory()) {
114                     addFilesInDir(fls, fl, localization);
115                 }
116                 else if (fl.exists() && fl.getName().endsWith(".properties"))
117                 {
118                     addFiles(fls, fl, localization);
119                 }
120                 else
121                 {
122                     throw new IllegalArgumentException JavaDoc();
123                 }
124             }
125             if (fls.size() > 0)
126             {
127                 File[] fileArgs = new File[fls.size()];
128                 fileArgs = (File[])fls.toArray(fileArgs);
129                 command.setFiles(fileArgs);
130             } else {
131                 throw new IllegalArgumentException JavaDoc(ResourceBundle.getBundle("org/netbeans/lib/cvsclient/commandLine/command/Bundle").getString("locbundlecheck.no_file_spec"));
132             }
133         }
134         return command;
135     }
136     
137     private static void addFiles(Collection JavaDoc fileList, File origFile, String JavaDoc localization) {
138         String JavaDoc origPath = origFile.getAbsolutePath();
139         String JavaDoc enarg = origPath.substring(0, origPath.length() - ".properties".length()) + "_" + localization + ".properties";
140         //System.out.println("enarg=" + enarg);
141
File addfl = new File(enarg);
142         fileList.add(origFile);
143         if (addfl.exists()) {
144             fileList.add(addfl);
145         } else {
146             //TODO
147
}
148     }
149     
150     private static void addFilesInDir(Collection JavaDoc fileList, File origFile, String JavaDoc localization) {
151         File[] files = origFile.listFiles();
152         if (files != null && files.length > 0) {
153             for (int i = 0; i < files.length; i++) {
154                 if (files[i].exists() && files[i].isDirectory())
155                 {
156                     addFilesInDir(fileList, files[i], localization);
157                 } else if (files[i].exists() && "Bundle.properties".equals(files[i].getName())) {
158                     addFiles(fileList, files[i], localization);
159                 }
160             }
161         }
162     }
163     
164     private PrintStream JavaDoc out;
165     private PrintStream JavaDoc err;
166     private int realEnd = 0;
167     
168     private HashMap JavaDoc originalBundles;
169     private HashMap JavaDoc localizedBundles;
170     private String JavaDoc local;
171     private String JavaDoc workDir;
172     
173     /**
174      * A constructor that is used to create the CVSAdapter.
175      */

176     locbundlecheck(PrintStream JavaDoc stdout, PrintStream JavaDoc stderr, String JavaDoc localization, String JavaDoc workDir)
177     {
178         out = stdout;
179         err = stderr;
180         originalBundles = new HashMap JavaDoc();
181         localizedBundles = new HashMap JavaDoc();
182         local = localization;
183         this.workDir = workDir;
184     }
185
186     
187     public void fileInfoGenerated(org.netbeans.lib.cvsclient.event.FileInfoEvent e) {
188 // out.println("annotated " + e.getInfoContainer().getFile());
189
FileInfoContainer cont = e.getInfoContainer();
190         if (cont.getFile().getName().indexOf("_" + local) >= 0) {
191             localizedBundles.put(cont.getFile().getAbsolutePath(), cont);
192         } else {
193             originalBundles.put(cont.getFile().getAbsolutePath(), cont);
194         }
195 // out.println("orig size=" + originalBundles.keySet().size() + " loc size=" + localizedBundles.keySet().size());
196
if (realEnd == 2) {
197             // generate output.
198
// out.println("generating output....");
199
generateOutput();
200         }
201         
202     }
203     
204     
205     public void commandTerminated(org.netbeans.lib.cvsclient.event.TerminationEvent e) {
206         if (realEnd == 0) {
207             // now the event is triggered because of the validresponses request
208
realEnd = 1;
209             return;
210         }
211         realEnd = 2;
212         // the second time it's the real end. waiting for the last info object to be received.
213
// out.println("finish=" + e.isError());
214
}
215     
216     private void generateOutput() {
217         Iterator JavaDoc it = originalBundles.keySet().iterator();
218         while (it.hasNext())
219         {
220             String JavaDoc origPath = (String JavaDoc)it.next();
221             int dotIndex = origPath.lastIndexOf(".");
222             if (dotIndex < 0) {
223                 throw new IllegalStateException JavaDoc(ResourceBundle.getBundle("org/netbeans/lib/cvsclient/commandLine/command/Bundle").getString("locbundlecheck.illegal_state"));
224             }
225             String JavaDoc locPath = origPath.substring(0, dotIndex) + "_" + local + origPath.substring(dotIndex);
226 // System.out.println("locpath=" + locPath);
227
AnnotateInformation origInfo = (AnnotateInformation)originalBundles.get(origPath);
228             AnnotateInformation locInfo = (AnnotateInformation)localizedBundles.get(locPath);
229             if (locInfo == null) {
230                 out.println(MessageFormat.format(ResourceBundle.getBundle("org/netbeans/lib/cvsclient/commandLine/command/Bundle").getString("locbundlecheck.noLocalizedFile"),
231                             new Object JavaDoc[] {origPath}));
232                 continue;
233             }
234             // remove from locl bundles to figure out what was removed in the original..
235
localizedBundles.remove(locPath);
236             HashMap JavaDoc origPropMap = createPropMap(origInfo);
237             HashMap JavaDoc locPropMap = createPropMap(locInfo);
238             String JavaDoc printFile = origPath;
239             if (origPath.startsWith(workDir)) {
240                 printFile = origPath.substring(workDir.length());
241                 if (printFile.startsWith("/") || printFile.startsWith("\\") ) {
242                     printFile = printFile.substring(1);
243                 }
244             }
245             out.println(MessageFormat.format(ResourceBundle.getBundle("org/netbeans/lib/cvsclient/commandLine/command/Bundle").getString("locbundlecheck.File"),
246                         new Object JavaDoc[] {printFile}));
247             Iterator JavaDoc propIt = origPropMap.keySet().iterator();
248             while (propIt.hasNext()) {
249                 String JavaDoc prop = (String JavaDoc)propIt.next();
250                 AnnotateLine origLine = (AnnotateLine)origPropMap.get(prop);
251                 AnnotateLine locLine = (AnnotateLine)locPropMap.get(prop);
252                 if (locLine == null) {
253                     out.println(MessageFormat.format(ResourceBundle.getBundle("org/netbeans/lib/cvsclient/commandLine/command/Bundle").getString("locbundlecheck.propMissing"),
254                                 new Object JavaDoc[] {prop}));
255                     continue;
256                 }
257 // System.out.println("prop=" + prop);
258
// System.out.println("orig date:" + origLine.getDate());
259
// System.out.println("loc date:" + locLine.getDate());
260
if (origLine.getDate().compareTo(locLine.getDate()) > 0) {
261                     out.println(MessageFormat.format(ResourceBundle.getBundle("org/netbeans/lib/cvsclient/commandLine/command/Bundle").getString("locbundlecheck.prop_updated"),
262                                 new Object JavaDoc[] {prop}));
263                 }
264             }
265             
266         }
267         if (localizedBundles.size() > 0) {
268             Iterator JavaDoc locIt = localizedBundles.keySet().iterator();
269             while (locIt.hasNext()) {
270                 String JavaDoc prop = (String JavaDoc)locIt.next();
271                 out.println(MessageFormat.format(ResourceBundle.getBundle("org/netbeans/lib/cvsclient/commandLine/command/Bundle").getString("locbundlecheck.prop_removed"),
272                             new Object JavaDoc[] {prop}));
273             }
274         }
275     }
276     
277     private HashMap JavaDoc createPropMap(AnnotateInformation info) {
278         HashMap JavaDoc propMap = new HashMap JavaDoc();
279         AnnotateLine line = info.getFirstLine();
280         while (line != null) {
281             String JavaDoc content = line.getContent();
282             if (content.startsWith("#")) {
283                 // ignore commented lines.
284
line = info.getNextLine();
285                 continue;
286             }
287             int index = content.indexOf('=');
288             if (index > 0) {
289                 String JavaDoc key = content.substring(0, index);
290                 propMap.put(key, line);
291             } else {
292                 //TODO.. for properties that span across multiple lines, one should take all lines into account
293
}
294             line = info.getNextLine();
295         }
296         return propMap;
297     }
298     
299     private static class LocBundleAnnotateCommand extends AnnotateCommand implements ListenerProvider {
300         private String JavaDoc loc;
301         private String JavaDoc workDir;
302         
303         public CVSListener createCVSListener(PrintStream JavaDoc stdout, PrintStream JavaDoc stderr) {
304             return new locbundlecheck(stdout, stderr, loc, workDir);
305         }
306         
307         public void setLocalization(String JavaDoc loc) {
308             this.loc = loc;
309         }
310         
311         public void setWorkDir(String JavaDoc dir) {
312             workDir = dir;
313         }
314         
315     }
316     
317     
318     
319 }
Popular Tags