+/* $Id: Xml2Archi.java 1 2010-02-24 13:03:05Z haidw $ */
+package dol.parser.xml.archischema;
+
+import java.util.Stack;
+
+import org.xml.sax.Attributes;
+
+import dol.datamodel.architecture.ArchiConnection;
+import dol.datamodel.architecture.ArchiResource;
+import dol.datamodel.architecture.Architecture;
+import dol.datamodel.architecture.Configuration;
+import dol.datamodel.architecture.HWChannel;
+import dol.datamodel.architecture.Memory;
+import dol.datamodel.architecture.Node;
+import dol.datamodel.architecture.PortNode;
+import dol.datamodel.architecture.Processor;
+import dol.datamodel.architecture.ReadPath;
+import dol.datamodel.architecture.Variable;
+import dol.datamodel.architecture.WritePath;
+
+/**
+ * Parse architecture XML file.
+ */
+public class Xml2Archi {
+
+ /**
+ * Constructor.
+ */
+ public Xml2Archi() {
+ }
+
+ /**
+ * Process the start of the Architecture tag in the XML.
+ *
+ * @param attributes attributes of the tag
+ * @return an Architecture object
+ */
+ public Architecture processArchitecture(Attributes attributes) {
+ Architecture arch = new Architecture(attributes.getValue("name"));
+ return arch;
+
+ }
+
+ /**
+ * Process the end of the Architecture tag in the XML.
+ *
+ * @param stack
+ */
+ public void processArchitecture(Stack<Object> stack) {
+ }
+
+ /**
+ * Process the start of a processor tag in the XML.
+ *
+ * @param attributes The attributes of the tag.
+ * @return a processor object.
+ */
+ public Processor processProcessor(Attributes attributes) {
+ String name = (String) attributes.getValue("name");
+ String basename = (String) attributes.getValue("basename");
+ String range = (String) attributes.getValue("range");
+ String type = (String) attributes.getValue("type");
+ Processor processor = new Processor(name);
+ if (basename != null) processor.setBasename(basename);
+ if (range != null) processor.setRange(range);
+ if (type != null) processor.setType(type);
+ return processor;
+ }
+
+ /**
+ * Process the end of a processor tag in the XML.
+ *
+ * @param stack
+ */
+ public void processProcessor(Stack<Object> stack) {
+ Processor p = (Processor) stack.pop();
+
+ if (stack.peek() instanceof ReadPath) {
+ ReadPath r = (ReadPath) stack.peek();
+ Processor pro = ((Architecture)stack.elementAt(0)).
+ getProcessor(p.getName());
+ if (pro != null) {
+ r.setProcessor(pro);
+ }
+ else {
+ undefinedReference("ReadPath", r.getName(),
+ "processor", p.getName());
+ }
+ } else if (stack.peek() instanceof WritePath) {
+ WritePath w = (WritePath) stack.peek();
+ Processor pro = ((Architecture)stack.elementAt(0)).
+ getProcessor(p.getName());
+ if (pro != null) {
+ w.setProcessor(pro);
+ }
+ else {
+ undefinedReference("ReadPath", w.getName(),
+ "processor", p.getName());
+ }
+ } else if (stack.peek() instanceof Architecture) {
+ Architecture a = (Architecture)stack.peek();
+ p.setParentResource(a);
+ a.getProcessorList().add(p);
+ }
+ }
+
+ /**
+ * Process the start of a memory tag in the XML.
+ *
+ * @param attributes The attributes of the tag.
+ * @return a memory object.
+ */
+ public Memory processMemory(Attributes attributes) {
+ String name = (String) attributes.getValue("name");
+ String basename = (String) attributes.getValue("basename");
+ String range = (String) attributes.getValue("range");
+ String type = (String) attributes.getValue("type");
+ Memory memory = new Memory(name);
+ if (basename != null) memory.setBasename(basename);
+ if (range != null) memory.setRange(range);
+ if (type != null) memory.setRange(type);
+ return memory;
+ }
+
+ /**
+ * Process the end of a memory tag in the XML.
+ *
+ * @param stack
+ */
+ public void processMemory(Stack<Object> stack) {
+ Memory m = (Memory) stack.pop();
+ Architecture a = (Architecture)stack.peek();
+ a.getMemoryList().add(m);
+ m.setParentResource(a);
+ }
+
+ /**
+ * Process the start of a hw_channel tag in the XML.
+ *
+ * @param attributes The attributes of the tag.
+ * @return a hw_chnannel object.
+ */
+ public HWChannel processHWChannel(Attributes attributes) {
+ String name = (String) attributes.getValue("name");
+ String basename = (String) attributes.getValue("basename");
+ String range = (String) attributes.getValue("range");
+ String type = (String) attributes.getValue("type");
+ HWChannel hw_channel = new HWChannel(name);
+ if (basename != null) hw_channel.setBasename(basename);
+ if (range != null) hw_channel.setRange(range);
+ if (type != null) hw_channel.setType(type);
+ return hw_channel;
+ }
+
+ /**
+ * Process the end of an hw_channel tag in the XML.
+ *
+ * @param stack
+ */
+ public void processHWChannel(Stack<Object> stack) {
+ HWChannel c = (HWChannel) stack.pop();
+ if (stack.peek() instanceof Architecture) {
+ Architecture a = (Architecture)stack.peek();
+ a.getHWChannelList().add(c);
+ c.setParentResource(a);
+ } else if (stack.peek() instanceof WritePath) {
+ WritePath w = (WritePath)stack.peek();
+ HWChannel ch = ((Architecture)stack.elementAt(0)).
+ getHWChannel(c.getName());
+ if (ch != null) {
+ w.getHWChannelList().add(ch);
+ }
+ else {
+ undefinedReference("WritePath", w.getName(),
+ "hwchannel", c.getName());
+ }
+ } else if (stack.peek() instanceof ReadPath) {
+ ReadPath r = (ReadPath)stack.peek();
+ HWChannel ch = ((Architecture)stack.elementAt(0)).
+ getHWChannel(c.getName());
+ if (ch != null) {
+ r.getHWChannelList().add(ch);
+ }
+ else {
+ undefinedReference("ReadPath", r.getName(),
+ "hwchannel", c.getName());
+ }
+ }
+ }
+
+ /**
+ * Process the start of a node tag in the XML.
+ *
+ * @param attributes The attributes of the tag.
+ * @return a node object.
+ */
+ public Node processNode(Attributes attributes) {
+ String name = (String) attributes.getValue("name");
+ String basename = (String) attributes.getValue("basename");
+ String range = (String) attributes.getValue("range");
+ Node node = new Node(name);
+ if (basename != null) node.setBasename(basename);
+ if (range != null) node.setRange(range);
+ return node;
+ }
+
+ /**
+ * Process the end of an node tag in the XML.
+ *
+ * @param stack
+ */
+ public void processNode(Stack<Object> stack) {
+ Node n = (Node) stack.pop();
+ ArchiResource r = (ArchiResource)stack.peek();
+ r.getNodeList().add(n);
+ }
+
+ /**
+ * Process the start of a origin tag in the XML.
+ *
+ * @param attributes attributes of the tag
+ * @return a ArchiResource object
+ */
+ public ArchiResource processOrigin(Attributes attributes) {
+ String name = (String) attributes.getValue("name");
+ String basename = (String) attributes.getValue("basename");
+ ArchiResource resource = new ArchiResource(name);
+ if (basename != null) resource.setBasename(basename);
+
+ return resource;
+ }
+
+ /**
+ * Process the end of a origin tag in the XML.
+ *
+ * @param stack
+ */
+ public void processOrigin(Stack<Object> stack) {
+ ArchiResource resource = (ArchiResource) stack.pop();
+ ArchiConnection connection = (ArchiConnection) stack.peek();
+ connection.setOrigin(resource);
+ }
+
+ /**
+ * Process the start of a target tag in the XML.
+ *
+ * @param attributes attributes of the tag
+ * @return a ArchiResource object
+ */
+ public ArchiResource processTarget(Attributes attributes) {
+ String name = (String) attributes.getValue("name");
+ String basename = (String) attributes.getValue("basename");
+ ArchiResource resource = new ArchiResource(name);
+ if (basename != null) resource.setBasename(basename);
+
+ return resource;
+ }
+
+ /**
+ * Process the end of a target tag in the XML.
+ *
+ * @param stack
+ */
+ public void processTarget(Stack<Object> stack) {
+ ArchiResource resource = (ArchiResource) stack.pop();
+ ArchiConnection connection = (ArchiConnection) stack.peek();
+ connection.setTarget(resource);
+ }
+
+ /**
+ * Process the start of a connection tag in the XML.
+ *
+ * @param attributes attributes of the tag
+ * @return a ArchiConnection object
+ */
+ public ArchiConnection processConnection(Attributes attributes) {
+ String name = (String) attributes.getValue("name");
+ String basename = (String) attributes.getValue("basename");
+ ArchiConnection connection = new ArchiConnection(name);
+ if (basename != null) connection.setBasename(basename);
+
+ return connection;
+ }
+
+ /**
+ * Process the end of a connection tag in the XML.
+ *
+ * @param stack
+ */
+ public void processConnection(Stack<Object> stack) {
+ ArchiConnection connection = (ArchiConnection) stack.pop();
+ Architecture architecture = (Architecture)stack.peek();
+ architecture.getConnectionList().add(connection);
+ connection.setParentResource(architecture);
+ }
+
+ /**
+ * Process the start of an inputport/outputport/port tag in the XML.
+ *
+ * @param attributes attributes of the tag
+ * @return a PortNode object
+ */
+ public PortNode processInputPort(Attributes attributes) {
+ String name = (String) attributes.getValue("name");
+ String basename = (String) attributes.getValue("basename");
+ String range = (String) attributes.getValue("range");
+
+ PortNode port = new PortNode(name, PortNode.INPORT);
+
+ if (basename != null)
+ port.setBasename(basename);
+ else
+ port.setBasename(name);
+
+ if (range != null)
+ port.setRange(range);
+
+ return port;
+ }
+
+ public PortNode processOutputPort(Attributes attributes) {
+ String name = (String) attributes.getValue("name");
+ String basename = (String) attributes.getValue("basename");
+ String range = (String) attributes.getValue("range");
+
+ PortNode port = new PortNode(name, PortNode.OUTPORT);
+
+ if (basename != null)
+ port.setBasename(basename);
+ else
+ port.setBasename(name);
+
+ if (range != null)
+ port.setRange(range);
+
+ return port;
+ }
+
+ // duplexport
+ public PortNode processInOutPort(Attributes attributes) {
+ String name = (String) attributes.getValue("name");
+ String basename = (String) attributes.getValue("basename");
+ String range = (String) attributes.getValue("range");
+
+ PortNode port = new PortNode(name, PortNode.INOUTPORT);
+
+ if (basename != null)
+ port.setBasename(basename);
+ else
+ port.setBasename(name);
+
+ if (range != null)
+ port.setRange(range);
+
+ return port;
+ }
+
+ public PortNode processPort(Attributes attributes) {
+ String name = (String) attributes.getValue("name");
+ String basename = (String) attributes.getValue("basename");
+ String range = (String) attributes.getValue("range");
+
+ PortNode port = new PortNode(name);
+
+ if (basename != null)
+ port.setBasename(basename);
+ else
+ port.setBasename(name);
+
+ if (range != null)
+ port.setRange(range);
+
+ return port;
+ }
+
+ /**
+ * Process the end of an inputport/outputport/duplexport/port tag in the XML.
+ *
+ * @param stack
+ */
+ public void processInputPort(Stack<Object> stack) {
+ PortNode port = (PortNode) stack.pop();
+ Node node = (Node) stack.peek();
+ port.setNode(node);
+ node.getPortList().add(port);
+ }
+
+ public void processOutputPort(Stack<Object> stack) {
+ PortNode port = (PortNode) stack.pop();
+ Node node = (Node) stack.peek();
+ port.setNode(node);
+ node.getPortList().add(port);
+ }
+
+ public void processInOutPort(Stack<Object> stack) {
+ PortNode port = (PortNode) stack.pop();
+ Node node = (Node) stack.peek();
+ port.setNode(node);
+ node.getPortList().add(port);
+ }
+
+ public void processPort(Stack<Object> stack) {
+ PortNode port = (PortNode) stack.pop();
+ Node node = (Node) stack.peek();
+ port.setNode(node);
+ node.getPortList().add(port);
+ }
+
+ /**
+ * Process the start of a variable tag in the XML.
+ *
+ * @param attributes attributes of the tag
+ * @return a Variable object
+ */
+ public Variable processVariable(Attributes attributes) {
+ String name = (String) attributes.getValue("name");
+ String value = (String) attributes.getValue("value");
+ Variable variable = null;
+ variable = new Variable(name);
+ variable.setValue(Integer.parseInt(value));
+
+ return variable;
+ }
+
+ /**
+ * Process the end of a variable tag in the XML.
+ *
+ * @param stack
+ */
+ public void processVariable(Stack<Object> stack) {
+ Variable variable = (Variable) stack.pop();
+ Architecture architecture = (Architecture)stack.peek();
+ variable.setParentResource(architecture);
+ architecture.getVarList().add(variable);
+ }
+
+ /**
+ * Process the start of a configuration tag in the XML.
+ *
+ * @param attributes attributes of the tag
+ * @return a Configuration object
+ */
+ public Configuration processConfiguration(Attributes attributes) {
+ String name = (String) attributes.getValue("name");
+ String value = (String) attributes.getValue("value");
+ Configuration config = new Configuration(name);
+ config.setValue(value);
+ return config;
+ }
+
+ /**
+ * Process the end of a configuration tag in the XML.
+ *
+ * @param stack
+ */
+ public void processConfiguration(Stack<Object> stack) {
+ Configuration config = (Configuration) stack.pop();
+ ArchiResource res = (ArchiResource)stack.peek();
+ res.getCfgList().add(config);
+ }
+
+ /**
+ * Process the start of a readPath tag in the XML.
+ *
+ * @param attributes attributes of the tag
+ * @return a ReadPath object
+ */
+ public ReadPath processReadPath(Attributes attributes) {
+ String name = (String) attributes.getValue("name");
+ ReadPath readpath = new ReadPath(name);
+ return readpath;
+ }
+
+ /**
+ * Process the end of a readpath tag in the XML.
+ *
+ * @param stack
+ */
+ public void processReadPath(Stack<Object> stack) {
+ ReadPath readpath = (ReadPath) stack.pop();
+ Architecture res = (Architecture)stack.peek();
+ res.getReadPathList().add(readpath);
+ }
+
+ /**
+ * Process the start of a WritePath tag in the XML.
+ *
+ * @param attributes attributes of the tag
+ * @return a WritePath object
+ */
+ public WritePath processWritePath(Attributes attributes) {
+ String name = (String) attributes.getValue("name");
+ WritePath writepath = new WritePath(name);
+ return writepath;
+ }
+
+ /**
+ * Process the end of a WritePath tag in the XML.
+ *
+ * @param stack
+ */
+ public void processWritePath(Stack<Object> stack) {
+ WritePath writepath = (WritePath) stack.pop();
+ Architecture res = (Architecture)stack.peek();
+ res.getWritePathList().add(writepath);
+ }
+
+ /**
+ * Process the start of a TXBuf tag in the XML.
+ *
+ * @param attributes attributes of the tag
+ * @return a TXBuf object
+ */
+ public String processTXBuf(Attributes attributes) {
+ return attributes.getValue("name");
+ }
+
+ /**
+ * Process the end of a TXBuf tag in the XML.
+ *
+ * @param stack
+ */
+ public void processTXBuf(Stack<Object> stack) {
+ String txBuf = (String) stack.pop();
+ WritePath res = (WritePath)stack.peek();
+ Memory mem = ((Architecture)res.getProcessor().getParentResource()).getMemory(txBuf);
+ if (mem != null) {
+ res.setTXBuf(mem);
+ }
+ else {
+ undefinedReference("ReadPath", res.getName(),
+ "transmit buffer", txBuf);
+ }
+ }
+
+ /**
+ * Process the start of a RXBuf tag in the XML.
+ *
+ * @param attributes attributes of the tag
+ * @return a RXBuf object
+ */
+ public String processRXBuf(Attributes attributes) {
+ return attributes.getValue("name");
+ }
+
+ /**
+ * Process the end of a RXBuf tag in the XML.
+ *
+ * @param stack
+ */
+ public void processRXBuf(Stack<Object> stack) {
+ String rxBuf = (String) stack.pop();
+ ReadPath res = (ReadPath)stack.peek();
+ Memory mem = ((Architecture)res.getProcessor().getParentResource()).getMemory(rxBuf);
+ if (mem != null) {
+ res.setRXBuf(mem);
+ }
+ else {
+ undefinedReference("ReadPath", res.getName(),
+ "receive buffer", rxBuf);
+ }
+ }
+
+ /**
+ * Process the start of a CHBuf tag in the XML.
+ *
+ * @param attributes attributes of the tag
+ * @return a CHBuf object
+ */
+ public String processCHBuf(Attributes attributes) {
+ return attributes.getValue("name");
+ }
+
+ /**
+ * Process the end of a CHBuf tag in the XML.
+ *
+ * @param stack
+ */
+ public void processCHBuf(Stack<Object> stack) {
+ String value= (String) stack.pop();
+ if (stack.peek() instanceof ReadPath) {
+ ReadPath res = (ReadPath)stack.peek();
+ Memory mem = ((Architecture)res.getProcessor().
+ getParentResource()).getMemory(value);
+ if (mem != null) {
+ res.setCHBuf(mem);
+ }
+ else {
+ undefinedReference("ReadPath", res.getName(),
+ "channel buffer", value);
+ }
+ }
+ else if (stack.peek() instanceof WritePath) {
+ WritePath res = (WritePath)stack.peek();
+ Memory mem = ((Architecture)res.getProcessor().
+ getParentResource()).getMemory(value);
+ if (mem != null) {
+ res.setCHBuf(mem);
+ }
+ else {
+ undefinedReference("WritePath", res.getName(),
+ "channel buffer", value);
+ }
+ }
+ }
+
+
+ /**
+ * Write an error message and terminate program in case an
+ * architecture element is referenced which does not exist.
+ *
+ * @param elementType type of architecture element in which the error
+ * occurred
+ * @param elementName name of architecture element in which the error
+ * occurred
+ * @param referenceType type of referenced architecture element
+ * @param referenceName name of referenced architecture element
+ */
+ protected void undefinedReference(String elementType,
+ String elementName,
+ String referenceType,
+ String referenceName) {
+ System.out.println("Error: " + elementType + " " + elementName
+ + " references " + referenceType + " " + referenceName
+ + " that has not been declared.");
+ System.out.println("Exit.");
+ System.exit(-1);
+ }
+
+}