The java.verify.assertion option enables the assertion facility in the Relaxer Object.
The java.verify.assertion option takes one of the following values as a parameter:
The default configuration is false
.
No parameter implies that the parameter is true
.
The java.verify.assertion option adds the following methods to Relaxer objects:
boolean isValid()
org.relaxer.runtime.RVerifyReport verify()
verify(org.relaxer.runtime.RVerifReport, int, RVerifyContext)
org.relaxer.runtime.RVerifyReport verify(org.w3c.dom.Element)
verify(org.w3c.dom.Element, org.relaxer.runtime.RVerifReport, int, RVerifyContext)
verify(RStack, org.relaxer.runtime.RVerifyReport, int, org.relaxer.runtime.RVerifyContext
org.relaxer.runtime.RVerifyReport verifyProperty(datatype)
String typeStringProperty()
void rAssert()
void rAssert(org.w3c.dom.Element)
void rAssertProperty(datatype)
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 informs a type name
for a property.
The rAssert
method without parameter is a assert function which
verifies itself and raises a exception in case of invalid.
The rAssert
method with the org.w3c.dom.Element
is a assert function against the Element
.
The rAssertProperty
methods are asset functions
which verifies each properties.
List 7.11.3.1[javaVerifyAssertion.rng] is a sample schema for the java.verify.assertion option.
<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>
Execution of Relaxer with the java.verify.assertion option is as follows:
$ relaxer -java -java.verify -java.verify.assertion \ javaVerify.rng
To use the java.verify.assertion option, the java.verify option is required.
Because the Java generator is a default generator, execution of Relaxer as shown below has the same effect:
$ relaxer -java.verify -java.verify.assertion javaVerify.rng
As a result, Relaxer generates six files:
List 7.11.3.1.1[JavaVerifyAssertion.java] is a sample program for the java.verify.assertion option.
import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import org.xml.sax.SAXException; import org.relaxer.runtime.RAssertionException; public class JavaVerifyAssertion { public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException { try { String uri = args[0]; Account account = makeAccount(uri); printAccount(account); showAccount(account); } catch (RAssertionException e) { printExceptionItself(e); useExceptionData(e); } } 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); } public static void printExceptionItself(RAssertionException e) { System.out.println(e.getMessage()); } public static void useExceptionData(RAssertionException e) { } }
Compilation of the JavaVerifyAssertion
is shown here:
$ javac -classpath ".;RelaxerOrg.jar" JavaVerifyAssertion.java
If thejava.verify.assertion option is used, the RelaxerOrg.jar is needed for compilation and execution.
List 7.11.3.2.1[javaVerifyAssertion.xml] is a valid XML document with regard to the RENAX NG schema javaVerifyAssertion.rng.
<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
class for the valid document is shown here:
$ java -classpath ".;RelaxerOrg.jar" JavaVerifyAssertion \ javaVerifyAssertion.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.11.3.3.1[javaVerifyAssertionBadStructure.xml] is a valid XML document against the RELAX NG schema 'javaVerifyAssertion.rng'.
<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 JavaVerifyAssertion
for the invalid document is here:
$ java -classpath ".;RelaxerOrg.jar" JavaVerifyAssertion \ javaVerifyAssertionBadValue.xml *** makeAccount *** <report \ xmlns="http://www.relaxer.org/xmlns/verifyReport"><error><path>/account/</path><parent>/account</parent><leaf></leaf><status>ILLEGAL_ELEMENT</status><value></value><type><name>account</name></type><message></message></error></report> \
List 7.11.3.4.1[javaVerifyAssertionBadValue.xml] is a valid XML document against the RELAX NG schema 'javaVerifyAssertion.rng'.
<account accountNo="12345"> <balance>a12345</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 JavaVerifyAssertion
for the invalid document is here:
$ java -classpath ".;RelaxerOrg.jar" JavaVerifyAssertion \ javaVerifyAssertionBadValue.xml *** makeAccount *** <report \ xmlns="http://www.relaxer.org/xmlns/verifyReport"><error><path>/account/balance</path><parent>/account</parent><leaf>balance</leaf><status>INVALID_VALUE</status><value>a12345</value><type><name>int</name></type><message>invalid \ number format</message></error></report>