1 16 17 package com.sun.org.apache.xerces.internal.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 com.sun.org.apache.xerces.internal.impl.Constants; 33 import com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader; 34 import com.sun.org.apache.xerces.internal.util.DOMEntityResolverWrapper; 35 import com.sun.org.apache.xerces.internal.util.DOMInputSource; 36 import com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper; 37 import com.sun.org.apache.xerces.internal.util.SAXInputSource; 38 import com.sun.org.apache.xerces.internal.util.SAXMessageFormatter; 39 import com.sun.org.apache.xerces.internal.util.SecurityManager; 40 import com.sun.org.apache.xerces.internal.util.XMLGrammarPoolImpl; 41 import com.sun.org.apache.xerces.internal.xni.XNIException; 42 import com.sun.org.apache.xerces.internal.xni.grammars.Grammar; 43 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription; 44 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool; 45 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException; 46 import com.sun.org.apache.xerces.internal.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 fSecurityManager = new SecurityManager (); 114 fXMLSchemaLoader.setProperty(SECURITY_MANAGER, fSecurityManager); 115 } 116 117 129 public boolean isSchemaLanguageSupported(String schemaLanguage) { 130 if (schemaLanguage == null) { 131 throw new NullPointerException (JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 132 "SchemaLanguageNull", null)); 133 } 134 if (schemaLanguage.length() == 0) { 135 throw new IllegalArgumentException (JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 136 "SchemaLanguageLengthZero", null)); 137 } 138 return schemaLanguage.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI); 140 } 141 142 public LSResourceResolver getResourceResolver() { 143 return fLSResourceResolver; 144 } 145 146 public void setResourceResolver(LSResourceResolver resourceResolver) { 147 fLSResourceResolver = resourceResolver; 148 fDOMEntityResolverWrapper.setEntityResolver(resourceResolver); 149 fXMLSchemaLoader.setEntityResolver(fDOMEntityResolverWrapper); 150 } 151 152 public ErrorHandler getErrorHandler() { 153 return fErrorHandler; 154 } 155 156 public void setErrorHandler(ErrorHandler errorHandler) { 157 fErrorHandler = errorHandler; 158 fErrorHandlerWrapper.setErrorHandler(errorHandler != null ? errorHandler : DraconianErrorHandler.getInstance()); 159 fXMLSchemaLoader.setErrorHandler(fErrorHandlerWrapper); 160 } 161 162 public Schema newSchema( Source [] schemas ) throws SAXException { 163 164 XMLGrammarPoolImplExtension pool = new XMLGrammarPoolImplExtension(); 166 fXMLGrammarPoolWrapper.setGrammarPool(pool); 167 168 XMLInputSource[] xmlInputSources = new XMLInputSource[schemas.length]; 169 InputStream inputStream; 170 Reader reader; 171 for( int i=0; i<schemas.length; i++ ) { 172 Source source = schemas[i]; 173 if (source instanceof StreamSource ) { 174 StreamSource streamSource = (StreamSource ) source; 175 String publicId = streamSource.getPublicId(); 176 String systemId = streamSource.getSystemId(); 177 inputStream = streamSource.getInputStream(); 178 reader = streamSource.getReader(); 179 xmlInputSources[i] = new XMLInputSource(publicId, systemId, null); 180 xmlInputSources[i].setByteStream(inputStream); 181 xmlInputSources[i].setCharacterStream(reader); 182 } 183 else if (source instanceof SAXSource ) { 184 SAXSource saxSource = (SAXSource ) source; 185 InputSource inputSource = saxSource.getInputSource(); 186 if (inputSource == null) { 187 throw new SAXException (JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 188 "SAXSourceNullInputSource", null)); 189 } 190 xmlInputSources[i] = new SAXInputSource(saxSource.getXMLReader(), inputSource); 191 } 192 else if (source instanceof DOMSource ) { 193 DOMSource domSource = (DOMSource ) source; 194 Node node = domSource.getNode(); 195 String systemID = domSource.getSystemId(); 196 xmlInputSources[i] = new DOMInputSource(node, systemID); 197 } 198 else if (source == null) { 199 throw new NullPointerException (JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 200 "SchemaSourceArrayMemberNull", null)); 201 } 202 else { 203 throw new IllegalArgumentException (JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 204 "SchemaFactorySourceUnrecognized", 205 new Object [] {source.getClass().getName()})); 206 } 207 } 208 209 try { 210 fXMLSchemaLoader.loadGrammar(xmlInputSources); 211 } 212 catch (XNIException e) { 213 throw Util.toSAXException(e); 215 } 216 catch (IOException e) { 217 SAXParseException se = new SAXParseException (e.getMessage(),null,e); 219 fErrorHandler.error(se); 220 throw se; } 222 223 fXMLGrammarPoolWrapper.setGrammarPool(null); 225 226 final int grammarCount = pool.getGrammarCount(); 228 if (grammarCount > 1) { 229 return new XMLSchema(new ReadOnlyGrammarPool(pool)); 230 } 231 else if (grammarCount == 1) { 232 Grammar[] grammars = pool.retrieveInitialGrammarSet(XMLGrammarDescription.XML_SCHEMA); 233 return new SimpleXMLSchema(grammars[0]); 234 } 235 else { 236 return EmptyXMLSchema.getInstance(); 237 } 238 } 239 240 public Schema newSchema() throws SAXException { 241 return new WeakReferenceXMLSchema(); 243 } 244 245 public boolean getFeature(String name) 246 throws SAXNotRecognizedException , SAXNotSupportedException { 247 if (name == null) { 248 throw new NullPointerException (JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 249 "FeatureNameNull", null)); 250 } 251 if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) { 252 return (fSecurityManager != null); 253 } 254 try { 255 return fXMLSchemaLoader.getFeature(name); 256 } 257 catch (XMLConfigurationException e) { 258 String identifier = e.getIdentifier(); 259 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 260 throw new SAXNotRecognizedException ( 261 SAXMessageFormatter.formatMessage(Locale.getDefault(), 262 "feature-not-recognized", new Object [] {identifier})); 263 } 264 else { 265 throw new SAXNotSupportedException ( 266 SAXMessageFormatter.formatMessage(Locale.getDefault(), 267 "feature-not-supported", new Object [] {identifier})); 268 } 269 } 270 } 271 272 public Object getProperty(String name) 273 throws SAXNotRecognizedException , SAXNotSupportedException { 274 if (name == null) { 275 throw new NullPointerException (JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 276 "ProperyNameNull", null)); 277 } 278 if (name.equals(SECURITY_MANAGER)) { 279 return fSecurityManager; 280 } 281 else if (name.equals(XMLGRAMMAR_POOL)) { 282 throw new SAXNotSupportedException ( 283 SAXMessageFormatter.formatMessage(Locale.getDefault(), 284 "property-not-supported", new Object [] {name})); 285 } 286 try { 287 return fXMLSchemaLoader.getProperty(name); 288 } 289 catch (XMLConfigurationException e) { 290 String identifier = e.getIdentifier(); 291 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 292 throw new SAXNotRecognizedException ( 293 SAXMessageFormatter.formatMessage(Locale.getDefault(), 294 "property-not-recognized", new Object [] {identifier})); 295 } 296 else { 297 throw new SAXNotSupportedException ( 298 SAXMessageFormatter.formatMessage(Locale.getDefault(), 299 "property-not-supported", new Object [] {identifier})); 300 } 301 } 302 } 303 304 public void setFeature(String name, boolean value) 305 throws SAXNotRecognizedException , SAXNotSupportedException { 306 if (name == null) { 307 throw new NullPointerException (JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 308 "FeatureNameNull", null)); 309 } 310 if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) { 311 fSecurityManager = value ? new SecurityManager () : null; 312 fXMLSchemaLoader.setProperty(SECURITY_MANAGER, fSecurityManager); 313 return; 314 } 315 try { 316 fXMLSchemaLoader.setFeature(name, value); 317 } 318 catch (XMLConfigurationException e) { 319 String identifier = e.getIdentifier(); 320 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 321 throw new SAXNotRecognizedException ( 322 SAXMessageFormatter.formatMessage(Locale.getDefault(), 323 "feature-not-recognized", new Object [] {identifier})); 324 } 325 else { 326 throw new SAXNotSupportedException ( 327 SAXMessageFormatter.formatMessage(Locale.getDefault(), 328 "feature-not-supported", new Object [] {identifier})); 329 } 330 } 331 } 332 333 public void setProperty(String name, Object object) 334 throws SAXNotRecognizedException , SAXNotSupportedException { 335 if (name == null) { 336 throw new NullPointerException (JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 337 "ProperyNameNull", null)); 338 } 339 if (name.equals(SECURITY_MANAGER)) { 340 fSecurityManager = (SecurityManager ) object; 341 fXMLSchemaLoader.setProperty(SECURITY_MANAGER, fSecurityManager); 342 return; 343 } 344 else if (name.equals(XMLGRAMMAR_POOL)) { 345 throw new SAXNotSupportedException ( 346 SAXMessageFormatter.formatMessage(Locale.getDefault(), 347 "property-not-supported", new Object [] {name})); 348 } 349 try { 350 fXMLSchemaLoader.setProperty(name, object); 351 } 352 catch (XMLConfigurationException e) { 353 String identifier = e.getIdentifier(); 354 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 355 throw new SAXNotRecognizedException ( 356 SAXMessageFormatter.formatMessage(Locale.getDefault(), 357 "property-not-recognized", new Object [] {identifier})); 358 } 359 else { 360 throw new SAXNotSupportedException ( 361 SAXMessageFormatter.formatMessage(Locale.getDefault(), 362 "property-not-supported", new Object [] {identifier})); 363 } 364 } 365 } 366 367 371 static class XMLGrammarPoolImplExtension extends XMLGrammarPoolImpl { 372 373 374 public XMLGrammarPoolImplExtension() { 375 super(); 376 } 377 378 379 public XMLGrammarPoolImplExtension(int initialCapacity) { 380 super(initialCapacity); 381 } 382 383 384 int getGrammarCount() { 385 return fGrammarCount; 386 } 387 388 } 390 393 static class XMLGrammarPoolWrapper implements XMLGrammarPool { 394 395 private XMLGrammarPool fGrammarPool; 396 397 400 401 public Grammar[] retrieveInitialGrammarSet(String grammarType) { 402 return fGrammarPool.retrieveInitialGrammarSet(grammarType); 403 } 404 405 public void cacheGrammars(String grammarType, Grammar[] grammars) { 406 fGrammarPool.cacheGrammars(grammarType, grammars); 407 } 408 409 public Grammar retrieveGrammar(XMLGrammarDescription desc) { 410 return fGrammarPool.retrieveGrammar(desc); 411 } 412 413 public void lockPool() { 414 fGrammarPool.lockPool(); 415 } 416 417 public void unlockPool() { 418 fGrammarPool.unlockPool(); 419 } 420 421 public void clear() { 422 fGrammarPool.clear(); 423 } 424 425 428 429 void setGrammarPool(XMLGrammarPool grammarPool) { 430 fGrammarPool = grammarPool; 431 } 432 433 XMLGrammarPool getGrammarPool() { 434 return fGrammarPool; 435 } 436 437 } 439 } | Popular Tags |