dol: initial dol commit
[jump.git] / dol / src / dol / parser / xml / pnschema / Xml2PN.java
diff --git a/dol/src/dol/parser/xml/pnschema/Xml2PN.java b/dol/src/dol/parser/xml/pnschema/Xml2PN.java
new file mode 100644 (file)
index 0000000..5586c16
--- /dev/null
@@ -0,0 +1,457 @@
+/* $Id: Xml2PN.java 1 2010-02-24 13:03:05Z haidw $ */
+package dol.parser.xml.pnschema;
+
+import java.util.Stack;
+
+import org.xml.sax.Attributes;
+
+import dol.datamodel.pn.Channel;
+import dol.datamodel.pn.Configuration;
+import dol.datamodel.pn.Connection;
+import dol.datamodel.pn.Port;
+import dol.datamodel.pn.Process;
+import dol.datamodel.pn.ProcessNetwork;
+import dol.datamodel.pn.ProfilingConfiguration;
+import dol.datamodel.pn.Resource;
+import dol.datamodel.pn.SourceCode;
+import dol.datamodel.pn.Variable;
+
+/**
+ *
+ */
+public class Xml2PN {
+
+    /**
+     * Constructor.
+     */
+    public Xml2PN() {
+    }
+
+    /**
+     * Process the start of the process network tag in the XML.
+     *
+     * @param attributes attributes of the tag
+     * @return a ProcessNetwork object
+     */
+    public ProcessNetwork processPN(Attributes attributes) {
+        String name = (String) attributes.getValue("name");
+        ProcessNetwork p = new ProcessNetwork(name);
+        return p;
+    }
+
+    /**
+     * Process the end of the processnetwork tag in the XML.
+     *
+     * @param stack
+     */
+    public void processPN(Stack<Object> stack) {
+    }
+
+    /**
+     * Process the start of a process tag in the XML.
+     *
+     * @param  attributes The attributes of the tag.
+     * @return  a process object.
+     */
+    public Process processProcess(Attributes attributes) {
+        String name = (String) attributes.getValue("name");
+        String basename = (String) attributes.getValue("basename");
+        String range = (String) attributes.getValue("range");
+        Process process = new Process(name);
+        if (basename != null) process.setBasename(basename);
+        if (range != null) process.setRange(range);
+        return process;
+    }
+
+    /**
+     * Process the end of a process tag in the XML.
+     *
+     * @param stack
+     */
+    public void processProcess(Stack<Object> stack) {
+        Process p = (Process) stack.pop();
+        ProcessNetwork r = (ProcessNetwork)stack.peek();
+        r.getProcessList().add(p);
+    }
+
+    /**
+     * Process the start of a origin tag in the XML.
+     *
+     * @param attributes attributes of the tag
+     * @return a Resource object
+     */
+    public Resource processOrigin(Attributes attributes) {
+        String name = (String) attributes.getValue("name");
+        String basename = (String) attributes.getValue("basename");
+        Resource resource = new Resource(name);
+        if (basename != null) resource.setBasename(basename);
+        return resource;
+    }
+
+    /**
+     * Process the end of a origin tag in the XML.
+     *
+     * @param stack
+     */
+    public void processOrigin(Stack<Object> stack) {
+        Port port = (Port)stack.pop();
+        Resource resource = (Resource)stack.pop();
+        Connection connection = (Connection)stack.peek();
+
+        if(port != null) {
+            if (!port.isOutPort()) {
+                System.out.println("Error: Connection "
+                        + connection.getName()
+                        + " must be defined in the direction of the "
+                        + " data flow!");
+                System.out.println("Exit.");
+                System.exit(-1);
+            }
+            connection.setOriginPort(port);
+        } else {
+            undefinedReference("Connection", connection.getName(),
+                    "element", resource.getName());
+        }
+
+        Process process = ((ProcessNetwork)stack.elementAt(0)).
+                getProcess(resource.getName());
+        Channel channel = ((ProcessNetwork)stack.elementAt(0)).
+                getChannel(resource.getName());
+
+        if (process != null) {
+            connection.setOrigin(process);
+        }
+        else if (channel != null) {
+            connection.setOrigin(channel);
+        }
+        else {
+            undefinedReference("Connection", connection.getName(),
+                    "element", resource.getName());
+        }
+    }
+
+    /**
+     *  Process the start of a target tag in the XML.
+     *
+     * @param attributes attributes of the tag
+     * @return a Resource object
+     */
+    public Resource processTarget(Attributes attributes) {
+        String name = (String) attributes.getValue("name");
+        String basename = (String) attributes.getValue("basename");
+        Resource resource = new Resource(name);
+        if (basename != null) resource.setBasename(basename);
+        return resource;
+    }
+
+    /**
+     * Process the end of a target tag in the XML.
+     *
+     * @param stack
+     */
+    public void processTarget(Stack<Object> stack) {
+        Port port = (Port)stack.pop();
+        Resource resource = (Resource)stack.pop();
+        Connection connection = (Connection)stack.peek();
+
+        if(port != null) {
+            if (!port.isInPort()) {
+                System.out.println("Error: Connection "
+                        + connection.getName()
+                        + " must be defined in the direction of the "
+                        + " data flow!");
+                System.out.println("Exit.");
+                System.exit(-1);
+            }
+            connection.setTargetPort(port);
+        } else {
+            undefinedReference("Connection", connection.getName(),
+                    "element", resource.getName());
+        }
+
+        Process process = ((ProcessNetwork)stack.elementAt(0)).
+                getProcess(resource.getName());
+        Channel channel = ((ProcessNetwork)stack.elementAt(0)).
+                getChannel(resource.getName());
+
+        if (process != null) {
+            connection.setTarget(process);
+        }
+        else if (channel != null) {
+            connection.setTarget(channel);
+        }
+        else {
+            undefinedReference("Connection", connection.getName(),
+                    "element", resource.getName());
+        }
+    }
+
+    /**
+     * Process the start of a connection tag in the XML.
+     *
+     * @param attributes attributes of the tag
+     * @return a Connection object
+     */
+    public Connection processConnection(Attributes attributes) {
+        String name = (String) attributes.getValue("name");
+        String basename = (String) attributes.getValue("basename");
+        Connection connection = new Connection(name);
+        if (basename != null) connection.setBasename(basename);
+        return connection;
+    }
+
+    /**
+     * Process the end of a connection tag in the XML.
+     *
+     * @param stack
+     */
+    public void processConnection(Stack<Object> stack) {
+        Connection connection = (Connection) stack.pop();
+        ProcessNetwork pn = (ProcessNetwork)stack.peek();
+        pn.getConnectionList().add(connection);
+    }
+
+    /**
+     * Process the start of a port tag in the XML.
+     *
+     * @param attributes attributes of the tag
+     * @return a Port object
+     */
+    public Port processPort(Attributes attributes) {
+        String name = (String) attributes.getValue("name");
+        String type = (String) attributes.getValue("type");
+        String basename = (String) attributes.getValue("basename");
+        String range = (String) attributes.getValue("range");
+
+        Port port = null;
+
+        if (type != null) {
+            if( type.equals("input") ) {
+                port = new Port(name, Port.INPORT);
+            } else if( type.equals("output") ) {
+                port = new Port(name, Port.OUTPORT);
+            }
+        } else {
+            port = new Port(name);
+        }
+
+        if (basename != null)
+            port.setBasename(basename);
+        else
+            port.setBasename(name);
+
+        if (range != null) port.setRange(range);
+
+        return port;
+    }
+
+    /**
+     * Process the end of a port tag in the XML.
+     *
+     * @param stack
+     */
+    public void processPort(Stack<Object> stack) {
+        Port port = (Port)stack.pop();
+        Resource resource = (Resource)stack.peek();
+
+        //check if this is a port or just a port reference
+        if (!(stack.elementAt(stack.size() - 2) instanceof Connection)) {
+            port.setResource(resource);
+            resource.getPortList().add(port);
+        }
+        //port reference
+        else {
+            Process process = ((ProcessNetwork)stack.elementAt(0)).
+                    getProcess(resource.getName());
+            Channel channel = ((ProcessNetwork)stack.elementAt(0)).
+                    getChannel(resource.getName());
+
+            Resource refResource = null;
+
+            if (process != null) {
+                refResource = process;
+            }
+            else if (channel != null) {
+                refResource = channel;
+            }
+            else {
+                undefinedReference("Connection",
+                        ((Connection)stack.peek()).getName(),
+                        "element", resource.getName());
+            }
+
+            Port refPort = refResource.getPort(port.getName());
+
+            if (refPort == null) {
+                undefinedReference("Connection",
+                        ((Connection)stack.peek()).getName(),
+                        "port", port.getName());
+            }
+
+            // Put real port back on the stack. Will be popped in
+            // processOrigin and processTarget respectively
+            stack.push(refPort);
+
+        }
+    }
+
+    /**
+     * Process the start of a channel tag in the XML.
+     *
+     * @param attributes attributes of the tag
+     * @return a Channel object
+     */
+    public Channel processChannel(Attributes attributes) {
+        String name = (String) attributes.getValue("name");
+        String size = (String) attributes.getValue("size");
+        String type = (String) attributes.getValue("type");
+        String tSize = (String) attributes.getValue("tokensize");
+        Channel channel = new Channel(name);
+        channel.setSize(new Integer(size));
+        channel.setTokenSize(new Integer(tSize));
+        channel.setType(type);
+        return channel;
+    }
+
+    /**
+     * Process the end of a channel tag in the XML.
+     *
+     * @param stack
+     */
+    public void processChannel(Stack<Object> stack) {
+        Channel channel = (Channel) stack.pop();
+        ProcessNetwork r = (ProcessNetwork)stack.peek();
+        r.getChannelList().add(channel);
+    }
+
+    /**
+     * Process the end of a configuration tag in the XML.
+     *
+     * @param stack
+     */
+    public void processConfiguration(Stack<Object> stack) {
+        Configuration configuration = (Configuration) stack.pop();
+        Resource r = (Resource) stack.peek();
+        configuration.setParentResource(r);
+        r.getCfgList().add(configuration);
+    }
+
+    /**
+     * Process the start of a configuration tag in the XML.
+     *
+     * @param attributes attributes of the tag
+     * @return a Configuration object
+     */
+    public Configuration processConfiguration(Attributes attributes) {
+        String name = (String) attributes.getValue("name");
+        String value = (String) attributes.getValue("value");
+        Configuration code = new Configuration(name);
+        code.setName(name);
+        code.setValue(value);
+        return code;
+    }
+
+    /**
+     * Process the end of a profiling tag in the XML.
+     *
+     * @param stack
+     */
+    public void processProfiling(Stack<Object> stack) {
+        ProfilingConfiguration configuration = ( ProfilingConfiguration) stack.pop();
+        Resource r = (Resource) stack.peek();
+        configuration.setParentResource(r);
+        r.getProfilingList().add(configuration);
+    }
+
+    /**
+     * Process the start of a profiling tag in the XML.
+     *
+     * @param attributes attributes of the tag
+     * @return a profiling object
+     */
+    public Configuration processProfiling(Attributes attributes) {
+        String name = (String) attributes.getValue("name");
+        String value = (String) attributes.getValue("value");
+        ProfilingConfiguration code = new  ProfilingConfiguration(name);
+        code.setName(name);
+        code.setValue(value);
+        return code;
+    }
+
+    /**
+     * Process the start of a channel source tag in the XML.
+     *
+     * @param attributes attributes of the tag
+     * @return a SourceCode object
+     */
+    public SourceCode processSource(Attributes attributes) {
+        String name = (String) attributes.getValue("name");
+        String type = (String) attributes.getValue("type");
+        String l = (String) attributes.getValue("location");
+        SourceCode code = new SourceCode(name);
+        code.setType(type);
+        code.setLocality(l);
+        return code;
+    }
+
+    /**
+     * Process the end of a source tag in the XML.
+     *
+     * @param stack
+     */
+    public void processSource(Stack<Object> stack) {
+        SourceCode source = (SourceCode) stack.pop();
+        Process process = (Process) stack.peek();
+        source.setProcess(process);
+        process.getSrcList().add(source);
+    }
+
+    /**
+     * Process the start of a variable tag in the XML.
+     *
+     * @param attributes attributes of the tag
+     * @return a Variable object
+     */
+    public Variable processVariable(Attributes attributes) {
+        String name = (String) attributes.getValue("name");
+        String value = (String) attributes.getValue("value");
+        Variable variable = null;
+        variable = new Variable(name);
+        variable.setValue(Integer.parseInt(value));
+        return variable;
+    }
+
+    /**
+     * Process the end of a variable tag in the XML.
+     *
+     * @param stack
+     */
+    public void processVariable(Stack<Object> stack) {
+        Variable variable = (Variable) stack.pop();
+        ProcessNetwork pn = (ProcessNetwork)stack.peek();
+        variable.setParentResource(pn);
+        pn.getVarList().add(variable);
+    }
+
+    /**
+     * Write an error message and terminate program in case an
+     * processnetwork element is referenced which does not exist.
+     *
+     * @param elementType type of processnetwork element in which the error
+     *                    occurred
+     * @param elementName name of processnetwork element in which the error
+     *                    occurred
+     * @param referenceType type of referenced processnetwork element
+     * @param referenceName name of referenced processnetwork element
+     */
+    protected void undefinedReference(String elementType,
+                                      String elementName,
+                                      String referenceType,
+                                      String referenceName) {
+        System.out.println("Error: " + elementType + " " + elementName
+                + " references " + referenceType + " " + referenceName
+                + " that has not been declared.");
+        System.out.println("Exit.");
+        System.exit(-1);
+    }
+}