1 package hudson.util; 2 3 import com.thoughtworks.xstream.converters.Converter; 4 import com.thoughtworks.xstream.converters.MarshallingContext; 5 import com.thoughtworks.xstream.converters.UnmarshallingContext; 6 import com.thoughtworks.xstream.io.HierarchicalStreamReader; 7 import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 8 import net.sf.retrotranslator.runtime.java.lang.Enum_; 9 10 16 public class RetrotranslatorEnumConverter implements Converter { 17 18 public boolean canConvert(Class type) { 19 return Enum_.class.isAssignableFrom(type); 20 } 21 22 public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { 23 writer.setValue(((Enum_)source).name()); 24 } 25 26 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { 27 Class type = context.getRequiredType(); 28 if (type.getSuperclass() != Enum_.class) { 29 type = type.getSuperclass(); } 31 String value = reader.getValue(); 32 if(value==null || value.trim().length()==0) { 33 42 43 while(reader.hasMoreChildren()) { 44 reader.moveDown(); 45 if(reader.getNodeName().equals("name")) 46 value = reader.getValue(); 47 reader.moveUp(); 48 } 49 } 50 51 return Enum_.valueOf(type, value); 52 } 53 } 54 | Popular Tags |