The java option is a Java generator option. The java option enables Relaxer to generate Java classes that are mapped to RELAX schemas.
The java option takes no parameters. Specifing the java option indicates that you want to use the Java Generator.
Methods in a Relaxer Objects are shown in Table 7.2.2.1[Methods of a Relaxer object].
method | java | java.jaxp | java.text |
---|---|---|---|
Constructor() |
O | - | - |
Constructor(RStack) |
O | - | - |
Constructor(org.w3c.dom.Document) |
O | - | - |
Constructor(org.w3c.dom.Element) |
O | - | - |
Constructor(java.io.File) |
- | O | - |
Constructor(java.lang.String) |
- | O | - |
Constructor(java.net.URL) |
- | O | - |
Constructor(java.io.InputStream) |
- | O | - |
Constructor(org.xml.sax.InputSource) |
- | O | - |
Constructor(java.io.Reader) |
- | O | - |
setup(org.w3c.dom.Document) |
O | - | - |
setup(org.w3c.dom.Element) |
O | - | - |
setup(java.io.File) |
- | O | - |
setup(java.lang.String) |
- | O | - |
setup(java.net.URL) |
- | O | - |
setup(java.io.InputStream) |
- | O | - |
setup(org.xml.sax.InputSource) |
- | O | - |
setup(java.io.Reader) |
- | O | - |
makeDocument() |
- | O | - |
getProperty() |
O | - | - |
setProperty(Type) |
O | - | - |
makeTextDocument() |
- | - | O |
makeTextElement(java.lang.StringBuffer) |
- | - | O |
makeTextElement(java.io.Writer) |
- | - | O |
makeTextElement(java.io.PrintWriter) |
- | - | O |
makeTextAttribute(java.lang.StringBuffer) |
- | - | O |
makeTextAttribute(java.io.Writer) |
- | - | O |
makeTextAttribute(java.io.PrintWriter) |
- | - | O |
getPropertyNoAsString() |
- | - | O |
setPropertyByString(java.lang.String) |
- | - | O |
toString() |
- | - | O |
isMatch(org.w3c.dom.Element) |
O | - | - |
isMatch(RStack) |
O | - | - |
isMatchHungry() |
O | - | - |
List 7.2.3.1[The java.rng] is a sample shcema.
<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 option is as follows:
$ relaxer -verbose -java java.rng Copyright(c) 2000-2002 ASAMI,Tomoharu. All rights reserved. Relaxer Version 0.17b (20021201) by asami@relaxer.org Source file : file:/c:/docs/sample.rng artifact = Account.java artifact = Address.java artifact = Phone.java artifact = URelaxer.java artifact = RStack.java artifact = UJAXP.java
Because the Java generator is a default generator, it is omittable on the command line. Because of this, execution of Relaxer as shown below has the same result.
$ relaxer java.rng
As a result of either of these commands, Relaxer generates six files:
The file Account.java is a Java object that represents an XML document which is defined by the RELAX NG Schema java.rng.
The files URelaxer.java, RStack.java and UJAXP.java are utility classes to help execute the Relaxer object.
Because the java.jaxp option and
the java.text option are true
by default,
specifing just the java option implies
both the java.jaxp option and the java.text
options.
Execution of Relaxer disabling the java.jaxp is shown here:
$ relaxer -java -java.jaxp:false java.rng
This creates five files shown below:
The UJAXP.java is missing because the java.jaxp was
set to false
.
If you write an application, then,
you must write your own logic to create DOM objects from XML documents.
List 7.2.3.1.1[JavaWithoutJaxp.java] is a test program that demonstrates what you must to do when you don't use the java.jaxp option.
import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import org.xml.sax.SAXException; import java.io.File; import java.net.URL; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.Document; public class JavaWithoutJaxp { public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException { String filename = args[0]; Account account = makeAccount(filename); printAccount(account); showAccount(account); } private static Account makeAccount(String filename) throws IOException, SAXException, ParserConfigurationException { System.out.println("*** makeAccount ***"); Document doc = getDocument(filename); Account account = new Account(doc); 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 Document getDocument(String filename) throws IOException, SAXException, ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); return (builder.parse(new File(filename))); } }
What you must to do is get a DOM objects from an XML document. In this examle, the JAXP parser is used for this purpose explicitly.
The compilation of the JavaWithoutJaxp.java
is shown here:
$ javac JavaWithoutJaxp.java
Execution of the class JavaWithoutJaxp
is shown here:
$ java JavaWithoutJaxp java.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>
This program generates the same result as the one that uses the java.jaxp option.
Please refer to the section of the java.jaxp option for more information.
The following command disables the java.text option: here:
$ relaxer -java -java.text:false java.rng
As a result, Relaxer generates six files:
List 7.2.3.2.1[JavaWithoutText.java] is a sample program for the java.text option.
import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import org.xml.sax.SAXException; public class JavaWithoutText { 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 JavaWithoutText
is shown here:
$ javac JavaWithoutText.java
Execution of the JavaWithoutText
class follows:
$ java JavaWithoutText java.xml *** makeAccount *** *** printAccount *** AccountNo:12345 Balance:102030 Owner:XML Taro Address: [213] Yokohama Phone: [123] 456-7890 Phone: [090] 123-4567 *** showAccount *** XML:Account@5b7986
In this case, the string form of the object itself is the Java object's default form.