dol: initial dol commit
[jump.git] / dol / src / dol / parser / xml / archischema / ArchiXmlParser.java
1 /* $Id: ArchiXmlParser.java 1 2010-02-24 13:03:05Z haidw $ */\r
2 package dol.parser.xml.archischema;\r
3 \r
4 import java.util.Stack;\r
5 \r
6 import org.xml.sax.Attributes;\r
7 import org.xml.sax.InputSource;\r
8 import org.xml.sax.SAXException;\r
9 import org.xml.sax.SAXParseException;\r
10 \r
11 import dol.datamodel.XmlTag;\r
12 import dol.datamodel.architecture.Architecture;\r
13 import dol.parser.xml.XmlParser;\r
14 \r
15 /**\r
16  * Parse architecture XML file.\r
17  */\r
18 public class ArchiXmlParser extends XmlParser {\r
19 \r
20     /**\r
21      * Constructor.\r
22      */\r
23     public ArchiXmlParser() {\r
24         super();\r
25         _stack = new Stack<Object>();\r
26         _xml2Archi = new Xml2Archi();\r
27     }\r
28 \r
29     /**\r
30      * Do the parsing of an XML file describing an architecture.\r
31      *\r
32      * @param url The input XML file\r
33      * @return the architecture\r
34      */\r
35     public Architecture doParse(String url)  {\r
36         Architecture architecture = null;\r
37         System.out.println("Read architecture from XML file");\r
38 \r
39         try {\r
40             String uri = _makeAbsoluteURL(url);\r
41             _ui.printlnVerbose("-- processing XML file: " + uri);\r
42             _ui.printlnVerbose("-- read XML file: ");\r
43             _stack.clear();\r
44             _parser.parse(new InputSource(uri));\r
45             architecture = (Architecture) _stack.pop();\r
46             _ui.printlnVerbose(" [DONE] ");\r
47         } catch (SAXParseException err) {\r
48             System.out.println("** Parsing error, line "\r
49                 + err.getLineNumber() + ", uri " + err.getSystemId());\r
50             System.out.println("   " + err.getMessage());\r
51         } catch (SAXException e) {\r
52             e.printStackTrace();\r
53         } catch (Throwable t) {\r
54             t.printStackTrace();\r
55         }\r
56 \r
57         architecture.computePaths();\r
58 \r
59         System.out.println(" -- Architecture model from XML "\r
60                 + "[Finished]");\r
61         System.out.println();\r
62 \r
63         return architecture;\r
64     }\r
65 \r
66     /**\r
67      * Action to be done while parsing a start element of an XML.\r
68      *\r
69      * @param elementName Description of the Parameter\r
70      * @param attributes Description of the Parameter\r
71      * @exception  SAXException MyException If such and such occurs\r
72      */\r
73     public void startElement(String namespaceURI, String localName,\r
74             String elementName, Attributes attributes)\r
75             throws SAXException {\r
76         Object val = null;\r
77 \r
78         if (elementName.equals(_xt.getArchiTag())) {\r
79             val = _xml2Archi.processArchitecture(attributes);\r
80         } else if (elementName.equals(_xt.getVariableTag())) {\r
81             val = _xml2Archi.processVariable(attributes);\r
82         } else if (elementName.equals(_xt.getProcessorTag())) {\r
83             val = _xml2Archi.processProcessor(attributes);\r
84         } else if (elementName.equals(_xt.getMemoryTag())) {\r
85             val = _xml2Archi.processMemory(attributes);\r
86         } else if (elementName.equals(_xt.getHWChannelTag())) {\r
87             val = _xml2Archi.processHWChannel(attributes);\r
88         } else if (elementName.equals(_xt.getConfigurationTag())) {\r
89             val = _xml2Archi.processConfiguration(attributes);\r
90         // simplified architecture\r
91         } else if (elementName.equals(_xt.getConnectionTag())) {\r
92             val = _xml2Archi.processConnection(attributes);\r
93         } else if (elementName.equals(_xt.getOriginTag())) {\r
94             val = _xml2Archi.processOrigin(attributes);\r
95         } else if (elementName.equals(_xt.getTargetTag())) {\r
96             val = _xml2Archi.processTarget(attributes);\r
97         } else if (elementName.equals(_xt.getNodeTag())) {\r
98             val = _xml2Archi.processNode(attributes);\r
99         } else if (elementName.equals(_xt.getPortTag())) {\r
100             val = _xml2Archi.processPort(attributes);\r
101         } else if (elementName.equals(_xt.getInPortTag())) {\r
102             val = _xml2Archi.processInputPort(attributes);\r
103         } else if (elementName.equals(_xt.getOutPortTag())) {\r
104             val = _xml2Archi.processOutputPort(attributes);\r
105         } else if (elementName.equals(_xt.getDuplexPortTag())) {\r
106             val = _xml2Archi.processInOutPort(attributes);\r
107         // detailed architecture\r
108         } else if (elementName.equals(_xt.getReadPathTag())) {\r
109             val = _xml2Archi.processReadPath(attributes);\r
110         } else if (elementName.equals(_xt.getWritePathTag())) {\r
111             val = _xml2Archi.processWritePath(attributes);\r
112         } else if (elementName.equals(_xt.getTXBufTag())) {\r
113             val = _xml2Archi.processTXBuf(attributes);\r
114         } else if (elementName.equals(_xt.getRXBufTag())) {\r
115             val = _xml2Archi.processRXBuf(attributes);\r
116         } else if (elementName.equals(_xt.getCHBufTag())) {\r
117             val = _xml2Archi.processCHBuf(attributes);\r
118         } else {\r
119             System.out.println("--Warning, DOL doesn't "\r
120                     + "understand tag <" + elementName + "> ");\r
121         }\r
122 \r
123         if (val != null) {\r
124             _stack.push(val);\r
125         }\r
126     }\r
127 \r
128     /**\r
129      * Action to be done while parsing an end element of an XML.\r
130      *\r
131      * @param elementName Description of the Parameter\r
132      * @exception SAXException MyException If such and such occurs\r
133      */\r
134     public void endElement(String namespaceURI, String localName,\r
135             String elementName) throws SAXException {\r
136         if (elementName.equals(_xt.getArchiTag())) {\r
137             _xml2Archi.processArchitecture(_stack);\r
138         } else if (elementName.equals(_xt.getVariableTag())) {\r
139             _xml2Archi.processVariable(_stack);\r
140         } else if (elementName.equals(_xt.getProcessorTag())) {\r
141             _xml2Archi.processProcessor(_stack);\r
142         } else if (elementName.equals(_xt.getMemoryTag())) {\r
143             _xml2Archi.processMemory(_stack);\r
144         } else if (elementName.equals(_xt.getHWChannelTag())) {\r
145             _xml2Archi.processHWChannel(_stack);\r
146         } else if (elementName.equals(_xt.getConfigurationTag())) {\r
147             _xml2Archi.processConfiguration(_stack);\r
148         // simplified architecture\r
149         } else if (elementName.equals(_xt.getConnectionTag())) {\r
150             _xml2Archi.processConnection(_stack);\r
151         } else if (elementName.equals(_xt.getOriginTag())) {\r
152             _xml2Archi.processOrigin(_stack);\r
153         } else if (elementName.equals(_xt.getTargetTag())) {\r
154             _xml2Archi.processTarget(_stack);\r
155         } else if (elementName.equals(_xt.getNodeTag())) {\r
156             _xml2Archi.processNode(_stack);\r
157         } else if (elementName.equals(_xt.getPortTag())) {\r
158             _xml2Archi.processPort(_stack);\r
159         } else if (elementName.equals(_xt.getInPortTag())) {\r
160             _xml2Archi.processInputPort(_stack);\r
161         } else if (elementName.equals(_xt.getOutPortTag())) {\r
162             _xml2Archi.processOutputPort(_stack);\r
163         } else if (elementName.equals(_xt.getDuplexPortTag())) {\r
164             _xml2Archi.processInOutPort(_stack);\r
165         // detailed architecture\r
166         } else if (elementName.equals(_xt.getReadPathTag())) {\r
167             _xml2Archi.processReadPath(_stack);\r
168         } else if (elementName.equals(_xt.getWritePathTag())) {\r
169             _xml2Archi.processWritePath(_stack);\r
170         } else if (elementName.equals(_xt.getTXBufTag())) {\r
171             _xml2Archi.processTXBuf(_stack);\r
172         } else if (elementName.equals(_xt.getRXBufTag())) {\r
173             _xml2Archi.processRXBuf(_stack);\r
174         } else if (elementName.equals(_xt.getCHBufTag())) {\r
175             _xml2Archi.processCHBuf(_stack);\r
176         }\r
177     }\r
178 \r
179     /** stack containing the generated objects */\r
180     protected Stack<Object> _stack;\r
181 \r
182     /** actions to be taken while parsing every XML element */\r
183     protected Xml2Archi _xml2Archi;\r
184 \r
185     /** */\r
186     protected XmlTag _xt = XmlTag.getInstance();\r
187 }\r