dol: initial dol commit
[jump.git] / dol / src / dol / parser / xml / mapschema / MapXmlParser.java
1 /* $Id: MapXmlParser.java 1 2010-02-24 13:03:05Z haidw $ */
2 package dol.parser.xml.mapschema;
3
4 import java.util.Stack;
5
6 import org.xml.sax.Attributes;
7 import org.xml.sax.InputSource;
8 import org.xml.sax.SAXException;
9 import org.xml.sax.SAXParseException;
10
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;
16
17
18 /**
19  * Parse mapping XML file.
20  */
21 public class MapXmlParser extends XmlParser {
22
23     /**
24      * Constructor.
25      * @param pn the process network the mapping is referring to
26      * @param arch the architecture the mapping is referring to
27      */
28     public MapXmlParser(ProcessNetwork pn, Architecture arch) {
29         super();
30         _stack = new Stack<Object>();
31         _xml2Map = new Xml2Map(pn, arch);
32     }
33
34     /**
35      * Do the parsing of an XML file describing a mapping.
36      *
37      * @param  url The input XML file
38      * @return the mapping
39      */
40     public Mapping doParse(String url)  {
41         Mapping map = null;
42         System.out.println("Read mapping from XML file");
43
44         try {
45             String uri = _makeAbsoluteURL(url);
46             _ui.printlnVerbose("-- processing XML file: " + uri);
47             _ui.printlnVerbose("-- read XML file: ");
48             _stack.clear();
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();
58             e.getMessage();
59             System.exit(-1);
60         } catch (Throwable t) {
61             t.printStackTrace();
62         }
63
64         System.out.println(" -- Mapping from XML "
65                 + "[Finished]");
66         System.out.println();
67
68         return map;
69     }
70
71     /**
72      * Action to be done while parsing a start element of an XML.
73      *
74      * @param  elementName Description of the Parameter
75      * @param  attributes Description of the Parameter
76      * @exception  SAXException MyException If such and such occurs
77      */
78     public void startElement(String namespaceURI, String localName,
79             String elementName, Attributes attributes)
80             throws SAXException {
81         Object val = null;
82
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);
107         } else {
108             System.out.println(" -- Warning, DOL doesn't "
109                     + "understand tag <" + elementName + "> ");
110         }
111
112         if (val != null) {
113             _stack.push(val);
114         }
115
116         consistencyCheck(elementName, attributes, val);
117     }
118
119
120     /**
121      * Action to be done while parsing an end element of an XML.
122      *
123      * @param  elementName Description of the Parameter
124      * @exception  SAXException MyException If such and such occurs
125      */
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);
136         /*
137         } else if (elementName.equals(_xt.getTargetTag())) {
138             _xml2Map.processTarget(_stack);
139         */
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);
156         }
157     }
158
159     /**
160      *
161      */
162     protected void consistencyCheck(String elementName,
163             Attributes attributes, Object val) {
164         boolean isConsistent = true;
165
166         if (elementName.equals(_xt.getProcessorTag())
167                 && val == null) {
168             System.out.println(" -- Error: Could not find processor \""
169                     + attributes.getValue("name") + "\" in "
170                     + "architecture.");
171             isConsistent = false;
172         } else if (elementName.equals(_xt.getProcessTag())
173                 && val == null) {
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())
179                     && val == null) {
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())
185                 && val == null) {
186             System.out.println(" -- Error: Could not find read path \""
187                     + attributes.getValue("name") + "\" in "
188                     + "architecture.");
189             isConsistent = false;
190         } else if (elementName.equals(_xt.getWritePathTag())
191                 && val == null) {
192             System.out.println(" -- Error: Could not find write path \""
193                     + attributes.getValue("name") + "\" in "
194                     + "architecture.");
195             isConsistent = false;
196         }
197
198         if (!isConsistent) {
199             System.exit(-1);
200         }
201     }
202
203     /** stack containing the generated objects */
204     protected Stack<Object> _stack;
205
206     /** actions to be taken while parsing every XML element */
207     protected Xml2Map _xml2Map;
208
209     /** */
210     protected XmlTag _xt = XmlTag.getInstance();
211 }