Schema objects. Entry-point to
 the validation API.
 
 SchemaFactory is a schema compiler. It reads external
 representations of schemas and prepares them for validation.
 
 The SchemaFactory class is not thread-safe. In other words,
 it is the application's responsibility to ensure that at most
 one thread is using a SchemaFactory object at any
 given moment. Implementations are encouraged to mark methods
 as synchronized to protect themselves from broken clients.
 
 SchemaFactory is not re-entrant. While one of the
 newSchema methods is being invoked, applications
 may not attempt to recursively invoke the newSchema method,
 even from the same thread.
 
Schema Language
This spec uses a namespace URI to designate a schema language. The following table shows the values defined by this specification.
To be compliant with the spec, the implementation is only required to support W3C XML Schema 1.0. However, if it chooses to support other schema languages listed here, it must conform to the relevant behaviors described in this spec.
 Schema languages not listed here are expected to
 introduce their own URIs to represent themselves.
 The SchemaFactory class is capable of locating other
 implementations for other schema languages at run-time.
 
Note that because the XML DTD is strongly tied to the parsing process and has a significant effect on the parsing process, it is impossible to define the DTD validation as a process independent from parsing. For this reason, this specification does not define the semantics for the XML DTD. This doesn't prohibit implementors from implementing it in a way they see fit, but users are warned that any DTD validation implemented on this interface necessarily deviate from the XML DTD semantics as defined in the XML 1.0.
| value | language | 
|---|---|
| XMLConstants.W3C_XML_SCHEMA_NS_URI("http://www.w3.org/2001/XMLSchema") | W3C XML Schema 1.0 | 
| XMLConstants.RELAXNG_NS_URI("http://relaxng.org/ns/structure/1.0") | RELAX NG 1.0 | 
- Since:
- 1.5
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionabstract ErrorHandlerGets the currentErrorHandlerset to thisSchemaFactory.booleangetFeature(String name) Look up the value of a feature flag.getProperty(String name) Look up the value of a property.abstract LSResourceResolverGets the currentLSResourceResolverset to thisSchemaFactory.abstract booleanisSchemaLanguageSupported(String schemaLanguage) Is specified schema supported by thisSchemaFactory?static SchemaFactoryCreates a new instance of theSchemaFactorybuiltin system-default implementation.static SchemaFactorynewInstance(String schemaLanguage) Obtains a new instance of aSchemaFactorythat supports the specified schema language.static SchemaFactorynewInstance(String schemaLanguage, String factoryClassName, ClassLoader classLoader) Obtain a new instance of aSchemaFactoryfrom class name.abstract SchemaCreates a specialSchemaobject.Parses the specifiedFileas a schema and returns it as aSchema.Parses the specifiedURLas a schema and returns it as aSchema.Parses the specified source as a schema and returns it as a schema.abstract SchemaParses the specified source(s) as a schema and returns it as a schema.abstract voidsetErrorHandler(ErrorHandler errorHandler) Sets theErrorHandlerto receive errors encountered during thenewSchemamethod invocation.voidsetFeature(String name, boolean value) Set a feature for thisSchemaFactory,Schemas created by this factory, and by extension,Validators andValidatorHandlers created by thoseSchemas.voidsetProperty(String name, Object object) Set the value of a property.abstract voidsetResourceResolver(LSResourceResolver resourceResolver) Sets theLSResourceResolverto customize resource resolution when parsing schemas.
- 
Constructor Details- 
SchemaFactoryprotected SchemaFactory()Constructor for derived classes.The constructor does nothing. Derived classes must create SchemaFactoryobjects that havenullErrorHandlerandnullLSResourceResolver.
 
- 
- 
Method Details- 
newDefaultInstanceCreates a new instance of theSchemaFactorybuiltin system-default implementation.- Implementation Requirements:
- The SchemaFactorybuiltin system-default implementation is only required to support the W3C XML Schema 1.0, but may support additional schema languages.
- Returns:
- A new instance of the SchemaFactorybuiltin system-default implementation.
- Since:
- 9
 
- 
newInstanceObtains a new instance of aSchemaFactorythat supports the specified schema language. This method uses the JAXP Lookup Mechanism to determine and load theSchemaFactoryimplementation that supports the specified schema language.Tip for Trouble-shooting:See Properties.load(java.io.InputStream)for exactly how a property file is parsed. In particular, colons ':' need to be escaped in a property file, so make sure schema language URIs are properly escaped in it. For example:http\://www.w3.org/2001/XMLSchema=org.acme.foo.XSSchemaFactory - Parameters:
- schemaLanguage- Specifies the schema language which the returned SchemaFactory will understand. See the list of available schema languages for the possible values.
- Returns:
- New instance of a SchemaFactory
- Throws:
- IllegalArgumentException- If no implementation of the schema language is available.
- NullPointerException- If the- schemaLanguageparameter is null.
- SchemaFactoryConfigurationError- If a configuration error is encountered.
- See Also:
 
- 
newInstancepublic static SchemaFactory newInstance(String schemaLanguage, String factoryClassName, ClassLoader classLoader) Obtain a new instance of aSchemaFactoryfrom class name.SchemaFactoryis returned if specified factory class name supports the specified schema language. This function is useful when there are multiple providers in the classpath. It gives more control to the application as it can specify which provider should be loaded.Tip for Trouble-shootingSetting the jaxp.debugsystem property will cause this method to print a lot of debug messages toSystem.errabout what it is doing and where it is looking at.If you have problems try: java -Djaxp.debug=1 YourProgram .... - Parameters:
- schemaLanguage- Specifies the schema language which the returned- SchemaFactorywill understand. See the list of available schema languages for the possible values.
- factoryClassName- fully qualified factory class name that provides implementation of- javax.xml.validation.SchemaFactory.
- classLoader-- ClassLoaderused to load the factory class. If- nullcurrent- Thread's context classLoader is used to load the factory class.
- Returns:
- New instance of a SchemaFactory
- Throws:
- IllegalArgumentException- if- factoryClassNameis- null, or the factory class cannot be loaded, instantiated or doesn't support the schema language specified in- schemLanguageparameter.
- NullPointerException- If the- schemaLanguageparameter is null.
- Since:
- 1.6
- See Also:
 
- 
isSchemaLanguageSupportedIs specified schema supported by thisSchemaFactory?- Parameters:
- schemaLanguage- Specifies the schema language which the returned- SchemaFactorywill understand.- schemaLanguagemust specify a valid schema language.
- Returns:
- trueif- SchemaFactorysupports- schemaLanguage, else- false.
- Throws:
- NullPointerException- If- schemaLanguageis- null.
- IllegalArgumentException- If- schemaLanguage.length() == 0or- schemaLanguagedoes not specify a valid schema language.
 
- 
getFeatureLook up the value of a feature flag.The feature name is any fully-qualified URI. It is possible for a SchemaFactoryto recognize a feature name but temporarily be unable to return its value.Implementors are free (and encouraged) to invent their own features, using names built on their own URIs. - Parameters:
- name- The feature name, which is a non-null fully-qualified URI.
- Returns:
- The current value of the feature (true or false).
- Throws:
- SAXNotRecognizedException- If the feature value can't be assigned or retrieved.
- SAXNotSupportedException- When the- SchemaFactoryrecognizes the feature name but cannot determine its value at this time.
- NullPointerException- If- nameis- null.
- See Also:
 
- 
setFeaturepublic void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException Set a feature for thisSchemaFactory,Schemas created by this factory, and by extension,Validators andValidatorHandlers created by thoseSchemas.Implementors and developers should pay particular attention to how the special Schemaobject returned bynewSchema()is processed. In some cases, for example, when theSchemaFactoryand the class actually loading the schema come from different implementations, it may not be possible forSchemaFactoryfeatures to be inherited automatically. Developers should make sure that features, such as secure processing, are explicitly set in both places.The feature name is any fully-qualified URI. It is possible for a SchemaFactoryto expose a feature value but to be unable to change the current value.All implementations are required to support the XMLConstants.FEATURE_SECURE_PROCESSINGfeature. When the feature is:- 
     true: the implementation will limit XML processing to conform to implementation limits. Examples include entity expansion limits and XML Schema constructs that would consume large amounts of resources. If XML processing is limited for security reasons, it will be reported via a call to the registeredErrorHandler.fatalError(SAXParseException exception). SeesetErrorHandler(ErrorHandler errorHandler).
- 
     false: the implementation will processing XML according to the XML specifications without regard to possible implementation limits.
 - Parameters:
- name- The feature name, which is a non-null fully-qualified URI.
- value- The requested value of the feature (true or false).
- Throws:
- SAXNotRecognizedException- If the feature value can't be assigned or retrieved.
- SAXNotSupportedException- When the- SchemaFactoryrecognizes the feature name but cannot set the requested value.
- NullPointerException- If- nameis- null.
- See Also:
 
- 
     
- 
setPropertypublic void setProperty(String name, Object object) throws SAXNotRecognizedException, SAXNotSupportedException Set the value of a property.The property name is any fully-qualified URI. It is possible for a SchemaFactoryto recognize a property name but to be unable to change the current value.All implementations that implement JAXP 1.5 or newer are required to support the XMLConstants.ACCESS_EXTERNAL_DTDandXMLConstants.ACCESS_EXTERNAL_SCHEMAproperties.- 
      Access to external DTDs in Schema files is restricted to the protocols specified by the XMLConstants.ACCESS_EXTERNAL_DTDproperty. If access is denied during the creation of new Schema due to the restriction of this property,SAXExceptionwill be thrown by thenewSchema(Source)ornewSchema(File)ornewSchema(URL)ornewSchema(Source[])method.Access to external DTDs in xml source files is restricted to the protocols specified by the XMLConstants.ACCESS_EXTERNAL_DTDproperty. If access is denied during validation due to the restriction of this property,SAXExceptionwill be thrown by theValidator.validate(Source)orValidator.validate(Source, Result)method.Access to external reference set by the schemaLocation attribute is restricted to the protocols specified by the XMLConstants.ACCESS_EXTERNAL_SCHEMAproperty. If access is denied during validation due to the restriction of this property,SAXExceptionwill be thrown by theValidator.validate(Source)orValidator.validate(Source, Result)method.Access to external reference set by the Import and Include element is restricted to the protocols specified by the XMLConstants.ACCESS_EXTERNAL_SCHEMAproperty. If access is denied during the creation of new Schema due to the restriction of this property,SAXExceptionwill be thrown by thenewSchema(Source)ornewSchema(File)ornewSchema(URL)ornewSchema(Source[])method.
 - Parameters:
- name- The property name, which is a non-null fully-qualified URI.
- object- The requested value for the property.
- Throws:
- SAXNotRecognizedException- If the property value can't be assigned or retrieved.
- SAXNotSupportedException- When the- SchemaFactoryrecognizes the property name but cannot set the requested value.
- NullPointerException- If- nameis- null.
 
- 
      
- 
getPropertyLook up the value of a property.The property name is any fully-qualified URI. It is possible for a SchemaFactoryto recognize a property name but temporarily be unable to return its value.SchemaFactorys are not required to recognize any specific property names.Implementors are free (and encouraged) to invent their own properties, using names built on their own URIs. - Parameters:
- name- The property name, which is a non-null fully-qualified URI.
- Returns:
- The current value of the property.
- Throws:
- SAXNotRecognizedException- If the property value can't be assigned or retrieved.
- SAXNotSupportedException- When the XMLReader recognizes the property name but cannot determine its value at this time.
- NullPointerException- If- nameis- null.
- See Also:
 
- 
setErrorHandlerSets theErrorHandlerto receive errors encountered during thenewSchemamethod invocation.Error handler can be used to customize the error handling process during schema parsing. When an ErrorHandleris set, errors found during the parsing of schemas will be first sent to theErrorHandler.The error handler can abort the parsing of a schema immediately by throwing SAXExceptionfrom the handler. Or for example it can print an error to the screen and try to continue the processing by returning normally from theErrorHandlerIf any Throwable(or instances of its derived classes) is thrown from anErrorHandler, the caller of thenewSchemamethod will be thrown the sameThrowableobject.SchemaFactoryis not allowed to throwSAXExceptionwithout first reporting it toErrorHandler.Applications can call this method even during a Schemais being parsed.When the ErrorHandleris null, the implementation will behave as if the followingErrorHandleris set:class DraconianErrorHandler implements ErrorHandler{ public void fatalError(SAXParseExceptione ) throwsSAXException{ throw e; } public void error(SAXParseExceptione ) throwsSAXException{ throw e; } public void warning(SAXParseExceptione ) throwsSAXException{ // noop } }When a new SchemaFactoryobject is created, initially this field is set to null. This field will NOT be inherited toSchemas,Validators, orValidatorHandlers that are created from thisSchemaFactory.- Parameters:
- errorHandler- A new error handler to be set. This parameter can be- null.
 
- 
getErrorHandlerGets the currentErrorHandlerset to thisSchemaFactory.- Returns:
- This method returns the object that was last set through
      the setErrorHandler(ErrorHandler)method, or null if that method has never been called since thisSchemaFactoryhas created.
- See Also:
 
- 
setResourceResolverSets theLSResourceResolverto customize resource resolution when parsing schemas.SchemaFactoryuses aLSResourceResolverwhen it needs to locate external resources while parsing schemas, although exactly what constitutes "locating external resources" is up to each schema language. For example, for W3C XML Schema, this includes files<include>d or<import>ed, and DTD referenced from schema files, etc.Applications can call this method even during a Schemais being parsed.When the LSResourceResolveris null, the implementation will behave as if the followingLSResourceResolveris set:class DumbDOMResourceResolver implements LSResourceResolver{ publicLSInputresolveResource( String publicId, String systemId, String baseURI) { return null; // always return null } }If a LSResourceResolverthrows aRuntimeException(or instances of its derived classes), then theSchemaFactorywill abort the parsing and the caller of thenewSchemamethod will receive the sameRuntimeException.When a new SchemaFactoryobject is created, initially this field is set to null. This field will NOT be inherited toSchemas,Validators, orValidatorHandlers that are created from thisSchemaFactory.- Parameters:
- resourceResolver- A new resource resolver to be set. This parameter can be null.
 
- 
getResourceResolverGets the currentLSResourceResolverset to thisSchemaFactory.- Returns:
- This method returns the object that was last set through
      the setResourceResolver(LSResourceResolver)method, or null if that method has never been called since thisSchemaFactoryhas created.
- See Also:
 
- 
newSchemaParses the specified source as a schema and returns it as a schema.This is a convenience method for newSchema(Source[] schemas).- Parameters:
- schema- Source that represents a schema.
- Returns:
- New Schemafrom parsingschema.
- Throws:
- SAXException- If a SAX error occurs during parsing.
- NullPointerException- if- schemais null.
 
- 
newSchemaParses the specifiedFileas a schema and returns it as aSchema.This is a convenience method for newSchema(Source schema).- Parameters:
- schema- File that represents a schema.
- Returns:
- New Schemafrom parsingschema.
- Throws:
- SAXException- If a SAX error occurs during parsing.
- NullPointerException- if- schemais null.
 
- 
newSchemaParses the specifiedURLas a schema and returns it as aSchema.This is a convenience method for newSchema(Source schema).- Parameters:
- schema-- URLthat represents a schema.
- Returns:
- New Schemafrom parsingschema.
- Throws:
- SAXException- If a SAX error occurs during parsing.
- NullPointerException- if- schemais null.
 
- 
newSchemaParses the specified source(s) as a schema and returns it as a schema.The callee will read all the Sources and combine them into a single schema. The exact semantics of the combination depends on the schema language that thisSchemaFactoryobject is created for.When an ErrorHandleris set, the callee will report all the errors found in sources to the handler. If the handler throws an exception, it will abort the schema compilation and the same exception will be thrown from this method. Also, after an error is reported to a handler, the callee is allowed to abort the further processing by throwing it. If an error handler is not set, the callee will throw the first error it finds in the sources.W3C XML Schema 1.0The resulting schema contains components from the specified sources. The same result would be achieved if all these sources were imported, using appropriate values for schemaLocation and namespace, into a single schema document with a different targetNamespace and no components of its own, if the import elements were given in the same order as the sources. Section 4.2.3 of the XML Schema recommendation describes the options processors have in this regard. While a processor should be consistent in its treatment of JAXP schema sources and XML Schema imports, the behaviour between JAXP-compliant parsers may vary; in particular, parsers may choose to ignore all but the first <import>for a given namespace, regardless of information provided in schemaLocation.If the parsed set of schemas includes error(s) as specified in the section 5.1 of the XML Schema spec, then the error must be reported to the ErrorHandler.RELAX NGFor RELAX NG, this method must throw UnsupportedOperationExceptionifschemas.length!=1.- Parameters:
- schemas- inputs to be parsed.- SchemaFactoryis required to recognize- SAXSource,- StreamSource,- StAXSource, and- DOMSource. Input schemas must be XML documents or XML elements and must not be null. For backwards compatibility, the results of passing anything other than a document or element are implementation-dependent. Implementations must either recognize and process the input or thrown an IllegalArgumentException.
- Returns:
- Always return a non-null valid Schemaobject. Note that when an error has been reported, there is no guarantee that the returnedSchemaobject is meaningful.
- Throws:
- SAXException- If an error is found during processing the specified inputs. When an- ErrorHandleris set, errors are reported to there first. See- setErrorHandler(ErrorHandler).
- NullPointerException- If the- schemasparameter itself is null or any item in the array is null.
- IllegalArgumentException- If any item in the array is not recognized by this method.
- UnsupportedOperationException- If the schema language doesn't support this operation.
 
- 
newSchemaCreates a specialSchemaobject.The exact semantics of the returned Schemaobject depend on the schema language for which thisSchemaFactoryis created.Also, implementations are allowed to use implementation-specific property/feature to alter the semantics of this method. Implementors and developers should pay particular attention to how the features set on this SchemaFactoryare processed by this specialSchema. In some cases, for example, when theSchemaFactoryand the class actually loading the schema come from different implementations, it may not be possible forSchemaFactoryfeatures to be inherited automatically. Developers should make sure that features, such as secure processing, are explicitly set in both places.W3C XML Schema 1.0For XML Schema, this method creates a Schemaobject that performs validation by using location hints specified in documents.The returned Schemaobject assumes that if documents refer to the same URL in the schema location hints, they will always resolve to the same schema document. This asusmption allows implementations to reuse parsed results of schema documents so that multiple validations against the same schema will run faster.Note that the use of schema location hints introduces a vulnerability to denial-of-service attacks. RELAX NGRELAX NG does not support this operation. - Returns:
- Always return non-null valid Schemaobject.
- Throws:
- UnsupportedOperationException- If this operation is not supported by the callee.
- SAXException- If this operation is supported but failed for some reason.
 
 
-