Powered by SmartDoc

The java.pattern.factory option

The java.pattern.fctory option enables a factory facility for Relaxer objects.

Parameters

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

true
Enables a factory facility.
false
Disables a factory facility.

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

Artifacts

The java.pattern.factory option generates additional classes as follows:

The IGrammarFactory is a interface of the factory class.

The AbstractGrammarFactory is a abstract base class of the factory class implementation.

The DefaultGrammarFactory is a default concrete factory class.

GrammarFactory is a factory of the factory class.

Example

Basic usage

List 7.18.3.1.1[javaPatternFactory.rng] is a sample schema for the java.pattern.factory option.

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

Build

Execution of Relaxer with the java.pattern.factory option is as follows:

$ relaxer -java -java.pattern.factory javaPatternFactory.rng

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

$ relaxer -java.pattern.factory javaPatternFactory.rng

As a result, Relaxer generates nine files:

Account.java and Address.java are Relaxer objects.

URelaxer.java and RStack.java are helper classes for Relaxer objects. UJAXP is a helper class for using the JAXP parser.

The IJavaPatternFactoryFactory, the AbstractJavaPatternFactoryFactory, the DefaultJavaPatternFactoryFactory, and the JavaPatternFactoryFactory are factory classes.

List 7.18.3.2.1[JavaPatternFactory.java] is a sample program for the java.pattern.factory option.

In the makeAccount method, an object Account is created using the factory object.

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

public class JavaPatternFactory {
    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 ***");
        IJavaPatternFactoryFactory factory
	    = JavaPatternFactoryFactory.getFactory();
	Account account = (Account)factory.create(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 the JavaPatternFactory is shown here:

$ javac JavaPatternFactory.java

Execution

List 7.18.3.3.1[javaPatternFactory.xml] is an XML document for testing.

javaPatternFactory.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 JavaPatternFactory class is shown here:

$ java JavaPatternFactory javaPatternFactory.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>

User defined object

List 7.18.3.4.1[MyJavaPatternFactoryFactory.java] is a user define factory class 'MyJavaPatternFactoryFactory'. This class implements IJavaPatternFactory.

MyJavaPatternFactoryFactory.java
public class MyJavaPatternFactoryFactory extends \
    AbstractJavaPatternFactoryFactory {
    public Account createAccount() {
	return (new MyAccount());
    }

    public Address createAddress() {
        return (new MyAddress());
    }

    public Phone createPhone() {
        return (new MyPhone());
    }
}

The MyAccount(List 7.18.3.4.2[MyAccount.java]), the MyAddress(List 7.18.3.4.3[MyAddress.java]), and the MyPhone(List 7.18.3.4.4[MyPhone.java]) are user defined concrete objects.

MyAccount.java
public class MyAccount extends Account {
    public void printInfo() {
	System.out.println("AccountNo:" + getAccountNo());
	System.out.println("Balance:" + getBalance());
	System.out.println("Owner:" + getOwner());
	MyAddress myAddress = (MyAddress)getAddress();
	if (myAddress != null) {
	    myAddress.printInfo();
	}
	Phone[] phones = getPhone();
	for (int i = 0;i < phones.length;i++) {
	    MyPhone myPhone = (MyPhone)phones[i];
	    myPhone.printInfo();
	}
    }
}
MyAddress.java
public class MyAddress extends Address {
    public void printInfo() {
	String zip = getZip();
	String place = getContent();
	System.out.println("Address: [" + zip + "] " + place);
    }
}
MyPhone.java
public class MyPhone extends Phone {
    public void printInfo() {
	String area = getArea();
	String number = getContent();
	System.out.println("Phone: [" + area + "] " + number);
    }	
}

List 7.18.3.4.5[JavaPatternFactoryUser.java] is a sample program for the java.pattern.factory option.

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

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

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

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

	System.out.println("*** makeAccount ***");
	MyJavaPatternFactoryFactory
	    myFactory = new MyJavaPatternFactoryFactory();
	JavaPatternFactoryFactory.setFactory(myFactory);
        IJavaPatternFactoryFactory factory
	    = JavaPatternFactoryFactory.getFactory();
	Account account = (Account)factory.create(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 doMyJob(Account account) {
	System.out.println("*** doMyJob ***");
	MyAccount myAccount = (MyAccount)account;
	myAccount.printInfo();
    }
}

Execution of the JavaPatternFactoryUser is here:

$ java JavaPatternFactoryUser javaPatternFactory.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>
*** doMyJob ***
AccountNo:12345
Balance:102030
Owner:XML Taro
Address: [213] Yokohama
Phone: [123] 456-7890
Phone: [090] 123-4567

xml:base

List 7.18.3.5.1[JavaPatternFactoryXmlBase.java] is a sample program for the setBaseUri method in the factory object. In the makeAccount method, a base uri is setted as "docs" using the setBaseUri method. During object creation, the factory class uses the base uri to find a XML document.

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

public class JavaPatternFactoryXmlBase {
    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 ***");
        IJavaPatternFactoryFactory factory
	    = JavaPatternFactoryFactory.getFactory();
	factory.setBaseUri("docs");
	Account account = (Account)factory.create(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);
    }
}

List 7.18.3.5.2[javaPatternFactoryXmlBase.xml] is a XML document which is in the 'docs' directory.

javaPatternFactoryXmlBase.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 JavaPatternFactoryXmlBase is here:

$ java JavaPatternFactoryUserXmlBase \
    javaPatternFactoryXmlBase.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>