dol: initial dol commit
[jump.git] / dol / src / dol / parser / xml / pnschema / Xml2PN.java
1 /* $Id: Xml2PN.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
8 import dol.datamodel.pn.Channel;
9 import dol.datamodel.pn.Configuration;
10 import dol.datamodel.pn.Connection;
11 import dol.datamodel.pn.Port;
12 import dol.datamodel.pn.Process;
13 import dol.datamodel.pn.ProcessNetwork;
14 import dol.datamodel.pn.ProfilingConfiguration;
15 import dol.datamodel.pn.Resource;
16 import dol.datamodel.pn.SourceCode;
17 import dol.datamodel.pn.Variable;
18
19 /**
20  *
21  */
22 public class Xml2PN {
23
24     /**
25      * Constructor.
26      */
27     public Xml2PN() {
28     }
29
30     /**
31      * Process the start of the process network tag in the XML.
32      *
33      * @param attributes attributes of the tag
34      * @return a ProcessNetwork object
35      */
36     public ProcessNetwork processPN(Attributes attributes) {
37         String name = (String) attributes.getValue("name");
38         ProcessNetwork p = new ProcessNetwork(name);
39         return p;
40     }
41
42     /**
43      * Process the end of the processnetwork tag in the XML.
44      *
45      * @param stack
46      */
47     public void processPN(Stack<Object> stack) {
48     }
49
50     /**
51      * Process the start of a process tag in the XML.
52      *
53      * @param  attributes The attributes of the tag.
54      * @return  a process object.
55      */
56     public Process processProcess(Attributes attributes) {
57         String name = (String) attributes.getValue("name");
58         String basename = (String) attributes.getValue("basename");
59         String range = (String) attributes.getValue("range");
60         Process process = new Process(name);
61         if (basename != null) process.setBasename(basename);
62         if (range != null) process.setRange(range);
63         return process;
64     }
65
66     /**
67      * Process the end of a process tag in the XML.
68      *
69      * @param stack
70      */
71     public void processProcess(Stack<Object> stack) {
72         Process p = (Process) stack.pop();
73         ProcessNetwork r = (ProcessNetwork)stack.peek();
74         r.getProcessList().add(p);
75     }
76
77     /**
78      * Process the start of a origin tag in the XML.
79      *
80      * @param attributes attributes of the tag
81      * @return a Resource object
82      */
83     public Resource processOrigin(Attributes attributes) {
84         String name = (String) attributes.getValue("name");
85         String basename = (String) attributes.getValue("basename");
86         Resource resource = new Resource(name);
87         if (basename != null) resource.setBasename(basename);
88         return resource;
89     }
90
91     /**
92      * Process the end of a origin tag in the XML.
93      *
94      * @param stack
95      */
96     public void processOrigin(Stack<Object> stack) {
97         Port port = (Port)stack.pop();
98         Resource resource = (Resource)stack.pop();
99         Connection connection = (Connection)stack.peek();
100
101         if(port != null) {
102             if (!port.isOutPort()) {
103                 System.out.println("Error: Connection "
104                         + connection.getName()
105                         + " must be defined in the direction of the "
106                         + " data flow!");
107                 System.out.println("Exit.");
108                 System.exit(-1);
109             }
110             connection.setOriginPort(port);
111         } else {
112             undefinedReference("Connection", connection.getName(),
113                     "element", resource.getName());
114         }
115
116         Process process = ((ProcessNetwork)stack.elementAt(0)).
117                 getProcess(resource.getName());
118         Channel channel = ((ProcessNetwork)stack.elementAt(0)).
119                 getChannel(resource.getName());
120
121         if (process != null) {
122             connection.setOrigin(process);
123         }
124         else if (channel != null) {
125             connection.setOrigin(channel);
126         }
127         else {
128             undefinedReference("Connection", connection.getName(),
129                     "element", resource.getName());
130         }
131     }
132
133     /**
134      *  Process the start of a target tag in the XML.
135      *
136      * @param attributes attributes of the tag
137      * @return a Resource object
138      */
139     public Resource processTarget(Attributes attributes) {
140         String name = (String) attributes.getValue("name");
141         String basename = (String) attributes.getValue("basename");
142         Resource resource = new Resource(name);
143         if (basename != null) resource.setBasename(basename);
144         return resource;
145     }
146
147     /**
148      * Process the end of a target tag in the XML.
149      *
150      * @param stack
151      */
152     public void processTarget(Stack<Object> stack) {
153         Port port = (Port)stack.pop();
154         Resource resource = (Resource)stack.pop();
155         Connection connection = (Connection)stack.peek();
156
157         if(port != null) {
158             if (!port.isInPort()) {
159                 System.out.println("Error: Connection "
160                         + connection.getName()
161                         + " must be defined in the direction of the "
162                         + " data flow!");
163                 System.out.println("Exit.");
164                 System.exit(-1);
165             }
166             connection.setTargetPort(port);
167         } else {
168             undefinedReference("Connection", connection.getName(),
169                     "element", resource.getName());
170         }
171
172         Process process = ((ProcessNetwork)stack.elementAt(0)).
173                 getProcess(resource.getName());
174         Channel channel = ((ProcessNetwork)stack.elementAt(0)).
175                 getChannel(resource.getName());
176
177         if (process != null) {
178             connection.setTarget(process);
179         }
180         else if (channel != null) {
181             connection.setTarget(channel);
182         }
183         else {
184             undefinedReference("Connection", connection.getName(),
185                     "element", resource.getName());
186         }
187     }
188
189     /**
190      * Process the start of a connection tag in the XML.
191      *
192      * @param attributes attributes of the tag
193      * @return a Connection object
194      */
195     public Connection processConnection(Attributes attributes) {
196         String name = (String) attributes.getValue("name");
197         String basename = (String) attributes.getValue("basename");
198         Connection connection = new Connection(name);
199         if (basename != null) connection.setBasename(basename);
200         return connection;
201     }
202
203     /**
204      * Process the end of a connection tag in the XML.
205      *
206      * @param stack
207      */
208     public void processConnection(Stack<Object> stack) {
209         Connection connection = (Connection) stack.pop();
210         ProcessNetwork pn = (ProcessNetwork)stack.peek();
211         pn.getConnectionList().add(connection);
212     }
213
214     /**
215      * Process the start of a port tag in the XML.
216      *
217      * @param attributes attributes of the tag
218      * @return a Port object
219      */
220     public Port processPort(Attributes attributes) {
221         String name = (String) attributes.getValue("name");
222         String type = (String) attributes.getValue("type");
223         String basename = (String) attributes.getValue("basename");
224         String range = (String) attributes.getValue("range");
225
226         Port port = null;
227
228         if (type != null) {
229             if( type.equals("input") ) {
230                 port = new Port(name, Port.INPORT);
231             } else if( type.equals("output") ) {
232                 port = new Port(name, Port.OUTPORT);
233             }
234         } else {
235             port = new Port(name);
236         }
237
238         if (basename != null)
239             port.setBasename(basename);
240         else
241             port.setBasename(name);
242
243         if (range != null) port.setRange(range);
244
245         return port;
246     }
247
248     /**
249      * Process the end of a port tag in the XML.
250      *
251      * @param stack
252      */
253     public void processPort(Stack<Object> stack) {
254         Port port = (Port)stack.pop();
255         Resource resource = (Resource)stack.peek();
256
257         //check if this is a port or just a port reference
258         if (!(stack.elementAt(stack.size() - 2) instanceof Connection)) {
259             port.setResource(resource);
260             resource.getPortList().add(port);
261         }
262         //port reference
263         else {
264             Process process = ((ProcessNetwork)stack.elementAt(0)).
265                     getProcess(resource.getName());
266             Channel channel = ((ProcessNetwork)stack.elementAt(0)).
267                     getChannel(resource.getName());
268
269             Resource refResource = null;
270
271             if (process != null) {
272                 refResource = process;
273             }
274             else if (channel != null) {
275                 refResource = channel;
276             }
277             else {
278                 undefinedReference("Connection",
279                         ((Connection)stack.peek()).getName(),
280                         "element", resource.getName());
281             }
282
283             Port refPort = refResource.getPort(port.getName());
284
285             if (refPort == null) {
286                 undefinedReference("Connection",
287                         ((Connection)stack.peek()).getName(),
288                         "port", port.getName());
289             }
290
291             // Put real port back on the stack. Will be popped in
292             // processOrigin and processTarget respectively
293             stack.push(refPort);
294
295         }
296     }
297
298     /**
299      * Process the start of a channel tag in the XML.
300      *
301      * @param attributes attributes of the tag
302      * @return a Channel object
303      */
304     public Channel processChannel(Attributes attributes) {
305         String name = (String) attributes.getValue("name");
306         String size = (String) attributes.getValue("size");
307         String type = (String) attributes.getValue("type");
308         String tSize = (String) attributes.getValue("tokensize");
309         Channel channel = new Channel(name);
310         channel.setSize(new Integer(size));
311         channel.setTokenSize(new Integer(tSize));
312         channel.setType(type);
313         return channel;
314     }
315
316     /**
317      * Process the end of a channel tag in the XML.
318      *
319      * @param stack
320      */
321     public void processChannel(Stack<Object> stack) {
322         Channel channel = (Channel) stack.pop();
323         ProcessNetwork r = (ProcessNetwork)stack.peek();
324         r.getChannelList().add(channel);
325     }
326
327     /**
328      * Process the end of a configuration tag in the XML.
329      *
330      * @param stack
331      */
332     public void processConfiguration(Stack<Object> stack) {
333         Configuration configuration = (Configuration) stack.pop();
334         Resource r = (Resource) stack.peek();
335         configuration.setParentResource(r);
336         r.getCfgList().add(configuration);
337     }
338
339     /**
340      * Process the start of a configuration tag in the XML.
341      *
342      * @param attributes attributes of the tag
343      * @return a Configuration object
344      */
345     public Configuration processConfiguration(Attributes attributes) {
346         String name = (String) attributes.getValue("name");
347         String value = (String) attributes.getValue("value");
348         Configuration code = new Configuration(name);
349         code.setName(name);
350         code.setValue(value);
351         return code;
352     }
353
354     /**
355      * Process the end of a profiling tag in the XML.
356      *
357      * @param stack
358      */
359     public void processProfiling(Stack<Object> stack) {
360         ProfilingConfiguration configuration = ( ProfilingConfiguration) stack.pop();
361         Resource r = (Resource) stack.peek();
362         configuration.setParentResource(r);
363         r.getProfilingList().add(configuration);
364     }
365
366     /**
367      * Process the start of a profiling tag in the XML.
368      *
369      * @param attributes attributes of the tag
370      * @return a profiling object
371      */
372     public Configuration processProfiling(Attributes attributes) {
373         String name = (String) attributes.getValue("name");
374         String value = (String) attributes.getValue("value");
375         ProfilingConfiguration code = new  ProfilingConfiguration(name);
376         code.setName(name);
377         code.setValue(value);
378         return code;
379     }
380
381     /**
382      * Process the start of a channel source tag in the XML.
383      *
384      * @param attributes attributes of the tag
385      * @return a SourceCode object
386      */
387     public SourceCode processSource(Attributes attributes) {
388         String name = (String) attributes.getValue("name");
389         String type = (String) attributes.getValue("type");
390         String l = (String) attributes.getValue("location");
391         SourceCode code = new SourceCode(name);
392         code.setType(type);
393         code.setLocality(l);
394         return code;
395     }
396
397     /**
398      * Process the end of a source tag in the XML.
399      *
400      * @param stack
401      */
402     public void processSource(Stack<Object> stack) {
403         SourceCode source = (SourceCode) stack.pop();
404         Process process = (Process) stack.peek();
405         source.setProcess(process);
406         process.getSrcList().add(source);
407     }
408
409     /**
410      * Process the start of a variable tag in the XML.
411      *
412      * @param attributes attributes of the tag
413      * @return a Variable object
414      */
415     public Variable processVariable(Attributes attributes) {
416         String name = (String) attributes.getValue("name");
417         String value = (String) attributes.getValue("value");
418         Variable variable = null;
419         variable = new Variable(name);
420         variable.setValue(Integer.parseInt(value));
421         return variable;
422     }
423
424     /**
425      * Process the end of a variable tag in the XML.
426      *
427      * @param stack
428      */
429     public void processVariable(Stack<Object> stack) {
430         Variable variable = (Variable) stack.pop();
431         ProcessNetwork pn = (ProcessNetwork)stack.peek();
432         variable.setParentResource(pn);
433         pn.getVarList().add(variable);
434     }
435
436     /**
437      * Write an error message and terminate program in case an
438      * processnetwork element is referenced which does not exist.
439      *
440      * @param elementType type of processnetwork element in which the error
441      *                    occurred
442      * @param elementName name of processnetwork element in which the error
443      *                    occurred
444      * @param referenceType type of referenced processnetwork element
445      * @param referenceName name of referenced processnetwork element
446      */
447     protected void undefinedReference(String elementType,
448                                       String elementName,
449                                       String referenceType,
450                                       String referenceName) {
451         System.out.println("Error: " + elementType + " " + elementName
452                 + " references " + referenceType + " " + referenceName
453                 + " that has not been declared.");
454         System.out.println("Exit.");
455         System.exit(-1);
456     }
457 }