XStream
  1. XStream
  2. XSTR-16

New ObjectWithFieldsConverter

    Details

    • Type: Improvement Improvement
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Duplicate
    • Affects Version/s: 0.2
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None

      Description

      import com.thoughtworks.xstream.alias.ClassMapper;
      import com.thoughtworks.xstream.alias.ElementMapper;
      import com.thoughtworks.xstream.converters.Converter;
      import com.thoughtworks.xstream.converters.ConverterLookup;
      import com.thoughtworks.xstream.converters.reference.*;
      import com.thoughtworks.xstream.objecttree.ObjectTree;
      import com.thoughtworks.xstream.xml.XMLReader;
      import com.thoughtworks.xstream.xml.XMLWriter;

      public class ObjectWithFieldsConverter implements Converter {

      private ClassMapper classMapper;
      private CircularityTracker circularityTracker = new CircularityTracker();

      private ElementMapper elementMapper;

      public ObjectWithFieldsConverter(ClassMapper classMapper,ElementMapper elementMapper)

      { this.classMapper = classMapper; this.elementMapper = elementMapper; }

      public boolean canConvert(Class type)

      { return true; }

      public void toXML(ObjectTree objectGraph, XMLWriter xmlWriter, ConverterLookup converterLookup) {
      String[] fieldNames = objectGraph.fieldNames();

      for (int i = 0; i < fieldNames.length; i++) {
      String fieldName = fieldNames[i];

      objectGraph.push(fieldName);

      Object fieldObject = objectGraph.get();
      if ( fieldObject != null) {

      int referenceID = circularityTracker.contains(fieldObject);

      if( referenceID > -1 )

      { xmlWriter.startElement(fieldName); xmlWriter.addAttribute("idref", referenceID+""); xmlWriter.endElement(); }

      else

      { referenceID = circularityTracker.track(fieldObject); writeFieldAsXML(xmlWriter, elementMapper.toXml(fieldName), objectGraph, converterLookup, referenceID); }

      }else

      { // write null reference }

      objectGraph.pop();
      }
      }

      private void writeFieldAsXML(XMLWriter xmlWriter, String fieldName, ObjectTree objectGraph, ConverterLookup converterLookup, int referenceID)

      { xmlWriter.startElement(fieldName); xmlWriter.addAttribute("id", referenceID+""); writeClassAttributeInXMLIfNotDefaultImplementation(objectGraph, xmlWriter); Converter converter = converterLookup.lookupConverterForType(objectGraph.type()); converter.toXML(objectGraph, xmlWriter, converterLookup); xmlWriter.endElement(); }

      protected void writeClassAttributeInXMLIfNotDefaultImplementation(ObjectTree objectGraph, XMLWriter xmlWriter) {
      Class actualType = objectGraph.get().getClass();
      Class defaultType = classMapper.lookupDefaultType(objectGraph.type());
      if (!actualType.equals(defaultType))

      { xmlWriter.addAttribute("class", classMapper.lookupName(actualType)); }

      }

      public void fromXML(final ObjectTree objectGraph, XMLReader xmlReader, ConverterLookup converterLookup, Class requiredType) {
      objectGraph.create(requiredType);
      while (xmlReader.nextChild())

      { objectGraph.push(elementMapper.fromXml(xmlReader.name())); Class type = determineWhichImplementationToUse(xmlReader, objectGraph); Converter converter = converterLookup.lookupConverterForType(type); converter.fromXML(objectGraph, xmlReader, converterLookup, type); objectGraph.pop(); xmlReader.pop(); }

      }

      private Class determineWhichImplementationToUse(XMLReader xmlReader, final ObjectTree objectGraph) {
      String classAttribute = xmlReader.attribute("class");
      Class type;
      if (classAttribute == null)

      { type = objectGraph.type(); }

      else

      { type = classMapper.lookupType(classAttribute); }

      return type;
      }

      }

      // NOW - Try and use this on a JTable

      // What are we going to do about null pointers? They should still be referenced:

      //<someFieldName null/>

      // Otherwise the complete state of the object will not be serialised

        People

        • Assignee:
          Unassigned
          Reporter:
          ben
        • Votes:
          0 Vote for this issue
          Watchers:
          0 Start watching this issue

          Dates

          • Created:
            Updated:
            Resolved: