1 22 package org.netbeans.lib.cvsclient.commandLine.command; 23 24 import java.io.*; 25 import java.io.PrintStream ; 26 import java.text.MessageFormat ; 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.ResourceBundle ; 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 46 public class locbundlecheck extends CVSAdapter implements CommandProvider { 47 48 51 public locbundlecheck() { 52 } 53 54 public String getName() { 55 return "locbundlecheck"; } 57 58 public String [] getSynonyms() { 59 return new String [] { "lbch", "lbcheck" }; } 61 62 public String getUsage() { 63 return ResourceBundle.getBundle(CommandProvider.class.getPackage().getName()+".Bundle").getString("locbundlecheck.usage"); } 65 66 public void printShortDescription(PrintStream out) { 67 String msg = ResourceBundle.getBundle(CommandProvider.class.getPackage().getName()+".Bundle").getString("locbundlecheck.shortDescription"); out.print(msg); 69 } 70 71 public void printLongDescription(PrintStream out) { 72 String msg = ResourceBundle.getBundle(CommandProvider.class.getPackage().getName()+".Bundle").getString("locbundlecheck.longDescription"); out.println(msg); 74 } 75 76 public Command createCommand(String [] args, int index, GlobalOptions gopt, String workDir) { 77 LocBundleAnnotateCommand command = new LocBundleAnnotateCommand(); 78 final String 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 arg; 84 String 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 (getUsage()); 98 } 99 int fileArgsIndex = go.optIndexGet(); 100 if (fileArgsIndex < args.length) { 102 Collection fls = new ArrayList (); 103 104 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 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 (); 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 (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 fileList, File origFile, String localization) { 138 String origPath = origFile.getAbsolutePath(); 139 String enarg = origPath.substring(0, origPath.length() - ".properties".length()) + "_" + localization + ".properties"; 140 File addfl = new File(enarg); 142 fileList.add(origFile); 143 if (addfl.exists()) { 144 fileList.add(addfl); 145 } else { 146 } 148 } 149 150 private static void addFilesInDir(Collection fileList, File origFile, String 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 out; 165 private PrintStream err; 166 private int realEnd = 0; 167 168 private HashMap originalBundles; 169 private HashMap localizedBundles; 170 private String local; 171 private String workDir; 172 173 176 locbundlecheck(PrintStream stdout, PrintStream stderr, String localization, String workDir) 177 { 178 out = stdout; 179 err = stderr; 180 originalBundles = new HashMap (); 181 localizedBundles = new HashMap (); 182 local = localization; 183 this.workDir = workDir; 184 } 185 186 187 public void fileInfoGenerated(org.netbeans.lib.cvsclient.event.FileInfoEvent e) { 188 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 if (realEnd == 2) { 197 generateOutput(); 200 } 201 202 } 203 204 205 public void commandTerminated(org.netbeans.lib.cvsclient.event.TerminationEvent e) { 206 if (realEnd == 0) { 207 realEnd = 1; 209 return; 210 } 211 realEnd = 2; 212 } 215 216 private void generateOutput() { 217 Iterator it = originalBundles.keySet().iterator(); 218 while (it.hasNext()) 219 { 220 String origPath = (String )it.next(); 221 int dotIndex = origPath.lastIndexOf("."); 222 if (dotIndex < 0) { 223 throw new IllegalStateException (ResourceBundle.getBundle("org/netbeans/lib/cvsclient/commandLine/command/Bundle").getString("locbundlecheck.illegal_state")); 224 } 225 String locPath = origPath.substring(0, dotIndex) + "_" + local + origPath.substring(dotIndex); 226 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 [] {origPath})); 232 continue; 233 } 234 localizedBundles.remove(locPath); 236 HashMap origPropMap = createPropMap(origInfo); 237 HashMap locPropMap = createPropMap(locInfo); 238 String 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 [] {printFile})); 247 Iterator propIt = origPropMap.keySet().iterator(); 248 while (propIt.hasNext()) { 249 String prop = (String )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 [] {prop})); 255 continue; 256 } 257 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 [] {prop})); 263 } 264 } 265 266 } 267 if (localizedBundles.size() > 0) { 268 Iterator locIt = localizedBundles.keySet().iterator(); 269 while (locIt.hasNext()) { 270 String prop = (String )locIt.next(); 271 out.println(MessageFormat.format(ResourceBundle.getBundle("org/netbeans/lib/cvsclient/commandLine/command/Bundle").getString("locbundlecheck.prop_removed"), 272 new Object [] {prop})); 273 } 274 } 275 } 276 277 private HashMap createPropMap(AnnotateInformation info) { 278 HashMap propMap = new HashMap (); 279 AnnotateLine line = info.getFirstLine(); 280 while (line != null) { 281 String content = line.getContent(); 282 if (content.startsWith("#")) { 283 line = info.getNextLine(); 285 continue; 286 } 287 int index = content.indexOf('='); 288 if (index > 0) { 289 String key = content.substring(0, index); 290 propMap.put(key, line); 291 } else { 292 } 294 line = info.getNextLine(); 295 } 296 return propMap; 297 } 298 299 private static class LocBundleAnnotateCommand extends AnnotateCommand implements ListenerProvider { 300 private String loc; 301 private String workDir; 302 303 public CVSListener createCVSListener(PrintStream stdout, PrintStream stderr) { 304 return new locbundlecheck(stdout, stderr, loc, workDir); 305 } 306 307 public void setLocalization(String loc) { 308 this.loc = loc; 309 } 310 311 public void setWorkDir(String dir) { 312 workDir = dir; 313 } 314 315 } 316 317 318 319 } | Popular Tags |