📄 arraydeserializer.java
字号:
// String loadableArrayClassName = JavaUtils.getLoadableClassName( JavaUtils.getTextClassName(arrayItemClass.getName()) + dims); arrayClass = ClassUtils.forName(loadableArrayClassName, true, arrayItemClass.getClassLoader()); } catch (Exception e) { throw new SAXException( Messages.getMessage("noComponent00", "" + defaultItemType)); } } } if (arrayClass == null) { arrayClass = context.getDestinationClass(); } if (arrayClass == null) { throw new SAXException( Messages.getMessage("noComponent00", "" + defaultItemType)); } if (dimString == null || dimString.length() == 0) { // Size determined using length of the members value = new ArrayListExtension(arrayClass); } else { try { StringTokenizer tokenizer; if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) { tokenizer = new StringTokenizer(dimString); } else { tokenizer = new StringTokenizer(dimString, "[],"); } length = Integer.parseInt(tokenizer.nextToken()); if (tokenizer.hasMoreTokens()) { // If the array is passed as a multi-dimensional array // (i.e. int[2][3]) then store all of the // mult-dim lengths. // The valueReady method uses this array to set the // proper mult-dim element. mDimLength = new ArrayList(); mDimLength.add(new Integer(length)); while(tokenizer.hasMoreTokens()) { mDimLength.add( new Integer( Integer.parseInt(tokenizer.nextToken()))); } } // Create an ArrayListExtension class to store the ArrayList // plus converted objects. ArrayList list = new ArrayListExtension(arrayClass, length); // This is expensive as our array may not grown this big. // Prevents problems when XML claims a huge size // that it doesn't actually fill. //for (int i = 0; i < length; i++) { // list.add(null); //} value = list; } catch (NumberFormatException e) { throw new IllegalArgumentException( Messages.getMessage("badInteger00", dimString)); } } // If soapenc:offset specified, set the current index accordingly String offset = Constants.getValue(attributes, Constants.URIS_SOAP_ENC, Constants.ATTR_OFFSET); if (offset != null) { if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) { throw new SAXException(Messages.getMessage("noSparseArray")); } int leftBracketIndex = offset.lastIndexOf('['); int rightBracketIndex = offset.lastIndexOf(']'); if (leftBracketIndex == -1 || rightBracketIndex == -1 || rightBracketIndex < leftBracketIndex) { throw new SAXException( Messages.getMessage("badOffset00", offset)); } curIndex = convertToIndex(offset.substring(leftBracketIndex + 1, rightBracketIndex), "badOffset00"); } if (log.isDebugEnabled()) { log.debug("Exit: ArrayDeserializer::startElement()"); } } /** * onStartChild is called on each child element. * @param namespace is the namespace of the child element * @param localName is the local name of the child element * @param prefix is the prefix used on the name of the child element * @param attributes are the attributes of the child element * @param context is the deserialization context. * @return is a Deserializer to use to deserialize a child (must be * a derived class of SOAPHandler) or null if no deserialization should * be performed. */ public SOAPHandler onStartChild(String namespace, String localName, String prefix, Attributes attributes, DeserializationContext context) throws SAXException { if (log.isDebugEnabled()) { log.debug("Enter: ArrayDeserializer.onStartChild()"); } // If the position attribute is set, // use it to update the current index if (attributes != null) { String pos = Constants.getValue(attributes, Constants.URIS_SOAP_ENC, Constants.ATTR_POSITION); if (pos != null) { if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) { throw new SAXException(Messages.getMessage("noSparseArray")); } int leftBracketIndex = pos.lastIndexOf('['); int rightBracketIndex = pos.lastIndexOf(']'); if (leftBracketIndex == -1 || rightBracketIndex == -1 || rightBracketIndex < leftBracketIndex) { throw new SAXException( Messages.getMessage("badPosition00", pos)); } curIndex = convertToIndex(pos.substring(leftBracketIndex + 1, rightBracketIndex), "badPosition00"); } // If the xsi:nil attribute, set the value to null // and return since there is nothing to deserialize. if (context.isNil(attributes)) { setChildValue(null, new Integer(curIndex++)); return null; } } // Use the xsi:type setting on the attribute if it exists. QName itemType = context.getTypeFromAttributes(namespace, localName, attributes); // Get the deserializer for the type. Deserializer dSer = null; if (itemType != null && (context.getCurElement().getHref() == null)) { dSer = context.getDeserializerForType(itemType); } if (dSer == null) { // No deserializer can be found directly. Need to look harder QName defaultType = defaultItemType; Class javaType = null; if (arrayClass != null && arrayClass.isArray() && defaultType == null) { javaType = arrayClass.getComponentType(); defaultType = context.getTypeMapping().getTypeQName(javaType); } // We don't have a deserializer, the safest thing to do // is to set up using the DeserializerImpl below. // The DeserializerImpl will take care of href/id and // install the appropriate serializer, etc. The problem // is that takes a lot of time and will occur // all the time if no xsi:types are sent. Most of the // time an item is a simple schema type (i.e. String) // so the following shortcut is used to get a Deserializer // for these cases. if (itemType == null && dSer == null) { if (defaultType != null && SchemaUtils.isSimpleSchemaType(defaultType)) { dSer = context.getDeserializer(javaType, defaultType); } } // If no deserializer is // found, the deserializer is set to DeserializerImpl(). // It is possible that the element has an href, thus we // won't know the type until the definitition is encountered. if (dSer == null) { dSer = new DeserializerImpl(); // Determine a default type for the deserializer if (itemType == null) { dSer.setDefaultType(defaultType); } } } // Register the callback value target, and // keep track of this index so we know when it has been set. dSer.registerValueTarget( new DeserializerTarget(this, new Integer(curIndex))); // The framework handles knowing when the value is complete, as // long as we tell it about each child we're waiting on... addChildDeserializer(dSer); curIndex++; // In case of multi-array, we need to specify the destination class // of the children elements of this element array deserializer. context.setDestinationClass(arrayClass.getComponentType()); if (log.isDebugEnabled()) { log.debug("Exit: ArrayDeserializer.onStartChild()"); } return (SOAPHandler)dSer; } public void onEndChild(String namespace, String localName, DeserializationContext context) throws SAXException { // reverse onStartChild operation. context.setDestinationClass(arrayClass); } public void characters(char[] chars, int i, int i1) throws SAXException { for (int idx = i; i < i1; i++) { if (!Character.isWhitespace(chars[idx])) throw new SAXException(Messages.getMessage("charsInArray")); } } /** * set is called during deserialization to assign * the Object value to the array position indicated by hint. * The hint is always a single Integer. If the array being * deserialized is a multi-dimensional array, the hint is * converted into a series of indices to set the correct * nested position. * The array deserializer always deserializes into * an ArrayList, which is converted and copied into the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -