1 /* $Id: MapXmlParser.java 1 2010-02-24 13:03:05Z haidw $ */
2 package dol.parser.xml.mapschema;
4 import java.util.Stack;
6 import org.xml.sax.Attributes;
7 import org.xml.sax.InputSource;
8 import org.xml.sax.SAXException;
9 import org.xml.sax.SAXParseException;
11 import dol.datamodel.XmlTag;
12 import dol.datamodel.architecture.Architecture;
13 import dol.datamodel.mapping.Mapping;
14 import dol.datamodel.pn.ProcessNetwork;
15 import dol.parser.xml.XmlParser;
19 * Parse mapping XML file.
21 public class MapXmlParser extends XmlParser {
25 * @param pn the process network the mapping is referring to
26 * @param arch the architecture the mapping is referring to
28 public MapXmlParser(ProcessNetwork pn, Architecture arch) {
30 _stack = new Stack<Object>();
31 _xml2Map = new Xml2Map(pn, arch);
35 * Do the parsing of an XML file describing a mapping.
37 * @param url The input XML file
40 public Mapping doParse(String url) {
42 System.out.println("Read mapping from XML file");
45 String uri = _makeAbsoluteURL(url);
46 _ui.printlnVerbose("-- processing XML file: " + uri);
47 _ui.printlnVerbose("-- read XML file: ");
49 _parser.parse(new InputSource(uri));
50 map = (Mapping) _stack.pop();
51 _ui.printlnVerbose(" [DONE] ");
52 } catch (SAXParseException err) {
53 System.out.println("** Parsing error, line "
54 + err.getLineNumber() + ", uri " + err.getSystemId());
55 System.out.println(" " + err.getMessage());
56 } catch (SAXException e) {
57 //e.printStackTrace();
60 } catch (Throwable t) {
64 System.out.println(" -- Mapping from XML "
72 * Action to be done while parsing a start element of an XML.
74 * @param elementName Description of the Parameter
75 * @param attributes Description of the Parameter
76 * @exception SAXException MyException If such and such occurs
78 public void startElement(String namespaceURI, String localName,
79 String elementName, Attributes attributes)
83 if (elementName.equals(_xt.getMappingTag())) {
84 val = _xml2Map.processMapping(attributes);
85 } else if (elementName.equals(_xt.getVariableTag())) {
86 val = _xml2Map.processVariable(attributes);
87 } else if (elementName.equals(_xt.getBindingTag())) {
88 val = _xml2Map.processBinding(attributes);
89 } else if (elementName.equals(_xt.getOriginTag())) {
90 val = _xml2Map.processOrigin(attributes);
91 } else if (elementName.equals(_xt.getProcessTag())) {
92 val = _xml2Map.processProcess(attributes);
93 } else if (elementName.equals(_xt.getProcessorTag())) {
94 val = _xml2Map.processProcessor(attributes);
95 } else if (elementName.equals(_xt.getSWChannelTag())) {
96 val = _xml2Map.processChannel(attributes);
97 } else if (elementName.equals(_xt.getReadPathTag())) {
98 val = _xml2Map.processReadPath(attributes);
99 } else if (elementName.equals(_xt.getWritePathTag())) {
100 val = _xml2Map.processWritePath(attributes);
101 } else if (elementName.equals(_xt.getScheduleTag())) {
102 val = _xml2Map.processSchedule(attributes);
103 } else if (elementName.equals(_xt.getResourceTag())) {
104 val = _xml2Map.processResource(attributes);
105 } else if (elementName.equals(_xt.getConfigurationTag())) {
106 val = _xml2Map.processConfiguration(attributes);
108 System.out.println(" -- Warning, DOL doesn't "
109 + "understand tag <" + elementName + "> ");
116 consistencyCheck(elementName, attributes, val);
121 * Action to be done while parsing an end element of an XML.
123 * @param elementName Description of the Parameter
124 * @exception SAXException MyException If such and such occurs
126 public void endElement(String namespaceURI, String localName,
127 String elementName) throws SAXException {
128 if (elementName.equals(_xt.getMappingTag())) {
129 _xml2Map.processMapping(_stack);
130 } else if (elementName.equals(_xt.getVariableTag())) {
131 _xml2Map.processVariable(_stack);
132 } else if (elementName.equals(_xt.getBindingTag())) {
133 _xml2Map.processBinding(_stack);
134 } else if (elementName.equals(_xt.getOriginTag())) {
135 _xml2Map.processOrigin(_stack);
137 } else if (elementName.equals(_xt.getTargetTag())) {
138 _xml2Map.processTarget(_stack);
140 } else if (elementName.equals(_xt.getProcessTag())) {
141 _xml2Map.processProcess(_stack);
142 } else if (elementName.equals(_xt.getProcessorTag())) {
143 _xml2Map.processProcessor(_stack);
144 } else if (elementName.equals(_xt.getSWChannelTag())) {
145 _xml2Map.processChannel(_stack);
146 } else if (elementName.equals(_xt.getReadPathTag())) {
147 _xml2Map.processReadPath(_stack);
148 } else if (elementName.equals(_xt.getWritePathTag())) {
149 _xml2Map.processWritePath(_stack);
150 } else if (elementName.equals(_xt.getScheduleTag())) {
151 _xml2Map.processSchedule(_stack);
152 } else if (elementName.equals(_xt.getResourceTag())) {
153 _xml2Map.processResource(_stack);
154 } else if (elementName.equals(_xt.getConfigurationTag())) {
155 _xml2Map.processConfiguration(_stack);
162 protected void consistencyCheck(String elementName,
163 Attributes attributes, Object val) {
164 boolean isConsistent = true;
166 if (elementName.equals(_xt.getProcessorTag())
168 System.out.println(" -- Error: Could not find processor \""
169 + attributes.getValue("name") + "\" in "
171 isConsistent = false;
172 } else if (elementName.equals(_xt.getProcessTag())
174 System.out.println(" -- Error: Could not find process \""
175 + attributes.getValue("name") + "\" in "
176 + "process network.");
177 isConsistent = false;
178 } else if (elementName.equals(_xt.getSWChannelTag())
180 System.out.println(" -- Error: Could not find channel \""
181 + attributes.getValue("name") + "\" in "
182 + "process network.");
183 isConsistent = false;
184 } else if (elementName.equals(_xt.getReadPathTag())
186 System.out.println(" -- Error: Could not find read path \""
187 + attributes.getValue("name") + "\" in "
189 isConsistent = false;
190 } else if (elementName.equals(_xt.getWritePathTag())
192 System.out.println(" -- Error: Could not find write path \""
193 + attributes.getValue("name") + "\" in "
195 isConsistent = false;
203 /** stack containing the generated objects */
204 protected Stack<Object> _stack;
206 /** actions to be taken while parsing every XML element */
207 protected Xml2Map _xml2Map;
210 protected XmlTag _xt = XmlTag.getInstance();