Factory that creates 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 implentors 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
javax.xml.XMLConstants#W3C_XML_SCHEMA_NS_URI ("http://www.w3.org/2001/XMLSchema") W3C XML Schema 1.0
javax.xml.XMLConstants#RELAXNG_NS_URI ("http://relaxng.org/ns/structure/1.0") RELAX NG 1.0

@version
$Revision: 1.20.10.1.2.3 $, $Date: 2004/09/16 09:24:47 $
@since
1.5
Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

Parameters
objthe reference object with which to compare.
Return
true if this object is the same as the obj argument; false otherwise.
Returns the runtime class of an object. That Class object is the object that is locked by static synchronized methods of the represented class.
Return
The java.lang.Class object that represents the runtime class of the object. The result is of type {@code Class} where X is the erasure of the static type of the expression on which getClass is called.
Gets the current ErrorHandler set to this SchemaFactory .
Return
This method returns the object that was last set through the {@link #setErrorHandler(ErrorHandler)} method, or null if that method has never been called since this {@link SchemaFactory} has created.
Look up the value of a feature flag.

The feature name is any fully-qualified URI. It is possible for a SchemaFactory to 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
nameThe feature name, which is a non-null fully-qualified URI.
Return
The current value of the feature (true or false).
Throws
org.xml.sax.SAXNotRecognizedExceptionIf the feature value can't be assigned or retrieved.
org.xml.sax.SAXNotSupportedExceptionWhen the {@link SchemaFactory} recognizes the feature name but cannot determine its value at this time.
NullPointerException if the name parameter is null.
Look up the value of a property.

The property name is any fully-qualified URI. It is possible for a SchemaFactory to recognize a property name but temporarily be unable to return its value.

SchemaFactory s 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
nameThe property name, which is a non-null fully-qualified URI.
Return
The current value of the property.
Throws
org.xml.sax.SAXNotRecognizedExceptionIf the property value can't be assigned or retrieved.
org.xml.sax.SAXNotSupportedExceptionWhen the XMLReader recognizes the property name but cannot determine its value at this time.
NullPointerException if the name parameter is null.
Gets the current LSResourceResolver set to this SchemaFactory .
Return
This method returns the object that was last set through the {@link #setResourceResolver(LSResourceResolver)} method, or null if that method has never been called since this {@link SchemaFactory} has created.
Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

Return
a hash code value for this object.

Is specified schema supported by this SchemaFactory?

Parameters
schemaLanguageSpecifies the schema language which the returned SchemaFactory will understand. schemaLanguage must specify a valid schema language.
Return
true if SchemaFactory supports schemaLanguage, else false.
Throws
NullPointerExceptionIf schemaLanguage is null.
IllegalArgumentExceptionIf schemaLanguage.length() == 0 or schemaLanguage does not specify a valid schema language.

Lookup an implementation of the SchemaFactory that supports the specified schema language and return it.

To find a SchemaFactory object for a given schema language, this method looks the following places in the following order where "the class loader" refers to the context class loader:

  1. If the system property "javax.xml.validation.SchemaFactory:schemaLanguage" is present (where schemaLanguage is the parameter to this method), then its value is read as a class name. The method will try to create a new instance of this class by using the class loader, and returns it if it is successfully created.
  2. $java.home/lib/jaxp.properties is read and the value associated with the key being the system property above is looked for. If present, the value is processed just like above.
  3. The class loader is asked for service provider provider-configuration files matching javax.xml.validation.SchemaFactory in the resource directory META-INF/services. See the JAR File Specification for file format and parsing rules. Each potential service provider is required to implement the method:

            
    
         
    The first service provider found in class loader order that supports the specified schema language is returned.
  4. Platform default SchemaFactory is located in a implementation specific way. There must be a platform default SchemaFactory for W3C XML Schema.

If everything fails, IllegalArgumentException will be thrown.

Tip for Trouble-shooting:

See 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.
Return
New instance of a SchemaFactory
Throws
IllegalArgumentException If no implementation of the schema language is available.
NullPointerException If the schemLanguage parameter is null.
Creates a special Schema object.

The exact semantics of the returned Schema object depends on the schema language that this SchemaFactory is created for.

Also, implementations are allowed to use implementation-specific property/feature to alter the semantics of this method.

W3C XML Schema 1.0

For XML Schema, this method creates a Schema object that performs validation by using location hints specified in documents.

The returned Schema object 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 NG

RELAX NG does not support this operation.

Return
Always return non-null valid {@link Schema} object.
Throws
UnsupportedOperationException If this operation is not supported by the callee.
SAXException If this operation is supported but failed for some reason.

Parses the specified File as a schema and returns it as a Schema.

This is a convenience method for .

Parameters
schemaFile that represents a schema.
Return
New Schema from parsing schema.
Throws
SAXExceptionIf a SAX error occurs during parsing.
NullPointerExceptionif schema is null.

Parses the specified source as a schema and returns it as a schema.

This is a convenience method for .

Parameters
schemaSource that represents a schema.
Return
New Schema from parsing schema.
Throws
SAXExceptionIf a SAX error occurs during parsing.
NullPointerExceptionif schema is null.
Parses the specified source(s) as a schema and returns it as a schema.

The callee will read all the Source s and combine them into a single schema. The exact semantics of the combination depends on the schema language that this SchemaFactory object is created for.

When an ErrorHandler is 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.0

The 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 NG

For RELAX NG, this method must throw UnsupportedOperationException if schemas.length!=1.

Parameters
schemas inputs to be parsed. {@link SchemaFactory} is required to recognize {@link javax.xml.transform.sax.SAXSource}, {@link StreamSource}, and {@link javax.xml.transform.dom.DOMSource}.
Return
Always return a non-null valid {@link Schema} object. Note that when an error has been reported, there is no guarantee that the returned {@link Schema} object is meaningful.
Throws
SAXException If an error is found during processing the specified inputs. When an {@link ErrorHandler} is set, errors are reported to there first. See {@link #setErrorHandler(ErrorHandler)}.
NullPointerException If the schemas parameter 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.

Parses the specified URL as a schema and returns it as a Schema.

This is a convenience method for .

Parameters
schemaURL that represents a schema.
Return
New Schema from parsing schema.
Throws
SAXExceptionIf a SAX error occurs during parsing.
NullPointerExceptionif schema is null.
Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. A thread waits on an object's monitor by calling one of the wait methods.

The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object. The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.

This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's monitor in one of three ways:

  • By executing a synchronized instance method of that object.
  • By executing the body of a synchronized statement that synchronizes on the object.
  • For objects of type Class, by executing a synchronized static method of that class.

Only one thread at a time can own an object's monitor.

Throws
IllegalMonitorStateExceptionif the current thread is not the owner of this object's monitor.
Wakes up all threads that are waiting on this object's monitor. A thread waits on an object's monitor by calling one of the wait methods.

The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object. The awakened threads will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened threads enjoy no reliable privilege or disadvantage in being the next thread to lock this object.

This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.

Throws
IllegalMonitorStateExceptionif the current thread is not the owner of this object's monitor.
Sets the ErrorHandler to receive errors encountered during the newSchema method invocation.

Error handler can be used to customize the error handling process during schema parsing. When an ErrorHandler is set, errors found during the parsing of schemas will be first sent to the ErrorHandler .

The error handler can abort the parsing of a schema immediately by throwing SAXException from the handler. Or for example it can print an error to the screen and try to continue the processing by returning normally from the ErrorHandler

If any Throwable (or instances of its derived classes) is thrown from an ErrorHandler , the caller of the newSchema method will be thrown the same Throwable object.

SchemaFactory is not allowed to throw SAXException without first reporting it to ErrorHandler .

Applications can call this method even during a Schema is being parsed.

When the ErrorHandler is null, the implementation will behave as if the following ErrorHandler is set:

 class DraconianErrorHandler implements ErrorHandler
 {
     public void fatalError( org.xml.sax.SAXParseException
 e ) throws SAXException
 {
         throw e;
     }
     public void error( org.xml.sax.SAXParseException
 e ) throws SAXException
 {
         throw e;
     }
     public void warning( org.xml.sax.SAXParseException
 e ) throws SAXException
 {
         // noop
     }
 }
 

When a new SchemaFactory object is created, initially this field is set to null. This field will NOT be inherited to Schema s, Validator s, or ValidatorHandler s that are created from this SchemaFactory .

Parameters
errorHandler A new error handler to be set. This parameter can be null.
Set the value of a feature flag.

Feature can be used to control the way a SchemaFactory parses schemas, although SchemaFactory s are not required to recognize any specific feature names.

The feature name is any fully-qualified URI. It is possible for a SchemaFactory to expose a feature value but to be unable to change the current value.

All implementations are required to support the javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING feature. When the feature is:

  • true: the implementation will limit XML processing to conform to implementation limits. Examples include enity 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 registered . See .
  • false: the implementation will processing XML according to the XML specifications without regard to possible implementation limits.
Parameters
nameThe feature name, which is a non-null fully-qualified URI.
valueThe requested value of the feature (true or false).
Throws
org.xml.sax.SAXNotRecognizedExceptionIf the feature value can't be assigned or retrieved.
org.xml.sax.SAXNotSupportedExceptionWhen the {@link SchemaFactory} recognizes the feature name but cannot set the requested value.
NullPointerException if the name parameter is null.
Set the value of a property.

The property name is any fully-qualified URI. It is possible for a SchemaFactory to recognize a property name but to be unable to change the current value.

SchemaFactory s are not required to recognize setting any specific property names.

Parameters
nameThe property name, which is a non-null fully-qualified URI.
objectThe requested value for the property.
Throws
org.xml.sax.SAXNotRecognizedExceptionIf the property value can't be assigned or retrieved.
org.xml.sax.SAXNotSupportedExceptionWhen the {@link SchemaFactory} recognizes the property name but cannot set the requested value.
NullPointerException if the name parameter is null.
Sets the LSResourceResolver to customize resource resolution when parsing schemas.

SchemaFactory uses a LSResourceResolver when 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 Schema is being parsed.

When the LSResourceResolver is null, the implementation will behave as if the following LSResourceResolver is set:

 class DumbDOMResourceResolver implements LSResourceResolver
 {
     public org.w3c.dom.ls.LSInput
 resolveResource(
         String publicId, String systemId, String baseURI) {
         
         return null; // always return null
     }
 }
 

If a LSResourceResolver throws a RuntimeException (or instances of its derived classes), then the SchemaFactory will abort the parsing and the caller of the newSchema method will receive the same RuntimeException .

When a new SchemaFactory object is created, initially this field is set to null. This field will NOT be inherited to Schema s, Validator s, or ValidatorHandler s that are created from this SchemaFactory .

Parameters
resourceResolver A new resource resolver to be set. This parameter can be null.
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

 getClass().getName() + '@' + Integer.toHexString(hashCode())
 
Return
a string representation of the object.
Causes current thread to wait until another thread invokes the method or the method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0).

The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:

     synchronized (obj) {
         while (<condition does not hold>)
             obj.wait();
         ... // Perform action appropriate to condition
     }
 
This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.
Throws
IllegalMonitorStateExceptionif the current thread is not the owner of the object's monitor.
InterruptedExceptionif another thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown.
Causes current thread to wait until either another thread invokes the method or the method for this object, or a specified amount of time has elapsed.

The current thread must own this object's monitor.

This method causes the current thread (call it T) to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. Thread T becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:

  • Some other thread invokes the notify method for this object and thread T happens to be arbitrarily chosen as the thread to be awakened.
  • Some other thread invokes the notifyAll method for this object.
  • Some other thread interrupts thread T.
  • The specified amount of real time has elapsed, more or less. If timeout is zero, however, then real time is not taken into consideration and the thread simply waits until notified.
The thread T is then removed from the wait set for this object and re-enabled for thread scheduling. It then competes in the usual manner with other threads for the right to synchronize on the object; once it has gained control of the object, all its synchronization claims on the object are restored to the status quo ante - that is, to the situation as of the time that the wait method was invoked. Thread T then returns from the invocation of the wait method. Thus, on return from the wait method, the synchronization state of the object and of thread T is exactly as it was when the wait method was invoked.

A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied. In other words, waits should always occur in loops, like this one:

     synchronized (obj) {
         while (<condition does not hold>)
             obj.wait(timeout);
         ... // Perform action appropriate to condition
     }
 
(For more information on this topic, see Section 3.2.3 in Doug Lea's "Concurrent Programming in Java (Second Edition)" (Addison-Wesley, 2000), or Item 50 in Joshua Bloch's "Effective Java Programming Language Guide" (Addison-Wesley, 2001).

If the current thread is interrupted by another thread while it is waiting, then an InterruptedException is thrown. This exception is not thrown until the lock status of this object has been restored as described above.

Note that the wait method, as it places the current thread into the wait set for this object, unlocks only this object; any other objects on which the current thread may be synchronized remain locked while the thread waits.

This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.

Parameters
timeoutthe maximum time to wait in milliseconds.
Throws
IllegalArgumentExceptionif the value of timeout is negative.
IllegalMonitorStateExceptionif the current thread is not the owner of the object's monitor.
InterruptedExceptionif another thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown.
Causes current thread to wait until another thread invokes the method or the method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.

This method is similar to the wait method of one argument, but it allows finer control over the amount of time to wait for a notification before giving up. The amount of real time, measured in nanoseconds, is given by:

 1000000*timeout+nanos

In all other respects, this method does the same thing as the method of one argument. In particular, wait(0, 0) means the same thing as wait(0).

The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until either of the following two conditions has occurred:

  • Another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method.
  • The timeout period, specified by timeout milliseconds plus nanos nanoseconds arguments, has elapsed.

The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:

     synchronized (obj) {
         while (<condition does not hold>)
             obj.wait(timeout, nanos);
         ... // Perform action appropriate to condition
     }
 
This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.
Parameters
timeoutthe maximum time to wait in milliseconds.
nanosadditional time, in nanoseconds range 0-999999.
Throws
IllegalArgumentExceptionif the value of timeout is negative or the value of nanos is not in the range 0-999999.
IllegalMonitorStateExceptionif the current thread is not the owner of this object's monitor.
InterruptedExceptionif another thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown.