Powered by SmartDoc

The java.pattern.visitor.style option

The java.pattern.visitor.style option specifies a visitor type for the visitor mechanism, which is generated by the java.pattern.visitor option.

This option should be used with the java.pattern.visitor

Parameter

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

light
Light-weight visitor
plain
Normal-weight visitor
heavy
Heavy-weight visitor

Default configuration is plain.

Artifacts

The java.pattern.visitor.style option affect the visitor class generated.

Example

List 7.21.3.1[javaPatternVisitorStyle.rng] is a sample schema for the java.pattern.visitor.style option.

javaPatternVisitorStyle.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 the relaxer command with the java.pattern.visitor.style option is as follows:

$ relaxer -java -java.pattern.visitor \
    -java.pattern.visitor.style:light javaPatternVisitorStyle.rng

The java.pattern.visitor option is required.

Because the java generator is a default generator, execution of the relaxer command shown below has the same effect:

$ relaxer -java.pattern.visitor -java.pattern.visitor.style:light \
    javaPatternVisitor.rng

As a result, Relaxer generates 11 files:

IRNode.java is a interface for the composite pattern. The composite pattern is used automatically when the visitor pattern is used.

IRVisitable.java, URVisitor.java, IRVisitor.java, and RVisitorBase.java are the visitor pattern specific classes.

List 7.21.3.1.1[JavaPatternVisitorStyle.java] is a sample program for the java.pattern.visitor.style option.

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

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

	String uri = args[0];
	Account account = makeAccount(uri);
	printAccount(account);
	showAccount(account);
	visitAccount(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);
    }

    private static void visitAccount(Account account) {
	System.out.println("*** visitAccount ***");
	PrintVisitor visitor = new PrintVisitor();
	URVisitor.traverse(account, visitor);
    }
}

In the JavaPatternVisitorStyle, the visitor class PrintVisitor as shown in List 7.21.3.1.2[PrintVisitor.java] is used.

PrintVisitor.java
public class PrintVisitor extends RVisitorBase {
    public void enter(Account account) {
	System.out.println("enter - Account");
	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);
    }

    public void leave(Account account) {
	System.out.println("leave - Account");
    }

    public void enter(Address address) {
	System.out.println("enter - Address");
	String zip = address.getZip();
	String place = address.getContent();
	System.out.println("Address: [" + zip + "] " + place);
    }

    public void leave(Address address) {
	System.out.println("leave - Address");
    }

    public void enter(Phone phone) {
	System.out.println("enter - Phone");
	String area = phone.getArea();
	String number = phone.getContent();
	System.out.println("Phone: [" + area + "] " + number);
    }

    public void leave(Phone phone) {
	System.out.println("leave - Phone");
    }
}

Compilation of JavaPatternVisitorStyle.java is shown here:

$ javac JavaPatternVisitorStyle.java

Execution

List 7.21.3.2.1[javaPatternVisitorStyle.xml] is an XML document for testing.

javaPatternVisitorStyle.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 JavaPatternVisitorStyle class is shown here:

$ java JavaPatternVisitorStyle javaPatternVisitorStyle.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>
*** visitAccount ***
enter - Account
AccountNo:12345
Balance:102030
Owner:XML Taro
enter - Address
Address: [213] Yokohama
leave - Address
enter - Phone
Phone: [123] 456-7890
leave - Phone
enter - Phone
Phone: [090] 123-4567
leave - Phone
leave - Account