1 package com.thoughtworks.xstream.converters.reflection; 2 3 import com.thoughtworks.xstream.converters.ConversionException; 4 import com.thoughtworks.xstream.converters.Converter; 5 import com.thoughtworks.xstream.converters.MarshallingContext; 6 import com.thoughtworks.xstream.converters.UnmarshallingContext; 7 import com.thoughtworks.xstream.core.util.CustomObjectInputStream; 8 import com.thoughtworks.xstream.core.util.CustomObjectOutputStream; 9 import com.thoughtworks.xstream.io.HierarchicalStreamReader; 10 import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 11 import com.thoughtworks.xstream.mapper.Mapper; 12 13 import java.io.Externalizable ; 14 import java.io.IOException ; 15 import java.io.NotActiveException ; 16 import java.io.ObjectInput ; 17 import java.io.ObjectInputValidation ; 18 import java.io.ObjectOutput ; 19 import java.util.Map ; 20 21 27 public class ExternalizableConverter implements Converter { 28 29 private Mapper mapper; 30 31 public ExternalizableConverter(Mapper mapper) { 32 this.mapper = mapper; 33 } 34 35 public boolean canConvert(Class type) { 36 return Externalizable .class.isAssignableFrom(type); 37 } 38 39 public void marshal(Object source, final HierarchicalStreamWriter writer, final MarshallingContext context) { 40 try { 41 Externalizable externalizable = (Externalizable ) source; 42 CustomObjectOutputStream.StreamCallback callback = new CustomObjectOutputStream.StreamCallback() { 43 public void writeToStream(Object object) { 44 if (object == null) { 45 writer.startNode("null"); 46 writer.endNode(); 47 } else { 48 writer.startNode(mapper.serializedClass(object.getClass())); 49 context.convertAnother(object); 50 writer.endNode(); 51 } 52 } 53 54 public void writeFieldsToStream(Map fields) { 55 throw new UnsupportedOperationException (); 56 } 57 58 public void defaultWriteObject() { 59 throw new UnsupportedOperationException (); 60 } 61 62 public void flush() { 63 writer.flush(); 64 } 65 66 public void close() { 67 throw new UnsupportedOperationException ("Objects are not allowed to call ObjecOutput.close() from writeExternal()"); 68 } 69 }; 70 ObjectOutput objectOutput = CustomObjectOutputStream.getInstance(context, callback); 71 externalizable.writeExternal(objectOutput); 72 } catch (IOException e) { 73 throw new ConversionException("Cannot serialize " + source.getClass().getName() + " using Externalization", e); 74 } 75 } 76 77 public Object unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext context) { 78 final Class type = context.getRequiredType(); 79 try { 80 Externalizable externalizable = (Externalizable ) type.newInstance(); 81 CustomObjectInputStream.StreamCallback callback = new CustomObjectInputStream.StreamCallback() { 82 public Object readFromStream() { 83 reader.moveDown(); 84 Object streamItem = context.convertAnother(type, mapper.realClass(reader.getNodeName())); 85 reader.moveUp(); 86 return streamItem; 87 } 88 89 public Map readFieldsFromStream() { 90 throw new UnsupportedOperationException (); 91 } 92 93 public void defaultReadObject() { 94 throw new UnsupportedOperationException (); 95 } 96 97 public void registerValidation(ObjectInputValidation validation, int priority) throws NotActiveException { 98 throw new NotActiveException ("stream inactive"); 99 } 100 101 public void close() { 102 throw new UnsupportedOperationException ("Objects are not allowed to call ObjectInput.close() from readExternal()"); 103 } 104 }; 105 ObjectInput objectInput = CustomObjectInputStream.getInstance(context, callback); 106 externalizable.readExternal(objectInput); 107 return externalizable; 108 } catch (InstantiationException e) { 109 throw new ConversionException("Cannot construct " + type.getClass(), e); 110 } catch (IllegalAccessException e) { 111 throw new ConversionException("Cannot construct " + type.getClass(), e); 112 } catch (IOException e) { 113 throw new ConversionException("Cannot externalize " + type.getClass(), e); 114 } catch (ClassNotFoundException e) { 115 throw new ConversionException("Cannot externalize " + type.getClass(), e); 116 } 117 } 118 } 119 | Popular Tags |