1 /* $Id: Xml2PN.java 1 2010-02-24 13:03:05Z haidw $ */
2 package dol.parser.xml.pnschema;
4 import java.util.Stack;
6 import org.xml.sax.Attributes;
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;
31 * Process the start of the process network tag in the XML.
33 * @param attributes attributes of the tag
34 * @return a ProcessNetwork object
36 public ProcessNetwork processPN(Attributes attributes) {
37 String name = (String) attributes.getValue("name");
38 ProcessNetwork p = new ProcessNetwork(name);
43 * Process the end of the processnetwork tag in the XML.
47 public void processPN(Stack<Object> stack) {
51 * Process the start of a process tag in the XML.
53 * @param attributes The attributes of the tag.
54 * @return a process object.
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);
67 * Process the end of a process tag in the XML.
71 public void processProcess(Stack<Object> stack) {
72 Process p = (Process) stack.pop();
73 ProcessNetwork r = (ProcessNetwork)stack.peek();
74 r.getProcessList().add(p);
78 * Process the start of a origin tag in the XML.
80 * @param attributes attributes of the tag
81 * @return a Resource object
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);
92 * Process the end of a origin tag in the XML.
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();
102 if (!port.isOutPort()) {
103 System.out.println("Error: Connection "
104 + connection.getName()
105 + " must be defined in the direction of the "
107 System.out.println("Exit.");
110 connection.setOriginPort(port);
112 undefinedReference("Connection", connection.getName(),
113 "element", resource.getName());
116 Process process = ((ProcessNetwork)stack.elementAt(0)).
117 getProcess(resource.getName());
118 Channel channel = ((ProcessNetwork)stack.elementAt(0)).
119 getChannel(resource.getName());
121 if (process != null) {
122 connection.setOrigin(process);
124 else if (channel != null) {
125 connection.setOrigin(channel);
128 undefinedReference("Connection", connection.getName(),
129 "element", resource.getName());
134 * Process the start of a target tag in the XML.
136 * @param attributes attributes of the tag
137 * @return a Resource object
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);
148 * Process the end of a target tag in the XML.
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();
158 if (!port.isInPort()) {
159 System.out.println("Error: Connection "
160 + connection.getName()
161 + " must be defined in the direction of the "
163 System.out.println("Exit.");
166 connection.setTargetPort(port);
168 undefinedReference("Connection", connection.getName(),
169 "element", resource.getName());
172 Process process = ((ProcessNetwork)stack.elementAt(0)).
173 getProcess(resource.getName());
174 Channel channel = ((ProcessNetwork)stack.elementAt(0)).
175 getChannel(resource.getName());
177 if (process != null) {
178 connection.setTarget(process);
180 else if (channel != null) {
181 connection.setTarget(channel);
184 undefinedReference("Connection", connection.getName(),
185 "element", resource.getName());
190 * Process the start of a connection tag in the XML.
192 * @param attributes attributes of the tag
193 * @return a Connection object
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);
204 * Process the end of a connection tag in the XML.
208 public void processConnection(Stack<Object> stack) {
209 Connection connection = (Connection) stack.pop();
210 ProcessNetwork pn = (ProcessNetwork)stack.peek();
211 pn.getConnectionList().add(connection);
215 * Process the start of a port tag in the XML.
217 * @param attributes attributes of the tag
218 * @return a Port object
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");
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);
235 port = new Port(name);
238 if (basename != null)
239 port.setBasename(basename);
241 port.setBasename(name);
243 if (range != null) port.setRange(range);
249 * Process the end of a port tag in the XML.
253 public void processPort(Stack<Object> stack) {
254 Port port = (Port)stack.pop();
255 Resource resource = (Resource)stack.peek();
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);
264 Process process = ((ProcessNetwork)stack.elementAt(0)).
265 getProcess(resource.getName());
266 Channel channel = ((ProcessNetwork)stack.elementAt(0)).
267 getChannel(resource.getName());
269 Resource refResource = null;
271 if (process != null) {
272 refResource = process;
274 else if (channel != null) {
275 refResource = channel;
278 undefinedReference("Connection",
279 ((Connection)stack.peek()).getName(),
280 "element", resource.getName());
283 Port refPort = refResource.getPort(port.getName());
285 if (refPort == null) {
286 undefinedReference("Connection",
287 ((Connection)stack.peek()).getName(),
288 "port", port.getName());
291 // Put real port back on the stack. Will be popped in
292 // processOrigin and processTarget respectively
299 * Process the start of a channel tag in the XML.
301 * @param attributes attributes of the tag
302 * @return a Channel object
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);
317 * Process the end of a channel tag in the XML.
321 public void processChannel(Stack<Object> stack) {
322 Channel channel = (Channel) stack.pop();
323 ProcessNetwork r = (ProcessNetwork)stack.peek();
324 r.getChannelList().add(channel);
328 * Process the end of a configuration tag in the XML.
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);
340 * Process the start of a configuration tag in the XML.
342 * @param attributes attributes of the tag
343 * @return a Configuration object
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);
350 code.setValue(value);
355 * Process the end of a profiling tag in the XML.
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);
367 * Process the start of a profiling tag in the XML.
369 * @param attributes attributes of the tag
370 * @return a profiling object
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);
377 code.setValue(value);
382 * Process the start of a channel source tag in the XML.
384 * @param attributes attributes of the tag
385 * @return a SourceCode object
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);
398 * Process the end of a source tag in the XML.
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);
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));
425 * Process the end of a variable tag in the XML.
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);
437 * Write an error message and terminate program in case an
438 * processnetwork element is referenced which does not exist.
440 * @param elementType type of processnetwork element in which the error
442 * @param elementName name of processnetwork element in which the error
444 * @param referenceType type of referenced processnetwork element
445 * @param referenceName name of referenced processnetwork element
447 protected void undefinedReference(String elementType,
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.");