comparison WebServiceToolWorkflow/lib/SAWADLParser/src/edu/uga/cs/lsdis/meteors/wadls/xml/WADLWriterImpl.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 * (c) Copyright IBM Corp 2001, 2005
3 */
4
5 package edu.uga.cs.lsdis.meteors.wadls.xml;
6
7 import java.io.*;
8 import java.net.URISyntaxException;
9 import java.util.*;
10 import javax.xml.namespace.*;
11 import javax.xml.parsers.*;
12 import org.w3c.dom.*;
13 import org.xml.sax.*;
14
15 import javax.wadls.*;
16 import javax.wadls.extensions.*;
17 import javax.wadls.factory.*;
18 import javax.wadls.xml.*;
19
20 import edu.uga.cs.lsdis.meteors.wadls.*;
21 import edu.uga.cs.lsdis.meteors.wadls.util.*;
22 import edu.uga.cs.lsdis.meteors.wadls.util.xml.*;
23
24 /**
25 * This class describes a collection of methods
26 * that allow a WSDL-S model to be written to a writer
27 * in an XML format that follows the WSDL schema.
28 *
29 * @author Zixin Wu (wuzixin@uga.edu)
30 * @author Matthew J. Duftler
31 * @author Nirmal Mukhi
32 */
33 public class WADLWriterImpl implements WADLWriter
34 {
35 /**
36 * Sets the specified feature to the specified value.
37 * <p>
38 * There are no minimum features that must be supported.
39 * <p>
40 * All feature names must be fully-qualified, Java package style. All
41 * names starting with javax.wsdls. are reserved for features defined
42 * by the JWSDL specification. It is recommended that implementation-
43 * specific features be fully-qualified to match the package name
44 * of that implementation. For example: com.abc.featureName
45 *
46 * @param name the name of the feature to be set.
47 * @param value the value to set the feature to.
48 * @throws IllegalArgumentException if the feature name is not recognized.
49 * @see #getFeature(String)
50 */
51 public void setFeature(String name, boolean value)
52 throws IllegalArgumentException
53 {
54 if (name == null)
55 {
56 throw new IllegalArgumentException("Feature name must not be null.");
57 }
58 else
59 {
60 throw new IllegalArgumentException("Feature name '" + name +
61 "' not recognized.");
62 }
63 }
64
65 /**
66 * Gets the value of the specified feature.
67 *
68 * @param name the name of the feature to get the value of.
69 * @throws IllegalArgumentException if the feature name is not recognized.
70 * @see #setFeature(String, boolean)
71 */
72 public boolean getFeature(String name) throws IllegalArgumentException
73 {
74 if (name == null)
75 {
76 throw new IllegalArgumentException("Feature name must not be null.");
77 }
78 else
79 {
80 throw new IllegalArgumentException("Feature name '" + name +
81 "' not recognized.");
82 }
83 }
84
85 protected void printDefinition(Application def, PrintWriter pw)
86 throws WADLSException, URISyntaxException
87 {
88 if (def == null)
89 {
90 return;
91 }
92
93 if (def.getPrefix(Constants.NS_URI_WADL) == null)
94 {
95 String prefix = "wsdl";
96 int subscript = 0;
97
98 while (def.getNamespace(prefix) != null)
99 {
100 prefix = "wsdl" + subscript++;
101 }
102
103 def.addNamespace(prefix, Constants.NS_URI_WADL);
104 }
105
106 if (def.getPrefix(Constants.NS_URI_WADLS) == null)
107 {
108 String prefix = Constants.PREFIX_WSDLS;
109 int subscript = 0;
110
111 while (def.getNamespace(prefix) != null)
112 {
113 prefix = Constants.PREFIX_WSDLS + subscript++;
114 }
115
116 def.addNamespace(prefix, Constants.NS_URI_WADLS);
117 }
118
119 String tagName =
120 DOMUtils.getQualifiedValue(Constants.NS_URI_WADL,
121 Constants.ELEM_DEFINITIONS,
122 def);
123
124 pw.print('<' + tagName);
125
126 QName name = def.getQName();
127 String targetNamespace = def.getTargetNamespace();
128 Map namespaces = def.getNamespaces();
129
130 if (name != null)
131 {
132 DOMUtils.printAttribute(Constants.ATTR_NAME, name.getLocalPart(), pw);
133 }
134
135 DOMUtils.printAttribute(Constants.ATTR_TARGET_NAMESPACE,
136 targetNamespace,
137 pw);
138
139 printNamespaceDeclarations(namespaces, pw);
140
141 pw.println('>');
142
143 printDocumentation(def.getDocumentationElement(), pw);
144 printIncludes(def.getIncludes(), def, pw);
145 printTypes(def.getParams(), def, pw);
146 printResources(def.getResources(), def, pw);
147
148
149
150
151
152 pw.println("</" + tagName + '>');
153
154 pw.flush();
155 }
156
157
158
159
160
161
162
163
164 protected void printResources(Map portTypes,
165 Application def,
166 PrintWriter pw)
167 throws WADLSException, URISyntaxException
168 {
169 if (portTypes != null)
170 {
171 String tagName =
172 DOMUtils.getQualifiedValue(Constants.NS_URI_WADL,
173 Constants.ELEM_RESOURCE,
174 def);
175 Iterator portTypeIterator = portTypes.values().iterator();
176
177 while (portTypeIterator.hasNext())
178 {
179 Resource portType = (Resource)portTypeIterator.next();
180
181 if (!portType.isUndefined())
182 {
183 pw.print(" <" + tagName);
184
185 QName name = portType.getQName();
186
187 if (name != null)
188 {
189 DOMUtils.printAttribute(Constants.ATTR_NAME,
190 name.getLocalPart(),
191 pw);
192 }
193
194
195
196 pw.println('>');
197
198 printDocumentation(portType.getDocumentationElement(), pw);
199 printOperations(portType.getMethods(), def, pw);
200 pw.println(" </" + tagName + '>');
201 }
202 }
203 }
204 }
205
206 protected void printOperations(List operations,
207 Application def,
208 PrintWriter pw)
209 throws WADLSException, URISyntaxException
210 {
211 if (operations != null)
212 {
213 String tagName =
214 DOMUtils.getQualifiedValue(Constants.NS_URI_WADL,
215 Constants.ELEM_OPERATION,
216 def);
217 Iterator operationIterator = operations.iterator();
218
219 while (operationIterator.hasNext())
220 {
221 Method operation = (Method)operationIterator.next();
222
223 if (!operation.isUndefined())
224 {
225 pw.print(" <" + tagName);
226
227 DOMUtils.printAttribute(Constants.ATTR_NAME,
228 operation.getName(),
229 pw);
230 //write modelReference
231 ModelReference mr = operation.getModelReference();
232 if (mr != null){
233 if (mr.getNamespace() != null) //has namespaceURI
234 mr.setPrefix(def.getPrefix(mr.getNamespace().toString()));
235 else //no namespaceURI, try prefix
236 mr.setNamespace(def.getNamespace(mr.getPrefix()));
237 if (mr.getNamespace() == null) //error
238 throw new WADLSException(WADLSException.UNBOUND_PREFIX,
239 "Cannot find the namespace for the prefix " +
240 mr.getPrefix());
241 DOMUtils.printQualifiedAttribute(Constants.Q_ATTR_MODELREF,
242 mr.value(),
243 def,
244 pw);
245 }
246
247 DOMUtils.printAttribute(Constants.ATTR_PARAMETER_ORDER,
248 StringUtils.getNMTokens(operation.getParameterOrdering()),
249 pw);
250
251 pw.println('>');
252
253 printDocumentation(operation.getDocumentationElement(), pw);
254 // Must be OperationType.REQUEST_RESPONSE.
255 printInput(operation.getRequest(), def, pw);
256 printOutput(operation.getResponse(), def, pw);
257
258 printPreCondition(operation.getPreCondition(), def, pw);
259 printEffect(operation.getEffect(), def, pw);
260
261
262
263
264
265
266
267 pw.println(" </" + tagName + '>');
268 }
269 }
270 }
271 }
272
273 protected void printPreCondition(PreCondition preCondition,
274 Application def,
275 PrintWriter pw)
276 throws WADLSException, URISyntaxException
277 {
278 if (preCondition != null)
279 {
280 String tagName =
281 DOMUtils.getQualifiedValue(Constants.NS_URI_WADLS,
282 Constants.ELEM_PRECON,
283 def);
284
285 pw.print(" <" + tagName);
286
287 DOMUtils.printAttribute(Constants.ATTR_NAME, preCondition.getName(), pw);
288
289 //write modelReference
290 ModelReference mr = preCondition.getModelReference();
291 if (mr != null){
292 if (mr.getNamespace() != null) //has namespaceURI
293 mr.setPrefix(def.getPrefix(mr.getNamespace().toString()));
294 else //no namespaceURI, try prefix
295 mr.setNamespace(def.getNamespace(mr.getPrefix()));
296 if (mr.getNamespace() == null) //error
297 throw new WADLSException(WADLSException.UNBOUND_PREFIX,
298 "Cannot find the namespace for the prefix " +
299 mr.getPrefix());
300 DOMUtils.printQualifiedAttribute(Constants.Q_ATTR_MODELREF,
301 mr.value(),
302 def,
303 pw);
304 }
305
306 DOMUtils.printAttribute(Constants.ATTR_EXPRESSION, preCondition.getExpression(), pw);
307
308 // printExtensibilityAttributes(PreCondition.class, preCondition, def, pw);
309
310 Element docEl = preCondition.getDocumentationElement();
311
312 if (docEl == null)
313 {
314 pw.println("/>");
315 }
316 else
317 {
318 pw.print('>');
319
320 printDocumentation(docEl, pw);
321
322 pw.println("</" + tagName + '>');
323 }
324 }
325 }
326 protected void printEffect(Effect effect,
327 Application def,
328 PrintWriter pw)
329 throws WADLSException, URISyntaxException
330 {
331 if (effect != null)
332 {
333 String tagName =
334 DOMUtils.getQualifiedValue(Constants.NS_URI_WADLS,
335 Constants.ELEM_EFFECT,
336 def);
337
338 pw.print(" <" + tagName);
339
340 DOMUtils.printAttribute(Constants.ATTR_NAME, effect.getName(), pw);
341
342 //write modelReference
343 ModelReference mr = effect.getModelReference();
344 if (mr != null){
345 if (mr.getNamespace() != null) //has namespaceURI
346 mr.setPrefix(def.getPrefix(mr.getNamespace().toString()));
347 else //no namespaceURI, try prefix
348 mr.setNamespace(def.getNamespace(mr.getPrefix()));
349 if (mr.getNamespace() == null) //error
350 throw new WADLSException(WADLSException.UNBOUND_PREFIX,
351 "Cannot find the namespace for the prefix " +
352 mr.getPrefix());
353 DOMUtils.printQualifiedAttribute(Constants.Q_ATTR_MODELREF,
354 mr.value(),
355 def,
356 pw);
357 }
358
359 DOMUtils.printAttribute(Constants.ATTR_EXPRESSION, effect.getExpression(), pw);
360
361 // printExtensibilityAttributes(Effect.class, effect, def, pw);
362
363 Element docEl = effect.getDocumentationElement();
364
365 if (docEl == null)
366 {
367 pw.println("/>");
368 }
369 else
370 {
371 pw.print('>');
372
373 printDocumentation(docEl, pw);
374
375 pw.println("</" + tagName + '>');
376 }
377 }
378 }
379
380 protected void printInput(Request input,
381 Application def,
382 PrintWriter pw)
383 throws WADLSException
384 {
385 if (input != null)
386 {
387 String tagName =
388 DOMUtils.getQualifiedValue(Constants.NS_URI_WADL,
389 Constants.ELEM_INPUT,
390 def);
391
392 pw.print(" <" + tagName);
393
394 DOMUtils.printAttribute(Constants.ATTR_NAME, input.getName(), pw);
395
396 Element docEl = input.getDocumentationElement();
397
398 if (docEl == null)
399 {
400 pw.println("/>");
401 }
402 else
403 {
404 pw.println('>');
405
406 printDocumentation(docEl, pw);
407
408 pw.println(" </" + tagName + '>');
409 }
410 }
411 }
412
413 protected void printOutput(Response output,
414 Application def,
415 PrintWriter pw)
416 throws WADLSException
417 {
418 if (output != null)
419 {
420 String tagName =
421 DOMUtils.getQualifiedValue(Constants.NS_URI_WADL,
422 Constants.ELEM_OUTPUT,
423 def);
424
425 pw.print(" <" + tagName);
426
427 DOMUtils.printAttribute(Constants.ATTR_NAME, output.getName(), pw);
428
429 Element docEl = output.getDocumentationElement();
430
431 if (docEl == null)
432 {
433 pw.println("/>");
434 }
435 else
436 {
437 pw.println('>');
438
439 printDocumentation(docEl, pw);
440
441 pw.println(" </" + tagName + '>');
442 }
443 }
444 }
445
446
447
448
449
450 protected void printDocumentation(Element docElement,
451 PrintWriter pw)
452 throws WADLSException
453 {
454 if (docElement != null)
455 {
456 DOM2Writer.serializeAsXML(docElement, pw);
457
458 pw.println();
459 }
460 }
461
462 protected void printTypes(Params types, Application def, PrintWriter pw)
463 throws WADLSException
464 {
465 if (types != null)
466 {
467 String tagName =
468 DOMUtils.getQualifiedValue(Constants.NS_URI_WADL,
469 Constants.ELEM_TYPES,
470 def);
471 pw.print(" <" + tagName);
472
473 pw.println('>');
474
475 printDocumentation(types.getDocumentationElement(), pw);
476
477
478
479
480
481 pw.println(" </" + tagName + '>');
482 }
483 }
484
485 protected void printIncludes(Map imports, Application def, PrintWriter pw)
486 throws WADLSException
487 {
488 if (imports != null)
489 {
490 String tagName =
491 DOMUtils.getQualifiedValue(Constants.NS_URI_WADL,
492 Constants.ELEM_INCLUDE,
493 def);
494 Iterator importListIterator = imports.values().iterator();
495
496 while (importListIterator.hasNext())
497 {
498 List importList = (List)importListIterator.next();
499 Iterator importIterator = importList.iterator();
500
501 while (importIterator.hasNext())
502 {
503 Include importDef = (Include)importIterator.next();
504
505 pw.print(" <" + tagName);
506
507 DOMUtils.printAttribute(Constants.ATTR_NAMESPACE,
508 importDef.getNamespaceURI(),
509 pw);
510 DOMUtils.printAttribute(Constants.ATTR_LOCATION,
511 importDef.getLocationURI(),
512 pw);
513
514
515
516 Element docEl = importDef.getDocumentationElement();
517
518 if (docEl == null)
519 {
520 pw.println("/>");
521 }
522 else
523 {
524 pw.println('>');
525
526 printDocumentation(docEl, pw);
527
528 pw.println(" </" + tagName + '>');
529 }
530 }
531 }
532 }
533 }
534
535 protected void printNamespaceDeclarations(Map namespaces,
536 PrintWriter pw)
537 throws WADLSException
538 {
539 if (namespaces != null)
540 {
541 Set keys = namespaces.keySet();
542 Iterator keyIterator = keys.iterator();
543
544 while (keyIterator.hasNext())
545 {
546 String prefix = (String)keyIterator.next();
547
548 if (prefix == null)
549 {
550 prefix = "";
551 }
552
553 DOMUtils.printAttribute(Constants.ATTR_XMLNS +
554 (!prefix.equals("") ? ":" + prefix : ""),
555 (String)namespaces.get(prefix),
556 pw);
557 }
558 }
559 }
560
561
562 private static Document getDocument(InputSource inputSource,
563 String desc) throws WADLSException
564 {
565 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
566
567 factory.setNamespaceAware(true);
568 factory.setValidating(false);
569
570 try
571 {
572 DocumentBuilder builder = factory.newDocumentBuilder();
573 Document doc = builder.parse(inputSource);
574
575 return doc;
576 }
577 catch (Throwable t)
578 {
579 throw new WADLSException(WADLSException.XSDPARSER_ERROR,
580 "Problem parsing '" + desc + "'.",
581 t);
582 }
583 }
584
585 /**
586 * Return a document generated from the specified WSDL model.
587 * @throws URISyntaxException
588 */
589 public Document getDocument(Application wsdlDef) throws WADLSException, URISyntaxException
590 {
591 StringWriter sw = new StringWriter();
592 PrintWriter pw = new PrintWriter(sw);
593
594 writeWADL(wsdlDef, pw);
595
596 StringReader sr = new StringReader(sw.toString());
597 InputSource is = new InputSource(sr);
598
599 return getDocument(is, "- WSDL Document -");
600 }
601
602 /**
603 * Write the specified WSDL definition to the specified Writer.
604 *
605 * @param wsdlDef the WSDL definition to be written.
606 * @param sink the Writer to write the xml to.
607 * @throws URISyntaxException
608 */
609 public void writeWADL(Application wsdlDef, Writer sink)
610 throws WADLSException, URISyntaxException
611 {
612 PrintWriter pw = new PrintWriter(sink);
613 String javaEncoding = (sink instanceof OutputStreamWriter)
614 ? ((OutputStreamWriter)sink).getEncoding()
615 : null;
616
617 String xmlEncoding = DOM2Writer.java2XMLEncoding(javaEncoding);
618
619 if (xmlEncoding == null)
620 {
621 throw new WADLSException(WADLSException.CONFIGURATION_ERROR,
622 "Unsupported Java encoding for writing " +
623 "wsdl file: '" + javaEncoding + "'.");
624 }
625
626 pw.println(Constants.XML_DECL_START +
627 xmlEncoding +
628 Constants.XML_DECL_END);
629
630 printDefinition(wsdlDef, pw);
631 }
632
633 /**
634 * Write the specified WSDL definition to the specified OutputStream.
635 *
636 * @param wsdlDef the WSDL definition to be written.
637 * @param sink the OutputStream to write the xml to.
638 * @throws URISyntaxException
639 */
640 public void writeWADL(Application wsdlDef, OutputStream sink)
641 throws WADLSException, URISyntaxException
642 {
643 Writer writer = null;
644
645 try
646 {
647 writer = new OutputStreamWriter(sink, "UTF8");
648 }
649 catch (UnsupportedEncodingException e)
650 {
651 e.printStackTrace();
652
653 writer = new OutputStreamWriter(sink);
654 }
655
656 writeWADL(wsdlDef, writer);
657 }
658
659 /**
660 * A test driver.
661 *<code>
662 *<pre>Usage:</pre>
663 *<p>
664 *<pre> java edu.uga.cs.lsdis.meteors.wsdls.xml.WSDLWriterImpl filename|URL</pre>
665 *<p>
666 *<pre> This test driver simply reads a WSDL document into a model
667 * (using a WSDLReader), and then serializes it back to
668 * standard out. In effect, it performs a round-trip test on
669 * the specified WSDL document.</pre>
670 * @throws URISyntaxException
671 */
672 public static void main(String[] argv) throws WADLSException, URISyntaxException
673 {
674 if (argv.length == 1)
675 {
676 WADLFactory wsdlFactory = WADLFactory.newInstance();
677 WADLReader wsdlReader = wsdlFactory.newWADLReader();
678 WADLWriter wsdlWriter = wsdlFactory.newWADLWriter();
679
680 wsdlWriter.writeWADL(wsdlReader.readWADL(null, argv[0]), System.out);
681 }
682 else
683 {
684 System.err.println("Usage:");
685 System.err.println();
686 System.err.println(" java " + WADLWriterImpl.class.getName() +
687 " filename|URL");
688 System.err.println();
689 System.err.println("This test driver simply reads a WSDL document " +
690 "into a model (using a WSDLReader), and then " +
691 "serializes it back to standard out. In effect, " +
692 "it performs a round-trip test on the specified " +
693 "WSDL document.");
694 }
695 }
696 }