1 | // WSDL generated classes |
---|
2 | import edu.isi.www.fedd_types.*; |
---|
3 | import edu.isi.www.topdl.*; |
---|
4 | |
---|
5 | import java.io.*; |
---|
6 | |
---|
7 | import java.util.*; |
---|
8 | |
---|
9 | import javax.xml.parsers.*; |
---|
10 | |
---|
11 | import org.xml.sax.*; |
---|
12 | import org.xml.sax.helpers.*; |
---|
13 | |
---|
14 | class ParseTopdl { |
---|
15 | /** Parser instance */ |
---|
16 | protected XMLReader xr; |
---|
17 | |
---|
18 | protected class TopdlHandler extends DefaultHandler { |
---|
19 | protected String topName; |
---|
20 | |
---|
21 | // Topology parameters |
---|
22 | protected TopologyType topo; |
---|
23 | protected ElementType element; |
---|
24 | protected ComputerType comp; |
---|
25 | protected TestbedType tb; |
---|
26 | protected SegmentType seg; |
---|
27 | protected OtherType other; |
---|
28 | |
---|
29 | // Many elements have a name |
---|
30 | protected String name; |
---|
31 | |
---|
32 | // CPU parameters |
---|
33 | protected String type; |
---|
34 | protected int ncpus; |
---|
35 | |
---|
36 | // Operatingsystem parameters |
---|
37 | protected String version; |
---|
38 | protected String distribution; |
---|
39 | protected String distributionversion; |
---|
40 | |
---|
41 | // Software parameters |
---|
42 | protected String install; |
---|
43 | protected String location; |
---|
44 | |
---|
45 | // Storage parameters |
---|
46 | protected double amount; |
---|
47 | protected PersistenceType persistence; |
---|
48 | |
---|
49 | // Interface Parameters |
---|
50 | protected Vector<String> ifsubs; |
---|
51 | protected edu.isi.www.topdl.CapacityType cap; |
---|
52 | protected LatencyType lat; |
---|
53 | |
---|
54 | // Capacity parameters |
---|
55 | protected double rate; |
---|
56 | protected edu.isi.www.topdl.KindType kind; |
---|
57 | |
---|
58 | // Latency parameters (shares kind) |
---|
59 | protected double time; |
---|
60 | |
---|
61 | // Testbed parameters (shares interfaces, type) |
---|
62 | protected String uri; |
---|
63 | |
---|
64 | // Segment parameters (shares interfaces, type, uri) |
---|
65 | protected IDType id; |
---|
66 | // IDType parameters |
---|
67 | protected byte[] uuid; |
---|
68 | protected byte[] fedid; |
---|
69 | protected String id_uri; |
---|
70 | protected String localname; |
---|
71 | protected String kerberosUsername; |
---|
72 | protected boolean inID; |
---|
73 | |
---|
74 | // Attribute parameters |
---|
75 | protected String aname; |
---|
76 | protected String aval; |
---|
77 | |
---|
78 | protected Vector<ElementType> elements; |
---|
79 | protected Vector<SubstrateType> subs; |
---|
80 | protected Vector<AttributeType> attrs; |
---|
81 | protected Vector<CpuType> cpus; |
---|
82 | protected Vector<OperatingsystemType> oses; |
---|
83 | protected Vector<SoftwareType> software; |
---|
84 | protected Vector<StorageType> storage; |
---|
85 | protected Vector<InterfaceType> interfaces; |
---|
86 | |
---|
87 | protected Set<String> attrElements; |
---|
88 | protected Stack<Vector<AttributeType>> attrStack; |
---|
89 | protected char[] c; |
---|
90 | |
---|
91 | TopdlHandler(String top) { |
---|
92 | topName = top; |
---|
93 | topo = null; |
---|
94 | element = null; |
---|
95 | comp = null; |
---|
96 | tb = null; |
---|
97 | seg = null; |
---|
98 | other = null; |
---|
99 | |
---|
100 | type = null; |
---|
101 | name = null; |
---|
102 | ncpus = 1; |
---|
103 | |
---|
104 | version = distribution = distributionversion = null; |
---|
105 | |
---|
106 | install = location = null; |
---|
107 | |
---|
108 | amount = 0.0; |
---|
109 | persistence = null; |
---|
110 | |
---|
111 | ifsubs = new Vector<String>(); |
---|
112 | cap = null; |
---|
113 | lat = null; |
---|
114 | |
---|
115 | rate = 0.0; |
---|
116 | kind = null; |
---|
117 | |
---|
118 | time = 0.0; |
---|
119 | |
---|
120 | uri = null; |
---|
121 | |
---|
122 | id = null; |
---|
123 | |
---|
124 | uuid = fedid = null; |
---|
125 | id_uri = localname = kerberosUsername = null; |
---|
126 | inID = false; |
---|
127 | |
---|
128 | aname = aval = null; |
---|
129 | |
---|
130 | elements = new Vector<ElementType>(); |
---|
131 | subs = new Vector<SubstrateType>(); |
---|
132 | attrs = new Vector<AttributeType>(); |
---|
133 | oses = new Vector<OperatingsystemType>(); |
---|
134 | software = new Vector<SoftwareType>(); |
---|
135 | storage = new Vector<StorageType>(); |
---|
136 | interfaces = new Vector<InterfaceType>(); |
---|
137 | |
---|
138 | attrElements = new TreeSet<String>(); |
---|
139 | for (String e : new String[] { |
---|
140 | topName, "computer", "cpu", "os", "software", "storage", |
---|
141 | "interface", "segment", "testbed", "other", "substrates" |
---|
142 | }) attrElements.add(e); |
---|
143 | |
---|
144 | attrStack = new Stack<Vector<AttributeType>>(); |
---|
145 | c = new char[0]; |
---|
146 | } |
---|
147 | |
---|
148 | public void startElement(String u, String l, String qn, Attributes a) |
---|
149 | throws SAXException { |
---|
150 | |
---|
151 | c = new char[0]; |
---|
152 | if ( attrElements.contains(qn) ) { |
---|
153 | attrStack.push(attrs); |
---|
154 | attrs = new Vector<AttributeType>(); |
---|
155 | } |
---|
156 | if (qn.equals("id")) inID = true; |
---|
157 | if (qn.equals("cpu")) { |
---|
158 | String n = a.getValue("count"); |
---|
159 | if ( n != null) ncpus = Integer.valueOf(n); |
---|
160 | else ncpus = 1; |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | public void endElement(String u, String l, String qn) |
---|
165 | throws SAXException { |
---|
166 | |
---|
167 | if (qn.equals(topName)) { |
---|
168 | topo = new TopologyType("1.0", |
---|
169 | subs.toArray(new SubstrateType[subs.size()]), |
---|
170 | elements.toArray(new ElementType[elements.size()]), |
---|
171 | attrs.toArray(new AttributeType[attrs.size()])); |
---|
172 | elements = new Vector<ElementType>(); |
---|
173 | subs = new Vector<SubstrateType>(); |
---|
174 | attrs = attrStack.pop(); |
---|
175 | } |
---|
176 | else if (qn.equals("element")) { |
---|
177 | elements.add(new ElementType(comp, tb, seg, other)); |
---|
178 | comp = null; |
---|
179 | tb = null; |
---|
180 | seg = null; |
---|
181 | other = null; |
---|
182 | } |
---|
183 | else if (qn.equals("computer")) { |
---|
184 | comp = new ComputerType(name, |
---|
185 | cpus.toArray(new CpuType[cpus.size()]), |
---|
186 | oses.toArray(new OperatingsystemType[oses.size()]), |
---|
187 | software.toArray(new SoftwareType[software.size()]), |
---|
188 | storage.toArray(new StorageType[storage.size()]), |
---|
189 | interfaces.toArray( |
---|
190 | new InterfaceType[interfaces.size()]), |
---|
191 | attrs.toArray(new AttributeType[attrs.size()])); |
---|
192 | name = null; |
---|
193 | cpus = new Vector<CpuType>(); |
---|
194 | oses = new Vector<OperatingsystemType>(); |
---|
195 | software = new Vector<SoftwareType>(); |
---|
196 | storage = new Vector<StorageType>(); |
---|
197 | interfaces = new Vector<InterfaceType>(); |
---|
198 | attrs = attrStack.pop(); |
---|
199 | } |
---|
200 | else if (qn.equals("cpu")) { |
---|
201 | cpus.add(new CpuType(type, |
---|
202 | attrs.toArray(new AttributeType[attrs.size()]), ncpus)); |
---|
203 | type = null; |
---|
204 | attrs = attrStack.pop(); |
---|
205 | ncpus = 1; |
---|
206 | } |
---|
207 | else if (qn.equals("type")) { type = new String(c).trim(); } |
---|
208 | else if (qn.equals("os")) { |
---|
209 | oses.add(new OperatingsystemType(name, version, |
---|
210 | distribution, distributionversion, |
---|
211 | attrs.toArray(new AttributeType[attrs.size()]))); |
---|
212 | name = version = distribution = distributionversion = null; |
---|
213 | attrs = attrStack.pop(); |
---|
214 | } |
---|
215 | else if ( qn.equals("version")) { version = new String(c).trim(); } |
---|
216 | else if ( qn.equals("distribution")) { |
---|
217 | distribution = new String(c).trim(); |
---|
218 | } |
---|
219 | else if ( qn.equals("distributionversion")) { |
---|
220 | distributionversion = new String(c).trim(); |
---|
221 | } |
---|
222 | else if (qn.equals("software")) { |
---|
223 | software.add(new SoftwareType(location, install, |
---|
224 | attrs.toArray(new AttributeType[attrs.size()]))); |
---|
225 | location = install = null; |
---|
226 | attrs = attrStack.pop(); |
---|
227 | } |
---|
228 | else if ( qn.equals("location")) { |
---|
229 | location = new String(c).trim(); |
---|
230 | } |
---|
231 | else if ( qn.equals("install")) { install = new String(c).trim(); } |
---|
232 | else if (qn.equals("storage")) { |
---|
233 | storage.add(new StorageType(amount, persistence, |
---|
234 | attrs.toArray(new AttributeType[attrs.size()]))); |
---|
235 | amount = 0.0; |
---|
236 | persistence = null; |
---|
237 | attrs = attrStack.pop(); |
---|
238 | } |
---|
239 | else if ( qn.equals("amount")) { |
---|
240 | amount = Double.valueOf(new String(c)); |
---|
241 | } |
---|
242 | else if ( qn.equals("persistence")) { |
---|
243 | persistence = PersistenceType.fromValue(new String(c)); |
---|
244 | } |
---|
245 | else if (qn.equals("interface")) { |
---|
246 | interfaces.add(new InterfaceType( |
---|
247 | ifsubs.toArray(new String[ifsubs.size()]), |
---|
248 | name, |
---|
249 | cap, lat, |
---|
250 | attrs.toArray(new AttributeType[attrs.size()]))); |
---|
251 | ifsubs = new Vector<String>(); |
---|
252 | cap = null; |
---|
253 | lat = null; |
---|
254 | attrs = attrStack.pop(); |
---|
255 | } |
---|
256 | else if (qn.equals("substrate")) { |
---|
257 | ifsubs.add(new String(c).trim()); |
---|
258 | } |
---|
259 | else if (qn.equals("capacity")) { |
---|
260 | cap = new edu.isi.www.topdl.CapacityType(rate, kind); |
---|
261 | rate = 0.0; |
---|
262 | kind = null; |
---|
263 | } |
---|
264 | else if ( qn.equals("rate")) { |
---|
265 | rate = Double.valueOf(new String(c)); |
---|
266 | } |
---|
267 | else if ( qn.equals("kind")) { |
---|
268 | kind = edu.isi.www.topdl.KindType.fromValue(new String(c)); |
---|
269 | } |
---|
270 | else if (qn.equals("latency")) { |
---|
271 | lat = new LatencyType(time, kind); |
---|
272 | time = 0.0; |
---|
273 | kind = null; |
---|
274 | } |
---|
275 | else if ( qn.equals("time")) { |
---|
276 | time = Double.valueOf(new String(c)); |
---|
277 | } |
---|
278 | else if (qn.equals("testbed")) { |
---|
279 | tb = new TestbedType(uri, type, |
---|
280 | interfaces.toArray( |
---|
281 | new InterfaceType[interfaces.size()]), |
---|
282 | attrs.toArray(new AttributeType[attrs.size()])); |
---|
283 | uri = type = null; |
---|
284 | interfaces = new Vector<InterfaceType>(); |
---|
285 | attrs = attrStack.pop(); |
---|
286 | } |
---|
287 | else if (qn.equals("uri")) { |
---|
288 | if (inID) id_uri = new String(c).trim(); |
---|
289 | else uri = new String(c).trim(); |
---|
290 | } |
---|
291 | else if (qn.equals("segment")) { |
---|
292 | seg = new SegmentType(id, type, uri, |
---|
293 | interfaces.toArray( |
---|
294 | new InterfaceType[interfaces.size()]), |
---|
295 | attrs.toArray(new AttributeType[attrs.size()])); |
---|
296 | id = null; |
---|
297 | type = uri = null; |
---|
298 | interfaces = new Vector<InterfaceType>(); |
---|
299 | attrs = attrStack.pop(); |
---|
300 | } |
---|
301 | else if (qn.equals("id")) { |
---|
302 | id = new IDType(uuid, fedid, id_uri, localname, |
---|
303 | kerberosUsername); |
---|
304 | uuid = null; |
---|
305 | fedid = null; |
---|
306 | id_uri = null; |
---|
307 | localname = null; |
---|
308 | kerberosUsername = null; |
---|
309 | inID = false; |
---|
310 | } |
---|
311 | else if (qn.equals("uuid")) { |
---|
312 | uuid = new String(c).trim().getBytes(); |
---|
313 | } |
---|
314 | else if (qn.equals("fedid")) { |
---|
315 | fedid = new String(c).trim().getBytes(); |
---|
316 | } |
---|
317 | else if (qn.equals("localname")) { |
---|
318 | localname = new String(c).trim(); |
---|
319 | } |
---|
320 | else if (qn.equals("kerberosUsername")) { |
---|
321 | kerberosUsername = new String(c).trim(); |
---|
322 | } |
---|
323 | else if (qn.equals("other")) { |
---|
324 | other = new OtherType( |
---|
325 | interfaces.toArray( |
---|
326 | new InterfaceType[interfaces.size()]), |
---|
327 | attrs.toArray(new AttributeType[attrs.size()])); |
---|
328 | interfaces = new Vector<InterfaceType>(); |
---|
329 | attrs = attrStack.pop(); |
---|
330 | } |
---|
331 | else if (qn.equals("substrates")) { |
---|
332 | subs.add(new SubstrateType(name, cap, lat, |
---|
333 | attrs.toArray(new AttributeType[attrs.size()]))); |
---|
334 | name = null; |
---|
335 | cap = null; |
---|
336 | lat = null; |
---|
337 | attrs = attrStack.pop(); |
---|
338 | } |
---|
339 | else if (qn.equals("attribute")) { |
---|
340 | if ( aname != null && aval != null ) { |
---|
341 | attrs.add(new AttributeType(aname, aval)); |
---|
342 | aname = aval = null; |
---|
343 | } |
---|
344 | else { aname = new String(c).trim(); } |
---|
345 | } |
---|
346 | else if ( qn.equals("value")) { aval = new String(c).trim(); } |
---|
347 | else if ( qn.equals("name")) { name = new String(c).trim(); } |
---|
348 | |
---|
349 | c = new char[0]; |
---|
350 | } |
---|
351 | |
---|
352 | /** |
---|
353 | * Collect text. |
---|
354 | */ |
---|
355 | public void characters(char[] ch, int s, int l) { |
---|
356 | char[] nc = new char[c.length + l]; |
---|
357 | System.arraycopy(c, 0, nc, 0, c.length); |
---|
358 | System.arraycopy(ch, s, nc, c.length, l); |
---|
359 | c = nc; |
---|
360 | } |
---|
361 | } |
---|
362 | |
---|
363 | public ParseTopdl(InputStream s) |
---|
364 | throws IOException, SAXException, ParserConfigurationException { |
---|
365 | xr = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); |
---|
366 | |
---|
367 | xr.parse(new InputSource(s)); |
---|
368 | } |
---|
369 | |
---|
370 | |
---|
371 | } |
---|