public JavaSource getEnumClass(SimpleTypeSG pController) throws SAXException {
JavaQName valueType = super.getRuntimeType(pController);
JavaQName qNameArray = JavaQNameImpl.getArray(qName);
JavaSource js = pController.getSchema().getJavaSourceFactory().newJavaSource(qName, JavaSource.PUBLIC);
js.addImplements(Serializable.class);
JavaField name = js.newJavaField("name", String.class, JavaSource.PRIVATE);
name.setFinal(true);
JavaField value = js.newJavaField("value", valueType, JavaSource.PRIVATE);
value.setFinal(true);
JavaField lexicalValue = js.newJavaField("lexicalValue", String.class, JavaSource.PRIVATE);
lexicalValue.setFinal(true);
List instanceParams = new ArrayList();
for (int i = 0; i < values.length; i++) {
JavaField _f = js.newJavaField("_" + values[i].getName(), String.class, JavaSource.PUBLIC);
_f.setStatic(true);
_f.setFinal(true);
_f.addLine(JavaSource.getQuoted(values[i].getName()));
JavaField f = js.newJavaField(values[i].getName(), qName, JavaSource.PUBLIC);
f.addLine("new ", qName, "(", JavaSource.getQuoted(values[i].getName()), ", ",
super.getCastFromString(pController, values[i].getValue()), ", ",
JavaSource.getQuoted(values[i].getValue()), ");");
f.setStatic(true);
f.setFinal(true);
if (!instanceParams.isEmpty()) instanceParams.add(", ");
instanceParams.add(f);
}
JavaField instances = js.newJavaField("instances", qNameArray, JavaSource.PRIVATE);
instances.addLine(new Object[]{"new ", qNameArray, "{", instanceParams, "}"});
instances.setStatic(true);
instances.setFinal(true);
JavaConstructor con = js.newJavaConstructor(JavaSource.PRIVATE);
DirectAccessible pName = con.addParam(String.class, "pName");
DirectAccessible pValue = con.addParam(super.getRuntimeType(pController), "pValue");
DirectAccessible pLexicalValue = con.addParam(String.class, "pLexicalValue");
con.addLine(name, " = ", pName, ";");
con.addLine(value, " = ", pValue, ";");
con.addLine(lexicalValue, " = ", pLexicalValue, ";");
JavaMethod toStringMethod = js.newJavaMethod("toString", String.class, JavaSource.PUBLIC);
toStringMethod.addLine("return ", lexicalValue, ";");
JavaMethod getValueMethod = js.newJavaMethod("getValue", valueType, JavaSource.PUBLIC);
getValueMethod.addLine("return ", value, ";");
JavaMethod getNameMethod = js.newJavaMethod("getName", String.class, JavaSource.PUBLIC);
getNameMethod.addLine("return ", name, ";");
JavaMethod getInstancesMethod = js.newJavaMethod("getInstances", qNameArray, JavaSource.PUBLIC);
getInstancesMethod.setStatic(true);
getInstancesMethod.addLine("return ", instances, ";");
JavaMethod fromValueMethod = js.newJavaMethod("fromValue", qName, JavaSource.PUBLIC);
pValue = fromValueMethod.addParam(valueType, "pValue");
fromValueMethod.setStatic(true);
DirectAccessible i = fromValueMethod.addForArray(instances);
fromValueMethod.addIf(pController.getEqualsCheck(fromValueMethod, new Object[]{instances, "[", i, "].value"}, pValue));
fromValueMethod.addLine("return ", instances, "[", i, "];");
fromValueMethod.addEndIf();
fromValueMethod.addEndFor();
fromValueMethod.addThrowNew(IllegalArgumentException.class, JavaSource.getQuoted("Invalid value: "),
" + ", pValue);
JavaMethod fromNameMethod = js.newJavaMethod("fromName", qName, JavaSource.PUBLIC);
pName = fromNameMethod.addParam(String.class, "pName");
fromNameMethod.setStatic(true);
i = fromNameMethod.addForArray(instances);
fromNameMethod.addIf(instances, "[", i, "].name.equals(", pName, ")");
fromNameMethod.addLine("return ", instances, "[", i, "];");
fromNameMethod.addEndIf();
fromNameMethod.addEndFor();
fromNameMethod.addThrowNew(IllegalArgumentException.class, JavaSource.getQuoted("Invalid name: "),
" + ", pName);
JavaMethod fromStringMethod = js.newJavaMethod("fromString", qName, JavaSource.PUBLIC);
pValue = fromStringMethod.addParam(String.class, "pValue");
fromStringMethod.setStatic(true);
fromStringMethod.addLine("return ", fromValueMethod, "(",
super.getCastFromString(pController, fromStringMethod, pValue, null), ");");
if (js.isImplementing(Serializable.class)) {
JavaMethod readResolveMethod = js.newJavaMethod("readResolve", Object.class, JavaSource.PRIVATE);
readResolveMethod.addLine("return ", fromValueMethod, "(", value, ");");
}
return js;
}