KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > util > build > AntL10AnalysisTask


1 package org.tigris.scarab.util.build;
2
3 /* ================================================================
4  * Copyright (c) 2005 CollabNet. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowlegement: "This product includes
19  * software developed by Collab.Net <http://www.Collab.Net/>."
20  * Alternately, this acknowlegement may appear in the software itself, if
21  * and wherever such third-party acknowlegements normally appear.
22  *
23  * 4. The hosted project names must not be used to endorse or promote
24  * products derived from this software without prior written
25  * permission. For written permission, please contact info@collab.net.
26  *
27  * 5. Products derived from this software may not use the "Tigris" or
28  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
29  * prior written permission of Collab.Net.
30  *
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * ====================================================================
44  *
45  * This software consists of voluntary contributions made by many
46  * individuals on behalf of Collab.Net.
47  */

48
49 import java.io.BufferedWriter JavaDoc;
50 import java.io.File JavaDoc;
51 import java.io.FileWriter JavaDoc;
52 import java.io.IOException JavaDoc;
53 import java.util.Iterator JavaDoc;
54 import java.util.Vector JavaDoc;
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 /**
67  *
68  * Ant task to check for localisation problems.
69  *
70  * <p>Ant parameters:
71  *
72  * <ul>
73  * <li>FileSet: Files to check
74  * <li>messageSet: Set the severity for a particular message.
75  * This is done by setting the attributes <code>error</code> to the
76  * issue that has to be used (e.g. <code>error="CantParseIssue"</code>)
77  * and <code>severity</code> to one of the defined severity levels:
78  * <ul>
79  * <li><strong>ERROR</strong>: This issue will be marked as error
80  * <li><strong>WARNING</strong>: This issue will be marked as error
81  * <li><strong>INFORMATION</strong>: This issue will be marked as error
82  * <li><strong>IGNORED</strong>: This issue will be marked to be
83  * ignored. By default all issues are marked as "IGNORED".
84  * </ul>
85  * <li>reffile: Reference file
86  * <li>verbose: Verbosity:
87  * <ul>
88  * <li>0: Errors only
89  * <li>1: Errors and warnings
90  * <li>2: Errors, warnings and information.
91  * </ul>
92  * <li>outfile: IF given, all output is written to this file
93  * <li>failonerr: Stop after first checked file in case of errors.
94  * </ul>
95  *
96  * In case the output is redirected to a file, summary information is displayed
97  * on the ant output.
98  *
99  * @author sreindl
100  *
101  */

102 public class AntL10AnalysisTask extends Task
103 {
104     /* intput files */
105     private Vector JavaDoc filesets = new Vector JavaDoc();
106     
107     /* message set settings */
108     private Vector JavaDoc messages = new Vector JavaDoc ();
109     
110     /* verbosity */
111     private int verbose = 0;
112
113     private String JavaDoc refFile;
114
115     private boolean failonerr = false;
116
117     private String JavaDoc outFileName = null;
118
119     private BufferedWriter JavaDoc outFile = null;
120
121     /*
122      * Read all parameters and execute
123      *
124      * @see org.apache.tools.ant.Task#execute()
125      */

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 JavaDoc(new FileWriter JavaDoc(outFileName));
135             }
136             catch (IOException JavaDoc 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             // set severities
149
Iterator JavaDoc it = messages.iterator();
150             while (it.hasNext())
151             {
152                 Message msg = (Message)it.next();
153                 try
154                 {
155                     Class JavaDoc _clazz = Class.forName("org.tigris.scarab.util.build.l10nchecker.issues." + msg.id + "Issue");
156                     L10nIssueTemplates.setMessageType(_clazz, msg.severity);
157                 }
158                 catch (ClassNotFoundException JavaDoc 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(""); // empty line for readability
180
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 JavaDoc srcDir = fs.getDir(getProject());
187                 String JavaDoc[] files = ds.getIncludedFiles();
188                 for (int i = 0; i < files.length; i++)
189                 {
190                     File JavaDoc f = new File JavaDoc (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(""); // empty line for readability
212
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 JavaDoc e)
264         {
265             log("Exception " + e + " raised");
266             throw new BuildException(e);
267         }
268     }
269
270     /**
271      * Set the output file
272      *
273      * @param filename the file to write
274      */

275     public void setOutfile(String JavaDoc filename)
276     {
277         this.outFileName = filename;
278     }
279
280     /**
281      * Handle the attribute "refFile"
282      * @param aFile The reference file to be used
283      */

284     public void setRefFile(String JavaDoc aFile)
285     {
286         refFile = aFile;
287     }
288
289     
290     /**
291      * Handle the attribute "verbose".
292      *
293      * @param verbose The verbosity to be set
294      */

295     public void setVerbose(int verbose)
296     {
297         this.verbose = verbose;
298     }
299
300     /**
301      * Handle the attribute fileonerr
302      *
303      * @param failonerr The failonerr to set.
304      */

305     public void setFailonerr(boolean failonerr)
306     {
307         this.failonerr = failonerr;
308     }
309
310     /**
311      * Files to load
312      *
313      * @return A new {@link org.apache.tools.ant.types.FileSet} that
314      * represents a list of files to be read.
315      */

316     public FileSet createFileset()
317     {
318         FileSet set = new FileSet();
319         filesets.add(set);
320         return set;
321     }
322
323     
324     /* output routines */
325     private void output(String JavaDoc what)
326     {
327         output (what, false);
328     }
329
330     /* print output. In case dumpToConsole is true and output is redirected
331      * the line is printed to the console also
332      */

333     private void output(String JavaDoc 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 JavaDoc 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     /**
356      * ant handler for the messageSet token
357      *
358      * @return a new allocated message
359      */

360     public Message createMessageSet ()
361     {
362         Message msg = new Message ();
363         messages.add(msg);
364         return msg;
365     }
366
367
368     /**
369      * sub Class that represents a severity setting message
370      *
371      */

372     public class Message
373     {
374         private String JavaDoc id;
375         private int severity;
376         
377         /** bean constructor */
378         public Message() {}
379         
380         /** ant entrypoint to set the error name */
381         public void setError (String JavaDoc _id)
382         {
383             this.id = _id;
384         }
385     
386         /** ant entrypoint to set the severity */
387         public void setSeverity (String JavaDoc 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