1 package com.thaiopensource.validate.xerces; 2 3 import com.thaiopensource.util.PropertyMap; 4 import com.thaiopensource.validate.ValidateProperty; 5 import com.thaiopensource.validate.Validator; 6 import org.apache.xerces.impl.XMLErrorReporter; 7 import org.apache.xerces.impl.validation.EntityState; 8 import org.apache.xerces.impl.validation.ValidationManager; 9 import org.apache.xerces.impl.xs.XMLSchemaValidator; 10 import org.apache.xerces.util.NamespaceSupport; 11 import org.apache.xerces.util.ParserConfigurationSettings; 12 import org.apache.xerces.util.SymbolTable; 13 import org.apache.xerces.util.XMLAttributesImpl; 14 import org.apache.xerces.util.XMLSymbols; 15 import org.apache.xerces.util.ErrorHandlerWrapper; 16 import org.apache.xerces.xni.NamespaceContext; 17 import org.apache.xerces.xni.QName; 18 import org.apache.xerces.xni.XMLAttributes; 19 import org.apache.xerces.xni.XMLLocator; 20 import org.apache.xerces.xni.XMLResourceIdentifier; 21 import org.apache.xerces.xni.XMLString; 22 import org.apache.xerces.xni.XNIException; 23 import org.apache.xerces.xni.grammars.XMLGrammarPool; 24 import org.apache.xerces.xni.parser.XMLComponent; 25 import org.apache.xerces.xni.parser.XMLEntityResolver; 26 import org.apache.xerces.xni.parser.XMLInputSource; 27 import org.apache.xerces.xni.parser.XMLParseException; 28 import org.apache.xerces.xni.parser.XMLErrorHandler; 29 import org.xml.sax.Attributes ; 30 import org.xml.sax.Locator ; 31 import org.xml.sax.SAXException ; 32 import org.xml.sax.SAXParseException ; 33 import org.xml.sax.DTDHandler ; 34 import org.xml.sax.ContentHandler ; 35 36 import java.io.IOException ; 37 import java.util.Hashtable ; 38 39 class ValidatorImpl extends ParserConfigurationSettings implements Validator, ContentHandler , DTDHandler , XMLLocator, XMLEntityResolver, EntityState { 40 41 private final XMLSchemaValidator schemaValidator = new XMLSchemaValidator(); 42 private final XMLErrorReporter errorReporter = new XMLErrorReporter(); 43 private final ValidationManager validationManager = new ValidationManager(); 44 private final NamespaceContext namespaceContext = new NamespaceSupport(); 45 private final XMLAttributes attributes = new XMLAttributesImpl(); 46 private final SymbolTable symbolTable; 47 private final XMLComponent[] components; 48 private Locator locator; 49 private final Hashtable entityTable = new Hashtable (); 50 private boolean pushedContext = false; 51 52 54 static private final String [] recognizedFeatures = { 55 Features.SCHEMA_AUGMENT_PSVI, 56 Features.SCHEMA_FULL_CHECKING, 57 Features.VALIDATION, 58 Features.SCHEMA_VALIDATION, 59 }; 60 61 static private final String [] recognizedProperties = { 62 Properties.XMLGRAMMAR_POOL, 63 Properties.SYMBOL_TABLE, 64 Properties.ERROR_REPORTER, 65 Properties.ERROR_HANDLER, 66 Properties.VALIDATION_MANAGER, 67 Properties.ENTITY_MANAGER, 68 Properties.ENTITY_RESOLVER, 69 }; 70 71 ValidatorImpl(SymbolTable symbolTable, XMLGrammarPool grammarPool, PropertyMap properties) { 72 this.symbolTable = symbolTable; 73 XMLErrorHandler errorHandlerWrapper = new ErrorHandlerWrapper(ValidateProperty.ERROR_HANDLER.get(properties)); 74 components = new XMLComponent[] { errorReporter, schemaValidator }; 75 for (int i = 0; i < components.length; i++) { 76 addRecognizedFeatures(components[i].getRecognizedFeatures()); 77 addRecognizedProperties(components[i].getRecognizedProperties()); 78 } 79 addRecognizedFeatures(recognizedFeatures); 80 addRecognizedProperties(recognizedProperties); 81 setFeature(Features.SCHEMA_AUGMENT_PSVI, false); 82 setFeature(Features.SCHEMA_FULL_CHECKING, true); 83 setFeature(Features.VALIDATION, true); 84 setFeature(Features.SCHEMA_VALIDATION, true); 85 setProperty(Properties.XMLGRAMMAR_POOL, grammarPool); 86 setProperty(Properties.SYMBOL_TABLE, symbolTable); 87 errorReporter.setDocumentLocator(this); 88 setProperty(Properties.ERROR_REPORTER, errorReporter); 89 setProperty(Properties.ERROR_HANDLER, errorHandlerWrapper); 90 setProperty(Properties.VALIDATION_MANAGER, validationManager); 91 setProperty(Properties.ENTITY_MANAGER, this); 94 setProperty(Properties.ENTITY_RESOLVER, this); 95 reset(); 96 } 97 98 public void reset() { 99 validationManager.reset(); 100 namespaceContext.reset(); 101 for (int i = 0; i < components.length; i++) 102 components[i].reset(this); 103 validationManager.setEntityState(this); 104 } 105 106 public ContentHandler getContentHandler() { 107 return this; 108 } 109 110 public DTDHandler getDTDHandler() { 111 return this; 112 } 113 114 public void setDocumentLocator(Locator locator) { 115 this.locator = locator; 116 } 117 118 public void notationDecl(String name, 119 String publicId, 120 String systemId) { 121 } 123 124 public void unparsedEntityDecl(String name, 125 String publicId, 126 String systemId, 127 String notationName) { 128 entityTable.put(name, name); 129 } 130 131 public boolean isEntityDeclared(String name) { 132 return entityTable.get(name) != null; 133 } 134 135 public boolean isEntityUnparsed(String name) { 136 return entityTable.get(name) != null; 137 } 138 139 public void startDocument() 140 throws SAXException { 141 try { 142 schemaValidator.startDocument(locator == null ? null : this, null, namespaceContext, null); 143 } 144 catch (XNIException e) { 145 throw toSAXException(e); 146 } 147 } 148 149 public void endDocument() 150 throws SAXException { 151 try { 152 schemaValidator.endDocument(null); 153 } 154 catch (XNIException e) { 155 throw toSAXException(e); 156 } 157 } 158 159 public void startElement(String namespaceURI, String localName, 160 String qName, Attributes atts) 161 throws SAXException { 162 try { 163 if (!pushedContext) 164 namespaceContext.pushContext(); 165 else 166 pushedContext = false; 167 for (int i = 0, len = atts.getLength(); i < len; i++) 168 attributes.addAttribute(makeQName(atts.getURI(i), atts.getLocalName(i), atts.getQName(i)), 169 symbolTable.addSymbol(atts.getType(i)), 170 atts.getValue(i)); 171 schemaValidator.startElement(makeQName(namespaceURI, localName, qName), attributes, null); 172 attributes.removeAllAttributes(); 173 } 174 catch (XNIException e) { 175 throw toSAXException(e); 176 } 177 } 178 179 public void endElement(String namespaceURI, String localName, 180 String qName) 181 throws SAXException { 182 try { 183 schemaValidator.endElement(makeQName(namespaceURI, localName, qName), null); 184 namespaceContext.popContext(); 185 } 186 catch (XNIException e) { 187 throw toSAXException(e); 188 } 189 } 190 191 public void startPrefixMapping(String prefix, String uri) 192 throws SAXException { 193 try { 194 if (!pushedContext) { 195 namespaceContext.pushContext(); 196 pushedContext = true; 197 } 198 if (prefix == null) 199 prefix = XMLSymbols.EMPTY_STRING; 200 else 201 prefix = symbolTable.addSymbol(prefix); 202 if (uri != null) { 203 if (uri.equals("")) 204 uri = null; 205 else 206 uri = symbolTable.addSymbol(uri); 207 } 208 namespaceContext.declarePrefix(prefix, uri); 209 } 210 catch (XNIException e) { 211 throw toSAXException(e); 212 } 213 } 214 215 public void endPrefixMapping(String prefix) 216 throws SAXException { 217 } 219 220 public void characters(char ch[], int start, int length) 221 throws SAXException { 222 try { 223 schemaValidator.characters(new XMLString(ch, start, length), null); 224 } 225 catch (XNIException e) { 226 throw toSAXException(e); 227 } 228 } 229 230 public void ignorableWhitespace(char ch[], int start, int length) 231 throws SAXException { 232 try { 233 schemaValidator.ignorableWhitespace(new XMLString(ch, start, length), null); 234 } 235 catch (XNIException e) { 236 throw toSAXException(e); 237 } 238 } 239 240 public void processingInstruction(String target, String data) 241 throws SAXException { 242 } 244 245 public void skippedEntity(String name) 246 throws SAXException { 247 } 249 250 private QName makeQName(String namespaceURI, String localName, String qName) { 251 localName = symbolTable.addSymbol(localName); 252 String prefix; 253 if (namespaceURI.equals("")) { 254 namespaceURI = null; 255 prefix = XMLSymbols.EMPTY_STRING; 256 qName = localName; 257 } 258 else { 259 namespaceURI = symbolTable.addSymbol(namespaceURI); 260 if (qName.equals("")) { 261 prefix = namespaceContext.getPrefix(namespaceURI); 262 if (prefix == XMLSymbols.EMPTY_STRING) 263 qName = localName; 264 else if (prefix == null) 265 qName = localName; else 267 qName = symbolTable.addSymbol(prefix + ":" + localName); 268 } 269 else { 270 qName = symbolTable.addSymbol(qName); 271 int colon = qName.indexOf(':'); 272 if (colon > 0) 273 prefix = symbolTable.addSymbol(qName.substring(0, colon)); 274 else 275 prefix = XMLSymbols.EMPTY_STRING; 276 } 277 } 278 return new QName(prefix, localName, qName, namespaceURI); 279 } 280 281 public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier) 282 throws XNIException, IOException { 283 return null; 284 } 285 286 public String getPublicId() { 287 return locator.getPublicId(); 288 } 289 290 public String getEncoding() { 291 return null; 292 } 293 294 public String getBaseSystemId() { 295 return null; 296 } 297 298 public String getLiteralSystemId() { 299 return null; 300 } 301 302 public String getExpandedSystemId() { 303 return locator.getSystemId(); 304 } 305 306 public int getLineNumber() { 307 return locator.getLineNumber(); 308 } 309 310 public int getColumnNumber() { 311 return locator.getColumnNumber(); 312 } 313 314 static SAXException toSAXException(XNIException e) { 315 if (e instanceof XMLParseException) { 316 XMLParseException pe = (XMLParseException)e; 317 return new SAXParseException (pe.getMessage(), 318 pe.getPublicId(), 319 pe.getExpandedSystemId(), 320 pe.getLineNumber(), 321 pe.getColumnNumber(), 322 pe.getException()); 323 } 324 Exception nested = e.getException(); 325 if (nested == null) 326 return new SAXException (e.getMessage()); 327 if (nested instanceof SAXException ) 328 return (SAXException )nested; 329 if (nested instanceof RuntimeException ) 330 throw (RuntimeException )nested; 331 return new SAXException (nested); 332 } 333 } 334 | Popular Tags |