📄 deserializationcontext.java
字号:
/* * Copyright 2001-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.encoding;import org.apache.axis.MessageContext;import org.apache.axis.Constants;import org.apache.axis.Message;import org.apache.axis.AxisFault;import org.apache.axis.constants.Use;import org.apache.axis.attachments.Attachments;import org.apache.axis.description.TypeDesc;import org.apache.axis.soap.SOAPConstants;import org.apache.axis.utils.NSStack;import org.apache.axis.utils.XMLUtils;import org.apache.axis.utils.JavaUtils;import org.apache.axis.utils.Messages;import org.apache.axis.utils.cache.MethodCache;import org.apache.axis.schema.SchemaVersion;import org.apache.axis.components.logger.LogFactory;import org.apache.axis.message.IDResolver;import org.apache.axis.message.MessageElement;import org.apache.axis.message.SAX2EventRecorder;import org.apache.axis.message.SOAPEnvelope;import org.apache.axis.message.SOAPHandler;import org.apache.axis.message.EnvelopeBuilder;import org.apache.axis.message.EnvelopeHandler;import org.apache.axis.message.NullAttributes;import org.apache.commons.logging.Log;import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.Locator;import org.xml.sax.InputSource;import org.xml.sax.ext.LexicalHandler;import org.xml.sax.helpers.AttributesImpl;import org.xml.sax.helpers.DefaultHandler;import javax.xml.namespace.QName;import javax.xml.parsers.SAXParser;import javax.xml.rpc.JAXRPCException;import java.util.ArrayList;import java.util.HashMap;import java.io.IOException;import java.lang.reflect.Method;/** * This interface describes the AXIS DeserializationContext, note that * an AXIS compliant DeserializationContext must extend the org.xml.sax.helpers.DefaultHandler. */public class DeserializationContext extends DefaultHandler implements javax.xml.rpc.encoding.DeserializationContext, LexicalHandler { protected static Log log = LogFactory.getLog(DeserializationContext.class.getName()); // invariant member variable to track low-level logging requirements // we cache this once per instance lifecycle to avoid repeated lookups // in heavily used code. private final boolean debugEnabled = log.isDebugEnabled(); static final SchemaVersion schemaVersions[] = new SchemaVersion [] { SchemaVersion.SCHEMA_1999, SchemaVersion.SCHEMA_2000, SchemaVersion.SCHEMA_2001, }; private NSStack namespaces = new NSStack(); private Locator locator; // Class used for deserialization using class metadata from // downstream deserializers private Class destClass; // for performance reasons, keep the top of the stack separate from // the remainder of the handlers, and therefore readily available. private SOAPHandler topHandler = null; private ArrayList pushedDownHandlers = new ArrayList(); //private SAX2EventRecorder recorder = new SAX2EventRecorder(); private SAX2EventRecorder recorder = null; private SOAPEnvelope envelope; /* A map of IDs -> IDResolvers */ private HashMap idMap; private LocalIDResolver localIDs; private HashMap fixups; static final SOAPHandler nullHandler = new SOAPHandler(); protected MessageContext msgContext; private boolean doneParsing = false; protected InputSource inputSource = null; private MessageElement curElement; protected int startOfMappingsPos = -1; private static final Class[] DESERIALIZER_CLASSES = new Class[] {String.class, Class.class, QName.class}; private static final String DESERIALIZER_METHOD = "getDeserializer"; // This is a hack to associate the first schema namespace we see with // the correct SchemaVersion. It assumes people won't often be mixing // schema versions in a given document, which I think is OK. --Glen protected boolean haveSeenSchemaNS = false; public void deserializing(boolean isDeserializing) { doneParsing = isDeserializing; } /** * Construct Deserializer using MessageContext and EnvelopeBuilder handler * @param ctx is the MessageContext * @param initialHandler is the EnvelopeBuilder handler */ public DeserializationContext(MessageContext ctx, SOAPHandler initialHandler) { msgContext = ctx; // If high fidelity is required, record the whole damn thing. if (ctx == null || ctx.isHighFidelity()) recorder = new SAX2EventRecorder(); if (initialHandler instanceof EnvelopeBuilder) { envelope = ((EnvelopeBuilder)initialHandler).getEnvelope(); envelope.setRecorder(recorder); } pushElementHandler(new EnvelopeHandler(initialHandler)); } /** * Construct Deserializer * @param is is the InputSource * @param ctx is the MessageContext * @param messageType is the MessageType to construct an EnvelopeBuilder */ public DeserializationContext(InputSource is, MessageContext ctx, String messageType) { msgContext = ctx; EnvelopeBuilder builder = new EnvelopeBuilder(messageType, ctx != null ? ctx.getSOAPConstants() : null); // If high fidelity is required, record the whole damn thing. if (ctx == null || ctx.isHighFidelity()) recorder = new SAX2EventRecorder(); envelope = builder.getEnvelope(); envelope.setRecorder(recorder); pushElementHandler(new EnvelopeHandler(builder)); inputSource = is; } private SOAPConstants soapConstants = null; /** * returns the soap constants. */ public SOAPConstants getSOAPConstants(){ if (soapConstants != null) return soapConstants; if (msgContext != null) { soapConstants = msgContext.getSOAPConstants(); return soapConstants; } else { return Constants.DEFAULT_SOAP_VERSION; } } /** * Construct Deserializer * @param is is the InputSource * @param ctx is the MessageContext * @param messageType is the MessageType to construct an EnvelopeBuilder * @param env is the SOAPEnvelope to construct an EnvelopeBuilder */ public DeserializationContext(InputSource is, MessageContext ctx, String messageType, SOAPEnvelope env) { EnvelopeBuilder builder = new EnvelopeBuilder(env, messageType); msgContext = ctx; // If high fidelity is required, record the whole damn thing. if (ctx == null || ctx.isHighFidelity()) recorder = new SAX2EventRecorder(); envelope = builder.getEnvelope(); envelope.setRecorder(recorder); pushElementHandler(new EnvelopeHandler(builder)); inputSource = is; } /** * Create a parser and parse the inputSource */ public void parse() throws SAXException { if (inputSource != null) { SAXParser parser = XMLUtils.getSAXParser(); try { parser.setProperty("http://xml.org/sax/properties/lexical-handler", this); parser.parse(inputSource, this); try { // cleanup - so that the parser can be reused. parser.setProperty("http://xml.org/sax/properties/lexical-handler", nullLexicalHandler); } catch (Exception e){ // Ignore. } // only release the parser for reuse if there wasn't an // error. While parsers should be reusable, don't trust // parsers that died to clean up appropriately. XMLUtils.releaseSAXParser(parser); } catch (IOException e) { throw new SAXException(e); } inputSource = null; } } /** * Get current MessageElement **/ public MessageElement getCurElement() { return curElement; } /** * Set current MessageElement **/ public void setCurElement(MessageElement el) { curElement = el; if (curElement != null && curElement.getRecorder() != recorder) { recorder = curElement.getRecorder(); } } /** * Get MessageContext */ public MessageContext getMessageContext() { return msgContext; } /** * Returns this context's encoding style. If we've got a message * context then we'll get the style from that; otherwise we'll * return a default. * * @return a <code>String</code> value */ public String getEncodingStyle() { return msgContext == null ? Use.ENCODED.getEncoding() : msgContext.getEncodingStyle(); } /** * Get Envelope */ public SOAPEnvelope getEnvelope() { return envelope; } /** * Get Event Recorder */ public SAX2EventRecorder getRecorder() { return recorder; } /** * Set Event Recorder */ public void setRecorder(SAX2EventRecorder recorder) { this.recorder = recorder; } /** * Get the Namespace Mappings. Returns null if none are present. **/ public ArrayList getCurrentNSMappings() { return namespaces.cloneFrame(); } /** * Get the Namespace for a particular prefix */ public String getNamespaceURI(String prefix) { String result = namespaces.getNamespaceURI(prefix); if (result != null) return result; if (curElement != null) return curElement.getNamespaceURI(prefix); return null; } /** * Construct a QName from a string of the form <prefix>:<localName> * @param qNameStr is the prefixed name from the xml text * @return QName */ public QName getQNameFromString(String qNameStr) { if (qNameStr == null) return null; // OK, this is a QName, so look up the prefix in our current mappings. int i = qNameStr.indexOf(':'); String nsURI; if (i == -1) { nsURI = getNamespaceURI(""); } else { nsURI = getNamespaceURI(qNameStr.substring(0, i)); } return new QName(nsURI, qNameStr.substring(i + 1)); } /** * Create a QName for the type of the element defined by localName and * namespace from the XSI type. * @param namespace of the element * @param localName is the local name of the element * @param attrs are the attributes on the element */ public QName getTypeFromXSITypeAttr(String namespace, String localName, Attributes attrs) { // Check for type String type = Constants.getValue(attrs, Constants.URIS_SCHEMA_XSI, "type"); if (type != null) { // Return the type attribute value converted to a QName return getQNameFromString(type); } return null; } /** * Create a QName for the type of the element defined by localName and * namespace with the specified attributes. * @param namespace of the element * @param localName is the local name of the element * @param attrs are the attributes on the element */ public QName getTypeFromAttributes(String namespace, String localName, Attributes attrs) { QName typeQName = getTypeFromXSITypeAttr(namespace, localName, attrs); if ( (typeQName == null) && Constants.isSOAP_ENC(namespace) ) { // If the element is a SOAP-ENC element, the name of the element is the type. // If the default type mapping accepts SOAP 1.2, then use then set // the typeQName to the SOAP-ENC type. // Else if the default type mapping accepts SOAP 1.1, then // convert the SOAP-ENC type to the appropriate XSD Schema Type. if (namespace.equals(Constants.URI_SOAP12_ENC)) { typeQName = new QName(namespace, localName); } else if (localName.equals(Constants.SOAP_ARRAY.getLocalPart())) { typeQName = Constants.SOAP_ARRAY; } else if (localName.equals(Constants.SOAP_STRING.getLocalPart())) { typeQName = Constants.SOAP_STRING; } else if (localName.equals(Constants.SOAP_BOOLEAN.getLocalPart())) { typeQName = Constants.SOAP_BOOLEAN; } else if (localName.equals(Constants.SOAP_DOUBLE.getLocalPart())) { typeQName = Constants.SOAP_DOUBLE; } else if (localName.equals(Constants.SOAP_FLOAT.getLocalPart())) { typeQName = Constants.SOAP_FLOAT; } else if (localName.equals(Constants.SOAP_INT.getLocalPart())) { typeQName = Constants.SOAP_INT; } else if (localName.equals(Constants.SOAP_LONG.getLocalPart())) { typeQName = Constants.SOAP_LONG; } else if (localName.equals(Constants.SOAP_SHORT.getLocalPart())) { typeQName = Constants.SOAP_SHORT; } else if (localName.equals(Constants.SOAP_BYTE.getLocalPart())) { typeQName = Constants.SOAP_BYTE; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -