Index: codegen/src/main/java/org/exolab/javasource/JClass.java =================================================================== --- codegen/src/main/java/org/exolab/javasource/JClass.java (revision 7165) +++ codegen/src/main/java/org/exolab/javasource/JClass.java (working copy) @@ -99,7 +99,7 @@ /** * {@inheritDoc} */ - public final void addMember(final JMember jMember) { + public void addMember(final JMember jMember) { if (jMember instanceof JField) { addField((JField) jMember); } else if (jMember instanceof JMethod) { @@ -153,7 +153,7 @@ /** * {@inheritDoc} */ - public final void print(final JSourceWriter jsw, final boolean classOnly) { + public void print(final JSourceWriter jsw, final boolean classOnly) { if (jsw == null) { throw new IllegalArgumentException("argument 'jsw' should not be null."); } Index: codegen/src/main/java/org/exolab/javasource/JType.java =================================================================== --- codegen/src/main/java/org/exolab/javasource/JType.java (revision 7165) +++ codegen/src/main/java/org/exolab/javasource/JType.java (working copy) @@ -88,9 +88,9 @@ * * @param name The name of the type. */ - protected JType(final String name) { + public JType(final String name) { super(); - + setName(name); } @@ -117,7 +117,7 @@ /** * Returns true if this type represents an Array. - * + * * @return True if this type represents an Array. */ public final boolean isArray() { @@ -126,20 +126,20 @@ /** * Returns true if this type represents a Java primitive type. - * + * * @return True if this type represents a Java primitive type. */ public final boolean isPrimitive() { return (this instanceof JPrimitiveType); } - + /** * Sets the qualified name of this type. * @param name the (qualified) name of the type */ protected void setName(final String name) { this._name = name; - } + } //-------------------------------------------------------------------------- } Index: codegen/src/main/java/org/exolab/castor/builder/FactoryState.java =================================================================== --- codegen/src/main/java/org/exolab/castor/builder/FactoryState.java (revision 7165) +++ codegen/src/main/java/org/exolab/castor/builder/FactoryState.java (working copy) @@ -56,6 +56,7 @@ import org.exolab.castor.builder.info.FieldInfo; import org.exolab.castor.xml.schema.Annotated; import org.exolab.javasource.JClass; +import org.exolab.javasource.JEnum; /** * A class used to save State information for the SourceFactory. @@ -117,6 +118,25 @@ */ public FactoryState(final String className, final SGStateInfo sgState, final String packageName, final XMLBindingComponent component) { + this(className, sgState, packageName, component, false); + } + + /** + * Constructs a factory state with the option of choosing between JClass and JEnum. + * + * @param className + * Class name of the class currently being generated. + * @param sgState + * Source Generator State object + * @param packageName + * package name for generated code. + * @param component + * TODO + * @param enumeration + * use a JEnum instead if a JClass + */ + public FactoryState(final String className, final SGStateInfo sgState, + final String packageName, final XMLBindingComponent component, final boolean enumeration) { if (sgState == null) { throw new IllegalArgumentException("SGStateInfo cannot be null."); } @@ -129,7 +149,11 @@ // _processed = ((FactoryState)resolver)._processed; // } - _jClass = new JClass(className); + if (enumeration) { + _jClass = new JEnum(className); + } else { + _jClass = new JClass(className); + } // if configured, try automatic class name conflict resolution if (_sgState.getSourceGenerator().isAutomaticConflictResolution()) { Index: codegen/src/main/java/org/exolab/castor/builder/factory/SourceFactory.java =================================================================== --- codegen/src/main/java/org/exolab/castor/builder/factory/SourceFactory.java (revision 7165) +++ codegen/src/main/java/org/exolab/castor/builder/factory/SourceFactory.java (working copy) @@ -792,7 +793,8 @@ className = resolveClassName(className, packageName); - FactoryState state = new FactoryState(className, sgState, packageName, comp); + FactoryState state = new FactoryState(className, sgState, packageName, comp, + (enumeration && getConfig().useJava50())); state.setParent(sgState.getCurrentFactoryState()); @@ -1938,6 +1940,9 @@ private void processEnumerationAsNewObject(final ExtendedBinding binding, final SimpleType simpleType, final FactoryState state) { _enumerationFactory.processEnumerationAsNewObject(binding, simpleType, state); + if (_testable) { + createTestableMethods(state.getJClass(), state); + } } //-- processEnumerationAsNewObject /** Index: codegen/src/main/java/org/exolab/castor/builder/factory/EnumerationFactory.java =================================================================== --- codegen/src/main/java/org/exolab/castor/builder/factory/EnumerationFactory.java (revision 7165) +++ codegen/src/main/java/org/exolab/castor/builder/factory/EnumerationFactory.java (working copy) @@ -17,8 +17,6 @@ import java.util.Enumeration; -import org.castor.xml.JavaNaming; -import org.castor.xml.JavaNamingImpl; import org.exolab.castor.builder.BuilderConfiguration; import org.exolab.castor.builder.FactoryState; import org.exolab.castor.builder.GroupNaming; @@ -37,6 +35,8 @@ import org.exolab.javasource.JClass; import org.exolab.javasource.JConstructor; import org.exolab.javasource.JDocComment; +import org.exolab.javasource.JEnum; +import org.exolab.javasource.JEnumConstant; import org.exolab.javasource.JField; import org.exolab.javasource.JMethod; import org.exolab.javasource.JModifiers; @@ -116,8 +116,7 @@ component.setBinding(binding); component.setView(simpleType); } - - + //-- select naming for types and instances boolean useValuesAsName = true; useValuesAsName = selectNamingScheme(component, enumeration, useValuesAsName); @@ -130,6 +129,13 @@ if (component.getJavaClassName() != null) { className = component.getJavaClassName(); } + + // the java 5 way -> create an enum + if (state.getJClass() instanceof JEnum) { + createJava5Enum(simpleType, state, component, + useValuesAsName, enumeration); + return; + } jClass.addImport("java.util.Hashtable"); JField field = null; @@ -271,7 +277,78 @@ createGetTypeMethod(jClass, className); } //-- processEnumerationAsNewObject + + private void createJava5Enum(final SimpleType simpleType, + final FactoryState state, final XMLBindingComponent component, + final boolean useValuesAsName, final Enumeration enumeration) { + JEnum jEnum = (JEnum) state.getJClass(); + // add value field + JField jField = new JField(new JClass("java.lang.String"), "value"); + JModifiers modifiers = new JModifiers(); + modifiers.setFinal(true); + modifiers.makePrivate(); + jField.setModifiers(modifiers); + jEnum.addField(jField); + + JMethod valueMethod = new JMethod("value", new JClass("java.lang.String"), + "the value of this constant"); + valueMethod.setSourceCode("return this.value;"); + jEnum.addMethod(valueMethod, false); + + JMethod fromValueMethod = new JMethod("fromValue", jEnum, "the constant for this value"); + fromValueMethod.addParameter(new JParameter(new JClass("java.lang.String"), "value")); + JSourceCode sourceCode = new JSourceCode(); + sourceCode.add("for (" + jEnum.getLocalName() + " c: " + + jEnum.getLocalName() + ".values()) {"); + sourceCode.add("if (c.value.equals(value)) {"); + sourceCode.add("return c;"); + sourceCode.add("}"); + sourceCode.add("}"); + sourceCode.add("throw new IllegalArgumentException(value);"); + fromValueMethod.setSourceCode(sourceCode); + modifiers = new JModifiers(); + modifiers.setStatic(true); + fromValueMethod.setModifiers(modifiers); + jEnum.addMethod(fromValueMethod, false); + + JMethod setValueMethod = new JMethod("setValue"); + setValueMethod.addParameter(new JParameter(new JClass("java.lang.String"), "value")); + jEnum.addMethod(setValueMethod, false); + + JMethod toStringMethod = new JMethod("toString", + new JClass("java.lang.String"), "the value of this constant"); + toStringMethod.setSourceCode("return this.value;"); + jEnum.addMethod(toStringMethod, false); + + JConstructor constructor = jEnum.createConstructor(); + constructor.addParameter(new JParameter(new JClass("java.lang.String"), "value")); + constructor.setSourceCode("this.value = value;"); + modifiers = new JModifiers(); + modifiers.makePrivate(); + constructor.setModifiers(modifiers); + jEnum.addConstructor(constructor); + + int enumCount = 0; + while (enumeration.hasMoreElements()) { + Facet facet = (Facet) enumeration.nextElement(); + JEnumConstant enumConstant; + if (useValuesAsName) { + enumConstant = new JEnumConstant( + translateEnumValueToIdentifier(component.getEnumBinding(), facet), + new String[]{"\"" + facet.getValue() + "\""}); + } else { + enumConstant = new JEnumConstant( + "VALUE_" + enumCount, new String[]{"\"" + + facet.getValue() + "\""}); + } + jEnum.addConstant(enumConstant); + enumCount++; + } + } + + + /** * Returns the JSourceCode instance for the current init() method, dealing with * static initializer limits of the JVM by creating new init() methods