collectionconverter.java
来自「xstream是一个把java object序列化成xml文件的开源库,轻便好用」· Java 代码 · 共 67 行
JAVA
67 行
package com.thoughtworks.xstream.converters.collections;import com.thoughtworks.xstream.alias.ClassMapper;import com.thoughtworks.xstream.converters.MarshallingContext;import com.thoughtworks.xstream.converters.UnmarshallingContext;import com.thoughtworks.xstream.core.JVM;import com.thoughtworks.xstream.io.HierarchicalStreamReader;import com.thoughtworks.xstream.io.HierarchicalStreamWriter;import com.thoughtworks.xstream.mapper.Mapper;import java.util.*;/** * Converts most common Collections (Lists and Sets) to XML, specifying a nested * element for each item. * <p/> * <p>Supports java.util.ArrayList, java.util.HashSet, * java.util.LinkedList, java.util.Vector and java.util.LinkedHashSet.</p> * * @author Joe Walnes */public class CollectionConverter extends AbstractCollectionConverter { /** * @deprecated As of 1.1.1, use other constructor. */ public CollectionConverter(ClassMapper classMapper, String classAttributeIdentifier) { super(classMapper, classAttributeIdentifier); } public CollectionConverter(Mapper mapper) { super(mapper); } public boolean canConvert(Class type) { return type.equals(ArrayList.class) || type.equals(HashSet.class) || type.equals(LinkedList.class) || type.equals(Vector.class) || (JVM.is14() && type.getName().equals("java.util.LinkedHashSet")); } public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { Collection collection = (Collection) source; for (Iterator iterator = collection.iterator(); iterator.hasNext();) { Object item = iterator.next(); writeItem(item, context, writer); } } public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { Collection collection = (Collection) createCollection(context.getRequiredType()); populateCollection(reader, context, collection); return collection; } protected void populateCollection(HierarchicalStreamReader reader, UnmarshallingContext context, Collection collection) { while (reader.hasMoreChildren()) { reader.moveDown(); Object item = readItem(reader, context, collection); collection.add(item); reader.moveUp(); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?