| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 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 | | | |
| 52 | | | |
| 53 | | | <p> |
| 54 | | | |
| 55 | | | |
| 56 | | | |
| 57 | | | @author |
| 58 | | | |
| 59 | 0 | | 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 | | | |
| 72 | | | private static final String DEFAULT_STYLESHEET |
| 73 | | | = "xinclude.xsl"; |
| 74 | | | |
| 75 | | | private static final String APP_INFO_SCHEMA = "app-info.xsd"; |
| 76 | | | |
| 77 | | | |
| 78 | 0 | | private boolean mValidate = true; |
| 79 | | | |
| 80 | | | |
| 81 | | | |
| 82 | | | <tt></tt> |
| 83 | | | |
| 84 | | | @param |
| 85 | | | |
| 86 | | | public void setValidate (boolean b) |
| 87 | | | { |
| 88 | 0 | | mValidate = b; |
| 89 | 0 | | } |
| 90 | | | |
| 91 | | | |
| 92 | | | String getDefaultStyleSheet () |
| 93 | | | { |
| 94 | 0 | | return DEFAULT_STYLESHEET; |
| 95 | | | } |
| 96 | | | |
| 97 | | | void checkAttributes () |
| 98 | | | throws BuildException |
| 99 | | | { |
| 100 | | | |
| 101 | | | |
| 102 | 0 | | checkAttributeInFile(); |
| 103 | 0 | | checkAttributeOutFile(); |
| 104 | 0 | | checkAttributeXslFile(); |
| 105 | 0 | | } |
| 106 | | | |
| 107 | | | |
| 108 | | | void postExecute () |
| 109 | | | { |
| 110 | 0 | | if (mValidate) |
| 111 | | | { |
| 112 | 0 | | performSchemaValidation(getOutFile()); |
| 113 | | | } |
| 114 | 0 | | } |
| 115 | | | |
| 116 | | | private void performSchemaValidation (File inFile) |
| 117 | | | { |
| 118 | | | try |
| 119 | | | { |
| 120 | | | |
| 121 | 0 | | final SAXParserFactory factory = SAXParserFactory.newInstance(); |
| 122 | 0 | | factory.setNamespaceAware(true); |
| 123 | 0 | | factory.setValidating(true); |
| 124 | 0 | | final SAXParser parser = factory.newSAXParser(); |
| 125 | 0 | | parser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); |
| 126 | 0 | | parser.setProperty(JAXP_SCHEMA_SOURCE, |
| 127 | | | AppInfoTask.class.getResource(APP_INFO_SCHEMA).toExternalForm()); |
| 128 | | | |
| 129 | 0 | | final AppInfoSaxHandler handler = new AppInfoSaxHandler(); |
| 130 | 0 | | parser.parse(new InputSource(new FileInputStream(inFile)), handler); |
| 131 | 0 | | if (handler.hasValidationErrors()) |
| 132 | | | { |
| 133 | 0 | | final SAXException e = handler.getParseException(); |
| 134 | 0 | | throw new BuildException("XML Schema validation failed: " + e, e); |
| 135 | | | } |
| 136 | 0 | | if (handler.hasWarningMessages()) |
| 137 | | | { |
| 138 | 0 | | final List messages = handler.getWarningMessages(); |
| 139 | 0 | | for (final Iterator iterator = messages.iterator(); |
| 140 | 0 | | iterator.hasNext();) |
| 141 | | | { |
| 142 | 0 | | final String msg = (String) iterator.next(); |
| 143 | 0 | | log(msg, Project.MSG_WARN); |
| 144 | 0 | | } |
| 145 | | | } |
| 146 | 0 | | log(inFile + " validated successfully.", Project.MSG_INFO); |
| 147 | | | } |
| 148 | 0 | | catch (Exception e) |
| 149 | | | { |
| 150 | 0 | | throw new BuildException("XML Schema validation failed: " + e, e); |
| 151 | 0 | | } |
| 152 | 0 | | } |
| 153 | | | } |