dol: initial dol commit
[jump.git] / dol / src / dol / parser / xml / archischema / Xml2Archi.java
1 /* $Id: Xml2Archi.java 1 2010-02-24 13:03:05Z haidw $ */
2 package dol.parser.xml.archischema;
3
4 import java.util.Stack;
5
6 import org.xml.sax.Attributes;
7
8 import dol.datamodel.architecture.ArchiConnection;
9 import dol.datamodel.architecture.ArchiResource;
10 import dol.datamodel.architecture.Architecture;
11 import dol.datamodel.architecture.Configuration;
12 import dol.datamodel.architecture.HWChannel;
13 import dol.datamodel.architecture.Memory;
14 import dol.datamodel.architecture.Node;
15 import dol.datamodel.architecture.PortNode;
16 import dol.datamodel.architecture.Processor;
17 import dol.datamodel.architecture.ReadPath;
18 import dol.datamodel.architecture.Variable;
19 import dol.datamodel.architecture.WritePath;
20
21 /**
22  * Parse architecture XML file.
23  */
24 public class Xml2Archi {
25
26     /**
27      * Constructor.
28      */
29     public Xml2Archi() {
30     }
31
32     /**
33      * Process the start of the Architecture tag in the XML.
34      *
35      * @param attributes attributes of the tag
36      * @return an Architecture object
37      */
38     public Architecture processArchitecture(Attributes attributes) {
39         Architecture arch = new Architecture(attributes.getValue("name"));
40         return arch;
41
42     }
43
44     /**
45      * Process the end of the Architecture tag in the XML.
46      *
47      * @param stack
48      */
49     public void processArchitecture(Stack<Object> stack) {
50     }
51
52     /**
53      * Process the start of a processor tag in the XML.
54      *
55      * @param attributes The attributes of the tag.
56      * @return a processor object.
57      */
58     public Processor processProcessor(Attributes attributes) {
59         String name = (String) attributes.getValue("name");
60         String basename = (String) attributes.getValue("basename");
61         String range = (String) attributes.getValue("range");
62         String type = (String) attributes.getValue("type");
63         Processor processor = new Processor(name);
64         if (basename != null) processor.setBasename(basename);
65         if (range != null) processor.setRange(range);
66         if (type != null) processor.setType(type);
67         return processor;
68     }
69
70     /**
71      * Process the end of a processor tag in the XML.
72      *
73      * @param stack
74      */
75     public void processProcessor(Stack<Object> stack) {
76         Processor p = (Processor) stack.pop();
77
78         if (stack.peek() instanceof ReadPath) {
79             ReadPath r = (ReadPath) stack.peek();
80             Processor pro = ((Architecture)stack.elementAt(0)).
81                     getProcessor(p.getName());
82             if (pro != null) {
83                 r.setProcessor(pro);
84             }
85             else {
86                 undefinedReference("ReadPath", r.getName(),
87                         "processor", p.getName());
88             }
89         } else if (stack.peek() instanceof WritePath) {
90             WritePath w = (WritePath) stack.peek();
91             Processor pro = ((Architecture)stack.elementAt(0)).
92                     getProcessor(p.getName());
93             if (pro != null) {
94                 w.setProcessor(pro);
95             }
96             else {
97                 undefinedReference("ReadPath", w.getName(),
98                         "processor", p.getName());
99             }
100         } else if (stack.peek() instanceof Architecture) {
101             Architecture a = (Architecture)stack.peek();
102             p.setParentResource(a);
103             a.getProcessorList().add(p);
104         }
105     }
106
107     /**
108      * Process the start of a memory tag in the XML.
109      *
110      * @param attributes The attributes of the tag.
111      * @return a memory object.
112      */
113     public Memory processMemory(Attributes attributes) {
114         String name = (String) attributes.getValue("name");
115         String basename = (String) attributes.getValue("basename");
116         String range = (String) attributes.getValue("range");
117         String type = (String) attributes.getValue("type");
118         Memory memory = new Memory(name);
119         if (basename != null) memory.setBasename(basename);
120         if (range != null) memory.setRange(range);
121         if (type != null) memory.setRange(type);
122         return memory;
123     }
124
125     /**
126      * Process the end of a memory tag in the XML.
127      *
128      * @param stack
129      */
130     public void processMemory(Stack<Object> stack) {
131         Memory m = (Memory) stack.pop();
132         Architecture a = (Architecture)stack.peek();
133         a.getMemoryList().add(m);
134         m.setParentResource(a);
135     }
136
137     /**
138      * Process the start of a hw_channel tag in the XML.
139      *
140      * @param attributes The attributes of the tag.
141      * @return a hw_chnannel object.
142      */
143     public HWChannel processHWChannel(Attributes attributes) {
144         String name = (String) attributes.getValue("name");
145         String basename = (String) attributes.getValue("basename");
146         String range = (String) attributes.getValue("range");
147         String type = (String) attributes.getValue("type");
148         HWChannel hw_channel = new HWChannel(name);
149         if (basename != null) hw_channel.setBasename(basename);
150         if (range != null) hw_channel.setRange(range);
151         if (type != null) hw_channel.setType(type);
152         return hw_channel;
153     }
154
155     /**
156      * Process the end of an hw_channel tag in the XML.
157      *
158      * @param stack
159      */
160     public void processHWChannel(Stack<Object> stack) {
161         HWChannel c = (HWChannel) stack.pop();
162         if (stack.peek() instanceof Architecture) {
163             Architecture a = (Architecture)stack.peek();
164             a.getHWChannelList().add(c);
165             c.setParentResource(a);
166         } else if (stack.peek() instanceof WritePath) {
167             WritePath w = (WritePath)stack.peek();
168             HWChannel ch = ((Architecture)stack.elementAt(0)).
169                     getHWChannel(c.getName());
170             if (ch != null) {
171                 w.getHWChannelList().add(ch);
172             }
173             else {
174                 undefinedReference("WritePath", w.getName(),
175                         "hwchannel", c.getName());
176             }
177         } else if (stack.peek() instanceof ReadPath) {
178             ReadPath r = (ReadPath)stack.peek();
179             HWChannel ch = ((Architecture)stack.elementAt(0)).
180                     getHWChannel(c.getName());
181             if (ch != null) {
182                 r.getHWChannelList().add(ch);
183             }
184             else {
185                 undefinedReference("ReadPath", r.getName(),
186                         "hwchannel", c.getName());
187             }
188         }
189     }
190
191     /**
192      * Process the start of a node tag in the XML.
193      *
194      * @param attributes The attributes of the tag.
195      * @return a node object.
196      */
197     public Node processNode(Attributes attributes) {
198         String name = (String) attributes.getValue("name");
199         String basename = (String) attributes.getValue("basename");
200         String range = (String) attributes.getValue("range");
201         Node node = new Node(name);
202         if (basename != null) node.setBasename(basename);
203         if (range != null) node.setRange(range);
204         return node;
205     }
206
207     /**
208      * Process the end of an node tag in the XML.
209      *
210      * @param stack
211      */
212     public void processNode(Stack<Object> stack) {
213         Node n = (Node) stack.pop();
214         ArchiResource r = (ArchiResource)stack.peek();
215         r.getNodeList().add(n);
216     }
217
218     /**
219      * Process the start of a origin tag in the XML.
220      *
221      * @param attributes attributes of the tag
222      * @return a ArchiResource object
223      */
224     public ArchiResource processOrigin(Attributes attributes) {
225         String name = (String) attributes.getValue("name");
226         String basename = (String) attributes.getValue("basename");
227         ArchiResource resource = new ArchiResource(name);
228         if (basename != null) resource.setBasename(basename);
229
230         return resource;
231     }
232
233     /**
234      * Process the end of a origin tag in the XML.
235      *
236      * @param stack
237      */
238     public void processOrigin(Stack<Object> stack) {
239         ArchiResource resource = (ArchiResource) stack.pop();
240         ArchiConnection connection = (ArchiConnection) stack.peek();
241         connection.setOrigin(resource);
242     }
243
244     /**
245      *  Process the start of a target tag in the XML.
246      *
247      * @param attributes attributes of the tag
248      * @return a ArchiResource object
249      */
250     public ArchiResource processTarget(Attributes attributes) {
251         String name = (String) attributes.getValue("name");
252         String basename = (String) attributes.getValue("basename");
253         ArchiResource resource = new ArchiResource(name);
254         if (basename != null) resource.setBasename(basename);
255   
256         return resource;
257     }
258
259     /**
260      * Process the end of a target tag in the XML.
261      *
262      * @param stack
263      */
264     public void processTarget(Stack<Object> stack) {
265         ArchiResource resource = (ArchiResource) stack.pop();
266         ArchiConnection connection = (ArchiConnection) stack.peek();
267         connection.setTarget(resource);
268     }
269
270     /**
271      * Process the start of a connection tag in the XML.
272      *
273      * @param attributes attributes of the tag
274      * @return a ArchiConnection object
275      */
276     public ArchiConnection processConnection(Attributes attributes) {
277         String name = (String) attributes.getValue("name");
278         String basename = (String) attributes.getValue("basename");
279         ArchiConnection connection = new ArchiConnection(name);
280         if (basename != null) connection.setBasename(basename);
281  
282         return connection;
283     }
284
285     /**
286      * Process the end of a connection tag in the XML.
287      *
288      * @param stack
289      */
290     public void processConnection(Stack<Object> stack) {
291         ArchiConnection connection = (ArchiConnection) stack.pop();
292         Architecture architecture = (Architecture)stack.peek();
293         architecture.getConnectionList().add(connection);
294         connection.setParentResource(architecture);
295     }
296
297     /**
298      * Process the start of an inputport/outputport/port tag in the XML.
299      *
300      * @param attributes attributes of the tag
301      * @return a PortNode object
302      */
303     public PortNode processInputPort(Attributes attributes) {
304         String name = (String) attributes.getValue("name");
305         String basename = (String) attributes.getValue("basename");
306         String range = (String) attributes.getValue("range");
307
308         PortNode port = new PortNode(name, PortNode.INPORT);
309
310         if (basename != null)
311             port.setBasename(basename);
312         else
313             port.setBasename(name);
314
315         if (range != null)
316             port.setRange(range);
317
318          return port;
319     }
320
321    public PortNode processOutputPort(Attributes attributes) {
322         String name = (String) attributes.getValue("name");
323         String basename = (String) attributes.getValue("basename");
324         String range = (String) attributes.getValue("range");
325
326         PortNode port = new PortNode(name, PortNode.OUTPORT);
327
328         if (basename != null)
329             port.setBasename(basename);
330         else
331             port.setBasename(name);
332
333         if (range != null)
334             port.setRange(range);
335
336         return port;
337     }
338
339     // duplexport
340     public PortNode processInOutPort(Attributes attributes) {
341         String name = (String) attributes.getValue("name");
342         String basename = (String) attributes.getValue("basename");
343         String range = (String) attributes.getValue("range");
344
345         PortNode port = new PortNode(name, PortNode.INOUTPORT);
346
347         if (basename != null)
348             port.setBasename(basename);
349         else
350             port.setBasename(name);
351
352         if (range != null)
353             port.setRange(range);
354
355         return port;
356     }
357
358    public PortNode processPort(Attributes attributes) {
359         String name = (String) attributes.getValue("name");
360         String basename = (String) attributes.getValue("basename");
361         String range = (String) attributes.getValue("range");
362
363         PortNode port = new PortNode(name);
364
365         if (basename != null)
366             port.setBasename(basename);
367         else
368             port.setBasename(name);
369
370         if (range != null)
371             port.setRange(range);
372
373         return port;
374     }
375
376     /**
377      * Process the end of an inputport/outputport/duplexport/port tag in the XML.
378      *
379      * @param stack
380      */
381     public void processInputPort(Stack<Object> stack) {
382         PortNode port = (PortNode) stack.pop();
383         Node node = (Node) stack.peek();
384         port.setNode(node);
385         node.getPortList().add(port);
386     }
387
388     public void processOutputPort(Stack<Object> stack) {
389         PortNode port = (PortNode) stack.pop();
390         Node node = (Node) stack.peek();
391         port.setNode(node);
392         node.getPortList().add(port);
393     }
394
395     public void processInOutPort(Stack<Object> stack) {
396         PortNode port = (PortNode) stack.pop();
397         Node node = (Node) stack.peek();
398         port.setNode(node);
399         node.getPortList().add(port);
400     }
401
402     public void processPort(Stack<Object> stack) {
403          PortNode port = (PortNode) stack.pop();
404          Node node = (Node) stack.peek();
405          port.setNode(node);
406          node.getPortList().add(port);
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       
422         return variable;
423     }
424
425     /**
426      * Process the end of a variable tag in the XML.
427      *
428      * @param stack
429      */
430     public void processVariable(Stack<Object> stack) {
431         Variable variable = (Variable) stack.pop();
432         Architecture architecture = (Architecture)stack.peek();
433         variable.setParentResource(architecture);
434         architecture.getVarList().add(variable);
435     }
436
437     /**
438      * Process the start of a configuration tag in the XML.
439      *
440      * @param attributes attributes of the tag
441      * @return a Configuration object
442      */
443     public Configuration processConfiguration(Attributes attributes) {
444         String name = (String) attributes.getValue("name");
445         String value = (String) attributes.getValue("value");
446         Configuration config = new Configuration(name);
447         config.setValue(value);
448         return config;
449     }
450
451     /**
452      * Process the end of a configuration tag in the XML.
453      *
454      * @param stack
455      */
456     public void processConfiguration(Stack<Object> stack) {
457         Configuration config = (Configuration) stack.pop();
458         ArchiResource res = (ArchiResource)stack.peek();
459         res.getCfgList().add(config);
460     }
461
462     /**
463      * Process the start of a readPath tag in the XML.
464      *
465      * @param attributes attributes of the tag
466      * @return a ReadPath object
467      */
468     public ReadPath processReadPath(Attributes attributes) {
469         String name = (String) attributes.getValue("name");
470         ReadPath readpath = new ReadPath(name);
471         return readpath;
472     }
473
474     /**
475      * Process the end of a readpath tag in the XML.
476      *
477      * @param stack
478      */
479     public void processReadPath(Stack<Object> stack) {
480         ReadPath readpath = (ReadPath) stack.pop();
481         Architecture res = (Architecture)stack.peek();
482         res.getReadPathList().add(readpath);
483     }
484
485     /**
486      * Process the start of a WritePath tag in the XML.
487      *
488      * @param attributes attributes of the tag
489      * @return a WritePath object
490      */
491     public WritePath processWritePath(Attributes attributes) {
492         String name = (String) attributes.getValue("name");
493         WritePath writepath = new WritePath(name);
494         return writepath;
495     }
496
497     /**
498      * Process the end of a WritePath tag in the XML.
499      *
500      * @param stack
501      */
502     public void processWritePath(Stack<Object> stack) {
503         WritePath writepath = (WritePath) stack.pop();
504         Architecture res = (Architecture)stack.peek();
505         res.getWritePathList().add(writepath);
506     }
507
508     /**
509      * Process the start of a TXBuf tag in the XML.
510      *
511      * @param attributes attributes of the tag
512      * @return a TXBuf object
513      */
514     public String processTXBuf(Attributes attributes) {
515         return attributes.getValue("name");
516     }
517
518     /**
519      * Process the end of a TXBuf tag in the XML.
520      *
521      * @param stack
522      */
523     public void processTXBuf(Stack<Object> stack) {
524         String txBuf = (String) stack.pop();
525         WritePath res = (WritePath)stack.peek();
526         Memory mem = ((Architecture)res.getProcessor().getParentResource()).getMemory(txBuf);
527         if (mem != null) {
528             res.setTXBuf(mem);
529         }
530         else {
531             undefinedReference("ReadPath", res.getName(),
532                     "transmit buffer", txBuf);
533         }
534     }
535
536     /**
537      * Process the start of a RXBuf tag in the XML.
538      *
539      * @param attributes attributes of the tag
540      * @return a RXBuf object
541      */
542     public String processRXBuf(Attributes attributes) {
543         return attributes.getValue("name");
544     }
545
546     /**
547      * Process the end of a RXBuf tag in the XML.
548      *
549      * @param stack
550      */
551     public void processRXBuf(Stack<Object> stack) {
552         String rxBuf = (String) stack.pop();
553         ReadPath res = (ReadPath)stack.peek();
554         Memory mem = ((Architecture)res.getProcessor().getParentResource()).getMemory(rxBuf);
555         if (mem != null) {
556             res.setRXBuf(mem);
557         }
558         else {
559             undefinedReference("ReadPath", res.getName(),
560                     "receive buffer", rxBuf);
561         }
562     }
563
564     /**
565      * Process the start of a CHBuf tag in the XML.
566      *
567      * @param attributes attributes of the tag
568      * @return a CHBuf object
569      */
570     public String processCHBuf(Attributes attributes) {
571         return attributes.getValue("name");
572     }
573
574     /**
575      * Process the end of a CHBuf tag in the XML.
576      *
577      * @param stack
578      */
579     public void processCHBuf(Stack<Object> stack) {
580         String value= (String) stack.pop();
581         if (stack.peek() instanceof ReadPath) {
582             ReadPath res = (ReadPath)stack.peek();
583             Memory mem = ((Architecture)res.getProcessor().
584                     getParentResource()).getMemory(value);
585             if (mem != null) {
586                 res.setCHBuf(mem);
587             }
588             else {
589                 undefinedReference("ReadPath", res.getName(),
590                         "channel buffer", value);
591             }
592         }
593         else if (stack.peek() instanceof WritePath) {
594             WritePath res = (WritePath)stack.peek();
595             Memory mem = ((Architecture)res.getProcessor().
596                     getParentResource()).getMemory(value);
597             if (mem != null) {
598                 res.setCHBuf(mem);
599             }
600             else {
601                 undefinedReference("WritePath", res.getName(),
602                         "channel buffer", value);
603             }
604         }
605     }
606
607
608     /**
609      * Write an error message and terminate program in case an
610      * architecture element is referenced which does not exist.
611      *
612      * @param elementType type of architecture element in which the error
613      *                    occurred
614      * @param elementName name of architecture element in which the error
615      *                    occurred
616      * @param referenceType type of referenced architecture element
617      * @param referenceName name of referenced architecture element
618      */
619     protected void undefinedReference(String elementType,
620                                       String elementName,
621                                       String referenceType,
622                                       String referenceName) {
623         System.out.println("Error: " + elementType + " " + elementName
624                 + " references " + referenceType + " " + referenceName
625                 + " that has not been declared.");
626         System.out.println("Exit.");
627         System.exit(-1);
628     }
629
630 }