sujinwen
2017-07-26 0a7e6639ae4e193de0e15c86fde488c950f315d0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.awsle.aibatis.xml.engine.converters.collections;
 
import com.awsle.aibatis.xml.engine.alias.ClassMapper;
import com.awsle.aibatis.xml.engine.converters.ConverterLookup;
import com.awsle.aibatis.xml.engine.objecttree.ObjectTree;
import com.awsle.aibatis.xml.engine.xml.XMLReader;
import com.awsle.aibatis.xml.engine.xml.XMLWriter;
 
import java.util.Collection;
import java.util.Iterator;
 
public class CollectionConverter extends AbstractCollectionConverter {
 
    public CollectionConverter(ClassMapper classMapper) {
        super(classMapper);
    }
 
    public boolean canConvert(Class type) {
        return Collection.class.isAssignableFrom(type);
    }
 
    public void toXML(ObjectTree objectGraph, XMLWriter xmlWriter, ConverterLookup converterLookup) {
        Collection collection = (Collection) objectGraph.get();
        for (Iterator iterator = collection.iterator(); iterator.hasNext();) {
            Object item = iterator.next();
            writeItem(item, xmlWriter, converterLookup, objectGraph);
        }
    }
 
    public void fromXML(ObjectTree objectGraph, XMLReader xmlReader, ConverterLookup converterLookup, Class requiredType) {
        Collection collection = (Collection) createCollection(requiredType);
        int childCount = xmlReader.childCount();
        for (int i = 0; i < childCount; i++) {
            Object item = readItem(xmlReader, i, objectGraph, converterLookup);
            collection.add(item);
        }
        objectGraph.set(collection);
    }
 
}