📄 types.java
字号:
/* * Copyright 2001-2002,2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.axis.wsdl.fromJava;import org.apache.axis.AxisFault;import org.apache.axis.AxisProperties;import org.apache.axis.Constants;import org.apache.axis.InternalException;import org.apache.axis.MessageContext;import org.apache.axis.handlers.soap.SOAPService;import org.apache.axis.components.logger.LogFactory;import org.apache.axis.constants.Style;import org.apache.axis.description.ServiceDesc;import org.apache.axis.encoding.Serializer;import org.apache.axis.encoding.SerializerFactory;import org.apache.axis.encoding.SimpleType;import org.apache.axis.encoding.TypeMapping;import org.apache.axis.encoding.ser.BeanSerializerFactory;import org.apache.axis.encoding.ser.EnumSerializerFactory;import org.apache.axis.soap.SOAPConstants;import org.apache.axis.utils.JavaUtils;import org.apache.axis.utils.Messages;import org.apache.axis.utils.XMLUtils;import org.apache.axis.utils.StringUtils;import org.apache.axis.wsdl.symbolTable.BaseTypeMapping;import org.apache.axis.wsdl.symbolTable.SymbolTable;import org.apache.axis.wsdl.symbolTable.TypeEntry;import org.apache.commons.logging.Log;import org.w3c.dom.Attr;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.xml.sax.SAXException;import javax.wsdl.Definition;import javax.wsdl.WSDLException;import javax.xml.namespace.QName;import javax.xml.parsers.ParserConfigurationException;import javax.xml.rpc.holders.Holder;import java.io.IOException;import java.lang.reflect.Field;import java.lang.reflect.Modifier;import java.net.URL;import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;/** * <p>Description: </p> This class is used to recursively serializes a Java Class into * an XML Schema representation. * <p/> * It has utility methods to create a schema node, assosiate namespaces to the various types * * @author unascribed */public class Types { /** Field log */ protected static Log log = LogFactory.getLog(Types.class.getName()); /** Field def */ Definition def; /** Field namespaces */ Namespaces namespaces = null; /** Field tm */ TypeMapping tm; /** Field defaultTM */ TypeMapping defaultTM; /** Field targetNamespace */ String targetNamespace; /** Field wsdlTypesElem */ Element wsdlTypesElem = null; /** Field schemaTypes */ HashMap schemaTypes = null; /** Field schemaElementNames */ HashMap schemaElementNames = null; /** Field schemaUniqueElementNames */ HashMap schemaUniqueElementNames = null; /** Field wrapperMap */ HashMap wrapperMap = new HashMap(); /** Field stopClasses */ List stopClasses = null; /** Field beanCompatErrs */ List beanCompatErrs = new ArrayList(); /** Field serviceDesc */ private ServiceDesc serviceDesc = null; /** Keep track of the element QNames we've written to avoid dups */ private Set writtenElementQNames = new HashSet(); /** Which types have we already written? */ Class [] mappedTypes = null; /** The java to wsdl emitter */ Emitter emitter = null; public static boolean isArray(Class clazz) { return clazz.isArray() || java.util.Collection.class.isAssignableFrom(clazz); } private static Class getComponentType(Class clazz) { if (clazz.isArray()) { return clazz.getComponentType(); } else if (java.util.Collection.class.isAssignableFrom(clazz)) { return Object.class; } else { return null; } } /** * This class serailizes a <code>Class</code> to XML Schema. The constructor * provides the context for the streamed node within the WSDL document * * @param def WSDL Definition Element to declare namespaces * @param tm TypeMappingRegistry to handle known types * @param defaultTM default TM * @param namespaces user defined or autogenerated namespace and prefix maps * @param targetNamespace targetNamespace of the document * @param stopClasses * @param serviceDesc */ public Types(Definition def, TypeMapping tm, TypeMapping defaultTM, Namespaces namespaces, String targetNamespace, List stopClasses, ServiceDesc serviceDesc) { this.def = def; this.serviceDesc = serviceDesc; createDocumentFragment(); this.tm = tm; this.defaultTM = defaultTM; mappedTypes = tm.getAllClasses(); this.namespaces = namespaces; this.targetNamespace = targetNamespace; this.stopClasses = stopClasses; schemaElementNames = new HashMap(); schemaUniqueElementNames = new HashMap(); schemaTypes = new HashMap(); } /** * This class serailizes a <code>Class</code> to XML Schema. The constructor * provides the context for the streamed node within the WSDL document * * @param def WSDL Definition Element to declare namespaces * @param tm TypeMappingRegistry to handle known types * @param defaultTM default TM * @param namespaces user defined or autogenerated namespace and prefix maps * @param targetNamespace targetNamespace of the document * @param stopClasses * @param serviceDesc * @param emitter Java2Wsdl emitter */ public Types(Definition def, TypeMapping tm, TypeMapping defaultTM, Namespaces namespaces, String targetNamespace, List stopClasses, ServiceDesc serviceDesc, Emitter emitter) { this(def, tm, defaultTM, namespaces, targetNamespace, stopClasses, serviceDesc); this.emitter = emitter; } /** * Return the namespaces object for the current context * * @return */ public Namespaces getNamespaces() { return namespaces; } /** * Loads the types from the input schema file. * * @param inputSchema file or URL * @throws IOException * @throws WSDLException * @throws SAXException * @throws ParserConfigurationException */ public void loadInputSchema(String inputSchema) throws IOException, WSDLException, SAXException, ParserConfigurationException { // Read the input wsdl file into a Document Document doc = XMLUtils.newDocument(inputSchema); // Ensure that the root element is xsd:schema Element root = doc.getDocumentElement(); if (root.getLocalName().equals("schema") && Constants.isSchemaXSD(root.getNamespaceURI())) { Node schema = docHolder.importNode(root, true); if (null == wsdlTypesElem) { writeWsdlTypesElement(); } wsdlTypesElem.appendChild(schema); // Create a symbol table and populate it with the input types BaseTypeMapping btm = new BaseTypeMapping() { public String getBaseName(QName qNameIn) { QName qName = new QName(qNameIn.getNamespaceURI(), qNameIn.getLocalPart()); Class cls = defaultTM.getClassForQName(qName); if (cls == null) { return null; } else { return JavaUtils.getTextClassName(cls.getName()); } } }; SymbolTable symbolTable = new SymbolTable(btm, true, false, false); symbolTable.populateTypes(new URL(inputSchema), doc); processSymTabEntries(symbolTable); } else { // If not, we'll just bail out... perhaps we should log a warning // or throw an exception? ; } } /** * Walk the type/element entries in the symbol table and * add each one to the list of processed types. This prevents * the types from being duplicated. * * @param symbolTable */ private void processSymTabEntries(SymbolTable symbolTable) { Iterator iterator = symbolTable.getElementIndex().entrySet().iterator(); while (iterator.hasNext()) { Map.Entry me = (Map.Entry) iterator.next(); QName name = (QName) me.getKey(); TypeEntry te = (TypeEntry) me.getValue(); String prefix = XMLUtils.getPrefix(name.getNamespaceURI(), te.getNode()); if (!((null == prefix) || "".equals(prefix))) { namespaces.putPrefix(name.getNamespaceURI(), prefix); def.addNamespace(prefix, name.getNamespaceURI()); } addToElementsList(name); } iterator = symbolTable.getTypeIndex().entrySet().iterator(); while (iterator.hasNext()) { Map.Entry me = (Map.Entry) iterator.next(); QName name = (QName) me.getKey(); TypeEntry te = (TypeEntry) me.getValue(); String prefix = XMLUtils.getPrefix(name.getNamespaceURI(), te.getNode()); if (!((null == prefix) || "".equals(prefix))) { namespaces.putPrefix(name.getNamespaceURI(), prefix); def.addNamespace(prefix, name.getNamespaceURI()); } addToTypesList(name); } } /** * Load the types from the input wsdl file. * * @param inputWSDL file or URL * @throws IOException * @throws WSDLException * @throws SAXException * @throws ParserConfigurationException */ public void loadInputTypes(String inputWSDL) throws IOException, WSDLException, SAXException, ParserConfigurationException { // Read the input wsdl file into a Document Document doc = XMLUtils.newDocument(inputWSDL); // Search for the 'types' element NodeList elements = doc.getChildNodes(); if ((elements.getLength() > 0) && elements.item(0).getLocalName().equals("definitions")) { elements = elements.item(0).getChildNodes(); for (int i = 0; (i < elements.getLength()) && (wsdlTypesElem == null); i++) { Node node = elements.item(i); if ((node.getLocalName() != null) && node.getLocalName().equals("types")) { wsdlTypesElem = (Element) node; } } } // If types element not found, there is no need to continue. if (wsdlTypesElem == null) { return; } // Import the types element into the Types docHolder document wsdlTypesElem = (Element) docHolder.importNode(wsdlTypesElem, true); docHolder.appendChild(wsdlTypesElem); // Create a symbol table and populate it with the input wsdl document BaseTypeMapping btm = new BaseTypeMapping() { public String getBaseName(QName qNameIn) { QName qName = new QName(qNameIn.getNamespaceURI(), qNameIn.getLocalPart()); Class cls = tm.getClassForQName(qName); if (cls == null) { return null; } else { return JavaUtils.getTextClassName(cls.getName()); } } }; SymbolTable symbolTable = new SymbolTable(btm, true, false, false); symbolTable.populate(null, doc); processSymTabEntries(symbolTable); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -