dol: initial dol commit
[jump.git] / dol / src / dol / parser / xml / mapschema / MapXmlParser.java
diff --git a/dol/src/dol/parser/xml/mapschema/MapXmlParser.java b/dol/src/dol/parser/xml/mapschema/MapXmlParser.java
new file mode 100644 (file)
index 0000000..4849601
--- /dev/null
@@ -0,0 +1,211 @@
+/* $Id: MapXmlParser.java 1 2010-02-24 13:03:05Z haidw $ */
+package dol.parser.xml.mapschema;
+
+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.architecture.Architecture;
+import dol.datamodel.mapping.Mapping;
+import dol.datamodel.pn.ProcessNetwork;
+import dol.parser.xml.XmlParser;
+
+
+/**
+ * Parse mapping XML file.
+ */
+public class MapXmlParser extends XmlParser {
+
+    /**
+     * Constructor.
+     * @param pn the process network the mapping is referring to
+     * @param arch the architecture the mapping is referring to
+     */
+    public MapXmlParser(ProcessNetwork pn, Architecture arch) {
+        super();
+        _stack = new Stack<Object>();
+        _xml2Map = new Xml2Map(pn, arch);
+    }
+
+    /**
+     * Do the parsing of an XML file describing a mapping.
+     *
+     * @param  url The input XML file
+     * @return the mapping
+     */
+    public Mapping doParse(String url)  {
+        Mapping map = null;
+        System.out.println("Read mapping 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));
+            map = (Mapping) _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();
+            e.getMessage();
+            System.exit(-1);
+        } catch (Throwable t) {
+            t.printStackTrace();
+        }
+
+        System.out.println(" -- Mapping from XML "
+                + "[Finished]");
+        System.out.println();
+
+        return map;
+    }
+
+    /**
+     * Action to be done while parsing a start element of an XML.
+     *
+     * @param  elementName Description of the Parameter
+     * @param  attributes Description of the Parameter
+     * @exception  SAXException MyException If such and such occurs
+     */
+    public void startElement(String namespaceURI, String localName,
+            String elementName, Attributes attributes)
+            throws SAXException {
+        Object val = null;
+
+        if (elementName.equals(_xt.getMappingTag())) {
+            val = _xml2Map.processMapping(attributes);
+        } else if (elementName.equals(_xt.getVariableTag())) {
+            val = _xml2Map.processVariable(attributes);
+        } else if (elementName.equals(_xt.getBindingTag())) {
+            val = _xml2Map.processBinding(attributes);
+        } else if (elementName.equals(_xt.getOriginTag())) {
+            val = _xml2Map.processOrigin(attributes);
+        } else if (elementName.equals(_xt.getProcessTag())) {
+            val = _xml2Map.processProcess(attributes);
+        } else if (elementName.equals(_xt.getProcessorTag())) {
+            val = _xml2Map.processProcessor(attributes);
+        } else if (elementName.equals(_xt.getSWChannelTag())) {
+            val = _xml2Map.processChannel(attributes);
+        } else if (elementName.equals(_xt.getReadPathTag())) {
+            val = _xml2Map.processReadPath(attributes);
+        } else if (elementName.equals(_xt.getWritePathTag())) {
+            val = _xml2Map.processWritePath(attributes);
+        } else if (elementName.equals(_xt.getScheduleTag())) {
+            val = _xml2Map.processSchedule(attributes);
+        } else if (elementName.equals(_xt.getResourceTag())) {
+            val = _xml2Map.processResource(attributes);
+        } else if (elementName.equals(_xt.getConfigurationTag())) {
+            val = _xml2Map.processConfiguration(attributes);
+        } else {
+            System.out.println(" -- Warning, DOL doesn't "
+                    + "understand tag <" + elementName + "> ");
+        }
+
+        if (val != null) {
+            _stack.push(val);
+        }
+
+        consistencyCheck(elementName, attributes, 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.getMappingTag())) {
+            _xml2Map.processMapping(_stack);
+        } else if (elementName.equals(_xt.getVariableTag())) {
+            _xml2Map.processVariable(_stack);
+        } else if (elementName.equals(_xt.getBindingTag())) {
+            _xml2Map.processBinding(_stack);
+        } else if (elementName.equals(_xt.getOriginTag())) {
+            _xml2Map.processOrigin(_stack);
+        /*
+        } else if (elementName.equals(_xt.getTargetTag())) {
+            _xml2Map.processTarget(_stack);
+        */
+        } else if (elementName.equals(_xt.getProcessTag())) {
+            _xml2Map.processProcess(_stack);
+        } else if (elementName.equals(_xt.getProcessorTag())) {
+            _xml2Map.processProcessor(_stack);
+        } else if (elementName.equals(_xt.getSWChannelTag())) {
+            _xml2Map.processChannel(_stack);
+        } else if (elementName.equals(_xt.getReadPathTag())) {
+            _xml2Map.processReadPath(_stack);
+        } else if (elementName.equals(_xt.getWritePathTag())) {
+            _xml2Map.processWritePath(_stack);
+        } else if (elementName.equals(_xt.getScheduleTag())) {
+            _xml2Map.processSchedule(_stack);
+        } else if (elementName.equals(_xt.getResourceTag())) {
+            _xml2Map.processResource(_stack);
+        } else if (elementName.equals(_xt.getConfigurationTag())) {
+            _xml2Map.processConfiguration(_stack);
+        }
+    }
+
+    /**
+     *
+     */
+    protected void consistencyCheck(String elementName,
+            Attributes attributes, Object val) {
+        boolean isConsistent = true;
+
+        if (elementName.equals(_xt.getProcessorTag())
+                && val == null) {
+            System.out.println(" -- Error: Could not find processor \""
+                    + attributes.getValue("name") + "\" in "
+                    + "architecture.");
+            isConsistent = false;
+        } else if (elementName.equals(_xt.getProcessTag())
+                && val == null) {
+            System.out.println(" -- Error: Could not find process \""
+                    + attributes.getValue("name") + "\" in "
+                    + "process network.");
+            isConsistent = false;
+        } else if (elementName.equals(_xt.getSWChannelTag())
+                    && val == null) {
+                System.out.println(" -- Error: Could not find channel \""
+                        + attributes.getValue("name") + "\" in "
+                        + "process network.");
+                isConsistent = false;
+        } else if (elementName.equals(_xt.getReadPathTag())
+                && val == null) {
+            System.out.println(" -- Error: Could not find read path \""
+                    + attributes.getValue("name") + "\" in "
+                    + "architecture.");
+            isConsistent = false;
+        } else if (elementName.equals(_xt.getWritePathTag())
+                && val == null) {
+            System.out.println(" -- Error: Could not find write path \""
+                    + attributes.getValue("name") + "\" in "
+                    + "architecture.");
+            isConsistent = false;
+        }
+
+        if (!isConsistent) {
+            System.exit(-1);
+        }
+    }
+
+    /** stack containing the generated objects */
+    protected Stack<Object> _stack;
+
+    /** actions to be taken while parsing every XML element */
+    protected Xml2Map _xml2Map;
+
+    /** */
+    protected XmlTag _xt = XmlTag.getInstance();
+}