?? wsdlwriter.java
字號:
/*Copyright (c) 2004-2007, Dennis M. Sosnoski.All rights reserved.Redistribution and use in source and binary forms, with or without modification,are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of JiBX nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" ANDANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIEDWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AREDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FORANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ONANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THISSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/package org.jibx.ws.wsdl;import java.io.IOException;import java.io.OutputStream;import java.util.Iterator;import java.util.Map;import java.util.Set;import org.jibx.runtime.BindingDirectory;import org.jibx.runtime.IBindingFactory;import org.jibx.runtime.IMarshallable;import org.jibx.runtime.IMarshaller;import org.jibx.runtime.IMarshallingContext;import org.jibx.runtime.IXMLWriter;import org.jibx.runtime.JiBXException;import org.jibx.runtime.impl.MarshallingContext;import org.jibx.schema.elements.SchemaElement;import org.jibx.util.StringIntSizedMap;/** * WSDL writer class. This handles writing generated WSDLs and schemas. * * @author Dennis M. Sosnoski */public class WsdlWriter{ /** Fixed URI for WSDL namespace. */ public static final String WSDL_NAMESPACE_URI = "http://schemas.xmlsoap.org/wsdl/"; /** Fixed prefix for WSDL namespace. */ public static final String WSDL_NAMESPACE_PREFIX = "wsdl"; /** Fixed URI for SOAP namespace. */ public static final String SOAP_NAMESPACE_URI = "http://schemas.xmlsoap.org/wsdl/soap/"; /** Fixed prefix for SOAP namespace. */ public static final String SOAP_NAMESPACE_PREFIX = "soap"; /** Fixed prefix for WSDL target namespace. */ public static final String DEFINITIONS_NAMESPACE_PREFIX = "wns"; /** Namespaces defined in binding. */ private StringIntSizedMap s_namespaceMap; /** Index of definitions class in binding. */ private int s_definitionsIndex; /** Namespace index for the WSDL namespace. */ private int s_wsdlNamespaceIndex; /** Map from extra namespace URIs to prefixes. */ private Map m_uriPrefixMap; /** Namespace index for the SOAP namespace. */ private int s_soapNamespaceIndex; /** Marshalling context. */ private final MarshallingContext m_marshalContext; /** * Constructor. * * @throws JiBXException on error creating marshaller */ public WsdlWriter() throws JiBXException { // set the marshalling contexts IBindingFactory ifact = BindingDirectory.getFactory(Definitions.class); m_marshalContext = (MarshallingContext)ifact.createMarshallingContext(); // initialize namespace URI to index map String[] nss = ifact.getNamespaces(); s_namespaceMap = new StringIntSizedMap(nss.length); for (int i = 0; i < nss.length; i++) { s_namespaceMap.add(nss[i], i); } // create other statics used in code s_wsdlNamespaceIndex = s_namespaceMap.get(WSDL_NAMESPACE_URI); s_soapNamespaceIndex = s_namespaceMap.get(SOAP_NAMESPACE_URI); // find the index for the root object in WSDL String[] classes = ifact.getMappedClasses(); s_definitionsIndex = -1; String cname = Definitions.class.getName(); for (int i = 0; i < classes.length; i++) { if (cname.equals(classes[i])) { s_definitionsIndex = i; break; } } if (s_definitionsIndex < 0) { throw new JiBXException("Missing binding definition for " + cname); } } /** * Write WSDL for service to output stream. * * @param def WSDL definitions information * @param os destination output stream * @exception JiBXException on error creating WSDL output */ public void writeWSDL(Definitions def, OutputStream os) throws JiBXException { // configure context for output stream m_marshalContext.setOutput(os, null); m_marshalContext.setIndent(2); m_marshalContext.setUserContext(def); // set up information for namespace indexes and prefixes Set uriset = def.getNamespaces(); String[] uris = new String[uriset.size()]; int[] indexes = new int[uris.length+2]; String[] prefs = new String[uris.length+2]; IXMLWriter writer = m_marshalContext.getXmlWriter(); int base = writer.getNamespaceCount(); int index = 0; for (Iterator iter = uriset.iterator(); iter.hasNext();) { String uri = (String)iter.next(); uris[index] = uri; indexes[index] = base + index; prefs[index++] = def.getPrefix(uri); } indexes[index] = s_wsdlNamespaceIndex; prefs[index++] = WSDL_NAMESPACE_PREFIX; indexes[index] = s_soapNamespaceIndex; prefs[index] = SOAP_NAMESPACE_PREFIX; // add the namespace declarations to current element writer.pushExtensionNamespaces(uris);/* writer.openNamespaces(indexes, prefs); for (int i = 0; i < uris.length; i++) { String prefix = prefs[i]; String name = prefix.length() > 0 ? "xmlns:" + prefix : "xmlns"; writer.addAttribute(0, name, uris[i]); } */ // write start tag with added namespaces m_marshalContext.startTagNamespaces(s_wsdlNamespaceIndex, "definitions", indexes, prefs); m_marshalContext.attribute(0, "targetNamespace", def.getWsdlNamespace()); m_marshalContext.closeStartContent(); // marshal out remaining data IMarshaller mar = m_marshalContext.getMarshaller(s_definitionsIndex, Definitions.class.getName()); mar.marshal(def, m_marshalContext); // finish with close tag m_marshalContext.endTag(s_wsdlNamespaceIndex, "definitions"); m_marshalContext.endDocument(); } /** * Serialize a reference to a name defined in the WSDL. This generates the * name text with the prefix (if any) used for the WSDL target namespace. * * @param name base name * @param ictx * @return qualified name with WSDL definitions prefix */ public static String WsdlPrefixSerializer(String name, IMarshallingContext ictx) { Definitions def = (Definitions)ictx.getUserContext(); return def.getWsdlPrefix() + ':' + name; } public static class SchemaMarshaller implements IMarshaller { /** Marshalling context for schema. */ private final MarshallingContext m_schemaContext; public SchemaMarshaller() throws JiBXException { IBindingFactory ifact = BindingDirectory.getFactory(SchemaElement.class); m_schemaContext = (MarshallingContext)ifact.createMarshallingContext(); } public boolean isExtension(int index) { return false; } public void marshal(Object obj, IMarshallingContext ctx) throws JiBXException { try { m_schemaContext.setFromContext((MarshallingContext)ctx); ((IMarshallable)obj).marshal(m_schemaContext); m_schemaContext.getXmlWriter().flush(); } catch (IOException e) { throw new JiBXException("Error writing schema", e); } } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -