You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@axis.apache.org by mike jones <mi...@gmail.com> on 2005/05/13 17:15:23 UTC

Enumeration help

Hi,
I have been looking through the mail list archive for an answer to this but 
could find an answer. 

I have a enumeration in java which I want to send via soap (see below) I 
have tried various things to do this but I have come up against probs for 
each one.
First I tried to write my own custon serializer (see below) the 
SerializerFactory instance was always lost in the typeMappings internally 
and defaulted back to a bean mapping.
Next I decided to try and use the EnumSerializer so change my class, which 
almost worked but this require the type of fields that define the values to 
be strings (whereas mine are instance of the class).
Has anyone got any advice about which way to go? 
Cheers 
Mike

8<--------------------------------------------------------------------------------------------

This is my class that I modified for the EnumSerializer...

public class WorkHome implements Serializable { 
protected static final Map INSTANCES = new HashMap();
public static final WorkHome WORK = new WorkHome("Work");
public static final WorkHome HOME = new WorkHome("Home");
static {
INSTANCES.put(WORK.toString(), WORK);
INSTANCES.put(HOME.toString(), HOME);
} 
private String name; 

private WorkHome(String name) { ... } 
protected Object readResolve() { ... }
public static Object getInstance(String name) { ... }

public String getValue() { ... }
public static Object fromValue(String value) { ... }
public static Object fromString(String value) { ... }
} 
}


Here are my attempts at my own serializer and deserialiser

public class EnumSer extends SimpleSerializer {
public EnumSer(Class javaType, QName xmlType) { 
super(javaType, xmlType); 
} 
public void serialize(QName name, Attributes attributes, Object value, 
SerializationContext context) throws IOException {
context.startElement(name, attributes); 
context.writeString(getValueAsString(value, context)); 
context.endElement(); 
} 
public String getValueAsString(Object arg0, SerializationContext arg1) {
// call to string
} 
public Element writeSchema(Class javaType, Types types) throws Exception { 
return writeEnumType(xmlType, javaType, types);
} 
public Element writeEnumType(QName qName, Class cls, Types types) throws 
NoSuchMethodException, IllegalAccessException, AxisFault {
Class base = cls;
// Create simpleType, restriction elements
Element simpleType = types.createElement("simpleType");
simpleType.setAttribute("name", cls.getName());
Element restriction = types.createElement("restriction");
simpleType.appendChild(restriction);
String baseType = types.writeType(base, null);
restriction.setAttribute("base", baseType);
// Create an enumeration using the field values
Field[] fields = cls.getDeclaredFields();

for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
int mod = field.getModifiers();

// Inspect each public static final field of the same type
// as the base
if (Modifier.isPublic(mod) && Modifier.isStatic(mod)
&& Modifier.isFinal(mod) && (field.getType() == base)) {

// Create an enumeration using the value specified
Element enumeration = types.createElement("enumeration");

enumeration.setAttribute("value", field.get(null).toString());
restriction.appendChild(enumeration);
}
}
return simpleType;
} 
}

public class EnumDeSer extends SimpleDeserializer {

private Method getInstanceMethod = null;
private static final Class[] STRING_CLASS = new Class[] { 
java.lang.String.class };
public EnumDeSer(Class arg0, QName arg1) {
super(arg0, arg1);
}
public Object makeValue(String source) throws Exception {
// Invoke the fromString static method to get the Enumeration value
if (isNil)
return null;
if (getInstanceMethod == null) {
try {
getInstanceMethod = MethodCache.getInstance().getMethod(
javaType, "getInstance", STRING_CLASS);
} catch (Exception e) {
throw new IntrospectionException(e.toString());
}
}
return getInstanceMethod.invoke(null, new Object[] { source });
}
}

the factories are exactly the same as the normal one (just with the 
serializer class changed)