📄 arraydeserializer.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.ser;import org.apache.axis.Constants;import org.apache.axis.components.logger.LogFactory;import org.apache.axis.encoding.DeserializationContext;import org.apache.axis.encoding.Deserializer;import org.apache.axis.encoding.DeserializerImpl;import org.apache.axis.encoding.DeserializerTarget;import org.apache.axis.message.SOAPHandler;import org.apache.axis.utils.ClassUtils;import org.apache.axis.utils.JavaUtils;import org.apache.axis.utils.Messages;import org.apache.axis.wsdl.symbolTable.SchemaUtils;import org.apache.commons.logging.Log;import org.apache.axis.soap.SOAPConstants;import org.xml.sax.Attributes;import org.xml.sax.SAXException;import javax.xml.namespace.QName;import java.util.ArrayList;import java.util.HashMap;import java.util.StringTokenizer;/** * An ArrayDeserializer handles deserializing SOAP * arrays. * * Some code borrowed from ApacheSOAP - thanks to Matt Duftler! * * @author Glen Daniels (gdaniels@apache.org) * * Multi-reference stuff: * @author Rich Scheuerle (scheu@us.ibm.com) */public class ArrayDeserializer extends DeserializerImpl{ protected static Log log = LogFactory.getLog(ArrayDeserializer.class.getName()); public QName arrayType = null; public int curIndex = 0; QName defaultItemType; int length; Class arrayClass = null; ArrayList mDimLength = null; // If set, array of multi-dim lengths ArrayList mDimFactor = null; // If set, array of factors for multi-dim [] SOAPConstants soapConstants = SOAPConstants.SOAP11_CONSTANTS; /** * This method is invoked after startElement when the element requires * deserialization (i.e. the element is not an href & the value is not nil) * DeserializerImpl provides default behavior, which simply * involves obtaining a correct Deserializer and plugging its handler. * @param namespace is the namespace of the element * @param localName is the name of the element * @param prefix is the prefix of the element * @param attributes are the attrs on the element...used to get the type * @param context is the DeserializationContext */ public void onStartElement(String namespace, String localName, String prefix, Attributes attributes, DeserializationContext context) throws SAXException { // Deserializing the xml array requires processing the // xsi:type= attribute, the soapenc:arrayType attribute, // and the xsi:type attributes of the individual elements. // // The xsi:type=<qName> attribute is used to determine the java // type of the array to instantiate. Axis expects it // to be set to the generic "soapenc:Array" or to // a specific qName. If the generic "soapenc:Array" // specification is used, Axis determines the array // type by examining the soapenc:arrayType attribute. // // The soapenc:arrayType=<qname><dims> is used to determine // i) the number of dimensions, // ii) the length of each dimension, // iii) the default xsi:type of each of the elements. // // If the arrayType attribute is missing, Axis assumes // a single dimension array with length equal to the number // of nested elements. In such cases, the default xsi:type of // the elements is determined using the array xsi:type. // // The xsi:type attributes of the individual elements of the // array are used to determine the java type of the element. // If the xsi:type attribute is missing for an element, the // default xsi:type value is used. if (log.isDebugEnabled()) { log.debug("Enter: ArrayDeserializer::startElement()"); } soapConstants = context.getSOAPConstants(); // Get the qname for the array type=, set it to null if // the generic type is used. QName typeQName = context.getTypeFromAttributes(namespace, localName, attributes); if (typeQName == null) { typeQName = getDefaultType(); } if (typeQName != null && Constants.equals(Constants.SOAP_ARRAY, typeQName)) { typeQName = null; } // Now get the arrayType value QName arrayTypeValue = context.getQNameFromString( Constants.getValue(attributes, Constants.URIS_SOAP_ENC, soapConstants.getAttrItemType())); // The first part of the arrayType expression is // the default item type qname. // The second part is the dimension information String dimString = null; QName innerQName = null; String innerDimString = ""; if (arrayTypeValue != null) { if (soapConstants != SOAPConstants.SOAP12_CONSTANTS) { // Doing SOAP 1.1 // Array dimension noted like this : [][x] String arrayTypeValueNamespaceURI = arrayTypeValue.getNamespaceURI(); String arrayTypeValueLocalPart = arrayTypeValue.getLocalPart(); int leftBracketIndex = arrayTypeValueLocalPart.lastIndexOf('['); int rightBracketIndex = arrayTypeValueLocalPart.lastIndexOf(']'); if (leftBracketIndex == -1 || rightBracketIndex == -1 || rightBracketIndex < leftBracketIndex) { throw new IllegalArgumentException( Messages.getMessage("badArrayType00", "" + arrayTypeValue)); } dimString = arrayTypeValueLocalPart.substring(leftBracketIndex + 1, rightBracketIndex); arrayTypeValueLocalPart = arrayTypeValueLocalPart.substring(0, leftBracketIndex); // If multi-dim array set to soapenc:Array if (arrayTypeValueLocalPart.endsWith("]")) { defaultItemType = Constants.SOAP_ARRAY; int bracket = arrayTypeValueLocalPart.indexOf("["); innerQName = new QName(arrayTypeValueNamespaceURI, arrayTypeValueLocalPart.substring(0, bracket)); innerDimString = arrayTypeValueLocalPart.substring(bracket); } else { defaultItemType = new QName(arrayTypeValueNamespaceURI, arrayTypeValueLocalPart); } } else { String arraySizeValue = attributes.getValue(soapConstants.getEncodingURI(), Constants.ATTR_ARRAY_SIZE); int leftStarIndex = arraySizeValue.lastIndexOf('*'); // Skip to num if any if (leftStarIndex != -1) { // "*" => "" if (leftStarIndex == 0 && arraySizeValue.length() == 1) { // "* *" => "" } else if (leftStarIndex == (arraySizeValue.length() - 1)) { throw new IllegalArgumentException( Messages.getMessage("badArraySize00", "" + arraySizeValue)); // "* N" => "N" } else { dimString = arraySizeValue.substring(leftStarIndex + 2); innerQName = arrayTypeValue; innerDimString = arraySizeValue.substring(0, leftStarIndex + 1); } } else { dimString = arraySizeValue; } if (innerDimString == null || innerDimString.length() == 0) { defaultItemType = arrayTypeValue; } else { defaultItemType = Constants.SOAP_ARRAY12; } } } // If no type QName and no defaultItemType qname, use xsd:anyType if (defaultItemType == null && typeQName == null) { Class destClass = context.getDestinationClass(); if (destClass != null && destClass.isArray()) { // This will get set OK down below... } else { defaultItemType = Constants.XSD_ANYTYPE; } } // Determine the class type for the array. arrayClass = null; if (typeQName != null) { arrayClass = context.getTypeMapping(). getClassForQName(typeQName); } if (typeQName == null || arrayClass == null) { // type= information is not sufficient. // Get an array of the default item type. Class arrayItemClass = null; QName compQName = defaultItemType; // Nested array, use the innermost qname String dims = "[]"; if (innerQName != null) { compQName = innerQName; if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) { // With SOAP 1.2 Array, we append [] for each * found int offset = 0; while ((offset = innerDimString.indexOf('*', offset)) != -1) { dims += "[]"; offset ++; } } else { // With SOAP 1.1 Array, we can append directly the complete innerDimString dims += innerDimString; } } // item Class arrayItemClass = context.getTypeMapping().getClassForQName(compQName); if (arrayItemClass != null) { try { // Append the dimension found to the classname computed from the itemClass // to form the array classname
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -