root/trunk/src/java/org/jcoderz/commons/taskdefs/AppInfoTask.java

Revision 1011, 5.0 kB (checked in by amandel, 4 years ago)

Aligned svn keyword settings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
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 */
33package org.jcoderz.commons.taskdefs;
34
35
36import java.io.File;
37import java.io.FileInputStream;
38import java.util.Iterator;
39import java.util.List;
40
41import javax.xml.parsers.SAXParser;
42import javax.xml.parsers.SAXParserFactory;
43
44import org.apache.tools.ant.BuildException;
45import org.apache.tools.ant.Project;
46import org.xml.sax.InputSource;
47import 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 */
59public 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. */
78   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   {
88      mValidate  = b;
89   }
90
91
92   String getDefaultStyleSheet ()
93   {
94      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
102      checkAttributeInFile();
103      checkAttributeOutFile();
104      checkAttributeXslFile();
105   }
106
107
108   void postExecute ()
109   {
110      if (mValidate)
111      {
112         performSchemaValidation(getOutFile());
113      }
114   }
115
116   private void performSchemaValidation (File inFile)
117   {
118      try
119      {
120         // create a new XML parser
121         final SAXParserFactory factory = SAXParserFactory.newInstance();
122         factory.setNamespaceAware(true);
123         factory.setValidating(true);
124         final SAXParser parser = factory.newSAXParser();
125         parser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
126         parser.setProperty(JAXP_SCHEMA_SOURCE,
127               AppInfoTask.class.getResource(APP_INFO_SCHEMA).toExternalForm());
128
129         final AppInfoSaxHandler handler = new AppInfoSaxHandler();
130         parser.parse(new InputSource(new FileInputStream(inFile)), handler);
131         if (handler.hasValidationErrors())
132         {
133            final SAXException e = handler.getParseException();
134            throw new BuildException("XML Schema validation failed: " + e, e);
135         }
136         if (handler.hasWarningMessages())
137         {
138            final List messages = handler.getWarningMessages();
139            for (final Iterator iterator = messages.iterator();
140                  iterator.hasNext();)
141            {
142               final String msg = (String) iterator.next();
143               log(msg, Project.MSG_WARN);
144            }
145         }
146         log(inFile + " validated successfully.", Project.MSG_INFO);
147      }
148      catch (Exception e)
149      {
150         throw new BuildException("XML Schema validation failed: " + e, e);
151      }
152   }
153}
Note: See TracBrowser for help on using the browser.