dol: initial dol commit
[jump.git] / dol / src / dol / visitor / xml / PNXmlVisitor.java
1 /* $Id: PNXmlVisitor.java 1 2010-02-24 13:03:05Z haidw $ */
2 package dol.visitor.xml;
3
4 import dol.datamodel.XmlTag;
5 import dol.datamodel.pn.Channel;
6 import dol.datamodel.pn.Configuration;
7 import dol.datamodel.pn.Connection;
8 import dol.datamodel.pn.Port;
9 import dol.datamodel.pn.Process;
10 import dol.datamodel.pn.ProcessNetwork;
11 import dol.datamodel.pn.ProfilingConfiguration;
12 import dol.datamodel.pn.SourceCode;
13 import dol.util.CodePrintString;
14 import dol.visitor.PNVisitor;
15
16 /**
17  * This is a class for a visitor that is used to generate
18  * a schema compatible XML for process network.
19  */
20 public class PNXmlVisitor extends PNVisitor {
21
22     /**
23      * Constructor.
24      *
25      * @param stringBuffer buffer where the result is stored
26      */
27     public PNXmlVisitor(StringBuffer stringBuffer) {
28         _ps = new CodePrintString(stringBuffer);
29     }
30
31     /**
32      *
33      * @param x process network that needs to be rendered.
34      */
35     public void visitComponent(ProcessNetwork x) {
36         String xmlns = dol.util.SchemaLocation.
37                 getProcessNetworkNamespace();
38         String xsiLocation = dol.util.SchemaLocation.
39                 getProcessNetworkSchemaLocation();
40
41
42         _ps.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
43         _ps.printOpeningTag(_xt.getPNTag());
44         _ps.println(" xmlns=\"" + xmlns
45                     + "\" xmlns:xsi=\"http://www.w3.org/2001/"
46                     + "XMLSchema-instance\""
47                     + System.getProperty("line.separator")
48                     + "  xsi:schemaLocation=\""
49                     + xmlns + " " + xsiLocation + "\" name=\""
50                     + x.getName() + "_annotated" + "\">");
51
52         //visit all processes
53         for (Process p : x.getProcessList()) {
54             p.accept(this);
55         }
56         _ps.println();
57
58         //visit all channels
59         for (Channel c : x.getChannelList()) {
60             c.accept(this);
61         }
62         _ps.println();
63
64         //visit all connections
65         for (Connection n : x.getConnectionList()) {
66             n.accept(this);
67         }
68
69         _ps.printClosingTag(_xt.getPNTag());
70     }
71
72     /**
73      * Print a line for the process in the correct format for XML.
74      *
75      * @param x process that needs to be rendered
76      */
77     public void visitComponent(Process x) {
78         _ps.printOpeningTag(_xt.getProcessTag());
79         _ps.println(" name=\"" + x.getName()
80                     + "\" basename=\"" + x.getBasename()
81                     + "\" range=\"" + x.getRange()
82                     + "\">");
83
84         for (Port p : x.getPortList()) {
85             p.accept(this);
86         }
87
88         for (SourceCode s : x.getSrcList()) {
89             s.accept(this);
90         }
91
92         for (Configuration c : x.getCfgList()) {
93             c.accept(this);
94         }
95
96         for (ProfilingConfiguration c : x.getProfilingList()) {
97             c.accept(this);
98         }
99
100         _ps.printClosingTag(_xt.getProcessTag());
101     }
102
103     /**
104      * Print a line for the channel in the correct format for XML.
105      *
106      * @param x channel that needs to be rendered
107      */
108     public void visitComponent(Channel x) {
109         _ps.printOpeningTag(_xt.getSWChannelTag());
110         _ps.println(" name=\"" + x.getName()
111                     + "\" basename=\"" + x.getBasename()
112                     + "\" type=\"" + x.getType()
113                     + "\" size=\"" + x.getSize()
114                     + "\">");
115
116         for (Port p : x.getPortList()) {
117             p.accept(this);
118         }
119
120         for (Configuration c : x.getCfgList()) {
121             c.accept(this);
122         }
123
124         for (ProfilingConfiguration c : x.getProfilingList()) {
125             c.accept(this);
126         }
127
128         _ps.printClosingTag(_xt.getSWChannelTag());
129     }
130
131     /**
132      * Print a line for the connection in the correct format for XML.
133      *
134      * @param x channel that needs to be rendered
135      */
136     public void visitComponent(Connection x) {
137         _ps.printOpeningTag(_xt.getConnectionTag());
138         _ps.println(" name=\"" + x.getName() + "\">");
139
140         //origin
141         _ps.printOpeningTag("origin");
142         _ps.println(" name=\"" + x.getOrigin().getName() + "\">");
143         _ps.println("          <port name=\""
144                 + x.getOriginPort().getName() + "\"/>");
145         _ps.printClosingTag("origin");
146
147         //target
148         _ps.printOpeningTag("target");
149         _ps.println(" name=\"" + x.getTarget().getName() + "\">");
150         _ps.println("          <port name=\""
151                 + x.getTargetPort().getName() + "\"/>");
152         _ps.printClosingTag("target");
153         _ps.printClosingTag("connection");
154     }
155
156     /**
157      * Print a line for the port in the correct format for XML.
158      *
159      * @param x port that needs to be rendered
160      */
161     public void visitComponent(Port x) {
162         _ps.printPrefix();
163         String s = "<" + _xt.getPortTag() 
164             + " name=\"" + x.getName()
165             + "\" basename=\"" + x.getBasename()
166             + "\" range=\"" + x.getRange()
167             + "\"";
168         if (!x.getType().equals(""))
169             s += " type=\"" + x.getType() + "\"";
170         s +=  " />";
171
172         _ps.println(s);
173     }
174
175     /**
176      * Print a line for the source code in the correct format for XML.
177      *
178      * @param x source that needs to be rendered
179      */
180     public void visitComponent(SourceCode x) {
181         _ps.printPrefix();
182         _ps.println("<" + _xt.getSourceTag()
183                     + " type=\"" + x.getType()
184                     + "\" location=\"" + x.getLocality()
185                     + "\" />");
186     }
187
188     /**
189      * Print a line for the configuration in the correct format for XML.
190      *
191      * @param x configuration that needs to be rendered
192      */
193     public void visitComponent(Configuration x) {
194         _ps.printPrefix();
195         _ps.println("<" + _xt.getConfigurationTag()
196                     + " name=\"" + x.getName()
197                     + "\" value=\"" + x.getValue()
198                     + "\" />");
199     }
200     
201     /**
202      * Print a line for the profiling configuration in the correct format for XML.
203      *
204      * @param x configuration that needs to be rendered
205      */
206     public void visitComponent(ProfilingConfiguration x) {
207         _ps.printPrefix();
208         _ps.println("<" + _xt.getProfilingTag()
209                     + " name=\"" + x.getName()
210                     + "\" value=\"" + x.getValue()
211                     + "\" />");
212     }
213
214     protected CodePrintString _ps = null;
215     
216     protected XmlTag _xt = XmlTag.getInstance();
217 }