XMLDialog Walks the DOM

We pass a valid XML document to XMLDialog's constructor.

XMLDialog calls the parseNode method recursively.

The first node is always the dialog itself. XMLDialog creates the dialog's widget container and pushes it onto the container stack.

When XMLDialog encounters a box, it pushes it onto the container stack, unpacks its contents, and then pops it off the stack.

When XMLDialog encounters a widget, it creates the widget, and adds it to the top container on the container stack.

public class XMLDialog extends KDialog {
    private Stack  containers = new Stack();

    public XMLDialog(Frame owner, Document doc, ...) {
        ...
        parseNode(doc.getDocumentElement());
        loadData();
        ...
    }

    private void parseNode(Node node) {
        switch (node.getNodeType()) {

        case Node.ELEMENT_NODE :
            int nodeID = getNodeID(node.getNodeName());
            switch (nodeID) {

            case NODE_ID_DIALOG :
                createDialog(node.getAttributes());
                parseChildren(node);
                break;

            case NODE_ID_BOX :
                containers.push(createXMLBox(node));
                parseChildren(node);
                containers.pop();
                break;

            default :
                buildWidget(nodeID, node);
                break;
            }
            break;

        default :
            break;
        }
    }

    private void parseChildren(Node node) {
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            parseNode(children.item(i));
        }
    }

    private void buildWidget(int nodeID, Node node) {
        String multiKey = (String)multiKeys.peek();

        switch (nodeID) {
        case NODE_ID_STRUT : addStrut(node); break;
        case NODE_ID_SPRING : addSpring(); break;

        case NODE_ID_LABEL :
            addComponent(new XMLLabel(node, multiKey));
            break;

        case NODE_ID_TEXT :
            addComponent(new XMLTextField(node, multiKey));
            break;

        case NODE_ID_TEXTAREA :
            addComponent(new XMLTextArea(node, multiKey));
            break;

        // many, many more...

        default :
            System.out.println("Error: unexpected element: "
                    + node.getNodeName());
        }
    }
}