KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > util > RetroweaverEnumConverter


1 package hudson.util;
2
3 import com.rc.retroweaver.runtime.Enum_;
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.io.HierarchicalStreamReader;
8 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
9
10 /**
11  * Converts retroweaver's enum class.
12  *
13  * <p>
14  * Hudson &lt; 1.60 used to store retroweaver's {@link Enum_} class as-is,
15  * which was incompatible with how it handles enums in JDK1.5. This converter
16  * makes sure that we use the same data format.
17  *
18  * @author Kohsuke Kawaguchi
19  */

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

46
47             while(reader.hasMoreChildren()) {
48                 reader.moveDown();
49                 if(reader.getNodeName().equals("name"))
50                     value = reader.getValue();
51                 reader.moveUp();
52             }
53         }
54
55         return Enum_.valueOf(type, value);
56     }
57 }
58
Popular Tags