1 57 package com.sun.org.apache.xerces.internal.jaxp; 58 59 import java.io.IOException ; 60 61 import javax.xml.validation.TypeInfoProvider ; 62 import javax.xml.validation.ValidatorHandler ; 63 64 import com.sun.org.apache.xerces.internal.dom.DOMInputImpl; 65 import com.sun.org.apache.xerces.internal.impl.Constants; 66 import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter; 67 import com.sun.org.apache.xerces.internal.util.AugmentationsImpl; 68 import com.sun.org.apache.xerces.internal.util.DraconianErrorHandler; 69 import com.sun.org.apache.xerces.internal.util.ErrorHandlerProxy; 70 import com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper; 71 import com.sun.org.apache.xerces.internal.util.SymbolTable; 72 import com.sun.org.apache.xerces.internal.util.TeeXMLDocumentFilterImpl; 73 import com.sun.org.apache.xerces.internal.util.XMLResourceIdentifierImpl; 74 import com.sun.org.apache.xerces.internal.xni.Augmentations; 75 import com.sun.org.apache.xerces.internal.xni.QName; 76 import com.sun.org.apache.xerces.internal.xni.XMLAttributes; 77 import com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler; 78 import com.sun.org.apache.xerces.internal.xni.XMLString; 79 import com.sun.org.apache.xerces.internal.xni.XNIException; 80 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent; 81 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager; 82 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException; 83 import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver; 84 import com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandler; 85 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource; 86 import org.w3c.dom.TypeInfo ; 87 import org.w3c.dom.ls.LSInput ; 88 import org.w3c.dom.ls.LSResourceResolver ; 89 import org.xml.sax.Attributes ; 90 import org.xml.sax.SAXException ; 91 import org.xml.sax.helpers.DefaultHandler ; 92 93 117 public class JAXPValidatorComponent extends TeeXMLDocumentFilterImpl implements XMLComponent { 118 119 private final ValidatorHandler validator; 121 private final XNI2SAX xni2sax = new XNI2SAX(); 122 private final SAX2XNI sax2xni = new SAX2XNI(); 123 124 private final TypeInfoProvider typeInfoProvider; 126 127 134 private Augmentations fCurrentAug; 135 136 139 private XMLAttributes fCurrentAttributes; 140 141 143 private SymbolTable fSymbolTable; 144 private XMLErrorReporter fErrorReporter; 145 private XMLEntityResolver fEntityResolver; 146 147 151 public JAXPValidatorComponent( ValidatorHandler validatorHandler ) { 152 this.validator = validatorHandler; 153 TypeInfoProvider tip = validatorHandler.getTypeInfoProvider();; 154 if(tip==null) tip = noInfoProvider; 155 this.typeInfoProvider = tip; 156 157 xni2sax.setContentHandler(validator); 159 validator.setContentHandler(sax2xni); 160 this.setSide(xni2sax); 161 162 validator.setErrorHandler(new ErrorHandlerProxy() { 164 protected XMLErrorHandler getErrorHandler() { 165 XMLErrorHandler handler = fErrorReporter.getErrorHandler(); 166 if(handler!=null) return handler; 167 return new ErrorHandlerWrapper(DraconianErrorHandler.theInstance); 168 } 169 }); 170 validator.setResourceResolver(new LSResourceResolver () { 171 public LSInput resolveResource(String type,String ns, String publicId, String systemId, String baseUri) { 172 if(fEntityResolver==null) return null; 173 try { 174 XMLInputSource is = fEntityResolver.resolveEntity( 175 new XMLResourceIdentifierImpl(publicId,systemId,baseUri,systemId)); 176 if(is==null) return null; 177 178 LSInput di = new DOMInputImpl(); 179 di.setBaseURI(is.getBaseSystemId()); 180 di.setByteStream(is.getByteStream()); 181 di.setCharacterStream(is.getCharacterStream()); 182 di.setEncoding(is.getEncoding()); 183 di.setPublicId(is.getPublicId()); 184 di.setSystemId(is.getSystemId()); 185 186 return di; 187 } catch( IOException e ) { 188 throw new XNIException(e); 191 } 192 } 193 }); 194 } 195 196 197 public void startElement(QName element, XMLAttributes attributes, Augmentations augs) throws XNIException { 198 fCurrentAttributes = attributes; 199 fCurrentAug = augs; 200 xni2sax.startElement(element,attributes,null); 201 fCurrentAttributes = null; } 203 204 public void endElement(QName element, Augmentations augs) throws XNIException { 205 fCurrentAug = augs; 206 xni2sax.endElement(element,null); 207 } 208 209 public void emptyElement(QName element, XMLAttributes attributes, Augmentations augs) throws XNIException { 210 startElement(element,attributes,augs); 211 endElement(element,augs); 212 } 213 214 215 public void characters(XMLString text, Augmentations augs) throws XNIException { 216 fCurrentAug = augs; 219 xni2sax.characters(text,null); 220 } 221 222 public void ignorableWhitespace(XMLString text, Augmentations augs) throws XNIException { 223 fCurrentAug = augs; 226 xni2sax.ignorableWhitespace(text,null); 227 } 228 229 public void reset(XMLComponentManager componentManager) throws XMLConfigurationException { 230 fSymbolTable = (SymbolTable)componentManager.getProperty( 232 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY); 233 234 fErrorReporter = (XMLErrorReporter)componentManager.getProperty( 235 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY); 236 237 } 238 239 240 249 private final class SAX2XNI extends DefaultHandler { 250 251 255 private final Augmentations fAugmentations = new AugmentationsImpl(); 256 257 261 private final QName fQName = new QName(); 262 263 public void characters(char[] ch, int start, int len) throws SAXException { 264 try { 265 handler().characters(new XMLString(ch,start,len),aug()); 266 } catch( XNIException e ) { 267 throw toSAXException(e); 268 } 269 } 270 271 public void ignorableWhitespace(char[] ch, int start, int len) throws SAXException { 272 try { 273 handler().ignorableWhitespace(new XMLString(ch,start,len),aug()); 274 } catch( XNIException e ) { 275 throw toSAXException(e); 276 } 277 } 278 279 public void startElement(String uri, String localName, String qname, Attributes atts) throws SAXException { 280 try { 281 updateAttributes(atts); 282 283 handler().startElement(toQName(uri,localName,qname), fCurrentAttributes, elementAug()); 284 } catch( XNIException e ) { 285 throw toSAXException(e); 286 } 287 } 288 289 public void endElement(String uri, String localName, String qname) throws SAXException { 290 try { 291 handler().endElement(toQName(uri,localName,qname),aug()); 292 } catch( XNIException e ) { 293 throw toSAXException(e); 294 } 295 } 296 297 private Augmentations elementAug() { 298 Augmentations aug = aug(); 299 aug.putItem(Constants.TYPEINFO,typeInfoProvider.getElementTypeInfo()); 300 return aug; 301 } 302 303 304 308 private Augmentations aug() { 309 if( fCurrentAug!=null ) { 310 Augmentations r = fCurrentAug; 311 fCurrentAug = null; return r; 313 } 314 fAugmentations.removeAllItems(); 315 return fAugmentations; 316 } 317 318 321 private XMLDocumentHandler handler() { 322 return JAXPValidatorComponent.this.getDocumentHandler(); 323 } 324 325 329 private SAXException toSAXException( XNIException xe ) { 330 Exception e = xe.getException(); 331 if( e==null ) e = xe; 332 if( e instanceof SAXException ) return (SAXException )e; 333 return new SAXException (e); 334 } 335 336 341 private QName toQName( String uri, String localName, String qname ) { 342 String prefix = null; 343 int idx = qname.indexOf(':'); 344 if( idx>0 ) 345 prefix = symbolize(qname.substring(0,idx)); 346 347 localName = symbolize(localName); 348 qname = symbolize(qname); 349 uri = symbolize(uri); 350 351 fQName.setValues(prefix, localName, qname, uri); 353 return fQName; 354 } 355 } 356 357 358 362 private void updateAttributes( Attributes atts ) { 363 int len = atts.getLength(); 364 for( int i=0; i<len; i++ ) { 365 String aqn = atts.getQName(i); 366 int j = fCurrentAttributes.getIndex(aqn); 367 String av = atts.getValue(i); 368 if(j==-1) { 369 371 String prefix; 372 int idx = aqn.indexOf(':'); 373 if( idx<0 ) { 374 prefix = null; 375 } else { 376 prefix = symbolize(aqn.substring(0,idx)); 377 } 378 379 j = fCurrentAttributes.addAttribute( 380 new QName( 381 prefix, 382 symbolize(atts.getLocalName(i)), 383 symbolize(aqn), 384 symbolize(atts.getURI(i))), 385 atts.getType(i),av); 386 } else { 387 if( !av.equals(fCurrentAttributes.getValue(j)) ) { 389 fCurrentAttributes.setValue(j,av); 391 } 392 } 393 394 Augmentations augs = fCurrentAttributes.getAugmentations(j); 395 augs.putItem( Constants.TYPEINFO, 396 typeInfoProvider.getAttributeTypeInfo(i) ); 397 augs.putItem( Constants.ID_ATTRIBUTE, 398 typeInfoProvider.isIdAttribute(i)?Boolean.TRUE:Boolean.FALSE ); 399 } 400 401 402 } 414 415 private String symbolize( String s ) { 416 return fSymbolTable.addSymbol(s); 417 } 418 419 420 423 private static final TypeInfoProvider noInfoProvider = new TypeInfoProvider () { 424 public TypeInfo getElementTypeInfo() { 425 return null; 426 } 427 public TypeInfo getAttributeTypeInfo(int index) { 428 return null; 429 } 430 public TypeInfo getAttributeTypeInfo(String attributeQName) { 431 return null; 432 } 433 public TypeInfo getAttributeTypeInfo(String attributeUri, String attributeLocalName) { 434 return null; 435 } 436 public boolean isIdAttribute(int index) { 437 return false; 438 } 439 public boolean isSpecified(int index) { 440 return false; 441 } 442 }; 443 444 450 public String [] getRecognizedFeatures() { 452 return null; 453 } 454 455 public void setFeature(String featureId, boolean state) throws XMLConfigurationException { 456 } 457 458 public String [] getRecognizedProperties() { 459 return new String []{Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY}; 460 } 461 462 public void setProperty(String propertyId, Object value) throws XMLConfigurationException { 463 if(propertyId.equals(Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY)) { 464 fEntityResolver = (XMLEntityResolver)value; 465 } 466 } 467 468 public Boolean getFeatureDefault(String featureId) { 469 return null; 470 } 471 472 public Object getPropertyDefault(String propertyId) { 473 return null; 474 } 475 476 } 477 | Popular Tags |