dol: initial dol commit
[jump.git] / dol / src / dol / parser / xml / pnschema / PNXmlParser.java
1 /* $Id: PNXmlParser.java 1 2010-02-24 13:03:05Z haidw $ */
2 package dol.parser.xml.pnschema;
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.pn.ProcessNetwork;
13 import dol.parser.xml.XmlParser;
14
15 /**
16  * Parse processnetwork XML file.
17  */
18 public class PNXmlParser extends XmlParser {
19
20     /**
21      * Constructor.
22      */
23     public PNXmlParser() {
24         super();
25         _stack = new Stack<Object>();
26         _xml2PN = new Xml2PN();
27     }
28
29     /**
30      * Do the parsing of an XML file describing a processnetwork.
31      *
32      * @param url input XML file
33      * @return the processnetwork
34      */
35     public ProcessNetwork doParse(String url)  {
36         ProcessNetwork pn = null;
37         System.out.println("Read process network from XML file");
38
39         try {
40             String uri = _makeAbsoluteURL(url);
41             _ui.printlnVerbose("-- processing XML file: " + uri);
42             _ui.printlnVerbose("-- read XML file: ");
43             _stack.clear();
44             _parser.parse(new InputSource(uri));
45             pn = (ProcessNetwork) _stack.pop();
46             _ui.printlnVerbose(" [DONE] ");
47         } catch (SAXParseException err) {
48             System.out.println("** Parsing error, line "
49                 + err.getLineNumber() + ", uri " + err.getSystemId());
50             System.out.println("   " + err.getMessage());
51         } catch (SAXException e) {
52             e.printStackTrace();
53         } catch (Throwable t) {
54             t.printStackTrace();
55         }
56
57         try {
58             pn.fillPortPeerInfo();
59         } catch (Exception e) {
60             System.out.println("Exception\n " + e.getMessage());
61             e.printStackTrace(System.out);
62         }
63
64         System.out.println(" -- Process network model from XML "
65                 + "[Finished]");
66         System.out.println();
67
68         return pn;
69     }
70
71     /**
72      * Action to be done while parsing a start element of an XML.
73      *
74      * @param namespaceURI
75      * @param localName
76      * @param elementName name of element
77      * @param attributes attributes of element
78      * @exception SAXException thrown when a parsing error occurs
79      */
80     public void startElement(String namespaceURI, String localName,
81             String elementName, Attributes attributes)
82             throws SAXException {
83         Object val = null;
84
85         if (elementName.equals(_xt.getPNTag())) {
86             val = _xml2PN.processPN(attributes);
87         } else if (elementName.equals(_xt.getVariableTag())) {
88             val = _xml2PN.processVariable(attributes);
89         } else if (elementName.equals(_xt.getProcessTag())) {
90             val = _xml2PN.processProcess(attributes);
91         } else if (elementName.equals(_xt.getSWChannelTag())) {
92             val = _xml2PN.processChannel(attributes);
93         } else if (elementName.equals(_xt.getConnectionTag())) {
94             val = _xml2PN.processConnection(attributes);
95         } else if (elementName.equals(_xt.getOriginTag())) {
96             val = _xml2PN.processOrigin(attributes);
97         } else if (elementName.equals(_xt.getTargetTag())) {
98             val = _xml2PN.processTarget(attributes);
99         } else if (elementName.equals(_xt.getPortTag())) {
100             val = _xml2PN.processPort(attributes);
101         } else if (elementName.equals(_xt.getSourceTag())) {
102             val = _xml2PN.processSource(attributes);
103         } else if (elementName.equals(_xt.getProfilingTag())) {
104             val = _xml2PN.processProfiling(attributes);
105         } else if (elementName.equals(_xt.getConfigurationTag())) {
106             val = _xml2PN.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
117     /**
118      * Action to be done while parsing an end element of an XML.
119      *
120      * @param  elementName Description of the Parameter
121      * @exception  SAXException MyException If such and such occurs
122      */
123     public void endElement(String namespaceURI, String localName,
124             String elementName) throws SAXException {
125         if (elementName.equals(_xt.getPNTag())) {
126             _xml2PN.processPN(_stack);
127         } else if (elementName.equals(_xt.getVariableTag())) {
128             _xml2PN.processVariable(_stack);
129         } else if (elementName.equals(_xt.getProcessTag())) {
130             _xml2PN.processProcess(_stack);
131         } else if (elementName.equals(_xt.getSWChannelTag())) {
132             _xml2PN.processChannel(_stack);
133         } else if (elementName.equals(_xt.getConnectionTag())) {
134             _xml2PN.processConnection(_stack);
135         } else if (elementName.equals(_xt.getOriginTag())) {
136             _xml2PN.processOrigin(_stack);
137         } else if (elementName.equals(_xt.getTargetTag())) {
138             _xml2PN.processTarget(_stack);
139         } else if (elementName.equals(_xt.getSourceTag())) {
140             _xml2PN.processSource(_stack);
141         } else if (elementName.equals(_xt.getProfilingTag())) {
142             _xml2PN.processProfiling(_stack);
143         } else if (elementName.equals(_xt.getConfigurationTag())) {
144             _xml2PN.processConfiguration(_stack);
145         } else if (elementName.equals(_xt.getPortTag())) {
146             _xml2PN.processPort(_stack);
147         }
148     }
149
150     /** stack containing the generated objects */
151     protected Stack<Object> _stack;
152
153     /** actions to be taken while parsing every XML element */
154     protected Xml2PN _xml2PN;
155
156     /** */
157     protected XmlTag _xt = XmlTag.getInstance();
158 }