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.io.OutputStream;
030: import java.util.HashMap;
031: import java.util.Iterator;
032: import java.util.Map;
033: import java.util.Set;
034:
035: import javax.xml.namespace.QName;
036:
037: import com.sun.tools.internal.ws.wsdl.document.Binding;
038: import com.sun.tools.internal.ws.wsdl.document.BindingFault;
039: import com.sun.tools.internal.ws.wsdl.document.BindingInput;
040: import com.sun.tools.internal.ws.wsdl.document.BindingOperation;
041: import com.sun.tools.internal.ws.wsdl.document.BindingOutput;
042: import com.sun.tools.internal.ws.wsdl.document.Definitions;
043: import com.sun.tools.internal.ws.wsdl.document.Documentation;
044: import com.sun.tools.internal.ws.wsdl.document.Fault;
045: import com.sun.tools.internal.ws.wsdl.document.Import;
046: import com.sun.tools.internal.ws.wsdl.document.Input;
047: import com.sun.tools.internal.ws.wsdl.document.Message;
048: import com.sun.tools.internal.ws.wsdl.document.MessagePart;
049: import com.sun.tools.internal.ws.wsdl.document.Operation;
050: import com.sun.tools.internal.ws.wsdl.document.Output;
051: import com.sun.tools.internal.ws.wsdl.document.Port;
052: import com.sun.tools.internal.ws.wsdl.document.PortType;
053: import com.sun.tools.internal.ws.wsdl.document.Service;
054: import com.sun.tools.internal.ws.wsdl.document.Types;
055: import com.sun.tools.internal.ws.wsdl.document.WSDLConstants;
056: import com.sun.tools.internal.ws.wsdl.document.WSDLDocument;
057: import com.sun.tools.internal.ws.wsdl.document.WSDLDocumentVisitor;
058: import com.sun.tools.internal.ws.wsdl.document.schema.SchemaKinds;
059: import com.sun.tools.internal.ws.wsdl.document.jaxws.JAXWSBindingsConstants;
060: import com.sun.tools.internal.ws.wsdl.framework.Extension;
061: import com.sun.tools.internal.ws.wsdl.framework.Kind;
062: import com.sun.tools.internal.ws.wsdl.framework.WriterContext;
063:
064: /**
065: * A writer for WSDL documents.
066: *
067: * @author WS Development Team
068: */
069: public class WSDLWriter {
070:
071: public WSDLWriter() throws IOException {
072: _extensionHandlers = new HashMap();
073:
074: // register handlers for default extensions
075: register(new SOAPExtensionHandler());
076: register(new HTTPExtensionHandler());
077: register(new MIMEExtensionHandler());
078: register(new SchemaExtensionHandler());
079: register(new JAXWSBindingExtensionHandler());
080: }
081:
082: public void register(ExtensionHandler h) {
083: _extensionHandlers.put(h.getNamespaceURI(), h);
084: h.setExtensionHandlers(_extensionHandlers);
085: }
086:
087: public void unregister(ExtensionHandler h) {
088: _extensionHandlers.put(h.getNamespaceURI(), null);
089: h.setExtensionHandlers(null);
090: }
091:
092: public void unregister(String uri) {
093: _extensionHandlers.put(uri, null);
094: }
095:
096: public void write(final WSDLDocument document, OutputStream os)
097: throws IOException {
098: final WriterContext context = new WriterContext(os);
099: try {
100: document.accept(new WSDLDocumentVisitor() {
101: public void preVisit(Definitions definitions)
102: throws Exception {
103: context.push();
104: initializePrefixes(context, document);
105: context.writeStartTag(definitions.getElementName());
106: context.writeAttribute(Constants.ATTR_NAME,
107: definitions.getName());
108: context.writeAttribute(
109: Constants.ATTR_TARGET_NAMESPACE,
110: definitions.getTargetNamespaceURI());
111: context.writeAllPendingNamespaceDeclarations();
112: }
113:
114: public void postVisit(Definitions definitions)
115: throws Exception {
116: context.writeEndTag(definitions.getElementName());
117: context.pop();
118: }
119:
120: public void visit(Import i) throws Exception {
121: context.writeStartTag(i.getElementName());
122: context.writeAttribute(Constants.ATTR_NAMESPACE, i
123: .getNamespace());
124: context.writeAttribute(Constants.ATTR_LOCATION, i
125: .getLocation());
126: context.writeEndTag(i.getElementName());
127: }
128:
129: public void preVisit(Types types) throws Exception {
130: context.writeStartTag(types.getElementName());
131: }
132:
133: public void postVisit(Types types) throws Exception {
134: context.writeEndTag(types.getElementName());
135: }
136:
137: public void preVisit(Message message) throws Exception {
138: context.writeStartTag(message.getElementName());
139: context.writeAttribute(Constants.ATTR_NAME, message
140: .getName());
141: }
142:
143: public void postVisit(Message message) throws Exception {
144: context.writeEndTag(message.getElementName());
145: }
146:
147: public void visit(MessagePart part) throws Exception {
148: context.writeStartTag(part.getElementName());
149: context.writeAttribute(Constants.ATTR_NAME, part
150: .getName());
151:
152: QName dname = part.getDescriptor();
153: Kind dkind = part.getDescriptorKind();
154: if (dname != null && dkind != null) {
155: if (dkind.equals(SchemaKinds.XSD_ELEMENT)) {
156: context.writeAttribute(
157: Constants.ATTR_ELEMENT, dname);
158: } else if (dkind.equals(SchemaKinds.XSD_TYPE)) {
159: context.writeAttribute(Constants.ATTR_TYPE,
160: dname);
161: } else {
162: // TODO - add support for attribute extensions here
163: }
164: }
165: context.writeEndTag(part.getElementName());
166: }
167:
168: public void preVisit(PortType portType)
169: throws Exception {
170: context.writeStartTag(portType.getElementName());
171: context.writeAttribute(Constants.ATTR_NAME,
172: portType.getName());
173: }
174:
175: public void postVisit(PortType portType)
176: throws Exception {
177: context.writeEndTag(portType.getElementName());
178: }
179:
180: public void preVisit(Operation operation)
181: throws Exception {
182: context.writeStartTag(operation.getElementName());
183: context.writeAttribute(Constants.ATTR_NAME,
184: operation.getName());
185: //bug fix: 4947340, parameterOder="" should not be generated
186: if (operation.getParameterOrder() != null
187: && operation.getParameterOrder().length() > 0) {
188: context.writeAttribute(
189: Constants.ATTR_PARAMETER_ORDER,
190: operation.getParameterOrder());
191: }
192: }
193:
194: public void postVisit(Operation operation)
195: throws Exception {
196: context.writeEndTag(operation.getElementName());
197: }
198:
199: public void preVisit(Input input) throws Exception {
200: context.writeStartTag(input.getElementName());
201: context.writeAttribute(Constants.ATTR_NAME, input
202: .getName());
203: context.writeAttribute(Constants.ATTR_MESSAGE,
204: input.getMessage());
205: }
206:
207: public void postVisit(Input input) throws Exception {
208: context.writeEndTag(input.getElementName());
209: }
210:
211: public void preVisit(Output output) throws Exception {
212: context.writeStartTag(output.getElementName());
213: context.writeAttribute(Constants.ATTR_NAME, output
214: .getName());
215: context.writeAttribute(Constants.ATTR_MESSAGE,
216: output.getMessage());
217: }
218:
219: public void postVisit(Output output) throws Exception {
220: context.writeEndTag(output.getElementName());
221: }
222:
223: public void preVisit(Fault fault) throws Exception {
224: context.writeStartTag(fault.getElementName());
225: context.writeAttribute(Constants.ATTR_NAME, fault
226: .getName());
227: context.writeAttribute(Constants.ATTR_MESSAGE,
228: fault.getMessage());
229: }
230:
231: public void postVisit(Fault fault) throws Exception {
232: context.writeEndTag(fault.getElementName());
233: }
234:
235: public void preVisit(Binding binding) throws Exception {
236: context.writeStartTag(binding.getElementName());
237: context.writeAttribute(Constants.ATTR_NAME, binding
238: .getName());
239: context.writeAttribute(Constants.ATTR_TYPE, binding
240: .getPortType());
241: }
242:
243: public void postVisit(Binding binding) throws Exception {
244: context.writeEndTag(binding.getElementName());
245: }
246:
247: public void preVisit(BindingOperation operation)
248: throws Exception {
249: context.writeStartTag(operation.getElementName());
250: context.writeAttribute(Constants.ATTR_NAME,
251: operation.getName());
252: }
253:
254: public void postVisit(BindingOperation operation)
255: throws Exception {
256: context.writeEndTag(operation.getElementName());
257: }
258:
259: public void preVisit(BindingInput input)
260: throws Exception {
261: context.writeStartTag(input.getElementName());
262: context.writeAttribute(Constants.ATTR_NAME, input
263: .getName());
264: }
265:
266: public void postVisit(BindingInput input)
267: throws Exception {
268: context.writeEndTag(input.getElementName());
269: }
270:
271: public void preVisit(BindingOutput output)
272: throws Exception {
273: context.writeStartTag(output.getElementName());
274: context.writeAttribute(Constants.ATTR_NAME, output
275: .getName());
276: }
277:
278: public void postVisit(BindingOutput output)
279: throws Exception {
280: context.writeEndTag(output.getElementName());
281: }
282:
283: public void preVisit(BindingFault fault)
284: throws Exception {
285: context.writeStartTag(fault.getElementName());
286: context.writeAttribute(Constants.ATTR_NAME, fault
287: .getName());
288: }
289:
290: public void postVisit(BindingFault fault)
291: throws Exception {
292: context.writeEndTag(fault.getElementName());
293: }
294:
295: public void preVisit(Service service) throws Exception {
296: context.writeStartTag(service.getElementName());
297: context.writeAttribute(Constants.ATTR_NAME, service
298: .getName());
299: }
300:
301: public void postVisit(Service service) throws Exception {
302: context.writeEndTag(service.getElementName());
303: }
304:
305: public void preVisit(Port port) throws Exception {
306: context.writeStartTag(port.getElementName());
307: context.writeAttribute(Constants.ATTR_NAME, port
308: .getName());
309: context.writeAttribute(Constants.ATTR_BINDING, port
310: .getBinding());
311: }
312:
313: public void postVisit(Port port) throws Exception {
314: context.writeEndTag(port.getElementName());
315: }
316:
317: public void preVisit(Extension extension)
318: throws Exception {
319: ExtensionHandler h = (ExtensionHandler) _extensionHandlers
320: .get(extension.getElementName()
321: .getNamespaceURI());
322: h.doHandleExtension(context, extension);
323: }
324:
325: public void postVisit(Extension extension)
326: throws Exception {
327: }
328:
329: public void visit(Documentation documentation)
330: throws Exception {
331: context.writeTag(WSDLConstants.QNAME_DOCUMENTATION,
332: null);
333: }
334: });
335: context.flush();
336: } catch (Exception e) {
337: if (e instanceof IOException) {
338: throw (IOException) e;
339: } else if (e instanceof RuntimeException) {
340: throw (RuntimeException) e;
341: } else {
342: // entirely unexpected exception
343: throw new IllegalStateException();
344: }
345: }
346: }
347:
348: private void initializePrefixes(WriterContext context,
349: WSDLDocument document) throws IOException {
350: // deal with the target namespace first
351: String tnsURI = document.getDefinitions()
352: .getTargetNamespaceURI();
353: if (tnsURI != null) {
354: context.setTargetNamespaceURI(tnsURI);
355: context.declarePrefix(TARGET_NAMESPACE_PREFIX, tnsURI);
356: }
357:
358: // then with the WSDL namespace
359: // context.declarePrefix(_commonPrefixes.get(Constants.NS_WSDL), Constants.NS_WSDL);
360: context.declarePrefix("", Constants.NS_WSDL);
361:
362: // then with all other namespaces
363: Set namespaces = document.collectAllNamespaces();
364: for (Iterator iter = namespaces.iterator(); iter.hasNext();) {
365: String nsURI = (String) iter.next();
366: if (context.getPrefixFor(nsURI) != null)
367: continue;
368:
369: String prefix = (String) _commonPrefixes.get(nsURI);
370: if (prefix == null) {
371: // create a new prefix for it
372: prefix = context
373: .findNewPrefix(NEW_NAMESPACE_PREFIX_BASE);
374: }
375: context.declarePrefix(prefix, nsURI);
376: }
377: }
378:
379: private Map _extensionHandlers;
380:
381: ////////
382:
383: private static Map<String, String> _commonPrefixes;
384:
385: static {
386: _commonPrefixes = new HashMap<String, String>();
387: _commonPrefixes.put(Constants.NS_WSDL, "wsdl");
388: _commonPrefixes.put(Constants.NS_WSDL_SOAP, "soap");
389: _commonPrefixes.put(Constants.NS_WSDL_HTTP, "http");
390: _commonPrefixes.put(Constants.NS_WSDL_MIME, "mime");
391: _commonPrefixes.put(Constants.NS_XSD, "xsd");
392: _commonPrefixes.put(Constants.NS_XSI, "xsi");
393: _commonPrefixes.put(JAXWSBindingsConstants.NS_JAXWS_BINDINGS,
394: "jaxws");
395: }
396:
397: private final static String TARGET_NAMESPACE_PREFIX = "tns";
398: private final static String NEW_NAMESPACE_PREFIX_BASE = "ns";
399: }
|