comparison WebServiceToolWorkflow/ParserForWADL/src/lsdis/WADLParser.java @ 0:d5cd409b8a18 default tip

Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
author ganjoo
date Tue, 07 Jun 2011 18:00:50 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d5cd409b8a18
1 /*
2 * Copyright (c) 2009 Srikalyan Swayampakula.. All rights reserved.
3 *
4 * Author : Srikalyan Swayampakula. .
5 * Name of the File : WADLParser.java .
6 * Created on : Nov 22, 2009 at 5:30:47 PM .
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer.
15 * 2. Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials
18 * provided with the distribution.
19 * 3. Neither the name of the University of Georgia nor the names
20 * of its contributors may be used to endorse or promote
21 * products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
35 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38 package lsdis;
39
40 import java.io.File;
41 import java.net.URI;
42 import java.net.URL;
43 import java.util.ArrayList;
44 import java.util.List;
45 import org.jdom.Attribute;
46 import org.jdom.Document;
47 import org.jdom.Element;
48 import org.jdom.Namespace;
49 import org.jdom.input.SAXBuilder;
50 import org.xml.sax.Attributes;
51
52 /**
53 *
54 * @author Srikalyan Swayampakula.
55 */
56 public class WADLParser
57 {
58
59 static Document currentDocument;
60 static Element rootElement;
61 static Namespace wadlNamespace;
62 public Application application;
63
64 public WADLParser(URL fileURL) throws Exception
65 {
66 currentDocument = generateDocumentation(fileURL);
67 rootElement = currentDocument.getRootElement();
68 wadlNamespace = rootElement.getNamespace();
69 application = getApplication();
70 }
71
72 public Application getApplicationOfWADL()
73 {
74 return application;
75 }
76
77 private Document generateDocumentation(URL fileName) throws Exception
78 {
79 Document doc = null;
80 SAXBuilder builder = new SAXBuilder();
81 //doc = builder.build(new File(fileName.toURI()));
82 doc = builder.build(fileName);
83 return doc;
84 }
85
86 private Application getApplication() throws Exception
87 {
88 List<Doc> docs = getDocs(rootElement);
89 List<Resources> resources = getResources(rootElement);
90 Grammar grammar = getGrammar(rootElement);
91 return new Application(resources, docs, grammar, null);
92 }
93
94 /***
95 * used to get docs list for an element eg. applicaiton.
96 * @param e
97 * @return
98 */
99 private List<Doc> getDocs(Element e)
100 {
101 if (e == null)
102 {
103 return null;
104 }
105 List<Doc> docs = null;
106 List<Element> xmlDoc = e.getChildren(WADLConstant.DOC, wadlNamespace);
107 if (xmlDoc != null)
108 {
109 docs = new ArrayList<Doc>();
110 for (Element e1 : xmlDoc)
111 {
112 Doc tempDoc = getDoc(e1);
113 if (tempDoc != null)
114 {
115 docs.add(tempDoc);
116 }
117 }
118 }
119 return docs;
120 }
121
122 private Doc getDoc(Element e)
123 {
124 if (e == null)
125 {
126 return null;
127 }
128 String title = e.getAttributeValue(WADLConstant.DOC_TITLE);
129
130 String lang = e.getAttributeValue(WADLConstant.DOC_LANG);
131 if (lang == null)
132 {
133 List<Attribute> temp = e.getAttributes();
134 for (Attribute temp1 : temp)
135 {
136 if((temp1.getName()).equals(WADLConstant.DOC_LANG))
137 lang=temp1.getValue();
138 }
139 }
140 String innerText = e.getText();
141 return new Doc(title, lang, innerText);
142 }
143
144 private Grammar getGrammar(Element e) throws Exception
145 {
146 if (e == null)
147 {
148 return null;
149 }
150 List<Doc> docs = getDocs(e);
151 List<Include> includes = getIncludes(e);
152 return new Grammar(docs, includes);
153 }
154
155 private List<Include> getIncludes(Element e) throws Exception
156 {
157 if (e == null)
158 {
159 return null;
160 }
161 List<Include> includes = null;
162 List<Element> xmlIncludes = e.getChildren(WADLConstant.INCLUDE, wadlNamespace);
163 if (xmlIncludes != null)
164 {
165 includes = new ArrayList<Include>();
166 for (Element xmlInclude : xmlIncludes)
167 {
168 Include tempInclude = getInclude(xmlInclude);
169 if (tempInclude != null)
170 {
171 includes.add(tempInclude);
172 }
173 }
174 }
175 return includes;
176 }
177
178 private Include getInclude(Element e) throws Exception
179 {
180 if (e == null)
181 {
182 return null;
183 }
184 List<Doc> docs = getDocs(e);
185 URI href = null;
186 String tempHref = e.getAttributeValue(WADLConstant.INCLUDE_HREF);
187 if (tempHref != null)
188 {
189 href = new URI(tempHref);
190 }
191 return new Include(docs, href);
192 }
193
194 private List<Resources> getResources(Element e) throws Exception
195 {
196 if (e == null)
197 {
198 return null;
199 }
200
201 List<Resources> resources = null;
202 List<Element> xmlResources = e.getChildren(WADLConstant.RESOURCES, wadlNamespace);
203 if (xmlResources != null)
204 {
205 resources = new ArrayList<Resources>();
206 for (Element tempResources : xmlResources)
207 {
208 Resources tempResource = getResourcesInstance(tempResources);
209 if (tempResource != null)
210 {
211 resources.add(tempResource);
212 }
213 }
214 }
215
216 return resources;
217
218 }
219
220 private Resources getResourcesInstance(Element e) throws Exception
221 {
222 if (e == null)
223 {
224 return null;
225 }
226 String tempBase = e.getAttributeValue(WADLConstant.RESOURCES_BASE);//, wadlNamespace);
227 URI base = null;
228 if (tempBase != null)
229 {
230 base = new URI(tempBase);
231 }
232 List<Doc> docs = getDocs(e);
233 List<Resource> subResources = getSubResources(e);
234 return new Resources(docs, subResources, base);
235 }
236
237 private List<Resource> getSubResources(Element e) throws Exception
238 {
239 if (e == null)
240 {
241 return null;
242
243 }
244 List<Resource> subResources = null;
245 List<Element> xmlSubResources = e.getChildren(WADLConstant.RESOURCE, wadlNamespace);
246 if (xmlSubResources != null)
247 {
248 subResources = new ArrayList<Resource>();
249 for (Element xmlSubResource : xmlSubResources)
250 {
251 Resource subResource = getSubResource(xmlSubResource);
252 if (subResource != null)
253 {
254 subResources.add(subResource);
255 }
256 }
257 }
258
259 return subResources;
260
261 }
262
263 private Resource getSubResource(Element e) throws Exception
264 {
265 if (e == null)
266 {
267 return null;
268 }
269 List<Doc> docs = getDocs(e);
270 List<Param> params = getParams(e);
271 List<Method> methods = getMethods(e);
272 List<Resource> resources = getSubResources(e);
273 String id = e.getAttributeValue(WADLConstant.RESOURCE_ID);//, wadlNamespace);
274 String queryType = e.getAttributeValue(WADLConstant.RESOURCE_QUERY_TYPE);//, wadlNamespace);
275 String path = e.getAttributeValue(WADLConstant.RESOURCE_PATH);
276 //System.out.println("the path is " + path + " wadlNampeSpace is " + wadlNamespace);
277 return new Resource(docs, params, methods, resources, id, queryType, path);
278 }
279
280 private List<Param> getParams(Element e) throws Exception
281 {
282 if (e == null)
283 {
284 return null;
285
286 }
287 List<Param> params = null;
288 List<Element> xmlParams = e.getChildren(WADLConstant.PARAM, wadlNamespace);
289 if (xmlParams != null)
290 {
291 params = new ArrayList<Param>();
292 for (Element xmlParam : xmlParams)
293 {
294 Param param = getParam(xmlParam);
295 if (param != null)
296 {
297 params.add(param);
298
299 }
300 }
301 }
302 return params;
303 }
304
305 private Param getParam(Element e) throws Exception
306 {
307 if (e == null)
308 {
309 return null;
310 }
311 List<Doc> docs = getDocs(e);
312 List<Option> options = getOptions(e);
313 Link link = getLink(e.getChild(WADLConstant.LINK, wadlNamespace));
314 String tempHref = e.getAttributeValue(WADLConstant.PARAM_HREF);//, wadlNamespace);
315 URI href = null;
316 if (tempHref != null)
317 {
318 href = new URI(tempHref);
319 }
320 String name = e.getAttributeValue(WADLConstant.PARAM_NAME);//, wadlNamespace);
321 String style = e.getAttributeValue(WADLConstant.PARAM_STYLE);//, wadlNamespace);
322 String id = e.getAttributeValue(WADLConstant.PARAM_ID);//, wadlNamespace);
323 String type = e.getAttributeValue(WADLConstant.PARAM_TYPE);//, wadlNamespace);
324 String default1 = e.getAttributeValue(WADLConstant.PARAM_DEFAULT);//, wadlNamespace);
325 String tempRequired = e.getAttributeValue(WADLConstant.PARAM_REQUIRED);//, wadlNamespace);
326 boolean required = false;
327 if (tempRequired != null)
328 {
329 required = Boolean.valueOf(tempRequired);
330 }
331 String tempRepeating = e.getAttributeValue(WADLConstant.PARAM_REPEATING);//, wadlNamespace);
332 boolean repeating = false;
333 if (tempRepeating != null)
334 {
335 repeating = Boolean.valueOf(tempRepeating);
336 }
337 String fixed = e.getAttributeValue(WADLConstant.PARAM_FIXED);//, wadlNamespace);
338 String path = e.getAttributeValue(WADLConstant.PARAM_PATH);//, wadlNamespace);
339 return new Param(docs, options, link, href, name, style, id, type, default1, fixed, path,required);
340 }
341
342 private List<Option> getOptions(Element e)
343 {
344 System.out.println(e.getName());
345 if (e == null)
346 {
347 return null;
348 }
349 List<Option> options = null;
350 List<Element> xmlOptions = e.getChildren(WADLConstant.OPTION, wadlNamespace);
351 if (xmlOptions != null)
352 {
353 options = new ArrayList<Option>();
354 for (Element xmlOption : xmlOptions)
355 {
356 Option option = getOption(xmlOption);
357 if (option != null)
358 {
359 options.add(option);
360 }
361 }
362 }
363 return options;
364 }
365
366 private Option getOption(Element e)
367 {
368 if (e == null)
369 {
370 return null;
371 }
372 List<Doc> docs = getDocs(e);
373 String value = e.getAttributeValue(WADLConstant.OPTION_VALUE);//, wadlNamespace);
374 String mediaType = e.getAttributeValue(WADLConstant.OPTION_MEDIA_TYPE);//, wadlNamespace);
375 return new Option(docs, value, mediaType);
376 }
377
378 private Link getLink(Element e)
379 {
380 if (e == null)
381 {
382 return null;
383 }
384 List<Doc> docs = getDocs(e);
385 String resourceType = e.getAttributeValue(WADLConstant.LINK_RESOURCE_TYPE);//, wadlNamespace);
386 String rel = e.getAttributeValue(WADLConstant.LINK_REL);//, wadlNamespace);
387 String rev = e.getAttributeValue(WADLConstant.LINK_REV);//, wadlNamespace);
388 return new Link(docs, resourceType, rel, rev);
389 }
390
391 private List<Method> getMethods(Element e) throws Exception
392 {
393 if (e == null)
394 {
395 return null;
396 }
397 List<Method> methods = null;
398 List<Element> xmlMethods = e.getChildren(WADLConstant.METHOD, wadlNamespace);
399 if (xmlMethods != null)
400 {
401 methods = new ArrayList<Method>();
402 for (Element xmlMethod : xmlMethods)
403 {
404 Method method = getMethod(xmlMethod);
405 if (method != null)
406 {
407 methods.add(method);
408 }
409 }
410 }
411 return methods;
412 }
413
414 private Method getMethod(Element e) throws Exception
415 {
416 if (e == null)
417 {
418 return null;
419 }
420 List<Doc> docs = getDocs(e);
421 Request request = getRequest(e.getChild(WADLConstant.REQUEST, wadlNamespace));
422 List<Response> responses = getResponses(e);
423 String id = e.getAttributeValue(WADLConstant.METHOD_ID);//, wadlNamespace);
424 String tempHref = e.getAttributeValue(WADLConstant.METHOD_HREF);//, wadlNamespace);
425 URI href = null;
426 if (tempHref != null)
427 {
428 href = new URI(tempHref);
429 }
430 String name = e.getAttributeValue(WADLConstant.METHOD_NAME);//, wadlNamespace);
431 return new Method(docs, request, responses, id, name, href);
432 }
433
434 private Request getRequest(Element e) throws Exception
435 {
436 if (e == null)
437 {
438 return null;
439 }
440 List<Doc> docs = getDocs(e);
441 List<Param> params = getParams(e);
442 List<Representation> representations = getRepresentations(e);
443 return new Request(docs, params, representations);
444 }
445
446 private List<Representation> getRepresentations(Element e) throws Exception
447 {
448 if (e == null)
449 {
450 return null;
451 }
452 List<Representation> representations = null;
453 List<Element> xmlRepresentations = e.getChildren(WADLConstant.REPRESENTATION, wadlNamespace);
454 if (xmlRepresentations != null)
455 {
456 representations = new ArrayList<Representation>();
457 for (Element xmlRepresentation : xmlRepresentations)
458 {
459 Representation representation = getRepresentation(e);
460 if (representation != null)
461 {
462 representations.add(representation);
463 }
464 }
465 }
466 return representations;
467 }
468
469 private Representation getRepresentation(Element e) throws Exception
470 {
471 if (e == null)
472 {
473 return null;
474 }
475 List<Doc> docs = getDocs(e);
476 List<Param> params = getParams(e);
477 String id = e.getAttributeValue(WADLConstant.REPRESENTATION_ID);//, wadlNamespace);
478 String element = e.getAttributeValue(WADLConstant.REPRESENTATION_ELEMENT);//, wadlNamespace);
479 String mediaType = e.getAttributeValue(WADLConstant.REPRESENTATION_MEDIA_TYPE);//, wadlNamespace);
480 String tempHref = e.getAttributeValue(WADLConstant.REPRESENTATION_HREF);//, wadlNamespace);
481 URI href = null;
482 if (tempHref != null)
483 {
484 href = new URI(tempHref);
485 }
486 String profile = e.getAttributeValue(WADLConstant.REPRESENTATION_PROFILE);//, wadlNamespace);
487 return new Representation(docs, params, id, element, mediaType, href, profile);
488 }
489
490 private List<Response> getResponses(Element e) throws Exception
491 {
492 if (e == null)
493 {
494 return null;
495 }
496 List<Response> responses = null;
497 List<Element> xmlResponses = e.getChildren(WADLConstant.RESPONSE, wadlNamespace);
498 if (xmlResponses != null)
499 {
500 responses = new ArrayList<Response>();
501 for (Element xmlResponse : xmlResponses)
502 {
503 Response response = getResponse(e);
504 if (response != null)
505 {
506 responses.add(response);
507 }
508 }
509 }
510 return responses;
511 }
512
513 private Response getResponse(Element e) throws Exception
514 {
515 if (e == null)
516 {
517 return null;
518 }
519 List<Doc> docs = getDocs(e);
520 List<Param> params = getParams(e);
521 List<Representation> representations = getRepresentations(e);
522 String tempStatus = e.getAttributeValue(WADLConstant.RESPONSE_STATUS);//, wadlNamespace);
523 int status = -1;
524 if (tempStatus != null)
525 {
526 status = Integer.parseInt(tempStatus);
527 }
528 return new Response(docs, params, representations, status);
529
530 }
531 }