KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > util > RetrotranslatorEnumConverter


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 /**
11  * Converts retrotranslator's enum class to make it match with JDK5's native data format.
12  *
13  *
14  * @author Kohsuke Kawaguchi
15  */

16 public class RetrotranslatorEnumConverter implements Converter {
17
18     public boolean canConvert(Class JavaDoc type) {
19         return Enum_.class.isAssignableFrom(type);
20     }
21
22     public void marshal(Object JavaDoc source, HierarchicalStreamWriter writer, MarshallingContext context) {
23         writer.setValue(((Enum_)source).name());
24     }
25
26     public Object JavaDoc unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
27         Class JavaDoc type = context.getRequiredType();
28         if (type.getSuperclass() != Enum_.class) {
29             type = type.getSuperclass(); // polymorphic enums
30
}
31         String JavaDoc value = reader.getValue();
32         if(value==null || value.trim().length()==0) {
33             /*
34              backward compatibility mode. read retroweaver's format from:
35
36               <mode>
37                 <description>Leave this machine for tied jobs only</description>
38                 <ordinal>1</ordinal>
39                 <name>EXCLUSIVE</name>
40               </mode>
41             */

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