1 package com.thaiopensource.validate; 2 3 import org.xml.sax.SAXException ; 4 import org.xml.sax.InputSource ; 5 import org.xml.sax.DTDHandler ; 6 import org.xml.sax.XMLReader ; 7 import org.xml.sax.ErrorHandler ; 8 9 import java.io.IOException ; 10 import java.io.File ; 11 import java.net.MalformedURLException ; 12 13 import com.thaiopensource.util.UriOrFile; 14 import com.thaiopensource.util.PropertyMap; 15 import com.thaiopensource.util.PropertyMapBuilder; 16 import com.thaiopensource.util.PropertyId; 17 import com.thaiopensource.xml.sax.XMLReaderCreator; 18 import com.thaiopensource.xml.sax.CountingErrorHandler; 19 import com.thaiopensource.xml.sax.Jaxp11XMLReaderCreator; 20 import com.thaiopensource.xml.sax.ErrorHandlerImpl; 21 import com.thaiopensource.validate.auto.AutoSchemaReader; 22 23 29 30 public class ValidationDriver { 31 private static final PropertyId[] requiredProperties = { 32 ValidateProperty.XML_READER_CREATOR, 33 ValidateProperty.ERROR_HANDLER 34 }; 35 36 private static final Class [] defaultClasses = { 37 Jaxp11XMLReaderCreator.class, 38 ErrorHandlerImpl.class 39 }; 40 41 private final XMLReaderCreator xrc; 42 private XMLReader xr; 43 private final CountingErrorHandler eh; 44 private final SchemaReader sr; 45 private final PropertyMap schemaProperties; 46 private final PropertyMap instanceProperties; 47 private Validator validator; 48 private Schema schema; 49 50 61 public ValidationDriver(PropertyMap schemaProperties, 62 PropertyMap instanceProperties, 63 SchemaReader schemaReader) { 64 PropertyMapBuilder builder = new PropertyMapBuilder(schemaProperties); 65 for (int i = 0; i < requiredProperties.length; i++) { 66 if (!builder.contains(requiredProperties[i])) { 67 try { 68 builder.put(requiredProperties[i], 69 defaultClasses[i].newInstance()); 70 } 71 catch (InstantiationException e) { 72 } 73 catch (IllegalAccessException e) { 74 } 75 } 76 } 77 this.schemaProperties = builder.toPropertyMap(); 78 builder = new PropertyMapBuilder(instanceProperties); 79 for (int i = 0; i < requiredProperties.length; i++) { 80 if (!builder.contains(requiredProperties[i])) 81 builder.put(requiredProperties[i], 82 this.schemaProperties.get(requiredProperties[i])); 83 } 84 eh = new CountingErrorHandler((ErrorHandler )builder.get(ValidateProperty.ERROR_HANDLER)); 85 ValidateProperty.ERROR_HANDLER.put(builder, eh); 86 this.instanceProperties = builder.toPropertyMap(); 87 this.xrc = ValidateProperty.XML_READER_CREATOR.get(this.instanceProperties); 88 this.sr = schemaReader == null ? new AutoSchemaReader() : schemaReader; 89 } 90 91 96 public ValidationDriver(PropertyMap schemaProperties, PropertyMap instanceProperties) { 97 this(schemaProperties, instanceProperties, null); 98 } 99 100 105 public ValidationDriver(PropertyMap properties, SchemaReader sr) { 106 this(properties, properties, sr); 107 } 108 109 114 public ValidationDriver(PropertyMap properties) { 115 this(properties, properties, null); 116 } 117 118 123 public ValidationDriver(SchemaReader sr) { 124 this(PropertyMap.EMPTY, sr); 125 } 126 127 132 public ValidationDriver() { 133 this(PropertyMap.EMPTY, PropertyMap.EMPTY, null); 134 } 135 136 146 public boolean loadSchema(InputSource in) throws SAXException , IOException { 147 try { 148 schema = sr.createSchema(in, schemaProperties); 149 validator = null; 150 return true; 151 } 152 catch (IncorrectSchemaException e) { 153 return false; 154 } 155 } 156 157 167 public boolean validate(InputSource in) throws SAXException , IOException { 168 if (schema == null) 169 throw new IllegalStateException ("cannot validate without schema"); 170 if (validator == null) 171 validator = schema.createValidator(instanceProperties); 172 if (xr == null) { 173 xr = xrc.createXMLReader(); 174 xr.setErrorHandler(eh); 175 } 176 eh.reset(); 177 xr.setContentHandler(validator.getContentHandler()); 178 DTDHandler dh = validator.getDTDHandler(); 179 if (dh != null) 180 xr.setDTDHandler(dh); 181 try { 182 xr.parse(in); 183 return !eh.getHadErrorOrFatalError(); 184 } 185 finally { 186 validator.reset(); 187 } 188 } 189 190 196 static public InputSource fileInputSource(String filename) throws MalformedURLException { 197 return ValidationDriver.fileInputSource(new File (filename)); 198 } 199 200 206 static public InputSource fileInputSource(File file) throws MalformedURLException { 207 return new InputSource (UriOrFile.fileToUri(file)); 208 } 209 210 218 static public InputSource uriOrFileInputSource(String uriOrFile) throws MalformedURLException { 219 return new InputSource (UriOrFile.toUri(uriOrFile)); 220 } 221 } 222 | Popular Tags |