1 /* $Id: Xml2Archi.java 1 2010-02-24 13:03:05Z haidw $ */
2 package dol.parser.xml.archischema;
4 import java.util.Stack;
6 import org.xml.sax.Attributes;
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;
22 * Parse architecture XML file.
24 public class Xml2Archi {
33 * Process the start of the Architecture tag in the XML.
35 * @param attributes attributes of the tag
36 * @return an Architecture object
38 public Architecture processArchitecture(Attributes attributes) {
39 Architecture arch = new Architecture(attributes.getValue("name"));
45 * Process the end of the Architecture tag in the XML.
49 public void processArchitecture(Stack<Object> stack) {
53 * Process the start of a processor tag in the XML.
55 * @param attributes The attributes of the tag.
56 * @return a processor object.
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);
71 * Process the end of a processor tag in the XML.
75 public void processProcessor(Stack<Object> stack) {
76 Processor p = (Processor) stack.pop();
78 if (stack.peek() instanceof ReadPath) {
79 ReadPath r = (ReadPath) stack.peek();
80 Processor pro = ((Architecture)stack.elementAt(0)).
81 getProcessor(p.getName());
86 undefinedReference("ReadPath", r.getName(),
87 "processor", p.getName());
89 } else if (stack.peek() instanceof WritePath) {
90 WritePath w = (WritePath) stack.peek();
91 Processor pro = ((Architecture)stack.elementAt(0)).
92 getProcessor(p.getName());
97 undefinedReference("ReadPath", w.getName(),
98 "processor", p.getName());
100 } else if (stack.peek() instanceof Architecture) {
101 Architecture a = (Architecture)stack.peek();
102 p.setParentResource(a);
103 a.getProcessorList().add(p);
108 * Process the start of a memory tag in the XML.
110 * @param attributes The attributes of the tag.
111 * @return a memory object.
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);
126 * Process the end of a memory tag in the XML.
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);
138 * Process the start of a hw_channel tag in the XML.
140 * @param attributes The attributes of the tag.
141 * @return a hw_chnannel object.
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);
156 * Process the end of an hw_channel tag in the XML.
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());
171 w.getHWChannelList().add(ch);
174 undefinedReference("WritePath", w.getName(),
175 "hwchannel", c.getName());
177 } else if (stack.peek() instanceof ReadPath) {
178 ReadPath r = (ReadPath)stack.peek();
179 HWChannel ch = ((Architecture)stack.elementAt(0)).
180 getHWChannel(c.getName());
182 r.getHWChannelList().add(ch);
185 undefinedReference("ReadPath", r.getName(),
186 "hwchannel", c.getName());
192 * Process the start of a node tag in the XML.
194 * @param attributes The attributes of the tag.
195 * @return a node object.
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);
208 * Process the end of an node tag in the XML.
212 public void processNode(Stack<Object> stack) {
213 Node n = (Node) stack.pop();
214 ArchiResource r = (ArchiResource)stack.peek();
215 r.getNodeList().add(n);
219 * Process the start of a origin tag in the XML.
221 * @param attributes attributes of the tag
222 * @return a ArchiResource object
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);
234 * Process the end of a origin tag in the XML.
238 public void processOrigin(Stack<Object> stack) {
239 ArchiResource resource = (ArchiResource) stack.pop();
240 ArchiConnection connection = (ArchiConnection) stack.peek();
241 connection.setOrigin(resource);
245 * Process the start of a target tag in the XML.
247 * @param attributes attributes of the tag
248 * @return a ArchiResource object
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);
260 * Process the end of a target tag in the XML.
264 public void processTarget(Stack<Object> stack) {
265 ArchiResource resource = (ArchiResource) stack.pop();
266 ArchiConnection connection = (ArchiConnection) stack.peek();
267 connection.setTarget(resource);
271 * Process the start of a connection tag in the XML.
273 * @param attributes attributes of the tag
274 * @return a ArchiConnection object
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);
286 * Process the end of a connection tag in the XML.
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);
298 * Process the start of an inputport/outputport/port tag in the XML.
300 * @param attributes attributes of the tag
301 * @return a PortNode object
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");
308 PortNode port = new PortNode(name, PortNode.INPORT);
310 if (basename != null)
311 port.setBasename(basename);
313 port.setBasename(name);
316 port.setRange(range);
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");
326 PortNode port = new PortNode(name, PortNode.OUTPORT);
328 if (basename != null)
329 port.setBasename(basename);
331 port.setBasename(name);
334 port.setRange(range);
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");
345 PortNode port = new PortNode(name, PortNode.INOUTPORT);
347 if (basename != null)
348 port.setBasename(basename);
350 port.setBasename(name);
353 port.setRange(range);
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");
363 PortNode port = new PortNode(name);
365 if (basename != null)
366 port.setBasename(basename);
368 port.setBasename(name);
371 port.setRange(range);
377 * Process the end of an inputport/outputport/duplexport/port tag in the XML.
381 public void processInputPort(Stack<Object> stack) {
382 PortNode port = (PortNode) stack.pop();
383 Node node = (Node) stack.peek();
385 node.getPortList().add(port);
388 public void processOutputPort(Stack<Object> stack) {
389 PortNode port = (PortNode) stack.pop();
390 Node node = (Node) stack.peek();
392 node.getPortList().add(port);
395 public void processInOutPort(Stack<Object> stack) {
396 PortNode port = (PortNode) stack.pop();
397 Node node = (Node) stack.peek();
399 node.getPortList().add(port);
402 public void processPort(Stack<Object> stack) {
403 PortNode port = (PortNode) stack.pop();
404 Node node = (Node) stack.peek();
406 node.getPortList().add(port);
410 * Process the start of a variable tag in the XML.
412 * @param attributes attributes of the tag
413 * @return a Variable object
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));
426 * Process the end of a variable tag in the XML.
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);
438 * Process the start of a configuration tag in the XML.
440 * @param attributes attributes of the tag
441 * @return a Configuration object
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);
452 * Process the end of a configuration tag in the XML.
456 public void processConfiguration(Stack<Object> stack) {
457 Configuration config = (Configuration) stack.pop();
458 ArchiResource res = (ArchiResource)stack.peek();
459 res.getCfgList().add(config);
463 * Process the start of a readPath tag in the XML.
465 * @param attributes attributes of the tag
466 * @return a ReadPath object
468 public ReadPath processReadPath(Attributes attributes) {
469 String name = (String) attributes.getValue("name");
470 ReadPath readpath = new ReadPath(name);
475 * Process the end of a readpath tag in the XML.
479 public void processReadPath(Stack<Object> stack) {
480 ReadPath readpath = (ReadPath) stack.pop();
481 Architecture res = (Architecture)stack.peek();
482 res.getReadPathList().add(readpath);
486 * Process the start of a WritePath tag in the XML.
488 * @param attributes attributes of the tag
489 * @return a WritePath object
491 public WritePath processWritePath(Attributes attributes) {
492 String name = (String) attributes.getValue("name");
493 WritePath writepath = new WritePath(name);
498 * Process the end of a WritePath tag in the XML.
502 public void processWritePath(Stack<Object> stack) {
503 WritePath writepath = (WritePath) stack.pop();
504 Architecture res = (Architecture)stack.peek();
505 res.getWritePathList().add(writepath);
509 * Process the start of a TXBuf tag in the XML.
511 * @param attributes attributes of the tag
512 * @return a TXBuf object
514 public String processTXBuf(Attributes attributes) {
515 return attributes.getValue("name");
519 * Process the end of a TXBuf tag in the XML.
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);
531 undefinedReference("ReadPath", res.getName(),
532 "transmit buffer", txBuf);
537 * Process the start of a RXBuf tag in the XML.
539 * @param attributes attributes of the tag
540 * @return a RXBuf object
542 public String processRXBuf(Attributes attributes) {
543 return attributes.getValue("name");
547 * Process the end of a RXBuf tag in the XML.
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);
559 undefinedReference("ReadPath", res.getName(),
560 "receive buffer", rxBuf);
565 * Process the start of a CHBuf tag in the XML.
567 * @param attributes attributes of the tag
568 * @return a CHBuf object
570 public String processCHBuf(Attributes attributes) {
571 return attributes.getValue("name");
575 * Process the end of a CHBuf tag in the XML.
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);
589 undefinedReference("ReadPath", res.getName(),
590 "channel buffer", value);
593 else if (stack.peek() instanceof WritePath) {
594 WritePath res = (WritePath)stack.peek();
595 Memory mem = ((Architecture)res.getProcessor().
596 getParentResource()).getMemory(value);
601 undefinedReference("WritePath", res.getName(),
602 "channel buffer", value);
609 * Write an error message and terminate program in case an
610 * architecture element is referenced which does not exist.
612 * @param elementType type of architecture element in which the error
614 * @param elementName name of architecture element in which the error
616 * @param referenceType type of referenced architecture element
617 * @param referenceName name of referenced architecture element
619 protected void undefinedReference(String elementType,
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.");