Powered by SmartDoc

The java.robust option

The java.robust option specifies a degree of robustness for the Relaxer Object.

Parameters

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

plain
Simple object construction. No consideration for robustness.
stable
Stable object construction. Handles illegal structures.
robust
Robust object construction. Handles illegal structures and values.

The default configuration is plain.

Artifacts

The java.robust option adds additional algorithms to be robust objects.

Example

List 7.12.3.1[javaRobust.rng] is a sample schema for the java.robust option.

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

List 7.12.3.2[JavaRobust.java] is a sample program for the java.robust option.

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

public class JavaRobust {
    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);
    }
}

Build - plain

Execution of Relaxer with the java.robust option with parameter plain is as follows:

$ relaxer -java -java.robust:plain javaRobust.rng

Because the Java generator is a default generator, and a default value for java.robust option is plain, shown below has the same effect:

$ relaxer javaRobust.rng

As a result, Relaxer generates six files:

Compilation of the JavaRobust is shown here:

$ javac JavaRobust.java

No special options required.

Execution - plain

List 7.12.3.2.1[javaRobust.xml] is a valid XML document.

javaRobust.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 JavaRobust class is shown here:

$ java JavaRobust javaRobust.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.12.3.2.2[javaRobustBadStructure.xml] is an XML document that is invalid, because of the lack of the element balance.

javaRobustBadStructure.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 JavaRobust class against the javaRobustBadStructure.xml is shown here:

$ java JavaRobust javaRobustBadStructure.xml
*** makeAccount ***
java.lang.IllegalArgumentException: \
    java.lang.NumberFormatException: For input string: "XML Taro"
	at URelaxer._invalidIntValue(URelaxer.java:8473)
	at URelaxer.getElementPropertyAsInt(URelaxer.java:1883)
	at Account.init(Account.java:244)
	at Account.setup(Account.java:223)
	at Account.setup(Account.java:214)
	at Account.setup(Account.java:319)
	at Account.<init>(Account.java:139)
	at JavaRobust.makeAccount(JavaRobust.java:19)
	at JavaRobust.main(JavaRobust.java:10)

Object construction is fail due to the lack of the element balance.

List 7.12.3.2.3[javaRobustBadValue.xml] is also an invalid XML document, because the value of the element balance is not a integer value.

javaRobustBadValue.xml
<account accountNo="12345">
  <balance>badNumber</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 JavaRobust class against the javaRobustBadValue.xml is shown here:

$ java JavaRobust javaRobustBadValue.xml
*** makeAccount ***
java.lang.IllegalArgumentException: \
    java.lang.NumberFormatException: For input string: \
    "badNumber"
	at URelaxer._invalidIntValue(URelaxer.java:8473)
	at URelaxer.getElementPropertyAsInt(URelaxer.java:1883)
	at Account.init(Account.java:244)
	at Account.setup(Account.java:223)
	at Account.setup(Account.java:214)
	at Account.setup(Account.java:319)
	at Account.<init>(Account.java:139)
	at JavaRobust.makeAccount(JavaRobust.java:19)
	at JavaRobust.main(JavaRobust.java:10)

Object construction is fail due to the illegal value of the element balance.

When the value of java.robust is simple, or default, object construction for a Relaxer object is fragail against both illegal structure and illegal value.

Build - stable

Execution of Relaxer with the java.robust option with parameter stable is as follows:

$ relaxer -java -java.robust:stable javaRobust.rng

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

$ relaxer -java.robust:stable javaRobust.rng

As a result, Relaxer generates six files:

Compilation of the JavaRobust is shown here:

$ javac JavaRobust.java

No special options required.

Execution - stable

List 7.12.3.4.1[javaRobust.xml] is a valid XML document.

javaRobust.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 JavaRobust class against the javaRobust.xml is shown here:

$ java JavaRobust javaRobust.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.12.3.4.2[javaRobustBadStructure.xml] is an XML document that is invalid, because of the lack of the element balance.

javaRobustBadStructure.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 JavaRobust class against the javaRobustBadStructure.xml is shown here:

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

Object construction is success.

List 7.12.3.4.3[javaRobustBadValue.xml] is also an invalid XML document, because the value of balance element is not integer.

javaRobustBadValue.xml
<account accountNo="12345">
  <balance>badNumber</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 JavaRobust class against the javaRobustBadValue.xml is shown here:

$ java JavaRobust javaRobustBadValue.xml
*** makeAccount ***
java.lang.IllegalArgumentException: \
    java.lang.NumberFormatException: For input string: \
    "badNumber"
	at URelaxer._invalidIntValue(URelaxer.java:8473)
	at URelaxer.getElementPropertyAsInt(URelaxer.java:1883)
	at Account.init(Account.java:244)
	at Account.setup(Account.java:223)
	at Account.setup(Account.java:214)
	at Account.setup(Account.java:319)
	at Account.<init>(Account.java:139)
	at JavaRobust.makeAccount(JavaRobust.java:19)
	at JavaRobust.main(JavaRobust.java:10)

Object construction is fail due to the illegal value of the element balance.

Build - robust

Execution of Relaxer with the java.robust option with parameter robust is as follows:

$ relaxer -java -java.robust:robust javaRobust.rng

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

$ relaxer -java.robust:robust javaRobust.rng

As a result, Relaxer generates six files:

Compilation of the JavaRobust is shown here:

$ javac JavaRobust.java

No special options required.

Execution - robust

List 7.12.3.6.1[javaRobust.xml] is a valid XML document.

javaRobust.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 JavaRobust class is shown here:

$ java JavaRobust javaRobust.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.12.3.6.2[javaRobustBadStructure.xml] is an XML document that is invalid, because of the lack of the element balance.

javaRobustBadStructure.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 JavaRobust class against the javaRobustBadStructure.xml is shown here:

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

Object construction is success.

List 7.12.3.6.3[javaRobustBadValue.xml] is also an invalid XML document, because the value of balance element is not integer.

javaRobustBadValue.xml
<account accountNo="12345">
  <balance>badNumber</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 JavaRobust class against the javaRobustBadValue.xml is shown here:

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

Object construction is success.