package com.craigc.progen;
import org.w3c.dom.*;
public class PlayData1 extends HandlerBase {
    public String name;
    public String title;
    public int width;
    public int height;
    public SceneData startScene;
    
    public PropData[] props;
    public SceneData[] scenes;
    
    public class PropData {
        public String name;
        public String label;      // initial value
        public ScriptData script; // only action script in Phase I
    }
    
    public class SceneData {
        public String name;
        public String color;
        public PropData[] addprops;
    }
    
    public class ScriptData {
        public SceneData nextScene;
        public TraitData[] traits;
    }
    
    public class TraitData {
        public PropData prop;
        public String newValue;
    }
    
    public PropData findProp(String n) {
        for (int j=0; j<props.length; ++j) {
            if (props[j].name.equals(n)) {
                return props[j];
            }
        }
        return null;
    }
    
    public SceneData findScene(String n) {
        for (int j=0; j<scenes.length; ++j) {
            if (scenes[j].name.equals(n)) {
                return scenes[j];
            }
        }
        return null;
    }
    
    public PlayData1(Document d) {
        convertDocument(d);
    }
    
    public PlayData1(String filename) throws Exception {
        convertDocument(DOM_Util.readDocument(filename));
    }
    
    /** Convert XML data using DOM to PlayData1 */
    public void convertDocument(Document d) {
        Element playNode = (Element) d.getDocumentElement();
        title = DOM_Util.get(playNode, "title", "Play Example");
        width = DOM_Util.getIntAttr(playNode, "width", 500);
        height = DOM_Util.getIntAttr(playNode, "height", 250);
        name = DOM_Util.getAttr(playNode, "name", "Unknown");
        
        // First must create data structures for all props & scenes
        // before doing scripts or traits
        
        // Create SceneData
        NodeList e = playNode.getElementsByTagName("scene");
        scenes = new SceneData[e.getLength()];
        // first create all scenes
        for (int j=0; j<e.getLength(); ++j) {
            Node bnode = e.item(j);
            SceneData n = new SceneData();
            n.name = DOM_Util.getAttr(bnode, "name", "?");
            n.color = DOM_Util.getAttr(bnode, "color", "?");
            scenes[j] = n;
        }
        
        // Create PropData
        e = playNode.getElementsByTagName("prop");
        props = new PropData[e.getLength()];
        for (int j=0; j<e.getLength(); ++j) {
            Node bnode = e.item(j);
            PropData n = new PropData();
            n.name = DOM_Util.getAttr(bnode, "name", "?");
            n.label = getLabel(bnode);
            props[j] = n;
        }
        
        for (int j=0; j<e.getLength(); ++j) {
            props[j].script = getScript(e.item(j), props[j].name);
        }
        
        // next do addprops & scripts with goto next scene
        e = playNode.getElementsByTagName("scene");
        for (int j=0; j<e.getLength(); ++j) {
            Element bnode = (Element) e.item(j);
            // Create AddPropData
            NodeList e2 = bnode.getElementsByTagName("addprop");
            scenes[j].addprops = new AddPropData[e2.getLength()];
            for (int j2=0; j2<e2.getLength(); ++j2) {
                Node anode = e2.item(j2);
                String pname = DOM_Util.getAttr(anode, "name", "?");
                scenes[j].addprops[j2] = findProp(pname);
            }
        }
        startScene = findScene(DOM_Util.getAttr(playNode,
                                 "start", "?"));
    }
    
    /** Gets label value of trait if any */
    String getLabel(Node n) {
        // ==> wrong way
        return DOM_Util.get(n, "trait", null);
        // return "?";
    }
    
    ScriptData getScript(Node n, String dname) {
        NodeList e = ((Element)n).getElementsByTagName("script");
        // only include the Action script
        for (int j=0; j<e.getLength(); ++j) {
            Element snode = (Element) e.item(j);
            // assume first and only script is Action script
            ScriptData sd = new ScriptData();
            sd.nextScene = findScene(DOM_Util.getAttr(snode,
                                                "goto", ""));
            NodeList e2 = snode.getElementsByTagName("trait");
            sd.traits = new TraitData[e2.getLength()];
            for (int j2=0; j2<e2.getLength(); ++j2) {
                Node tnode = e2.item(j2);
                sd.traits[j2] = new TraitData();
                sd.traits[j2].prop = findProp(
                            DOM_Util.getAttr(tnode, "prop", dname));
                sd.traits[j2].newValue = DOM_Util.getContent(tnode);
            }
            return sd;
        }
        return null;
    }
}
 
 |