Powered by SmartDoc

The java.verify option

The java.verify option enables the verify facility in the Relaxer Object.

Parameters

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

true
Enables the capability to verify with the Relaxer object itself.
false
Disables a capability to verify with the Relaxer object itself.

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

Artifacts

The java.verify option adds the following methods to Relaxer objects:

The isValid method just verifies itself and notifies whether or not be valid.

The verify method without parmeter verifies itself and notifies verification information.

The verify method with one parameter Element verifies

The verify method with four parameters Element, RVerifyReport, int, and RVerifyContext verifies itself and notifies verification information. This method is supporsed to be used by Relaxer runtime internally.

The verify method with four parameters RStack, RVerifyReport, int, and RVerifyContext verifies itself and notifies verification information. This method is supporsed to be used by Relaxer runtime internally.

The verifyProperty method verifies itself and and notifies verification information.

The typeStringProperty method provides a type name for a property.

Example

List 7.10.3.1[javaVerify.rng] is a sample schema for the java.verify option.

javaVerify.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">
          <param name="maxInclusive">200000</param>
        </data>
      </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.verify option is as follows:

$ relaxer -java -java.verify javaVerify.rng

Because the Java generator is a default generator, execution of Relaxer as shown below has the same effect:

$ relaxer -java.verify javaVerify.rng

As a result, Relaxer generates six files:

List 7.10.3.1.1[JavaVerify.java] is a sample program for the java.verify option.

JavaVerify.java
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import org.relaxer.runtime.RVerifyReport;

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

	String uri = args[0];
	Account account = makeAccount(uri);
	printAccount(account);
	showAccount(account);
	verifyAccount(account);
	verifyAccountWithMessage(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);
    }

    private static void verifyAccount(Account account) {
	System.out.println("*** verifyAccount ***");
	System.out.println("Valid? = " + account.isValid());
    }

    private static void verifyAccountWithMessage(Account account) {
	System.out.println("*** verifyAccountWithMessage ***");
	RVerifyReport report = account.verify();
	if (report != null) {
	    System.out.println("Message = " + report);
	}
    }
}

Compilation of JavaVerify.java is shown here:

$ javac -classpath ".;RelaxerOrg.jar" JavaVerify.java

If you use the java.verify option, the RelaxerOrg.jar is needed in the classpath for compilation and execution.

Execution

List 7.10.3.2.1[javaVerify.xml] is a valid XML document with regard to the RENAX NG schema javaVerify.rng.

javaVerify.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 JavaVerify for the valid document is shown here:

$ java -classpath ".;RelaxerOrg.jar" JavaVerify javaVerify.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>
*** verifyAccount ***
Valid? = true
*** verifyAccountWithMessage ***
Message = <report \
    xmlns="http://www.relaxer.org/xmlns/verifyReport"></report>

Invalid document - Bad value

List 7.10.3.3.1[javaVerifyBadStructure.xml] is a invalid XML document against the RENAX NG schema 'javaVerify.rng'.

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

Execution of the JavaVerify for the invalid document is here:

$ java -classpath ".;RelaxerOrg.jar" JavaVerify \
    javaVerifyBadValue.xml
*** makeAccount ***
*** printAccount ***
AccountNo:12345
Balance:203040
Owner:XML Taro
Address: [213] Yokohama
Phone: [123] 456-7890
Phone: [090] 123-4567
*** showAccount ***
XML:<account \
    accountNo="12345"><balance>203040</balance><owner>XML \
    Taro</owner><address zip="213">Yokohama</address><phone \
    area="123">456-7890</phone><phone \
    area="090">123-4567</phone></account>
*** verifyAccount ***
Valid? = false
*** verifyAccountWithMessage ***
Message = <report \
    xmlns="http://www.relaxer.org/xmlns/verifyReport"><error><path>/account/balance</path><parent>/account</parent><leaf>balance</leaf><status>INVALID_VALUE</status><value>203040</value><type><name>int</name><maxInclusive>200000</maxInclusive></type><message>out \
    of range</message></error></report>

In addtion that result of validation is notified as 'false', detail information for verification is displayed as a XML document.

Creating an XML document

Another example is making an XML document using A Relaxer Object. List 7.10.3.4.1[JavaVerifyMaker.java] is a Java program to make an XML document using a Relaxer Object.

JavaVerifyMaker.java
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import org.relaxer.runtime.RVerifyReport;

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

	Account account = makeAccount();
	printAccount(account);
	showAccount(account);
	verifyAccount(account);
	verifyAccountMessage(account);
    }

    private static Account makeAccount() {
	System.out.println("*** makeAccount ***");
	Account account = new Account();
	account.setAccountNo("12345");
	account.setBalance(102030);
	account.setOwner(null);	// set a invalid value
	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);
    }

    private static void verifyAccount(Account account) {
	System.out.println("Valid? = " + account.isValid());
    }

    private static void verifyAccountMessage(Account account) {
	RVerifyReport report = account.verify();
	if (report != null) {
	    System.out.println("Message = " + report);
	}
    }
}

Compilation of JavaVerifyMaker is shown here:

$ javac -classpath ".;RelaxerOrg.jar" JavaVerifyMaker.java

Execution of the JavaVerifyMaker class for the valid document is shown here:

$ java -classpath ".;RelaxerOrg.jar" JavaVerifyMaker
*** makeAccount ***
*** printAccount ***
AccountNo:12345
Balance:102030
Owner:null
*** showAccount ***
XML:Account@1cd2e5f
Valid? = false
Message = <report \
    xmlns="http://www.relaxer.org/xmlns/verifyReport"><error><path>/account/owner</path><parent>/account</parent><leaf>owner</leaf><status>LACK_OF_ELEMENT</status><value></value><type><name>token</name></type><message></message></error></report> \