1 package org.jbpm.jpdl.xml; 2 3 import java.io.IOException ; 4 import java.io.Reader ; 5 import java.util.ArrayList ; 6 import java.util.List ; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 import org.dom4j.Document; 11 import org.dom4j.DocumentException; 12 import org.dom4j.io.SAXReader; 13 import org.xml.sax.EntityResolver ; 14 import org.xml.sax.InputSource ; 15 import org.xml.sax.SAXException ; 16 import org.xml.sax.SAXParseException ; 17 import org.xml.sax.helpers.DefaultHandler ; 18 19 28 public class SchemaValidationHelper 29 { 30 private static final String JAXP_SCHEMA_LANGUAGE = 31 "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; 32 private static final String W3C_XML_SCHEMA = 33 "http://www.w3.org/2001/XMLSchema"; 34 35 39 40 private Reader reader; 41 private String schemaName; 42 private String subject; 43 private List errors = new ArrayList (); 44 private Document document; 45 private boolean valid = true; 46 47 55 public SchemaValidationHelper(Reader reader, String schemaName, String subject) 56 { 57 this.reader = reader; 58 this.schemaName = schemaName; 59 this.subject = subject; 60 } 61 62 70 public boolean isValid() 71 { 72 try 73 { 74 75 SAXReader saxReader = new SAXReader( ); 76 77 saxReader.setErrorHandler( new Handler () ); 79 saxReader.setEntityResolver( new Resolver () ); 80 81 try 82 { 83 saxReader.setFeature("http://apache.org/xml/features/validation/schema", true); 85 saxReader.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true); 87 saxReader.setFeature("http://apache.org/xml/features/validation/dynamic", true); 89 saxReader.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); 91 saxReader.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation", "http://jbpm.org/3/jpdl jpdl-3.0.xsd"); 93 } 94 catch(SAXException se) 95 { 96 log.warn("Unable to validate using schema. Make sure Xerces is first in your class path."); 97 } 98 99 document = saxReader.read( reader ); 100 101 } 102 catch(DocumentException de) 103 { 104 log.error("Parsing problems", de); 105 } 106 107 return valid; 108 } 109 110 115 public List getProblems() 116 { 117 return errors; 118 } 119 120 125 public Document getDocument() 126 { 127 return document; 128 } 129 130 135 class Handler extends DefaultHandler 136 { 137 public void warning(SAXParseException pe) 138 { 139 errors.add( 140 new Problem( Problem.LEVEL_WARNING, 141 subject + " line " + pe.getLineNumber() + 142 ": " + pe.getMessage() ) ); 143 } 144 145 public void error(SAXParseException pe) 146 { 147 errors.add( 148 new Problem( Problem.LEVEL_ERROR, 149 subject + " line " + pe.getLineNumber() + 150 ": " + pe.getMessage() ) ); 151 valid = false; 152 } 153 154 public void fatalError(SAXParseException pe) 155 { 156 errors.add( 157 new Problem( Problem.LEVEL_FATAL, 158 subject + " line " + pe.getLineNumber() + 159 ": " + pe.getMessage() ) ); 160 valid = false; 161 } 162 } 163 164 169 class Resolver implements EntityResolver 170 { 171 public InputSource resolveEntity(String publicId, String SystemId) 172 throws SAXException , IOException 173 { 174 return new InputSource ( this.getClass() 175 .getResourceAsStream( schemaName ) 176 ); 177 } 178 179 } 180 181 private static final Log log = LogFactory.getLog( SchemaValidationHelper.class ); 182 } 183 | Popular Tags |