Powered by SmartDoc

The java.jaxp.validation option

The java.jaxp.validation option specifies whether or not to use the parser's validation function when JAXP parser parses an XML document.

The java.jaxp.validation option is available when the java.jaxp option is availeble.

Normally a URI of a DTD used for validation is specified in a document type declaration (DOCTYPE) in a parsed XML document. However some application would want to use an DTD which is prepared the application itself. The java.jaxp.validation is used for this purpose. Relaxer Objects replace an DTD using the SAX's EntityResolver.

Parameters

The java.jaxp.validation option takes one of the following values as a parameter:

true
validates an XML document when parsing with a JAXP parser.
false
Does not validate an XML document when parsing with a JAXP parser.

The default configuration is false. No parameter implies that the parameter is true.

Artifacts

The same methods are generated just as using the java.jaxp option. However the behavior of the methods as follows are different from those that are generated without the java.jaxp.validation option.

Example

List 7.4.3.1[javaJaxpValidation.rng] is a sample schema for the java.jaxp.validation option.

javaJaxpValidation.rng
<grammar xmlns="http://relaxng.org/ns/structure/1.0"
         datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
  <start>
    <ref name="account"/>
  </start>
  <define name="account">
    <element name="account">
      <attribute name="accountNo">
        <data type="token"/>
      </attribute>
      <element name="balance">
        <data type="int"/>
      </element>
      <element name="owner">
        <data type="token"/>
      </element>
      <optional>
        <ref name="address"/>
      </optional>
      <zeroOrMore>
        <ref name="phone"/>
      </zeroOrMore>
    </element>
  </define>
  <define name="address">
    <element name="address">
      <attribute name="zip">
        <data type="token"/>
      </attribute>
      <text/>
    </element>
  </define>
  <define name="phone">
    <element name="phone">
      <attribute name="area">
        <data type="token"/>
      </attribute>
      <data type="token"/>
    </element>
  </define>
</grammar>

Build

Execution of Relaxer with the java.jaxp.validation option is as follows:

$ relaxer -java -java.jaxp -java.jaxp.validation \
    javaJaxpValidation.rng

Because the java option is a default option, and the default value for the java.jaxp option is true, execution of Relaxer shown below has the same effect:

$ relaxer -java.jaxp.validation javaJaxp.rng

As a result, Relaxer generates six files:

List 7.3.3.2[JavaJaxp.java] is a sample program for from the javaJaxpValidation.rng.

JavaJaxpValidation.java
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;

public class JavaJaxpValidation {
    public static void main(String[] args)
	throws IOException, SAXException, ParserConfigurationException {

	String uri = args[0];
	Account account = makeAccount(uri);
	printAccount(account);
	showAccount(account);
    }

    private static Account makeAccount(String uri)
	throws IOException, SAXException, ParserConfigurationException {

	System.out.println("*** makeAccount ***");
	Account account = new Account(uri);
	return (account);
    }

    private static void printAccount(Account account) {
	System.out.println("*** printAccount ***");
	String accountNo = account.getAccountNo();
	long balance = account.getBalance();
	String owner = account.getOwner();
	System.out.println("AccountNo:" + accountNo);
	System.out.println("Balance:" + balance);
	System.out.println("Owner:" + owner);
	Address address = account.getAddress();
	if (address != null) {
	    String zip = address.getZip();
	    String place = address.getContent();
	    System.out.println("Address: [" + zip + "] " + place);
	}
	Phone[] phones = account.getPhone();
	for (int i = 0;i < phones.length;i++) {
	    Phone phone = phones[i];
	    String area = phone.getArea();
	    String number = phone.getContent();
	    System.out.println("Phone: [" + area + "] " + number);
	}
    }

    private static void showAccount(Account account) {
	System.out.println("*** showAccount ***");
	System.out.println("XML:" + account);
    }
}

Compilation of JavaJaxpValidation is shown here:

$ javac JavaJaxpValidation.java

List 7.4.3.1.2[javaJaxpValidation.dtd] is a DTD for the javaJaxpValidation.rng.

javaJaxpValidation.dtd
<!ELEMENT account (balance, owner, address?, phone*)>
<!ATTLIST account accountNo CDATA #REQUIRED>

<!ELEMENT balance (#PCDATA)>

<!ELEMENT owner (#PCDATA)>

<!ELEMENT address (#PCDATA)>
<!ATTLIST address zip CDATA #REQUIRED>

<!ELEMENT phone (#PCDATA)>
<!ATTLIST phone area CDATA #REQUIRED>

Execution

List 7.4.3.2.1[javaJaxpValidation.xml] is a valid XML document with regard to the DTD javaJaxpValidation.dtd.

javaJaxpValidation.xml
<!DOCTYPE account SYSTEM "javaJaxpValidation.dtd">

<account accountNo="12345">
  <balance>102030</balance>
  <owner>XML Taro</owner>
  <address zip="213">Yokohama</address>
  <phone area="123">456-7890</phone>
  <phone area="090">123-4567</phone>
</account>

Execution of the JavaJaxpValidation class for validation of the document is shown here:

$ java JavaJaxpValidation javaJaxpValidation.xml
*** makeAccount ***
*** printAccount ***
AccountNo:12345
Balance:102030
Owner:XML Taro
Address: [213] Yokohama
Phone: [123] 456-7890
Phone: [090] 123-4567
*** showAccount ***
XML:<account \
    accountNo="12345"><balance>102030</balance><owner>XML \
    Taro</owner><address zip="213">Yokohama</address><phone \
    area="123">456-7890</phone><phone \
    area="090">123-4567</phone></account>

List 7.4.3.2.2[javaJaxpValidationBadStructure.xml] is an XML document that is invalid, because of the presence of the element badElement.

javaJaxpValidationBadStructure.xml
<!DOCTYPE account SYSTEM "javaJaxpValidation.dtd">

<account accountNo="12345">
  <balance>102030</balance>
  <owner>XML Taro</owner>
  <address zip="213">Yokohama</address>
  <phone area="123">456-7890</phone>
  <phone area="090">123-4567</phone>
  <badElement/>
</account>

Execution of the JavaJaxpValidation class is shown here:

$ java JavaJaxpValidation javaJaxpValidationBadStructure.xml
*** makeAccount ***
error : org.apache.crimson.parser/V-036 account badElement
error : org.apache.crimson.parser/V-005 badElement
*** printAccount ***
AccountNo:12345
Balance:102030
Owner:XML Taro
Address: [213] Yokohama
Phone: [123] 456-7890
Phone: [090] 123-4567
*** showAccount ***
XML:<account \
    accountNo="12345"><balance>102030</balance><owner>XML \
    Taro</owner><address zip="213">Yokohama</address><phone \
    area="123">456-7890</phone><phone \
    area="090">123-4567</phone></account>

Error message is displayed.

List 7.4.3.2.3[javaJaxpValidationNoDoctype.xml] is also an invalid XML document, because it does not have a DOCTYPE declaration.

javaJaxpValidationNoDoctype.xml
<account accountNo="12345">
  <balance>102030</balance>
  <owner>XML Taro</owner>
  <address zip="213">Yokohama</address>
  <phone area="123">456-7890</phone>
  <phone area="090">123-4567</phone>
</account>

Execution of the JavaJaxpValidation class is shown here:

$ java JavaJaxpValidation javaJaxpValidationNoDoctype.xml
*** makeAccount ***
warning : org.apache.crimson.parser/V-001
error : org.apache.crimson.parser/V-005 account
error : org.apache.crimson.parser/V-007 accountNo account
error : org.apache.crimson.parser/V-005 balance
error : org.apache.crimson.parser/V-005 owner
error : org.apache.crimson.parser/V-005 address
error : org.apache.crimson.parser/V-007 zip address
error : org.apache.crimson.parser/V-005 phone
error : org.apache.crimson.parser/V-007 area phone
error : org.apache.crimson.parser/V-007 area phone
*** printAccount ***
AccountNo:12345
Balance:102030
Owner:XML Taro
Address: [213] Yokohama
Phone: [123] 456-7890
Phone: [090] 123-4567
*** showAccount ***
XML:<account \
    accountNo="12345"><balance>102030</balance><owner>XML \
    Taro</owner><address zip="213">Yokohama</address><phone \
    area="123">456-7890</phone><phone \
    area="090">123-4567</phone></account>