Sun Java Solaris Communities My SDN Account
 
Article

JAXP 1.2 Change Requests

 

Description

Maintenance revision for the Java API for XML Processing Specification, version 1.2.

This document is dated 30-April-2002

Maintenance Lead

Rajiv Mordani, Sun Microsystems, Inc.

Feedback

Feedback should be sent to jsr-63-comments@jcp.org

Nature of the Release

This Java Specification Request (JSR) is a maintenance release that provides XML schema support, so it can be integrated into the J2SE and J2EE platforms. The features and properties defined must be supported by any JAXP 1.2 compliant parser.

Rationale for the proposed changes.

Up to now, there has been no standard way to enable schema validation. Each of the existing mechanisms is implementation specific, which results in non-portable code.

This JSR defines the property strings necessary to enable schema-based validation. The properties are used by the parser to locate the schema file. The proposal defines 2 properties -- one for defining the schema language being used (for example, W3C schemas) and one for specifying which schema file(s) to use.

Proposed Changes

javax.xml.parsers.SAXParserFactory

The validating property must have the value true for any of the property strings to take effect. Otherwise, the values of the properties defined below will be ignored. This value can be set by invoking setValidating(true).

javax.xml.parsers.SAXParser

 The setProperty method in SAXParser must support the property strings defined below to indicate the schema language and the source of the schema file(s) to the parser:

  • http://java.sun.com/xml/jaxp/properties/schemaLanguage

  • This property defines the schema language to be used for validation. The value of this property must be the URI of the schema language specification. To be compliant with this version of the specification, the implementation must support the W3C XML schema specification at this URI: http://www.w3.org/2001/XMLSchema.

    When setValidating is set to true and a schema language is set, then the parser must validate against that schema language only. For example if an application sets the schemaLanguage property to XML Schemas then the parser must try to validate against the XML schema only, even if the document has a DOCTYPE declaration that refers to a DTD.

  • http://java.sun.com/xml/jaxp/properties/schemaSource

  • The XML Schema Recommendation explicitly states that the inclusion of schemaLocation / noNamespaceSchemaLocation attributes in an instance document is only a hint; it does not mandate that these attributes must be used to locate schemas.

    The schemaSource property lets the user set the schema(s) to validate against. If the target namespace of a schema specified using this property matches the target namespace of a schema occuring in schemaLocation attribute, the schema specified by the user using this property will be used and the instance document's schemaLocation attribute will be effectively ignored. However if the target namespace of any schema specified using this property doesn't match the target namespace of a schema occuring in the instance document, then the hint specified in the instance document will be used for validation. The acceptable value for this property must be one of the following:

    • String that points to the URI of the schema
    • InputStream with the contents of the schema
    • SAX InputSource
    • File
    • an array of Objects with the contents being one of the types defined above. An array of Objects can be used only when the schema language has the ability to assemble a schema at runtime. When an array of Objects is passed it is illegal to have two schemas that share the same namespace.

    If no target namespace is defined, then only one schema can be referenced by the property and it must work exactly the way xsi:noNamespaceSchemaLocation does.

    It is illegal to set the schemaSource property if the schemaLanguage property has not been set. In that case, the implementation must throw a SAXNotSupportedException with a detailed message.

    If the schemaSource property is set using a String, the parser must pass the value of the property to the org.xml.sax.EntityResolver with the publicId set to null.

javax.xml.parsers.DocumentBuilderFactory

The same property strings as described above for the SAXParser must be supported by DocumentBuilderFactory.setAttribute method.

When setValidating is set to true and a schema language is set then the parser must validate against that schema language only. For example if an application sets the schema language property to XML Schemas the parser must try to validate against the XML schema only, even if the document has a DOCTYPE declaration that refers to a DTD.

It is illegal to set the schemaSource property if the schemaLanguage property has not been set. In that case, the implementation must throw an IllegalArgumentException with a detailed message.

Note: None of the properties will take effect till the setValidating(true) has been called on the SAXParserFactory or the DocumentBuilderFactory that was used to create the SAXParser or the DocumentBuilder.

The table below shows the results of various configuration scenarios. In all cases, we assume that setValidating(true) has been called.

Document has DOCTYPE? schema language property set to XML Schemas? schema source property set? any schema location attribute present in document? Document 
Validated 
against
Schema 
file 
used
no no no  no error as per JAXP 1.1 spec. Must have a DOCTYPE declaration when validation is turned on. N/A
no no no yes Error. Schema language must be set . N/A
no no yes yes / no Error schema language must be set N/A
yes / no yes no yes xml schemas schema files referred to using the schema location attributes present in the instance document
yes / no yes yes no xml schemas schema file referred to in the schema source property.
yes / no yes yes yes xml schemas schema file referred to in the schema source property, if the target namespace matches. The schema file referred to in the schema location attribute is ignored only if the target namespace matches.
yes no no yes / no DTD  DTD referred to in the DOCTYPE
yes no yes yes / no Error. Schema source cannot be set without setting the schema language. N/A

Samples using the properties

Sax parser sample
    try {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        spf.setValidating(true);
	SAXParser sp = spf.newSAXParser();
	sp.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
 		       "http://www.w3.org/2001/XMLSchema");
	sp.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource",
		       "http://www.example.com/Report.xsd");
	DefaultHandler dh = new DefaultHandler();
	sp.parse("http://www.wombats.com/foo.xml", dh);
    } catch(SAXException se) {
	se.printStackTrace();
    }

DOM parser sample
    try {
	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	dbf.setNamespaceAware(true);
	dbf.setValidating(true);
	dbf.setAttribute(
		  "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
		  "http://www.w3.org/2001/XMLSchema");
	dbf.setAttribute(
		  "http://java.sun.com/xml/jaxp/properties/schemaSource",
		  "http://www.example.com/Report.xsd");
	DocumentBuilder db = dbf.newDocumentBuilder();
	Document doc = db.parse("http://www.wombats.com/foo.xml");
    } catch(DOMException de) {
	de.printStackTrace();
    }

Recommended implementation of properties

It is recommended that parser implementations recognize properties defined in the form of a URI, as above. Such implementations avoid conflicts in the use of the feature and property strings among parser implementations. That is also the way in which SAX 2.0 defines feature and property strings.

Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.