⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 collectionconverter.java

📁 xstream是一个把java object序列化成xml文件的开源库,轻便好用
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -