XML, Reflective Pattern Matching, and Java
by Andrew Dwelly

Listing One
<!ELEMENT RECORD ((CAR|VAN)*)>
<!ATTLIST RECORD DATE CDATA #REQUIRED>
<!ELEMENT CAR (PASSENGER*)>
<!ELEMENT VAN (PASSENGER*)>
<!ELEMENT PASSENGER EMPTY>

Listing Two
<?xml version = "1.0" ?>
<!DOCTYPE RECORD SYSTEM "record.dtd">
<RECORD DATE = "03/03/1999">
    <CAR><PASSENGER/><PASSENGER/></CAR>
    <VAN><PASSENGER/></VAN>
</RECORD>

Listing Three
import org.w3c.dom.*;
public class Process {
    public int count = 0;
    public processRecord(Element root) {
        Element next = root.getFirstChild();
        while (next != null) {
            String name = next.getTagName();
            if (name.equals("CAR") {
                processCar(next);
            }
            next = (Element)next.getNextSibling();
        }
    }
    public processCar(Element car) {
    Element next = car.getFirstChild();
    while (next != null) {
        count++;
        next = (Element)next.getNextSibling(); 
    }
}

Listing Four
public class CarCountingDelegate implements Hexable {
    public int count = 0;
    private Hex hex;
    public void setHex(Hex hex) {
        this.hex = hex;
    }
    public void prePASSENGER_CAR(Element element) {
        count++;
    }
}

Listing Five
public class recordToEnglishDelegate implements Hexable {
    int count = 0;
    Hex hex;

    public void setHex(Hex hex) {
        this.hex = hex;
    }
    public void preRECORD(Element element) {
        String date = element.getAttribute("DATE");
        System.out.print("On " + date);
    }
    public void preCAR(Element element) {
        System.out.print("A car with ");
    }
    public void postCAR(Element element) {
        System.out.println(count + "passengers.");
        count = 0;
    }
    public void preVAN(Element element) {
        System.out.print("A van with ");
    }
    public void postVAN(Element element) {
        System.out.println(count + "passengers.");
        count = 0;
    }
    public void prePASSENGER(Element element) {
        count++;
    }
}

Listing Six
public class recordToEnglishDelegate implements Hexable {
    int count = 0;
    Hex hex;
    public void setHex(Hex hex) {
        this.hex = hex;
    }
    public void preRECORD(Element element) {
        String date = element.getAttribute("DATE");
        System.out.print("On " + date);
    }
    public void prePASSENGER(Element element) {
        count++;
    }
    public void postRECORD(Element element) {
        System.out.println("There were " + count + " passengers in total.");
        count = 0;
        hex.setMode("pass2");
        hex.redoFromStart();
    }
    public void prepass2CAR(Element element) {
        System.out.print("A car with ");
    }
    public void postpass2CAR(Element element) {
        System.out.println(count + "passengers.");
        count = 0;
    }
    public void prepass2VAN(Element element) {
        System.out.print("A van with ");
    }
    public void postpass2VAN(Element element) {
        System.out.println(count + "passengers.");
        count = 0;
    }
    public void prepass2PASSENGER(Element element) {
        count++;
    }
}

Listing Seven
public class CarVanCounter extends CarCountingDelegate {
    public int vanCount = 0;
    public prePASSENGER_VAN(Element element) {
        vanCount++;
    }
}








1


