dol: initial dol commit
[jump.git] / dol / src / dol / parser / xml / pnschema / PNXmlParser.java
diff --git a/dol/src/dol/parser/xml/pnschema/PNXmlParser.java b/dol/src/dol/parser/xml/pnschema/PNXmlParser.java
new file mode 100644 (file)
index 0000000..70a4f8a
--- /dev/null
@@ -0,0 +1,158 @@
+/* $Id: PNXmlParser.java 1 2010-02-24 13:03:05Z haidw $ */
+package dol.parser.xml.pnschema;
+
+import java.util.Stack;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+
+import dol.datamodel.XmlTag;
+import dol.datamodel.pn.ProcessNetwork;
+import dol.parser.xml.XmlParser;
+
+/**
+ * Parse processnetwork XML file.
+ */
+public class PNXmlParser extends XmlParser {
+
+    /**
+     * Constructor.
+     */
+    public PNXmlParser() {
+        super();
+        _stack = new Stack<Object>();
+        _xml2PN = new Xml2PN();
+    }
+
+    /**
+     * Do the parsing of an XML file describing a processnetwork.
+     *
+     * @param url input XML file
+     * @return the processnetwork
+     */
+    public ProcessNetwork doParse(String url)  {
+        ProcessNetwork pn = null;
+        System.out.println("Read process network from XML file");
+
+        try {
+            String uri = _makeAbsoluteURL(url);
+            _ui.printlnVerbose("-- processing XML file: " + uri);
+            _ui.printlnVerbose("-- read XML file: ");
+            _stack.clear();
+            _parser.parse(new InputSource(uri));
+            pn = (ProcessNetwork) _stack.pop();
+            _ui.printlnVerbose(" [DONE] ");
+        } catch (SAXParseException err) {
+            System.out.println("** Parsing error, line "
+                + err.getLineNumber() + ", uri " + err.getSystemId());
+            System.out.println("   " + err.getMessage());
+        } catch (SAXException e) {
+            e.printStackTrace();
+        } catch (Throwable t) {
+            t.printStackTrace();
+        }
+
+        try {
+            pn.fillPortPeerInfo();
+        } catch (Exception e) {
+            System.out.println("Exception\n " + e.getMessage());
+            e.printStackTrace(System.out);
+        }
+
+        System.out.println(" -- Process network model from XML "
+                + "[Finished]");
+        System.out.println();
+
+        return pn;
+    }
+
+    /**
+     * Action to be done while parsing a start element of an XML.
+     *
+     * @param namespaceURI
+     * @param localName
+     * @param elementName name of element
+     * @param attributes attributes of element
+     * @exception SAXException thrown when a parsing error occurs
+     */
+    public void startElement(String namespaceURI, String localName,
+            String elementName, Attributes attributes)
+            throws SAXException {
+        Object val = null;
+
+        if (elementName.equals(_xt.getPNTag())) {
+            val = _xml2PN.processPN(attributes);
+        } else if (elementName.equals(_xt.getVariableTag())) {
+            val = _xml2PN.processVariable(attributes);
+        } else if (elementName.equals(_xt.getProcessTag())) {
+            val = _xml2PN.processProcess(attributes);
+        } else if (elementName.equals(_xt.getSWChannelTag())) {
+            val = _xml2PN.processChannel(attributes);
+        } else if (elementName.equals(_xt.getConnectionTag())) {
+            val = _xml2PN.processConnection(attributes);
+        } else if (elementName.equals(_xt.getOriginTag())) {
+            val = _xml2PN.processOrigin(attributes);
+        } else if (elementName.equals(_xt.getTargetTag())) {
+            val = _xml2PN.processTarget(attributes);
+        } else if (elementName.equals(_xt.getPortTag())) {
+            val = _xml2PN.processPort(attributes);
+        } else if (elementName.equals(_xt.getSourceTag())) {
+            val = _xml2PN.processSource(attributes);
+        } else if (elementName.equals(_xt.getProfilingTag())) {
+            val = _xml2PN.processProfiling(attributes);
+        } else if (elementName.equals(_xt.getConfigurationTag())) {
+            val = _xml2PN.processConfiguration(attributes);
+        } else {
+            System.out.println("--Warning, DOL doesn't "
+                    + "understand tag <" + elementName + "> ");
+        }
+
+        if (val != null) {
+            _stack.push(val);
+        }
+    }
+
+    /**
+     * Action to be done while parsing an end element of an XML.
+     *
+     * @param  elementName Description of the Parameter
+     * @exception  SAXException MyException If such and such occurs
+     */
+    public void endElement(String namespaceURI, String localName,
+            String elementName) throws SAXException {
+        if (elementName.equals(_xt.getPNTag())) {
+            _xml2PN.processPN(_stack);
+        } else if (elementName.equals(_xt.getVariableTag())) {
+            _xml2PN.processVariable(_stack);
+        } else if (elementName.equals(_xt.getProcessTag())) {
+            _xml2PN.processProcess(_stack);
+        } else if (elementName.equals(_xt.getSWChannelTag())) {
+            _xml2PN.processChannel(_stack);
+        } else if (elementName.equals(_xt.getConnectionTag())) {
+            _xml2PN.processConnection(_stack);
+        } else if (elementName.equals(_xt.getOriginTag())) {
+            _xml2PN.processOrigin(_stack);
+        } else if (elementName.equals(_xt.getTargetTag())) {
+            _xml2PN.processTarget(_stack);
+        } else if (elementName.equals(_xt.getSourceTag())) {
+            _xml2PN.processSource(_stack);
+        } else if (elementName.equals(_xt.getProfilingTag())) {
+            _xml2PN.processProfiling(_stack);
+        } else if (elementName.equals(_xt.getConfigurationTag())) {
+            _xml2PN.processConfiguration(_stack);
+        } else if (elementName.equals(_xt.getPortTag())) {
+            _xml2PN.processPort(_stack);
+        }
+    }
+
+    /** stack containing the generated objects */
+    protected Stack<Object> _stack;
+
+    /** actions to be taken while parsing every XML element */
+    protected Xml2PN _xml2PN;
+
+    /** */
+    protected XmlTag _xt = XmlTag.getInstance();
+}