Powered by SmartDoc

The java.jarv.verify option

The java.jarv.verify option enables Relaxer objects to verify in runtime using JARV.

Parameters

The java.jarv.verify option takes one of the following values as a parameter:

none
Does not verify using a JARV verifier.
auto
Detects appropriate schema type automatically.
rng
Uses a RELAX NG schema DTD to verify.
rxm
Uses a RELAX Core schema to verify.
dtd
Uses a DTD schema to verify.
xsd
Uses a W3C XML Schema schema to verify.

If parameter is ommitted, "auto" is used implicitly.

value

The java.jarv.verify option can have values as follows:

The value none means that Relaxer Objects does not verify using JARV in unmarshalling process.

The value auto means that Relaxer Objects verifies using JARV in unmarshalling process by a schema selected automatically.

The value rng means that Relaxer Objects verifies using JARV in unmarshalling process by a RELAX NG schema.

The value rxm means that Relaxer Objects verifies using JARV in unmarshalling process by a RELAX Core schema.

The value dtd means that Relaxer Objects verifies using JARV in unmarshalling process by a DTD schema.

The value xsd means that Relaxer Objects verifies using JARV in unmarshalling process by a W3C XML Schema schema.

Artifacts

The java.jarv.verify option adds no methods to a Relaxer object. However, the exception org.iso_relax.verifier.VerifierConfigurationException is added to methods shown below:

Example

List 7.9.4.1[javaJarvVerify.rng] is a sample schema for the java.jarv.verify option.

javaJarvVerify.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.jarv.verify option is as follows:

$ relaxer -java -java.jaxp -java.jarv.verify javaJarvVerify.rng

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

$ relaxer -java.jarv.verify javaJarvVerify.rng

As a result, Relaxer generates six files:

List 7.9.4.1.1[JavaJarvVerify.java] is a sample program for the java.jarv.verify option.

JavaJarvVerify.java
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import org.iso_relax.verifier.VerifierConfigurationException;

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

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

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

	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 JavaJarvVerify.java is shown here:

$ javac -classpath ".;isorelax.jar" JavaJarvVerify.java

If you use the java.jarv.verify option, the isorelax.jar is needed on the classpath for compilation and execution.

Execution

List 7.9.4.2.1[JavaJarvVerify.xml] is a valid XML document with regard to the RENAX NG schema javaJarvVerify.rng.

JavaJarvVerify.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>

Executions of the JavaJarvVerify class for the valid document are as follows:

When using Jing for verification:

$ java -classpath ".;isorelax.jar;jing.jar" JavaJarvverify \
    javaJarvVerify.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>

When using MSV for verification:

*** 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>

Invalid document

List 7.9.4.3.1[javaJarvVerifyBadStructure.xml] is a invalid XML document against the RENAX NG schema 'javaJarvVerify.rng'.

javaJarvVerifyBadStructure.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>
  <badElement/>
</account>

Executions of the JavaJarvVerify for the invalid document with the jing verifier are as follows:

$ java -classpath ".;isorelax.jar;jing.jar" JavaJarvVerify \
    javaJarvVerifyBadStructure.xml
error : unknown element "badElement"
AccountNo:12345
Balance:102030
Owner:XML Taro
XML:<account \
    accountNo="12345"><balance>102030</balance><owner>XML \
    Taro</owner></account>

DTD

To verfy by DTD, a parameter of the java.jarv.verify option is setted as 'dtd'.

$ relaxer -java.jarv.verify:dtd javaJarvVerify.rng

Prepare a DTD shown in List 7.9.4.4.1[javaJarvVerify.dtd], which correspond with the RENAX NG schema 'javaJarvVerify.rng'. Put it to a appropriate directory in the resource.

javaJarvVerify.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>

Executions of the JavaJarvVerify for the invalid document with the msv verifier are as follows:

$ java -classpath ".;isorelax.jar;msv.jar" JavaJarvVerify \
    javaJarvVerifyBadStructure.xml
*** makeAccount ***
error : tag name "badElement" is not allowed. Possible tag names \
    are: <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>

Package

The java.package option affects a outcome of the java.jarv.verify option.

Here is a execution of the relaxer without the java.package option.

$ relaxer -java -java.jaxp -java.jarv.verify javaJarvVerify.rng

In this case, Resource name is /javaJarvVerify.rng. The file javaJarvVerify.rng must be prepared in the root directory of the resouce.

As a result, Relaxer generates six files:

Here is a execution of the Relaxer with the java.package option. The package name is com.example.javaJarvVerify.

$ relaxer -java -java.jaxp -java.jarv.verify \
    -java.package:com.example.javaJarvVerify javaJarvVerify.rng

In this case, the resource name is /com/example/javaJarvVerify/javaJarvVerify.rng. The file javaJarvVerify must be prepared in the /com/example/javaJarvVerify directory of the resouce.