1 package org.tigris.scarab.util.build; 2 3 48 49 import java.io.BufferedWriter ; 50 import java.io.File ; 51 import java.io.FileWriter ; 52 import java.io.IOException ; 53 import java.util.Iterator ; 54 import java.util.Vector ; 55 56 import org.apache.tools.ant.BuildException; 57 import org.apache.tools.ant.DirectoryScanner; 58 import org.apache.tools.ant.Task; 59 import org.apache.tools.ant.types.FileSet; 60 61 import org.tigris.scarab.util.build.l10nchecker.L10nInspector; 62 import org.tigris.scarab.util.build.l10nchecker.L10nIssue; 63 import org.tigris.scarab.util.build.l10nchecker.L10nIssueTemplates; 64 import org.tigris.scarab.util.build.l10nchecker.L10nMessage; 65 66 102 public class AntL10AnalysisTask extends Task 103 { 104 105 private Vector filesets = new Vector (); 106 107 108 private Vector messages = new Vector (); 109 110 111 private int verbose = 0; 112 113 private String refFile; 114 115 private boolean failonerr = false; 116 117 private String outFileName = null; 118 119 private BufferedWriter outFile = null; 120 121 126 public void execute() throws BuildException 127 { 128 L10nInspector ins = null; 129 DirectoryScanner ds; 130 if (outFileName != null) 131 { 132 try 133 { 134 outFile = new BufferedWriter (new FileWriter (outFileName)); 135 } 136 catch (IOException eIO) 137 { 138 throw new BuildException(eIO); 139 } 140 } 141 output("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>"); 142 output("<document>"); 143 output("<properties><title>L10N status report</title></properties>"); 144 output("<body>"); 145 try 146 { 147 ins = new L10nInspector(); 148 Iterator it = messages.iterator(); 150 while (it.hasNext()) 151 { 152 Message msg = (Message)it.next(); 153 try 154 { 155 Class _clazz = Class.forName("org.tigris.scarab.util.build.l10nchecker.issues." + msg.id + "Issue"); 156 L10nIssueTemplates.setMessageType(_clazz, msg.severity); 157 } 158 catch (ClassNotFoundException ex_cnf) 159 { 160 throw new BuildException ("Cannot locate issue " + msg.id); 161 } 162 } 163 output("<section name=\"Reference file "+refFile.substring(refFile.lastIndexOf("/")+1)+"\"><pre>"); 164 output("Loading reference file " + refFile, true); 165 int refCount = ins.setReference(refFile); 166 output("Loaded properties: " + refCount); 167 if (ins.hasErrors()) 168 { 169 output("Errors: " + ins.getErrors().size()); 170 it = ins.getErrors().iterator(); 171 while (it.hasNext()) 172 { 173 L10nMessage err = (L10nMessage) it.next(); 174 output("Error at line " + err.getLineNumber() + ": " 175 + err.getMessageText()); 176 } 177 } 178 output("</pre></section>"); 179 output(""); it = filesets.iterator(); 181 while (it.hasNext()) 182 { 183 FileSet fs = (FileSet) it.next(); 184 ds = fs.getDirectoryScanner(this.getProject()); 185 ds.scan(); 186 File srcDir = fs.getDir(getProject()); 187 String [] files = ds.getIncludedFiles(); 188 for (int i = 0; i < files.length; i++) 189 { 190 File f = new File (files[i]); 191 output(""); 192 output("<section name=\"Checking bundle " + files[i] +"\"><pre>", true); 193 int transcount = ins.checkFile(srcDir.getAbsolutePath() 194 + "/" + files[i]); 195 output("Translations found: " + transcount, true); 196 if (ins.getErrors().size() > 0) 197 { 198 output("Errors: " + 199 ins.getErrors().size(), true); 200 } 201 if (verbose > 0 && ins.getWarnings().size() > 0) 202 { 203 output("Warnings: " 204 + ins.getWarnings().size()); 205 } 206 if (verbose > 1 && ins.getInfos().size() > 0) 207 { 208 output("Information: " 209 + ins.getInfos().size()); 210 } 211 output(""); if (ins.hasErrors()) 213 { 214 it = ins.getErrors().iterator(); 215 while (it.hasNext()) 216 { 217 L10nMessage err = (L10nMessage) it.next(); 218 output("Error at line " + err.getLineNumber() 219 + ": " + err.getMessageText()); 220 } 221 if (failonerr) 222 { 223 throw new BuildException( 224 "Failed due to errors in reference bundle"); 225 } 226 } 227 if (verbose > 0 && ins.getWarnings().size() > 0) 228 { 229 it = ins.getWarnings().iterator(); 230 while (it.hasNext()) 231 { 232 L10nMessage err = (L10nMessage) it.next(); 233 output("Warning at line " + err.getLineNumber() 234 + ": " + err.getMessageText()); 235 } 236 } 237 if (verbose > 1 && ins.getInfos().size() > 0) 238 { 239 it = ins.getInfos().iterator(); 240 while (it.hasNext()) 241 { 242 L10nMessage err = (L10nMessage) it.next(); 243 244 if (err.getLineNumber() < 0) 245 { 246 output("Information: " 247 + err.getMessageText()); 248 } 249 else 250 { 251 output("Information for line " 252 + err.getLineNumber() + ": " 253 + err.getMessageText()); 254 } 255 } 256 } 257 output("</pre></section>"); 258 } 259 } 260 output("</body>"); 261 output("</document>"); 262 } 263 catch (Exception e) 264 { 265 log("Exception " + e + " raised"); 266 throw new BuildException(e); 267 } 268 } 269 270 275 public void setOutfile(String filename) 276 { 277 this.outFileName = filename; 278 } 279 280 284 public void setRefFile(String aFile) 285 { 286 refFile = aFile; 287 } 288 289 290 295 public void setVerbose(int verbose) 296 { 297 this.verbose = verbose; 298 } 299 300 305 public void setFailonerr(boolean failonerr) 306 { 307 this.failonerr = failonerr; 308 } 309 310 316 public FileSet createFileset() 317 { 318 FileSet set = new FileSet(); 319 filesets.add(set); 320 return set; 321 } 322 323 324 325 private void output(String what) 326 { 327 output (what, false); 328 } 329 330 333 private void output(String what, boolean dumpToConsole) 334 { 335 if (outFile != null) 336 { 337 try 338 { 339 outFile.write(what); 340 outFile.newLine(); 341 outFile.flush(); 342 } 343 catch (IOException eIO) 344 { 345 log("Cannot write " + what + " to " + outFileName + " (" 346 + eIO.getMessage() + ")"); 347 } 348 } 349 if (null == outFile || dumpToConsole) 350 { 351 log(what); 352 } 353 } 354 355 360 public Message createMessageSet () 361 { 362 Message msg = new Message (); 363 messages.add(msg); 364 return msg; 365 } 366 367 368 372 public class Message 373 { 374 private String id; 375 private int severity; 376 377 378 public Message() {} 379 380 381 public void setError (String _id) 382 { 383 this.id = _id; 384 } 385 386 387 public void setSeverity (String severity) 388 { 389 if (severity.equals("ERROR")) 390 { 391 this.severity = L10nIssue.MESSAGE_ERROR; 392 } 393 else if (severity.equals("WARNING")) 394 { 395 this.severity = L10nIssue.MESSAGE_WARNING; 396 } 397 else if (severity.equals("INFORMATION")) 398 { 399 this.severity = L10nIssue.MESSAGE_INFO; 400 } 401 else 402 { 403 this.severity = L10nIssue.MESSAGE_IGNORE; 404 } 405 } 406 } 407 } 408 | Popular Tags |