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.WSDLConstants;
036: import com.sun.tools.internal.ws.wsdl.document.mime.MIMEConstants;
037: import com.sun.tools.internal.ws.wsdl.document.mime.MIMEContent;
038: import com.sun.tools.internal.ws.wsdl.document.mime.MIMEMultipartRelated;
039: import com.sun.tools.internal.ws.wsdl.document.mime.MIMEPart;
040: import com.sun.tools.internal.ws.wsdl.document.mime.MIMEXml;
041: import com.sun.tools.internal.ws.wsdl.framework.Extensible;
042: import com.sun.tools.internal.ws.wsdl.framework.Extension;
043: import com.sun.tools.internal.ws.wsdl.framework.ParserContext;
044: import com.sun.tools.internal.ws.wsdl.framework.WriterContext;
045: import com.sun.tools.internal.ws.util.xml.XmlUtil;
046:
047: /**
048: * The MIME extension handler for WSDL.
049: *
050: * @author WS Development Team
051: */
052: public class MIMEExtensionHandler extends ExtensionHandler {
053:
054: public MIMEExtensionHandler() {
055: }
056:
057: public String getNamespaceURI() {
058: return Constants.NS_WSDL_MIME;
059: }
060:
061: public boolean doHandleExtension(ParserContext context,
062: Extensible parent, Element e) {
063: if (parent.getElementName().equals(WSDLConstants.QNAME_OUTPUT)) {
064: return handleInputOutputExtension(context, parent, e);
065: } else if (parent.getElementName().equals(
066: WSDLConstants.QNAME_INPUT)) {
067: return handleInputOutputExtension(context, parent, e);
068: } else if (parent.getElementName().equals(
069: MIMEConstants.QNAME_PART)) {
070: return handleMIMEPartExtension(context, parent, e);
071: } else {
072: context.fireIgnoringExtension(new QName(
073: e.getNamespaceURI(), e.getLocalName()), parent
074: .getElementName());
075: return false;
076: }
077: }
078:
079: protected boolean handleInputOutputExtension(ParserContext context,
080: Extensible parent, Element e) {
081: if (XmlUtil.matchesTagNS(e,
082: MIMEConstants.QNAME_MULTIPART_RELATED)) {
083: context.push();
084: context.registerNamespaces(e);
085:
086: MIMEMultipartRelated mpr = new MIMEMultipartRelated();
087:
088: for (Iterator iter = XmlUtil.getAllChildren(e); iter
089: .hasNext();) {
090: Element e2 = Util.nextElement(iter);
091: if (e2 == null)
092: break;
093:
094: if (XmlUtil.matchesTagNS(e2, MIMEConstants.QNAME_PART)) {
095: context.push();
096: context.registerNamespaces(e2);
097:
098: MIMEPart part = new MIMEPart();
099:
100: String name = XmlUtil.getAttributeOrNull(e2,
101: Constants.ATTR_NAME);
102: if (name != null) {
103: part.setName(name);
104: }
105:
106: for (Iterator iter2 = XmlUtil.getAllChildren(e2); iter2
107: .hasNext();) {
108: Element e3 = Util.nextElement(iter2);
109: if (e3 == null)
110: break;
111:
112: ExtensionHandler h = (ExtensionHandler) _extensionHandlers
113: .get(e3.getNamespaceURI());
114: boolean handled = false;
115: if (h != null) {
116: handled = h.doHandleExtension(context,
117: part, e3);
118: }
119:
120: if (!handled) {
121: String required = XmlUtil
122: .getAttributeNSOrNull(e3,
123: Constants.ATTR_REQUIRED,
124: Constants.NS_WSDL);
125: if (required != null
126: && required.equals(Constants.TRUE)) {
127: Util
128: .fail(
129: "parsing.requiredExtensibilityElement",
130: e3.getTagName(),
131: e3.getNamespaceURI());
132: } else {
133: context.fireIgnoringExtension(
134: new QName(e3.getNamespaceURI(),
135: e3.getLocalName()),
136: part.getElementName());
137: }
138: }
139: }
140:
141: mpr.add(part);
142: context.pop();
143: context.fireDoneParsingEntity(
144: MIMEConstants.QNAME_PART, part);
145: } else {
146: Util.fail("parsing.invalidElement",
147: e2.getTagName(), e2.getNamespaceURI());
148: }
149: }
150:
151: parent.addExtension(mpr);
152: context.pop();
153: context.fireDoneParsingEntity(
154: MIMEConstants.QNAME_MULTIPART_RELATED, mpr);
155: return true;
156: } else if (XmlUtil.matchesTagNS(e, MIMEConstants.QNAME_CONTENT)) {
157: MIMEContent content = parseMIMEContent(context, e);
158: parent.addExtension(content);
159: return true;
160: } else if (XmlUtil
161: .matchesTagNS(e, MIMEConstants.QNAME_MIME_XML)) {
162: MIMEXml mimeXml = parseMIMEXml(context, e);
163: parent.addExtension(mimeXml);
164: return true;
165: } else {
166: Util.fail("parsing.invalidExtensionElement",
167: e.getTagName(), e.getNamespaceURI());
168: return false; // keep compiler happy
169: }
170: }
171:
172: protected boolean handleMIMEPartExtension(ParserContext context,
173: Extensible parent, Element e) {
174: if (XmlUtil.matchesTagNS(e, MIMEConstants.QNAME_CONTENT)) {
175: MIMEContent content = parseMIMEContent(context, e);
176: parent.addExtension(content);
177: return true;
178: } else if (XmlUtil
179: .matchesTagNS(e, MIMEConstants.QNAME_MIME_XML)) {
180: MIMEXml mimeXml = parseMIMEXml(context, e);
181: parent.addExtension(mimeXml);
182: return true;
183: } else {
184: Util.fail("parsing.invalidExtensionElement",
185: e.getTagName(), e.getNamespaceURI());
186: return false; // keep compiler happy
187: }
188: }
189:
190: protected MIMEContent parseMIMEContent(ParserContext context,
191: Element e) {
192: context.push();
193: context.registerNamespaces(e);
194:
195: MIMEContent content = new MIMEContent();
196:
197: String part = XmlUtil
198: .getAttributeOrNull(e, Constants.ATTR_PART);
199: if (part != null) {
200: content.setPart(part);
201: }
202:
203: String type = XmlUtil
204: .getAttributeOrNull(e, Constants.ATTR_TYPE);
205: if (type != null) {
206: content.setType(type);
207: }
208:
209: context.pop();
210: context.fireDoneParsingEntity(MIMEConstants.QNAME_CONTENT,
211: content);
212: return content;
213: }
214:
215: protected MIMEXml parseMIMEXml(ParserContext context, Element e) {
216: context.push();
217: context.registerNamespaces(e);
218:
219: MIMEXml mimeXml = new MIMEXml();
220:
221: String part = XmlUtil
222: .getAttributeOrNull(e, Constants.ATTR_PART);
223: if (part != null) {
224: mimeXml.setPart(part);
225: }
226:
227: context.pop();
228: context.fireDoneParsingEntity(MIMEConstants.QNAME_MIME_XML,
229: mimeXml);
230: return mimeXml;
231: }
232:
233: public void doHandleExtension(WriterContext context,
234: Extension extension) throws IOException {
235: // NOTE - this ugliness can be avoided by moving all the XML parsing/writing code
236: // into the document classes themselves
237: if (extension instanceof MIMEContent) {
238: MIMEContent content = (MIMEContent) extension;
239: context.writeStartTag(content.getElementName());
240: context.writeAttribute(Constants.ATTR_PART, content
241: .getPart());
242: context.writeAttribute(Constants.ATTR_TYPE, content
243: .getType());
244: context.writeEndTag(content.getElementName());
245: } else if (extension instanceof MIMEXml) {
246: MIMEXml mimeXml = (MIMEXml) extension;
247: context.writeStartTag(mimeXml.getElementName());
248: context.writeAttribute(Constants.ATTR_PART, mimeXml
249: .getPart());
250: context.writeEndTag(mimeXml.getElementName());
251: } else if (extension instanceof MIMEMultipartRelated) {
252: MIMEMultipartRelated mpr = (MIMEMultipartRelated) extension;
253: context.writeStartTag(mpr.getElementName());
254: for (Iterator iter = mpr.getParts(); iter.hasNext();) {
255: MIMEPart part = (MIMEPart) iter.next();
256: context.writeStartTag(part.getElementName());
257: for (Iterator iter2 = part.extensions(); iter2
258: .hasNext();) {
259: Extension e = (Extension) iter2.next();
260: ExtensionHandler h = (ExtensionHandler) _extensionHandlers
261: .get(e.getElementName().getNamespaceURI());
262: if (h != null) {
263: h.doHandleExtension(context, e);
264: }
265: }
266: context.writeEndTag(part.getElementName());
267: }
268: context.writeEndTag(mpr.getElementName());
269: } else {
270: throw new IllegalArgumentException();
271: }
272: }
273: }
|