Powered by SmartDoc

The java.name.class.prefixes option

The java.name.class.prefixes option spacifies prefixes for class names with namespaces for Relaxer objects.

Parameters

The java.name.class.prefixes option takes prefix names with namespaces as a parameter.

Artifacts

The java.name.class.prefixes option generates no additional classes.

The java.name.class.prefixes option adds no additional methods to Relaxer objects.

The string specified by as a parameter for the java.name.class.prefixes option is used for prefixes for class names for Relaxer objects.

List 7.29.3.1[javaNameClassPrefixes.rng] is a sample schema for the java.name.class.prefixes option.

javaNameClassPrefixes.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" ns="http://example.com/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>
      <ref name="address"/>
      <zeroOrMore>
        <ref name="phone"/>
      </zeroOrMore>
    </element>
  </define>
  <define name="address">
    <element name="address" ns="http://example.com/address">
      <attribute name="zip">
        <data type="token"/>
      </attribute>
      <text/>
    </element>
  </define>
  <define name="phone">
    <element name="phone" ns="http://example.com/address">
      <attribute name="area">
        <data type="token"/>
      </attribute>
      <data type="token"/>
    </element>
  </define>
</grammar>

Build

Execution of the Relaxer with the java.name.class.prefixes option is as follows:

$ relaxer -java \
    -java.name.class.prefixes:http://example.com/account:X,http://example.com/address:Y \
    -java.xml.namespace javaNameClassPrefixes.rng

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

$ relaxer \
    -java.name.class.prefixes:http://example.com/account:X,http://example.com/address:Y \
    -java.xml.namespace javaNameClassPrefixes.rng

As a result, Relaxer generates six files:

The prefix 'X' is used for an "XAccount" class, and the prefix 'Y' is used for "YAddress" and "YPhone" classes.

List 7.29.3.1.1[JavaNameClassPrefixes.java] is a sample program for the java.name.class.prefixes option.

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

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

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

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

	System.out.println("*** makeAccount ***");
	XAccount account = new XAccount(uri);
	return (account);
    }

    private static void printAccount(XAccount 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);
	YAddress address = account.getAddress();
	if (address != null) {
	    String zip = address.getZip();
	    String place = address.getContent();
	    System.out.println("Address: [" + zip + "] " + place);
	}
	YPhone[] phones = account.getPhone();
	for (int i = 0;i < phones.length;i++) {
	    YPhone phone = phones[i];
	    String area = phone.getArea();
	    String number = phone.getContent();
	    System.out.println("Phone: [" + area + "] " + number);
	}
    }

    private static void showAccount(XAccount account) {
	System.out.println("*** showAccount ***");
	System.out.println("XML:" + account);
    }
}

Compilation of JavaNameClassPrefixes.java class is shown here:

$ javac JavaNameClassPrefixes.java

Execution

List 7.29.3.2.1[javaNameClassPrefixes.xml] is an XML document file for testing.

javaNameClassPrefixes.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 JavaNameClassPrefixes class is shown here:

$ java JavaNameClassPrefixes javaNameClassPrefixes.xml
*** makeAccount ***
*** printAccount ***
AccountNo:12345
Balance:102030
Owner:XML Taro
Address: [213] Yokohama
Phone: [123] 456-7890
Phone: [090] 123-4567
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>