Clover coverage report - XStream - 0.1
Coverage timestamp: Fri Oct 24 2003 13:50:04 BST
file stats: LOC: 86   Methods: 7
NCLOC: 67   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
ObjectWithFieldsConverter.java 100% 100% 100% 100%
coverage
 1   
 package com.thoughtworks.xstream.converters.composite;
 2   
 
 3   
 import com.thoughtworks.xstream.alias.ClassMapper;
 4   
 import com.thoughtworks.xstream.converters.Converter;
 5   
 import com.thoughtworks.xstream.converters.ConverterLookup;
 6   
 import com.thoughtworks.xstream.objecttree.ObjectTree;
 7   
 import com.thoughtworks.xstream.xml.XMLReader;
 8   
 import com.thoughtworks.xstream.xml.XMLWriter;
 9   
 
 10   
 public class ObjectWithFieldsConverter implements Converter {
 11   
 
 12   
     private ClassMapper classMapper;
 13   
 
 14  35
     public ObjectWithFieldsConverter(ClassMapper classMapper) {
 15  35
         this.classMapper = classMapper;
 16   
     }
 17   
 
 18  61
     public boolean canConvert(Class type) {
 19  61
         return true;
 20   
     }
 21   
 
 22  31
     public void toXML(ObjectTree objectGraph, XMLWriter xmlWriter, ConverterLookup converterLookup) {
 23  31
         String[] fieldNames = objectGraph.fieldNames();
 24  31
         for (int i = 0; i < fieldNames.length; i++) {
 25  64
             String fieldName = fieldNames[i];
 26   
 
 27  64
             objectGraph.push(fieldName);
 28   
 
 29  64
             if (objectGraph.get() != null) {
 30  62
                 writeFieldAsXML(xmlWriter, fieldName, objectGraph, converterLookup);
 31   
             }
 32   
 
 33  64
             objectGraph.pop();
 34   
         }
 35   
     }
 36   
 
 37  62
     private void writeFieldAsXML(XMLWriter xmlWriter, String fieldName, ObjectTree objectGraph, ConverterLookup converterLookup) {
 38  62
         xmlWriter.startElement(fieldName);
 39   
 
 40  62
         writeClassAttributeInXMLIfNotDefaultImplementation(objectGraph, xmlWriter);
 41  62
         Converter converter = converterLookup.lookupConverterForType(objectGraph.type());
 42  62
         converter.toXML(objectGraph, xmlWriter, converterLookup);
 43   
 
 44  62
         xmlWriter.endElement();
 45   
     }
 46   
 
 47  62
     protected void writeClassAttributeInXMLIfNotDefaultImplementation(ObjectTree objectGraph, XMLWriter xmlWriter) {
 48  62
         Class actualType = objectGraph.get().getClass();
 49  62
         Class defaultType = classMapper.lookupDefaultType(objectGraph.type());
 50  62
         if (!actualType.equals(defaultType)) {
 51  5
             xmlWriter.addAttribute("class", classMapper.lookupName(actualType));
 52   
         }
 53   
     }
 54   
 
 55  30
     public void fromXML(final ObjectTree objectGraph, XMLReader xmlReader, ConverterLookup converterLookup, Class requiredType) {
 56  30
         objectGraph.create(requiredType);
 57  30
         String[] fieldNames = objectGraph.fieldNames();
 58  30
         for (int i = 0; i < fieldNames.length; i++) {
 59  49
             String fieldName = fieldNames[i];
 60  49
             if (xmlReader.childExists(fieldName)) {
 61  47
                 objectGraph.push(fieldName);
 62  47
                 xmlReader.child(fieldName);
 63   
 
 64  47
                 Class type = determineWhichImplementationToUse(xmlReader, objectGraph);
 65  47
                 Converter converter = converterLookup.lookupConverterForType(type);
 66  47
                 converter.fromXML(objectGraph, xmlReader, converterLookup, type);
 67   
 
 68  47
                 xmlReader.pop();
 69  47
                 objectGraph.pop();
 70   
             }
 71   
         }
 72   
     }
 73   
 
 74  47
     private Class determineWhichImplementationToUse(XMLReader xmlReader, final ObjectTree objectGraph) {
 75  47
         String classAttribute = xmlReader.attribute("class");
 76  47
         Class type;
 77  47
         if (classAttribute == null) {
 78  42
             type = objectGraph.type();
 79   
         } else {
 80  5
             type = classMapper.lookupType(classAttribute);
 81   
         }
 82  47
         return type;
 83   
     }
 84   
 
 85   
 }
 86