Project Report: fawkez

Packagesummary org.jcoderz.commons.taskdefs

org.jcoderz.commons.taskdefs.AppInfoTask

LineHitsNoteSource
1  /*
2   * $Id: AppInfoTask.java 1011 2008-06-16 17:57:36Z amandel $
3   *
4   * Copyright 2006, The jCoderZ.org Project. 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   *    * Redistributions of source code must retain the above copyright
11   *      notice, this list of conditions and the following disclaimer.
12   *    * Redistributions in binary form must reproduce the above
13   *      copyright notice, this list of conditions and the following
14   *      disclaimer in the documentation and/or other materials
15   *      provided with the distribution.
16   *    * Neither the name of the jCoderZ.org Project nor the names of
17   *      its contributors may be used to endorse or promote products
18   *      derived from this software without specific prior written
19   *      permission.
20   *
21   * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25   * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32   */
33  package org.jcoderz.commons.taskdefs;
34  
35  
36  import java.io.File;
37  import java.io.FileInputStream;
38  import java.util.Iterator;
39  import java.util.List;
40  
41  import javax.xml.parsers.SAXParser;
42  import javax.xml.parsers.SAXParserFactory;
43  
44  import org.apache.tools.ant.BuildException;
45  import org.apache.tools.ant.Project;
46  import org.xml.sax.InputSource;
47  import org.xml.sax.SAXException;
48  
49  
50  /**
51   * Ant task that reads the master app-info.xml file, expands the XInclude
52   * elements and writes the whole XML document to a file.
53   * <p>
54   * This task can also perform a XML schema validation and perform
55   * some checks that cannot be done via the XML schema.
56   *
57   * @author Michael Griffel
58   */
590 public final class AppInfoTask
60        extends XsltBasedTask
61  {
62     private static final String JAXP_SCHEMA_LANGUAGE
63           = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
64  
65     private static final String W3C_XML_SCHEMA
66           = "http://www.w3.org/2001/XMLSchema";
67  
68     private static final String JAXP_SCHEMA_SOURCE
69           = "http://java.sun.com/xml/jaxp/properties/schemaSource";
70  
71     /** The default stylesheet name. */
72     private static final String DEFAULT_STYLESHEET
73           = "xinclude.xsl";
74  
75     private static final String APP_INFO_SCHEMA = "app-info.xsd";
76  
77     /** flag indicating if we should validate the app-info.xml. */
780    private boolean mValidate = true;
79  
80     /**
81      * Set whether we should validate the app-info.xml file or not.
82      * Default is <tt>true</tt>.
83      *
84      * @param b Whether we should validate the app-info.xml file or not.
85      */
86     public void setValidate (boolean b)
87     {
880       mValidate  = b;
890    }
90  
91  
92     String getDefaultStyleSheet ()
93     {
940       return DEFAULT_STYLESHEET;
95     }
96  
97     void checkAttributes ()
98           throws BuildException
99     {
100        // we don'T need the destDir attribute,
101        // so we don't call super.checkAttributes() here
1020       checkAttributeInFile();
1030       checkAttributeOutFile();
1040       checkAttributeXslFile();
1050    }
106  
107  
108     void postExecute ()
109     {
1100       if (mValidate)
111        {
1120          performSchemaValidation(getOutFile());
113        }
1140    }
115  
116     private void performSchemaValidation (File inFile)
117     {
118        try
119        {
120           // create a new XML parser
1210          final SAXParserFactory factory = SAXParserFactory.newInstance();
1220          factory.setNamespaceAware(true);
1230          factory.setValidating(true);
1240          final SAXParser parser = factory.newSAXParser();
1250          parser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
1260          parser.setProperty(JAXP_SCHEMA_SOURCE,
127                 AppInfoTask.class.getResource(APP_INFO_SCHEMA).toExternalForm());
128  
1290          final AppInfoSaxHandler handler = new AppInfoSaxHandler();
1300          parser.parse(new InputSource(new FileInputStream(inFile)), handler);
1310          if (handler.hasValidationErrors())
132           {
1330             final SAXException e = handler.getParseException();
1340             throw new BuildException("XML Schema validation failed: " + e, e);
135           }
1360          if (handler.hasWarningMessages())
137           {
1380             final List messages = handler.getWarningMessages();
1390             for (final Iterator iterator = messages.iterator();
1400                   iterator.hasNext();)
141              {
1420                final String msg = (String) iterator.next();
1430                log(msg, Project.MSG_WARN);
1440             }
145           }
1460          log(inFile + " validated successfully.", Project.MSG_INFO);
147        }
1480       catch (Exception e)
149        {
1500          throw new BuildException("XML Schema validation failed: " + e, e);
1510       }
1520    }
153  }

Findings in this File