001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.internal.ws.wsdl.parser;
027:
028: import java.io.IOException;
029: import java.util.Iterator;
030:
031: import javax.xml.namespace.QName;
032:
033: import org.w3c.dom.Element;
034:
035: import com.sun.tools.internal.ws.wsdl.document.soap.SOAPAddress;
036: import com.sun.tools.internal.ws.wsdl.document.soap.SOAPBinding;
037: import com.sun.tools.internal.ws.wsdl.document.soap.SOAPBody;
038: import com.sun.tools.internal.ws.wsdl.document.soap.SOAPConstants;
039: import com.sun.tools.internal.ws.wsdl.document.soap.SOAPFault;
040: import com.sun.tools.internal.ws.wsdl.document.soap.SOAPHeader;
041: import com.sun.tools.internal.ws.wsdl.document.soap.SOAPHeaderFault;
042: import com.sun.tools.internal.ws.wsdl.document.soap.SOAPOperation;
043: import com.sun.tools.internal.ws.wsdl.document.soap.SOAPStyle;
044: import com.sun.tools.internal.ws.wsdl.document.soap.SOAPUse;
045: import com.sun.tools.internal.ws.wsdl.framework.Extensible;
046: import com.sun.tools.internal.ws.wsdl.framework.Extension;
047: import com.sun.tools.internal.ws.wsdl.framework.ParserContext;
048: import com.sun.tools.internal.ws.wsdl.framework.WriterContext;
049: import com.sun.tools.internal.ws.util.xml.XmlUtil;
050:
051: /**
052: * The SOAP extension handler for WSDL.
053: *
054: * @author WS Development Team
055: */
056: public class SOAPExtensionHandler extends ExtensionHandlerBase {
057:
058: public SOAPExtensionHandler() {
059: }
060:
061: public String getNamespaceURI() {
062: return Constants.NS_WSDL_SOAP;
063: }
064:
065: protected boolean handleDefinitionsExtension(ParserContext context,
066: Extensible parent, Element e) {
067: Util.fail("parsing.invalidExtensionElement", e.getTagName(), e
068: .getNamespaceURI());
069: return false; // keep compiler happy
070: }
071:
072: protected boolean handleTypesExtension(ParserContext context,
073: Extensible parent, Element e) {
074: Util.fail("parsing.invalidExtensionElement", e.getTagName(), e
075: .getNamespaceURI());
076: return false; // keep compiler happy
077: }
078:
079: protected SOAPBinding getSOAPBinding() {
080: return new SOAPBinding();
081: }
082:
083: protected boolean handleBindingExtension(ParserContext context,
084: Extensible parent, Element e) {
085: if (XmlUtil.matchesTagNS(e, getBindingQName())) {
086: context.push();
087: context.registerNamespaces(e);
088:
089: SOAPBinding binding = getSOAPBinding();
090:
091: // NOTE - the "transport" attribute is required according to section 3.3 of the WSDL 1.1 spec,
092: // but optional according to the schema in appendix A 4.2 of the same document!
093: String transport = Util.getRequiredAttribute(e,
094: Constants.ATTR_TRANSPORT);
095: binding.setTransport(transport);
096:
097: String style = XmlUtil.getAttributeOrNull(e,
098: Constants.ATTR_STYLE);
099: if (style != null) {
100: if (style.equals(Constants.ATTRVALUE_RPC)) {
101: binding.setStyle(SOAPStyle.RPC);
102: } else if (style.equals(Constants.ATTRVALUE_DOCUMENT)) {
103: binding.setStyle(SOAPStyle.DOCUMENT);
104: } else {
105: Util.fail("parsing.invalidAttributeValue",
106: Constants.ATTR_STYLE, style);
107: }
108: }
109: parent.addExtension(binding);
110: context.pop();
111: context.fireDoneParsingEntity(getBindingQName(), binding);
112: return true;
113: } else {
114: Util.fail("parsing.invalidExtensionElement",
115: e.getTagName(), e.getNamespaceURI());
116: return false; // keep compiler happy
117: }
118: }
119:
120: protected boolean handleOperationExtension(ParserContext context,
121: Extensible parent, Element e) {
122: if (XmlUtil.matchesTagNS(e, getOperationQName())) {
123: context.push();
124: context.registerNamespaces(e);
125:
126: SOAPOperation operation = new SOAPOperation();
127:
128: String soapAction = XmlUtil.getAttributeOrNull(e,
129: Constants.ATTR_SOAP_ACTION);
130: if (soapAction != null) {
131: operation.setSOAPAction(soapAction);
132: }
133:
134: String style = XmlUtil.getAttributeOrNull(e,
135: Constants.ATTR_STYLE);
136: if (style != null) {
137: if (style.equals(Constants.ATTRVALUE_RPC)) {
138: operation.setStyle(SOAPStyle.RPC);
139: } else if (style.equals(Constants.ATTRVALUE_DOCUMENT)) {
140: operation.setStyle(SOAPStyle.DOCUMENT);
141: } else {
142: Util.fail("parsing.invalidAttributeValue",
143: Constants.ATTR_STYLE, style);
144: }
145: }
146: parent.addExtension(operation);
147: context.pop();
148: context.fireDoneParsingEntity(getOperationQName(),
149: operation);
150: return true;
151: } else {
152: Util.fail("parsing.invalidExtensionElement",
153: e.getTagName(), e.getNamespaceURI());
154: return false; // keep compiler happy
155: }
156: }
157:
158: protected boolean handleInputExtension(ParserContext context,
159: Extensible parent, Element e) {
160: return handleInputOutputExtension(context, parent, e);
161: }
162:
163: protected boolean handleOutputExtension(ParserContext context,
164: Extensible parent, Element e) {
165: return handleInputOutputExtension(context, parent, e);
166: }
167:
168: protected boolean handleMIMEPartExtension(ParserContext context,
169: Extensible parent, Element e) {
170: return handleInputOutputExtension(context, parent, e);
171: }
172:
173: protected boolean handleInputOutputExtension(ParserContext context,
174: Extensible parent, Element e) {
175: if (XmlUtil.matchesTagNS(e, getBodyQName())) {
176: context.push();
177: context.registerNamespaces(e);
178:
179: SOAPBody body = new SOAPBody();
180:
181: String use = XmlUtil.getAttributeOrNull(e,
182: Constants.ATTR_USE);
183: if (use != null) {
184: if (use.equals(Constants.ATTRVALUE_LITERAL)) {
185: body.setUse(SOAPUse.LITERAL);
186: } else if (use.equals(Constants.ATTRVALUE_ENCODED)) {
187: body.setUse(SOAPUse.ENCODED);
188: } else {
189: Util.fail("parsing.invalidAttributeValue",
190: Constants.ATTR_USE, use);
191: }
192: }
193:
194: String namespace = XmlUtil.getAttributeOrNull(e,
195: Constants.ATTR_NAMESPACE);
196: if (namespace != null) {
197: body.setNamespace(namespace);
198: }
199:
200: String encodingStyle = XmlUtil.getAttributeOrNull(e,
201: Constants.ATTR_ENCODING_STYLE);
202: if (encodingStyle != null) {
203: body.setEncodingStyle(encodingStyle);
204: }
205:
206: String parts = XmlUtil.getAttributeOrNull(e,
207: Constants.ATTR_PARTS);
208: if (parts != null) {
209: body.setParts(parts);
210: }
211:
212: parent.addExtension(body);
213: context.pop();
214: context.fireDoneParsingEntity(getBodyQName(), body);
215: return true;
216: } else if (XmlUtil.matchesTagNS(e, getHeaderQName())) {
217: context.push();
218: context.registerNamespaces(e);
219:
220: SOAPHeader header = new SOAPHeader();
221:
222: String use = XmlUtil.getAttributeOrNull(e,
223: Constants.ATTR_USE);
224: if (use != null) {
225: if (use.equals(Constants.ATTRVALUE_LITERAL)) {
226: header.setUse(SOAPUse.LITERAL);
227: } else if (use.equals(Constants.ATTRVALUE_ENCODED)) {
228: header.setUse(SOAPUse.ENCODED);
229: } else {
230: Util.fail("parsing.invalidAttributeValue",
231: Constants.ATTR_USE, use);
232: }
233: }
234:
235: String namespace = XmlUtil.getAttributeOrNull(e,
236: Constants.ATTR_NAMESPACE);
237: if (namespace != null) {
238: header.setNamespace(namespace);
239: }
240:
241: String encodingStyle = XmlUtil.getAttributeOrNull(e,
242: Constants.ATTR_ENCODING_STYLE);
243: if (encodingStyle != null) {
244: header.setEncodingStyle(encodingStyle);
245: }
246:
247: String part = XmlUtil.getAttributeOrNull(e,
248: Constants.ATTR_PART);
249: if (part != null) {
250: header.setPart(part);
251: }
252:
253: String messageAttr = XmlUtil.getAttributeOrNull(e,
254: Constants.ATTR_MESSAGE);
255: if (messageAttr != null) {
256: header.setMessage(context
257: .translateQualifiedName(messageAttr));
258: }
259:
260: for (Iterator iter = XmlUtil.getAllChildren(e); iter
261: .hasNext();) {
262: Element e2 = Util.nextElement(iter);
263: if (e2 == null)
264: break;
265:
266: if (XmlUtil.matchesTagNS(e2, getHeaderfaultQName())) {
267: context.push();
268: context.registerNamespaces(e);
269:
270: SOAPHeaderFault headerfault = new SOAPHeaderFault();
271:
272: String use2 = XmlUtil.getAttributeOrNull(e2,
273: Constants.ATTR_USE);
274: if (use2 != null) {
275: if (use2.equals(Constants.ATTRVALUE_LITERAL)) {
276: headerfault.setUse(SOAPUse.LITERAL);
277: } else if (use
278: .equals(Constants.ATTRVALUE_ENCODED)) {
279: headerfault.setUse(SOAPUse.ENCODED);
280: } else {
281: Util.fail("parsing.invalidAttributeValue",
282: Constants.ATTR_USE, use2);
283: }
284: }
285:
286: String namespace2 = XmlUtil.getAttributeOrNull(e2,
287: Constants.ATTR_NAMESPACE);
288: if (namespace2 != null) {
289: headerfault.setNamespace(namespace2);
290: }
291:
292: String encodingStyle2 = XmlUtil.getAttributeOrNull(
293: e2, Constants.ATTR_ENCODING_STYLE);
294: if (encodingStyle2 != null) {
295: headerfault.setEncodingStyle(encodingStyle2);
296: }
297:
298: String part2 = XmlUtil.getAttributeOrNull(e2,
299: Constants.ATTR_PART);
300: if (part2 != null) {
301: headerfault.setPart(part2);
302: }
303:
304: String messageAttr2 = XmlUtil.getAttributeOrNull(
305: e2, Constants.ATTR_MESSAGE);
306: if (messageAttr2 != null) {
307: headerfault.setMessage(context
308: .translateQualifiedName(messageAttr2));
309: }
310:
311: header.add(headerfault);
312: context.pop();
313: } else {
314: Util.fail("parsing.invalidElement",
315: e2.getTagName(), e2.getNamespaceURI());
316: }
317: }
318:
319: parent.addExtension(header);
320: context.pop();
321: context.fireDoneParsingEntity(getHeaderQName(), header);
322: return true;
323: } else {
324: Util.fail("parsing.invalidExtensionElement",
325: e.getTagName(), e.getNamespaceURI());
326: return false; // keep compiler happy
327: }
328: }
329:
330: protected boolean handleFaultExtension(ParserContext context,
331: Extensible parent, Element e) {
332: if (XmlUtil.matchesTagNS(e, getFaultQName())) {
333: context.push();
334: context.registerNamespaces(e);
335:
336: SOAPFault fault = new SOAPFault();
337:
338: String name = XmlUtil.getAttributeOrNull(e,
339: Constants.ATTR_NAME);
340: if (name != null) {
341: fault.setName(name);
342: }
343:
344: String use = XmlUtil.getAttributeOrNull(e,
345: Constants.ATTR_USE);
346: if (use != null) {
347: if (use.equals(Constants.ATTRVALUE_LITERAL)) {
348: fault.setUse(SOAPUse.LITERAL);
349: } else if (use.equals(Constants.ATTRVALUE_ENCODED)) {
350: fault.setUse(SOAPUse.ENCODED);
351: } else {
352: Util.fail("parsing.invalidAttributeValue",
353: Constants.ATTR_USE, use);
354: }
355: }
356:
357: String namespace = XmlUtil.getAttributeOrNull(e,
358: Constants.ATTR_NAMESPACE);
359: if (namespace != null) {
360: fault.setNamespace(namespace);
361: }
362:
363: String encodingStyle = XmlUtil.getAttributeOrNull(e,
364: Constants.ATTR_ENCODING_STYLE);
365: if (encodingStyle != null) {
366: fault.setEncodingStyle(encodingStyle);
367: }
368:
369: parent.addExtension(fault);
370: context.pop();
371: context.fireDoneParsingEntity(getFaultQName(), fault);
372: return true;
373: } else {
374: Util.fail("parsing.invalidExtensionElement",
375: e.getTagName(), e.getNamespaceURI());
376: return false; // keep compiler happy
377: }
378: }
379:
380: protected boolean handleServiceExtension(ParserContext context,
381: Extensible parent, Element e) {
382: Util.fail("parsing.invalidExtensionElement", e.getTagName(), e
383: .getNamespaceURI());
384: return false; // keep compiler happy
385: }
386:
387: protected boolean handlePortExtension(ParserContext context,
388: Extensible parent, Element e) {
389: if (XmlUtil.matchesTagNS(e, getAddressQName())) {
390: context.push();
391: context.registerNamespaces(e);
392:
393: SOAPAddress address = new SOAPAddress();
394:
395: String location = Util.getRequiredAttribute(e,
396: Constants.ATTR_LOCATION);
397: address.setLocation(location);
398:
399: parent.addExtension(address);
400: context.pop();
401: context.fireDoneParsingEntity(getAddressQName(), address);
402: return true;
403: } else {
404: Util.fail("parsing.invalidExtensionElement",
405: e.getTagName(), e.getNamespaceURI());
406: return false; // keep compiler happy
407: }
408: }
409:
410: public void doHandleExtension(WriterContext context,
411: Extension extension) throws IOException {
412: // NOTE - this ugliness can be avoided by moving all the XML parsing/writing code
413: // into the document classes themselves
414: if (extension instanceof SOAPAddress) {
415: SOAPAddress address = (SOAPAddress) extension;
416: context.writeStartTag(address.getElementName());
417: context.writeAttribute(Constants.ATTR_LOCATION, address
418: .getLocation());
419: context.writeEndTag(address.getElementName());
420: } else if (extension instanceof SOAPBinding) {
421: SOAPBinding binding = (SOAPBinding) extension;
422: context.writeStartTag(binding.getElementName());
423: context.writeAttribute(Constants.ATTR_TRANSPORT, binding
424: .getTransport());
425: String style = (binding.getStyle() == null ? null
426: : (binding.getStyle() == SOAPStyle.DOCUMENT ? Constants.ATTRVALUE_DOCUMENT
427: : Constants.ATTRVALUE_RPC));
428: context.writeAttribute(Constants.ATTR_STYLE, style);
429: context.writeEndTag(binding.getElementName());
430: } else if (extension instanceof SOAPBody) {
431: SOAPBody body = (SOAPBody) extension;
432: context.writeStartTag(body.getElementName());
433: context.writeAttribute(Constants.ATTR_ENCODING_STYLE, body
434: .getEncodingStyle());
435: context.writeAttribute(Constants.ATTR_PARTS, body
436: .getParts());
437: String use = (body.getUse() == null ? null
438: : (body.getUse() == SOAPUse.LITERAL ? Constants.ATTRVALUE_LITERAL
439: : Constants.ATTRVALUE_ENCODED));
440: context.writeAttribute(Constants.ATTR_USE, use);
441: context.writeAttribute(Constants.ATTR_NAMESPACE, body
442: .getNamespace());
443: context.writeEndTag(body.getElementName());
444: } else if (extension instanceof SOAPFault) {
445: SOAPFault fault = (SOAPFault) extension;
446: context.writeStartTag(fault.getElementName());
447: context
448: .writeAttribute(Constants.ATTR_NAME, fault
449: .getName());
450: context.writeAttribute(Constants.ATTR_ENCODING_STYLE, fault
451: .getEncodingStyle());
452: String use = (fault.getUse() == null ? null
453: : (fault.getUse() == SOAPUse.LITERAL ? Constants.ATTRVALUE_LITERAL
454: : Constants.ATTRVALUE_ENCODED));
455: context.writeAttribute(Constants.ATTR_USE, use);
456: context.writeAttribute(Constants.ATTR_NAMESPACE, fault
457: .getNamespace());
458: context.writeEndTag(fault.getElementName());
459: } else if (extension instanceof SOAPHeader) {
460: SOAPHeader header = (SOAPHeader) extension;
461: context.writeStartTag(header.getElementName());
462: context.writeAttribute(Constants.ATTR_MESSAGE, header
463: .getMessage());
464: context.writeAttribute(Constants.ATTR_PART, header
465: .getPart());
466: context.writeAttribute(Constants.ATTR_ENCODING_STYLE,
467: header.getEncodingStyle());
468: String use = (header.getUse() == null ? null
469: : (header.getUse() == SOAPUse.LITERAL ? Constants.ATTRVALUE_LITERAL
470: : Constants.ATTRVALUE_ENCODED));
471: context.writeAttribute(Constants.ATTR_USE, use);
472: context.writeAttribute(Constants.ATTR_NAMESPACE, header
473: .getNamespace());
474: context.writeEndTag(header.getElementName());
475: } else if (extension instanceof SOAPHeaderFault) {
476: SOAPHeaderFault headerfault = (SOAPHeaderFault) extension;
477: context.writeStartTag(headerfault.getElementName());
478: context.writeAttribute(Constants.ATTR_MESSAGE, headerfault
479: .getMessage());
480: context.writeAttribute(Constants.ATTR_PART, headerfault
481: .getPart());
482: context.writeAttribute(Constants.ATTR_ENCODING_STYLE,
483: headerfault.getEncodingStyle());
484: String use = (headerfault.getUse() == null ? null
485: : (headerfault.getUse() == SOAPUse.LITERAL ? Constants.ATTRVALUE_LITERAL
486: : Constants.ATTRVALUE_ENCODED));
487: context.writeAttribute(Constants.ATTR_USE, use);
488: context.writeAttribute(Constants.ATTR_NAMESPACE,
489: headerfault.getNamespace());
490: context.writeEndTag(headerfault.getElementName());
491: } else if (extension instanceof SOAPOperation) {
492: SOAPOperation operation = (SOAPOperation) extension;
493: context.writeStartTag(operation.getElementName());
494: context.writeAttribute(Constants.ATTR_SOAP_ACTION,
495: operation.getSOAPAction());
496: String style = (operation.getStyle() == null ? null
497: : (operation.isDocument() ? Constants.ATTRVALUE_DOCUMENT
498: : Constants.ATTRVALUE_RPC));
499: context.writeAttribute(Constants.ATTR_STYLE, style);
500: context.writeEndTag(operation.getElementName());
501: } else {
502: throw new IllegalArgumentException();
503: }
504: }
505:
506: /* (non-Javadoc)
507: * @see ExtensionHandlerBase#handlePortTypeExtension(ParserContext, Extensible, org.w3c.dom.Element)
508: */
509: protected boolean handlePortTypeExtension(ParserContext context,
510: Extensible parent, Element e) {
511: Util.fail("parsing.invalidExtensionElement", e.getTagName(), e
512: .getNamespaceURI());
513: return false; // keep compiler happy
514: }
515:
516: protected QName getBodyQName() {
517: return SOAPConstants.QNAME_BODY;
518: }
519:
520: protected QName getHeaderQName() {
521: return SOAPConstants.QNAME_HEADER;
522: }
523:
524: protected QName getHeaderfaultQName() {
525: return SOAPConstants.QNAME_HEADERFAULT;
526: }
527:
528: protected QName getOperationQName() {
529: return SOAPConstants.QNAME_OPERATION;
530: }
531:
532: protected QName getFaultQName() {
533: return SOAPConstants.QNAME_FAULT;
534: }
535:
536: protected QName getAddressQName() {
537: return SOAPConstants.QNAME_ADDRESS;
538: }
539:
540: protected QName getBindingQName() {
541: return SOAPConstants.QNAME_BINDING;
542: }
543: }
|