1 package org.codehaus.xfire.aegis.type.java5;
2
3 import org.codehaus.xfire.MessageContext;
4 import org.codehaus.xfire.XFireRuntimeException;
5 import org.codehaus.xfire.aegis.MessageReader;
6 import org.codehaus.xfire.aegis.MessageWriter;
7 import org.codehaus.xfire.aegis.type.Type;
8 import org.codehaus.xfire.fault.XFireFault;
9 import org.codehaus.xfire.soap.SoapConstants;
10 import org.codehaus.yom.Attribute;
11 import org.codehaus.yom.Element;
12
13 public class EnumType
14 extends Type
15 {
16 @Override
17 public Object readObject(MessageReader reader, MessageContext context)
18 throws XFireFault
19 {
20 String value = reader.getValue();
21
22 return Enum.valueOf(getTypeClass(), value);
23 }
24
25 @Override
26 public void writeObject(Object object, MessageWriter writer, MessageContext context)
27 throws XFireFault
28 {
29 writer.writeValue(((Enum) object).toString());
30 }
31
32 @Override
33 public void setTypeClass(Class typeClass)
34 {
35 if (!typeClass.isEnum())
36 {
37 throw new XFireRuntimeException("Type class must be an enum.");
38 }
39
40 super.setTypeClass(typeClass);
41 }
42
43 @Override
44 public void writeSchema(Element root)
45 {
46 Element simple = new Element(SoapConstants.XSD_PREFIX + ":simpleType",
47 SoapConstants.XSD);
48 simple.addAttribute(new Attribute("name", getSchemaType().getLocalPart()));
49 root.appendChild(simple);
50
51 Element restriction = new Element(SoapConstants.XSD_PREFIX + ":restriction",
52 SoapConstants.XSD);
53 restriction.addAttribute(new Attribute("base", SoapConstants.XSD_PREFIX + ":string"));
54 simple.appendChild(restriction);
55
56 Object[] constants = getTypeClass().getEnumConstants();
57
58 for (Object constant : constants)
59 {
60 Element enumeration = new Element(SoapConstants.XSD_PREFIX + ":enumeration",
61 SoapConstants.XSD);
62 enumeration.addAttribute(new Attribute("value", ((Enum) constant).toString()));
63 restriction.appendChild(enumeration);
64 }
65 }
66
67 @Override
68 public boolean isComplex()
69 {
70 return true;
71 }
72 }