private XStream createStream() { XStream stream = new XStream(); stream.registerConverter(new BeanDefinitionConverter(stream.getMapper())); stream.registerConverter(new BeanDefinitionHolderConverter(stream.getMapper())); stream.registerConverter(new TypedStringValueConverter()); stream.registerConverter(new ManagedCollectionConverter(stream.getMapper())); stream.registerConverter(new ManagedMapConverter(stream.getMapper())); stream.registerConverter(new RuntimeBeanReferenceConverter()); stream.alias("map", ManagedMap.class); stream.alias("list", ManagedList.class); stream.alias("set", ManagedSet.class); stream.alias("array", ManagedArray.class); stream.aliasType("bean", BeanDefinition.class); stream.alias("bean", BeanDefinitionHolder.class); stream.alias("ref", RuntimeBeanReference.class); return stream; }
/** * Parse an array element. */ public Object parseArrayElement(Element arrayEle, BeanDefinition bd) { String elementType = arrayEle.getAttribute(BeanDefinitionParserDelegate.VALUE_TYPE_ATTRIBUTE); NodeList nl = arrayEle.getChildNodes(); ManagedArray target = new ManagedArray(elementType, nl.getLength()); target.setSource(extractSource(arrayEle)); target.setElementTypeName(elementType); target.setMergeEnabled(parseMergeAttribute(arrayEle)); parseCollectionElements(nl, target, bd, elementType); return target; }
/** * Parse an array element. */ public Object parseArrayElement(Element arrayEle, BeanDefinition bd) { String elementType = arrayEle.getAttribute(VALUE_TYPE_ATTRIBUTE); NodeList nl = arrayEle.getChildNodes(); ManagedArray target = new ManagedArray(elementType, nl.getLength()); target.setSource(extractSource(arrayEle)); target.setElementTypeName(elementType); target.setMergeEnabled(parseMergeAttribute(arrayEle)); parseCollectionElements(nl, target, bd, elementType); return target; }
/** * Determines what kind of collection (or array) the given value is and call handlers based on the determined type * * @param value collection value to process * @param propertyName name of the property which has the collection value * @param nestedBeanStack stack of bean containers which contains the collection property */ protected void visitCollection(Object value, String propertyName, Stack<BeanDefinitionHolder> nestedBeanStack) { if (value instanceof Object[] || (value instanceof ManagedArray)) { visitArray(value, propertyName, nestedBeanStack); } else if (value instanceof List) { visitList((List<?>) value, propertyName, nestedBeanStack); } else if (value instanceof Set) { visitSet((Set<?>) value, propertyName, nestedBeanStack); } else if (value instanceof Map) { visitMap((Map<?, ?>) value, propertyName, nestedBeanStack); } }
/** * Iterates through the array values and calls helpers to process the value * * @param array the array to process * @param propertyName name of the property which has the array value * @param nestedBeanStack stack of bean containers which contains the array property */ protected void visitArray(Object array, String propertyName, Stack<BeanDefinitionHolder> nestedBeanStack) { Object[] arrayVal = null; if (array instanceof ManagedArray) { arrayVal = (Object[]) ((ManagedArray) array).getSource(); } else { arrayVal = (Object[]) array; } Object[] newArray = new Object[arrayVal.length]; for (int i = 0; i < arrayVal.length; i++) { Object elem = arrayVal[i]; if (isStringValue(elem)) { elem = processArrayStringPropertyValue(propertyName, arrayVal, getString(elem), i, nestedBeanStack, beanProcessors); } else { elem = visitPropertyValue(propertyName, elem, nestedBeanStack); } newArray[i] = elem; } if (array instanceof ManagedArray) { ((ManagedArray) array).setSource(newArray); } else { array = newArray; } }
/** * Indicate whether the given value is a collection * * @param value value to test * @return boolean true if the value is a collection, false if not */ protected boolean isCollectionValue(Object value) { boolean isCollection = false; if (value instanceof Object[] || (value instanceof ManagedArray) || (value instanceof List) || (value instanceof Set) || (value instanceof Map)) { isCollection = true; } return isCollection; }
public boolean canConvert(Class type) { return type.equals(ManagedList.class) || type.equals(ManagedArray.class) || type.equals(ManagedSet.class); }