1 16 17 package org.apache.xerces.jaxp.validation; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.Reader ; 22 import java.util.Locale ; 23 24 import javax.xml.XMLConstants ; 25 import javax.xml.transform.Source ; 26 import javax.xml.transform.dom.DOMSource ; 27 import javax.xml.transform.sax.SAXSource ; 28 import javax.xml.transform.stream.StreamSource ; 29 import javax.xml.validation.Schema ; 30 import javax.xml.validation.SchemaFactory ; 31 32 import org.apache.xerces.impl.Constants; 33 import org.apache.xerces.impl.xs.XMLSchemaLoader; 34 import org.apache.xerces.util.DOMEntityResolverWrapper; 35 import org.apache.xerces.util.DOMInputSource; 36 import org.apache.xerces.util.ErrorHandlerWrapper; 37 import org.apache.xerces.util.SAXInputSource; 38 import org.apache.xerces.util.SAXMessageFormatter; 39 import org.apache.xerces.util.SecurityManager; 40 import org.apache.xerces.util.XMLGrammarPoolImpl; 41 import org.apache.xerces.xni.XNIException; 42 import org.apache.xerces.xni.grammars.Grammar; 43 import org.apache.xerces.xni.grammars.XMLGrammarDescription; 44 import org.apache.xerces.xni.grammars.XMLGrammarPool; 45 import org.apache.xerces.xni.parser.XMLConfigurationException; 46 import org.apache.xerces.xni.parser.XMLInputSource; 47 import org.w3c.dom.Node ; 48 import org.w3c.dom.ls.LSResourceResolver ; 49 import org.xml.sax.ErrorHandler ; 50 import org.xml.sax.InputSource ; 51 import org.xml.sax.SAXException ; 52 import org.xml.sax.SAXNotRecognizedException ; 53 import org.xml.sax.SAXNotSupportedException ; 54 import org.xml.sax.SAXParseException ; 55 56 62 public final class XMLSchemaFactory extends SchemaFactory { 63 64 66 67 private static final String SCHEMA_FULL_CHECKING = 68 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_FULL_CHECKING; 69 70 71 private static final String XMLGRAMMAR_POOL = 72 Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY; 73 74 75 private static final String SECURITY_MANAGER = 76 Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY; 77 78 82 83 private final XMLSchemaLoader fXMLSchemaLoader = new XMLSchemaLoader(); 84 85 86 private ErrorHandler fErrorHandler; 87 88 89 private LSResourceResolver fLSResourceResolver; 90 91 92 private final DOMEntityResolverWrapper fDOMEntityResolverWrapper; 93 94 95 private ErrorHandlerWrapper fErrorHandlerWrapper; 96 97 98 private SecurityManager fSecurityManager; 99 100 101 private XMLGrammarPoolWrapper fXMLGrammarPoolWrapper; 102 103 public XMLSchemaFactory() { 104 fErrorHandlerWrapper = new ErrorHandlerWrapper(DraconianErrorHandler.getInstance()); 105 fDOMEntityResolverWrapper = new DOMEntityResolverWrapper(); 106 fXMLGrammarPoolWrapper = new XMLGrammarPoolWrapper(); 107 fXMLSchemaLoader.setFeature(SCHEMA_FULL_CHECKING, true); 108 fXMLSchemaLoader.setProperty(XMLGRAMMAR_POOL, fXMLGrammarPoolWrapper); 109 fXMLSchemaLoader.setEntityResolver(fDOMEntityResolverWrapper); 110 fXMLSchemaLoader.setErrorHandler(fErrorHandlerWrapper); 111 } 112 113 125 public boolean isSchemaLanguageSupported(String schemaLanguage) { 126 if (schemaLanguage == null) { 127 throw new NullPointerException (JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 128 "SchemaLanguageNull", null)); 129 } 130 if (schemaLanguage.length() == 0) { 131 throw new IllegalArgumentException (JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 132 "SchemaLanguageLengthZero", null)); 133 } 134 return schemaLanguage.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI); 136 } 137 138 public LSResourceResolver getResourceResolver() { 139 return fLSResourceResolver; 140 } 141 142 public void setResourceResolver(LSResourceResolver resourceResolver) { 143 fLSResourceResolver = resourceResolver; 144 fDOMEntityResolverWrapper.setEntityResolver(resourceResolver); 145 fXMLSchemaLoader.setEntityResolver(fDOMEntityResolverWrapper); 146 } 147 148 public ErrorHandler getErrorHandler() { 149 return fErrorHandler; 150 } 151 152 public void setErrorHandler(ErrorHandler errorHandler) { 153 fErrorHandler = errorHandler; 154 fErrorHandlerWrapper.setErrorHandler(errorHandler != null ? errorHandler : DraconianErrorHandler.getInstance()); 155 fXMLSchemaLoader.setErrorHandler(fErrorHandlerWrapper); 156 } 157 158 public Schema newSchema( Source [] schemas ) throws SAXException { 159 160 XMLGrammarPoolImplExtension pool = new XMLGrammarPoolImplExtension(); 162 fXMLGrammarPoolWrapper.setGrammarPool(pool); 163 164 XMLInputSource[] xmlInputSources = new XMLInputSource[schemas.length]; 165 InputStream inputStream; 166 Reader reader; 167 for( int i=0; i<schemas.length; i++ ) { 168 Source source = schemas[i]; 169 if (source instanceof StreamSource ) { 170 StreamSource streamSource = (StreamSource ) source; 171 String publicId = streamSource.getPublicId(); 172 String systemId = streamSource.getSystemId(); 173 inputStream = streamSource.getInputStream(); 174 reader = streamSource.getReader(); 175 xmlInputSources[i] = new XMLInputSource(publicId, systemId, null); 176 xmlInputSources[i].setByteStream(inputStream); 177 xmlInputSources[i].setCharacterStream(reader); 178 } 179 else if (source instanceof SAXSource ) { 180 SAXSource saxSource = (SAXSource ) source; 181 InputSource inputSource = saxSource.getInputSource(); 182 if (inputSource == null) { 183 throw new SAXException (JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 184 "SAXSourceNullInputSource", null)); 185 } 186 xmlInputSources[i] = new SAXInputSource(saxSource.getXMLReader(), inputSource); 187 } 188 else if (source instanceof DOMSource ) { 189 DOMSource domSource = (DOMSource ) source; 190 Node node = domSource.getNode(); 191 String systemID = domSource.getSystemId(); 192 xmlInputSources[i] = new DOMInputSource(node, systemID); 193 } 194 else if (source == null) { 195 throw new NullPointerException (JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 196 "SchemaSourceArrayMemberNull", null)); 197 } 198 else { 199 throw new IllegalArgumentException (JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 200 "SchemaFactorySourceUnrecognized", 201 new Object [] {source.getClass().getName()})); 202 } 203 } 204 205 try { 206 fXMLSchemaLoader.loadGrammar(xmlInputSources); 207 } 208 catch (XNIException e) { 209 throw Util.toSAXException(e); 211 } 212 catch (IOException e) { 213 SAXParseException se = new SAXParseException (e.getMessage(),null,e); 215 fErrorHandler.error(se); 216 throw se; } 218 219 fXMLGrammarPoolWrapper.setGrammarPool(null); 221 222 final int grammarCount = pool.getGrammarCount(); 224 if (grammarCount > 1) { 225 return new XMLSchema(new ReadOnlyGrammarPool(pool)); 226 } 227 else if (grammarCount == 1) { 228 Grammar[] grammars = pool.retrieveInitialGrammarSet(XMLGrammarDescription.XML_SCHEMA); 229 return new SimpleXMLSchema(grammars[0]); 230 } 231 else { 232 return EmptyXMLSchema.getInstance(); 233 } 234 } 235 236 public Schema newSchema() throws SAXException { 237 return new WeakReferenceXMLSchema(); 239 } 240 241 public boolean getFeature(String name) 242 throws SAXNotRecognizedException , SAXNotSupportedException { 243 if (name == null) { 244 throw new NullPointerException (JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 245 "FeatureNameNull", null)); 246 } 247 if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) { 248 return (fSecurityManager != null); 249 } 250 try { 251 return fXMLSchemaLoader.getFeature(name); 252 } 253 catch (XMLConfigurationException e) { 254 String identifier = e.getIdentifier(); 255 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 256 throw new SAXNotRecognizedException ( 257 SAXMessageFormatter.formatMessage(Locale.getDefault(), 258 "feature-not-recognized", new Object [] {identifier})); 259 } 260 else { 261 throw new SAXNotSupportedException ( 262 SAXMessageFormatter.formatMessage(Locale.getDefault(), 263 "feature-not-supported", new Object [] {identifier})); 264 } 265 } 266 } 267 268 public Object getProperty(String name) 269 throws SAXNotRecognizedException , SAXNotSupportedException { 270 if (name == null) { 271 throw new NullPointerException (JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 272 "ProperyNameNull", null)); 273 } 274 if (name.equals(SECURITY_MANAGER)) { 275 return fSecurityManager; 276 } 277 else if (name.equals(XMLGRAMMAR_POOL)) { 278 throw new SAXNotSupportedException ( 279 SAXMessageFormatter.formatMessage(Locale.getDefault(), 280 "property-not-supported", new Object [] {name})); 281 } 282 try { 283 return fXMLSchemaLoader.getProperty(name); 284 } 285 catch (XMLConfigurationException e) { 286 String identifier = e.getIdentifier(); 287 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 288 throw new SAXNotRecognizedException ( 289 SAXMessageFormatter.formatMessage(Locale.getDefault(), 290 "property-not-recognized", new Object [] {identifier})); 291 } 292 else { 293 throw new SAXNotSupportedException ( 294 SAXMessageFormatter.formatMessage(Locale.getDefault(), 295 "property-not-supported", new Object [] {identifier})); 296 } 297 } 298 } 299 300 public void setFeature(String name, boolean value) 301 throws SAXNotRecognizedException , SAXNotSupportedException { 302 if (name == null) { 303 throw new NullPointerException (JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 304 "FeatureNameNull", null)); 305 } 306 if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) { 307 fSecurityManager = value ? new SecurityManager () : null; 308 fXMLSchemaLoader.setProperty(SECURITY_MANAGER, fSecurityManager); 309 return; 310 } 311 try { 312 fXMLSchemaLoader.setFeature(name, value); 313 } 314 catch (XMLConfigurationException e) { 315 String identifier = e.getIdentifier(); 316 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 317 throw new SAXNotRecognizedException ( 318 SAXMessageFormatter.formatMessage(Locale.getDefault(), 319 "feature-not-recognized", new Object [] {identifier})); 320 } 321 else { 322 throw new SAXNotSupportedException ( 323 SAXMessageFormatter.formatMessage(Locale.getDefault(), 324 "feature-not-supported", new Object [] {identifier})); 325 } 326 } 327 } 328 329 public void setProperty(String name, Object object) 330 throws SAXNotRecognizedException , SAXNotSupportedException { 331 if (name == null) { 332 throw new NullPointerException (JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 333 "ProperyNameNull", null)); 334 } 335 if (name.equals(SECURITY_MANAGER)) { 336 fSecurityManager = (SecurityManager ) object; 337 fXMLSchemaLoader.setProperty(SECURITY_MANAGER, fSecurityManager); 338 return; 339 } 340 else if (name.equals(XMLGRAMMAR_POOL)) { 341 throw new SAXNotSupportedException ( 342 SAXMessageFormatter.formatMessage(Locale.getDefault(), 343 "property-not-supported", new Object [] {name})); 344 } 345 try { 346 fXMLSchemaLoader.setProperty(name, object); 347 } 348 catch (XMLConfigurationException e) { 349 String identifier = e.getIdentifier(); 350 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 351 throw new SAXNotRecognizedException ( 352 SAXMessageFormatter.formatMessage(Locale.getDefault(), 353 "property-not-recognized", new Object [] {identifier})); 354 } 355 else { 356 throw new SAXNotSupportedException ( 357 SAXMessageFormatter.formatMessage(Locale.getDefault(), 358 "property-not-supported", new Object [] {identifier})); 359 } 360 } 361 } 362 363 367 static class XMLGrammarPoolImplExtension extends XMLGrammarPoolImpl { 368 369 370 public XMLGrammarPoolImplExtension() { 371 super(); 372 } 373 374 375 public XMLGrammarPoolImplExtension(int initialCapacity) { 376 super(initialCapacity); 377 } 378 379 380 int getGrammarCount() { 381 return fGrammarCount; 382 } 383 384 } 386 389 static class XMLGrammarPoolWrapper implements XMLGrammarPool { 390 391 private XMLGrammarPool fGrammarPool; 392 393 396 397 public Grammar[] retrieveInitialGrammarSet(String grammarType) { 398 return fGrammarPool.retrieveInitialGrammarSet(grammarType); 399 } 400 401 public void cacheGrammars(String grammarType, Grammar[] grammars) { 402 fGrammarPool.cacheGrammars(grammarType, grammars); 403 } 404 405 public Grammar retrieveGrammar(XMLGrammarDescription desc) { 406 return fGrammarPool.retrieveGrammar(desc); 407 } 408 409 public void lockPool() { 410 fGrammarPool.lockPool(); 411 } 412 413 public void unlockPool() { 414 fGrammarPool.unlockPool(); 415 } 416 417 public void clear() { 418 fGrammarPool.clear(); 419 } 420 421 424 425 void setGrammarPool(XMLGrammarPool grammarPool) { 426 fGrammarPool = grammarPool; 427 } 428 429 XMLGrammarPool getGrammarPool() { 430 return fGrammarPool; 431 } 432 433 } 435 } | Popular Tags |