1 57 package com.sun.org.apache.xerces.internal.jaxp.validation; 58 59 import javax.xml.validation.TypeInfoProvider ; 60 import javax.xml.validation.ValidatorHandler ; 61 62 import com.sun.org.apache.xerces.internal.impl.Constants; 63 import com.sun.org.apache.xerces.internal.impl.XMLEntityManager; 64 import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter; 65 import com.sun.org.apache.xerces.internal.impl.dv.XSSimpleType; 66 import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager; 67 import com.sun.org.apache.xerces.internal.impl.xs.XSMessageFormatter; 68 import com.sun.org.apache.xerces.internal.util.DraconianErrorHandler; 69 import com.sun.org.apache.xerces.internal.util.LocatorWrapper; 70 import com.sun.org.apache.xerces.internal.util.NamespaceSupport; 71 import com.sun.org.apache.xerces.internal.util.SymbolTable; 72 import com.sun.org.apache.xerces.internal.util.XMLAttributesImpl; 73 import com.sun.org.apache.xerces.internal.xni.Augmentations; 74 import com.sun.org.apache.xerces.internal.xni.QName; 75 import com.sun.org.apache.xerces.internal.xni.XMLAttributes; 76 import com.sun.org.apache.xerces.internal.xni.XMLLocator; 77 import com.sun.org.apache.xerces.internal.xni.XMLString; 78 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager; 79 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException; 80 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentFilter; 81 import com.sun.org.apache.xerces.internal.xs.AttributePSVI; 82 import com.sun.org.apache.xerces.internal.xs.ElementPSVI; 83 import com.sun.org.apache.xerces.internal.xs.ItemPSVI; 84 import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition; 85 import org.w3c.dom.TypeInfo ; 86 import org.w3c.dom.ls.LSResourceResolver ; 87 import org.xml.sax.Attributes ; 88 import org.xml.sax.ContentHandler ; 89 import org.xml.sax.ErrorHandler ; 90 import org.xml.sax.Locator ; 91 import org.xml.sax.SAXException ; 92 import org.xml.sax.SAXNotRecognizedException ; 93 import org.xml.sax.SAXNotSupportedException ; 94 95 110 final class ValidatorHandlerImpl extends ValidatorHandler { 111 113 116 private final InsulatedValidatorComponent validator; 117 118 121 private final XMLDocumentFilter validatorFilter; 122 123 127 private final XNI2SAXEx xni2sax = new XNI2SAXEx(); 128 129 130 private final SymbolTable symbolTable = new SymbolTable(); 132 private final NamespaceSupport nsContext = new NamespaceSupport(); 133 private final ValidationManager validationManager = new ValidationManager(); 134 private final XMLEntityManager entityManager = new XMLEntityManager(); 135 136 137 private final XMLErrorReporter errorReporter = new XMLErrorReporter(); 138 139 140 private ErrorHandler errorHandler; 141 142 143 private boolean inStartElement; 144 145 146 private boolean namespacePrefixesFeature = false; 147 148 152 private final ErrorHandlerAdaptor xercesErrorHandler = new ErrorHandlerAdaptor() { 153 protected ErrorHandler getErrorHandler() { 154 if(errorHandler==null ) 155 return DraconianErrorHandler.theInstance; 156 else 157 return errorHandler; 158 } 159 }; 160 161 162 private LSResourceResolver resourceResolver; 163 164 165 166 167 ValidatorHandlerImpl( InsulatedValidatorComponent validator ) { 168 this.validator = validator; 169 this.validatorFilter = validator.getValidator(); 170 171 errorReporter.putMessageFormatter( 173 XSMessageFormatter.SCHEMA_DOMAIN, 174 new XSMessageFormatter()); 175 } 176 177 185 private final Augmentations getCurrentAugmentation() { 186 return xni2sax.getCurrentAugmentation(); 187 } 188 189 194 private final XMLAttributes getCurrentAttributes() { 195 return xni2sax.getCurrentAttributes(); 196 } 197 198 199 public boolean getFeature(String name) throws SAXNotRecognizedException , SAXNotSupportedException { 200 if( name.equals("http://xml.org/sax/features/namespace-prefixes") ) 201 return namespacePrefixesFeature; 202 return super.getFeature(name); 203 } 204 205 public void setFeature(String name, boolean value) throws SAXNotRecognizedException , SAXNotSupportedException { 206 if( name.equals("http://xml.org/sax/features/namespace-prefixes") ) { 207 namespacePrefixesFeature = value; 208 return; 209 } 210 super.setFeature(name, value); 211 } 212 213 214 215 221 public boolean isValidSoFar() { 222 return !xercesErrorHandler.hadError(); 223 } 224 225 public void setErrorHandler(ErrorHandler errorHandler) { 226 this.errorHandler = errorHandler; 227 } 228 229 public ErrorHandler getErrorHandler() { 230 return errorHandler; 231 } 232 233 public void setResourceResolver(LSResourceResolver entityResolver) { 234 this.resourceResolver = entityResolver; 235 } 236 237 public LSResourceResolver getResourceResolver() { 238 return resourceResolver; 239 } 240 241 public final void setContentHandler(ContentHandler result) { 242 xni2sax.setContentHandler(result); 243 if(result==null) validatorFilter.setDocumentHandler(null); 244 else validatorFilter.setDocumentHandler(xni2sax); 245 } 246 247 public final ContentHandler getContentHandler() { 248 return xni2sax.getContentHandler(); 249 } 250 251 252 253 254 private final XMLComponentManager manager = new XMLComponentManager() { 260 public Object getProperty( String propName ) { 261 if( propName.equals(XercesConstants.SYMBOL_TABLE) ) 262 return symbolTable; 263 if( propName.equals(XercesConstants.VALIDATION_MANAGER) ) 264 return validationManager; 265 if( propName.equals(XercesConstants.ERROR_REPORTER) ) 266 return errorReporter; 267 if( propName.equals(XercesConstants.ERROR_HANDLER) ) 268 return xercesErrorHandler; 269 if( propName.equals(XercesConstants.ENTITY_MANAGER) ) 270 return entityManager; 271 if( propName.equals(XercesConstants.ENTITY_RESOLVER) ) 272 return entityManager; 273 274 throw new XMLConfigurationException( 275 XMLConfigurationException.NOT_RECOGNIZED, propName ); 276 } 277 public boolean getFeature( String propName ) { 278 if( propName.equals(XercesConstants.VALIDATION) ) 282 return true; 284 throw new XMLConfigurationException( 285 XMLConfigurationException.NOT_RECOGNIZED, propName ); 286 } 287 }; 288 289 public void startDocument() throws SAXException { 295 try { 296 resetComponents(); 297 298 XMLLocator xmlLocator = (locator==null)?null:new LocatorWrapper(locator); 299 300 errorReporter.setDocumentLocator(xmlLocator); 302 303 validatorFilter.startDocument( 304 xmlLocator, 305 null, 306 nsContext, 307 null); 308 } catch( WrappedSAXException e ) { 309 throw e.exception; 310 } 311 } 312 313 316 private void resetComponents() { 317 xercesErrorHandler.reset(); 319 nsContext.reset(); 320 errorReporter.reset(manager); 321 validator.reset(manager); 322 } 323 324 public void endDocument() throws SAXException { 325 try { 326 validatorFilter.endDocument(null); 327 } catch( WrappedSAXException e ) { 328 throw e.exception; 329 } 330 } 331 332 public void startElement( String uri, String local, String qname, Attributes att ) throws SAXException { 333 try { 334 inStartElement = true; 335 validatorFilter.startElement(createQName(uri,local,qname),createAttributes(att),null); 336 } catch( WrappedSAXException e ) { 337 throw e.exception; 338 } finally { 339 inStartElement = false; 340 } 341 } 342 343 public void endElement( String uri, String local, String qname ) throws SAXException { 344 try { 345 validatorFilter.endElement(createQName(uri,local,qname),null); 346 } catch( WrappedSAXException e ) { 347 throw e.exception; 348 } 349 } 350 351 public void characters( char[] buf, int offset, int len ) throws SAXException { 352 try { 353 validatorFilter.characters(new XMLString(buf,offset,len),null); 354 } catch( WrappedSAXException e ) { 355 throw e.exception; 356 } 357 } 358 359 public void ignorableWhitespace( char[] buf, int offset, int len ) throws SAXException { 360 try { 361 validatorFilter.ignorableWhitespace(new XMLString(buf,offset,len),null); 362 } catch( WrappedSAXException e ) { 363 throw e.exception; 364 } 365 } 366 367 public void startPrefixMapping( String prefix, String uri ) { 368 nsContext.pushContext(); 369 nsContext.declarePrefix(prefix,uri); 370 } 371 372 public void endPrefixMapping( String prefix ) { 373 nsContext.popContext(); 374 } 375 376 public void processingInstruction( String target, String data ) throws SAXException { 377 try { 378 validatorFilter.processingInstruction( 379 symbolize(target),createXMLString(data),null); 380 } catch( WrappedSAXException e ) { 381 throw e.exception; 382 } 383 } 384 385 public void skippedEntity( String name ) { 386 ContentHandler handler = getContentHandler(); 389 if( handler!=null ) 390 skippedEntity(name); 391 } 392 393 private Locator locator; 394 public void setDocumentLocator( Locator _loc ) { 395 this.locator = _loc; 396 } 397 398 399 public TypeInfoProvider getTypeInfoProvider() { 400 return typeInfoProvider; 401 } 402 403 404 409 private final TypeInfoProvider typeInfoProvider = new TypeInfoProvider () { 410 415 private void checkState() { 416 if( !inStartElement ) 417 throw new IllegalStateException (); 418 } 419 420 public TypeInfo getAttributeTypeInfo(int index) { 421 checkState(); 422 return getAttributeType(index); 423 } 424 425 private XSTypeDefinition getAttributeType( int index ) { 426 checkState(); 427 XMLAttributes atts = getCurrentAttributes(); 428 if( index<0 || atts.getLength()<=index ) 429 throw new IndexOutOfBoundsException (Integer.toString(index)); 430 Augmentations augs = atts.getAugmentations(index); 431 if(augs==null) return null; 432 AttributePSVI psvi = (AttributePSVI)augs.getItem(Constants.ATTRIBUTE_PSVI); 433 return getTypeInfoFromPSVI(psvi); 434 } 435 436 public TypeInfo getAttributeTypeInfo(String attributeUri, String attributeLocalName) { 437 checkState(); 438 return getAttributeTypeInfo(getCurrentAttributes().getIndex(attributeUri,attributeLocalName)); 439 } 440 441 public TypeInfo getAttributeTypeInfo(String attributeQName) { 442 checkState(); 443 return getAttributeTypeInfo(getCurrentAttributes().getIndex(attributeQName)); 444 } 445 446 public TypeInfo getElementTypeInfo() { 447 checkState(); 448 Augmentations augs = getCurrentAugmentation(); 449 if(augs==null) return null; 450 ElementPSVI psvi = (ElementPSVI)augs.getItem(Constants.ELEMENT_PSVI); 451 return getTypeInfoFromPSVI(psvi); 452 } 453 454 private XSTypeDefinition getTypeInfoFromPSVI( ItemPSVI psvi ) { 455 if(psvi==null) return null; 456 457 if( psvi.getValidity()== ElementPSVI.VALIDITY_VALID ) { 462 XSTypeDefinition t = psvi.getMemberTypeDefinition(); 463 if(t!=null) return t; 464 } 465 466 XSTypeDefinition t = psvi.getTypeDefinition(); 467 if(t!=null) return t; return null; 469 } 470 471 public boolean isIdAttribute(int index) { 472 checkState(); 473 XSSimpleType type = (XSSimpleType)getAttributeType(index); 474 if(type==null) return false; 475 return type.isIDType(); 476 } 477 478 public boolean isSpecified(int index) { 479 checkState(); 480 return getCurrentAttributes().isSpecified(index); 481 } 482 }; 483 484 485 486 492 private String symbolize(String s) { 493 if (s == null) 494 return null; 495 else 496 return symbolTable.addSymbol(s); 497 } 498 499 500 private QName createQName(String uri, String local, String raw) { 501 502 if( local.length()==0 ) { 503 uri = ""; 506 local = raw; 507 } 508 509 int idx = raw.indexOf(':'); 510 String prefix; 511 if (idx < 0) 512 prefix = null; 513 else 514 prefix = raw.substring(0, idx); 515 516 if (uri != null && uri.length() == 0) 517 uri = null; 519 return new QName(symbolize(prefix), symbolize(local), symbolize(raw), symbolize(uri)); 520 } 521 522 523 private final XMLAttributes xa = new XMLAttributesImpl(); 524 525 526 private XMLAttributes createAttributes(Attributes att) { 527 xa.removeAllAttributes(); 528 int len = att.getLength(); 529 for (int i = 0; i < len; i++) { 530 int idx = xa.addAttribute( 531 createQName(att.getURI(i), att.getLocalName(i), att.getQName(i)), 532 att.getType(i), 533 att.getValue(i)); 534 xa.setSpecified(idx,true); 537 } 538 return xa; 539 } 540 541 private XMLString createXMLString(String str) { 542 545 return new XMLString(str.toCharArray(), 0, str.length()); 547 } 548 549 555 public void reset() { 556 resetComponents(); 557 errorHandler = null; 558 resourceResolver = null; 559 } 560 } 561 | Popular Tags |