xml-xmlbeans-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From daver...@apache.org
Subject cvs commit: xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/util XsTypeConverter.java
Date Sun, 21 Mar 2004 01:58:28 GMT
daveremy    2004/03/20 17:58:27

  Modified:    v2/src/binding/org/apache/xmlbeans/impl/binding/bts
                        JaxrpcEnumType.java
               v2/src/binding/org/apache/xmlbeans/impl/binding/compile
                        Schema2Java.java
               v2/src/binding/org/apache/xmlbeans/impl/binding/joust
                        ExpressionFactory.java JavaOutputStream.java
                        SourceJavaOutputStream.java
                        ValidatingJavaOutputStream.java
               v2/src/common/org/apache/xmlbeans/impl/common NameUtil.java
               v2/src/typeimpl/org/apache/xmlbeans/impl/util
                        XsTypeConverter.java
  Added:       v2/src/binding/org/apache/xmlbeans/impl/binding/compile
                        EnumerationPrintHelper.java
  Log:
  Added support for Jax-Rpc style enumerations.
  Contributed by Radu Preotiuc.
  
  Revision  Changes    Path
  1.2       +8 -0      xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/bts/JaxrpcEnumType.java
  
  Index: JaxrpcEnumType.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/bts/JaxrpcEnumType.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- JaxrpcEnumType.java	13 Mar 2004 03:46:57 -0000	1.1
  +++ JaxrpcEnumType.java	21 Mar 2004 01:58:27 -0000	1.2
  @@ -34,6 +34,14 @@
       private MethodName toXMLMethod;
   
       // ========================================================================
  +    // Constants
  +
  +    public final static MethodName DEFAULT_GET_VALUE = MethodName.create("getValue");
  +    public final static MethodName DEFAULT_FROM_VALUE = MethodName.create("fromValue");
  +    public final static MethodName DEFAULT_FROM_STRING = MethodName.create("fromString");
  +    public final static MethodName DEFAULT_TO_XML = MethodName.create("toXML");
  +
  +    // ========================================================================
       // Constructors
   
       public JaxrpcEnumType(BindingTypeName btName)
  
  
  
  1.17      +334 -29   xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/compile/Schema2Java.java
  
  Index: Schema2Java.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/compile/Schema2Java.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- Schema2Java.java	13 Mar 2004 00:33:22 -0000	1.16
  +++ Schema2Java.java	21 Mar 2004 01:58:27 -0000	1.17
  @@ -243,6 +243,7 @@
       for (Iterator i = scratchIterator(); i.hasNext();) {
         Scratch scratch = (Scratch) i.next();
         resolveJavaStructure(scratch);
  +      resolveJavaEnumeration(scratch);
         resolveJavaArray(scratch);
       }
     }
  @@ -265,7 +266,10 @@
         if (sType.isSimpleType()) {
           // simple types are atomic
           // todo: what about simple content, custom codecs, etc?
  -        scratch = new Scratch(sType, xmlName, Scratch.ATOMIC_TYPE);
  +        if (isEnumeration(sType))
  +          scratch = new Scratch(sType, xmlName, Scratch.ENUM_TYPE);
  +        else
  +          scratch = new Scratch(sType, xmlName, Scratch.ATOMIC_TYPE);
         } else if (sType.isDocumentType()) {
           scratch = new Scratch(sType, XmlTypeName.forGlobalName(XmlTypeName.ELEMENT, sType.getDocumentElementName()), Scratch.ELEMENT);
         } else if (sType.isAttributeType()) {
  @@ -318,6 +322,7 @@
           }
   
         case Scratch.STRUCT_TYPE:
  +      case Scratch.ENUM_TYPE:
           {
             structureCount += 1;
             JavaTypeName javaName = pickUniqueJavaName(scratch.getSchemaType());
  @@ -473,6 +478,16 @@
           bindingFile.addBindingType(structResult, true, true);
           break;
   
  +      case Scratch.ENUM_TYPE:
  +        JaxrpcEnumType enumResult = new JaxrpcEnumType(btName);
  +        enumResult.setGetValueMethod(JaxrpcEnumType.DEFAULT_GET_VALUE);
  +        enumResult.setFromValueMethod(JaxrpcEnumType.DEFAULT_FROM_VALUE);
  +        enumResult.setFromStringMethod(JaxrpcEnumType.DEFAULT_FROM_STRING);
  +        enumResult.setToXMLMethod(JaxrpcEnumType.DEFAULT_TO_XML);
  +        scratch.setBindingType(enumResult);
  +        bindingFile.addBindingType(enumResult, true, true);
  +        break;
  +
         case Scratch.LITERALARRAY_TYPE:
           WrappedArrayType arrayResult = new WrappedArrayType(btName);
           scratch.setBindingType(arrayResult);
  @@ -653,6 +668,28 @@
     }
   
     /**
  +   * Resolves a Java enumeration
  +   */
  +  private void resolveJavaEnumeration(Scratch scratch)
  +  {
  +    if (scratch.getCategory() != Scratch.ENUM_TYPE)
  +      return;
  +
  +    if (scratch.isStructureResolved())
  +      return;
  +
  +    scratch.setStructureResolved(true);
  +
  +    SchemaType baseType = scratch.getSchemaType().getBaseType();
  +
  +    BindingType bType = bindingTypeForSchemaType(baseType);
  +    assert bType != null : "Binding type for schema type \"" + baseType +
  +      "\" not found on the mLoader";
  +
  +    ((JaxrpcEnumType) scratch.getBindingType()).setBaseType(bType);
  +  }
  +
  +  /**
      * Picks a property name without colliding with names of
      * previously picked getters and setters.
      */
  @@ -776,7 +813,7 @@
         result = mLoader.getBindingType(boxedName);
         if (result != null)
           return result;
  -      
  +
         // Type is not in the current scratch area nor on the mLoader
         // We create it and add it to the file
         result = bindingFile.getBindingType(boxedName);
  @@ -1005,6 +1042,13 @@
     }
   
     /**
  +   * True if the given schema type is an enumeration type.
  +   */
  +  private static boolean isEnumeration(SchemaType sType) {
  +    return sType.getEnumerationValues() != null;
  +  }
  +
  +  /**
      * Scratch area corresponding to a schema type, used for the binding
      * computation.
      */
  @@ -1029,11 +1073,12 @@
       // categories of Scratch, established at ctor time
       public static final int ATOMIC_TYPE = 1;
       public static final int STRUCT_TYPE = 2;
  -    public static final int LITERALARRAY_TYPE = 3;
  -    public static final int SOAPARRAY_REF = 4;
  -    public static final int SOAPARRAY = 5;
  -    public static final int ELEMENT = 6;
  -    public static final int ATTRIBUTE = 7;
  +    public static final int ENUM_TYPE = 3;
  +    public static final int LITERALARRAY_TYPE = 4;
  +    public static final int SOAPARRAY_REF = 5;
  +    public static final int SOAPARRAY = 6;
  +    public static final int ELEMENT = 7;
  +    public static final int ATTRIBUTE = 8;
   
       public int getCategory() {
         return category;
  @@ -1185,7 +1230,8 @@
       private Scratch nextStructure() {
         while (si.hasNext()) {
           Scratch scratch = (Scratch) si.next();
  -        if (scratch.getCategory() == Scratch.STRUCT_TYPE)
  +        if (scratch.getCategory() == Scratch.STRUCT_TYPE ||
  +          scratch.getCategory() == Scratch.ENUM_TYPE)
             return scratch;
         }
         return null;
  @@ -1250,7 +1296,8 @@
      *
      */
     private void printClass(Scratch scratch) throws IOException {
  -    assert(scratch.getCategory() == Scratch.STRUCT_TYPE);
  +    assert(scratch.getCategory() == Scratch.STRUCT_TYPE ||
  +      scratch.getCategory() == Scratch.ENUM_TYPE);
       JavaTypeName javaName = scratch.getJavaName();
   
       String packageName = javaName.getPackage();
  @@ -1268,8 +1315,17 @@
       // begin writing class
       mJoust.startFile(packageName, shortClassName);
       mJoust.writeComment("Generated from schema type " + scratch.getXmlName());
  -    mJoust.startClass(Modifier.PUBLIC, baseJavaname, null);
   
  +    if (scratch.getCategory() == Scratch.STRUCT_TYPE)
  +      printJavaStruct(scratch, baseJavaname);
  +    else
  +      printJavaEnum(scratch, baseJavaname);
  +
  +    mJoust.endFile();
  +  }
  +
  +  private void printJavaStruct(Scratch scratch, String baseJavaName) throws IOException {
  +    mJoust.startClass(Modifier.PUBLIC, baseJavaName, null);
       Set seenFieldNames = new HashSet();
       // Write out the special "_value" property, if needed
       SimpleContentProperty scprop = scratch.getSimpleContentProperty();
  @@ -1304,25 +1360,6 @@
           prop.getGetterName().getSimpleName(), prop.getSetterName().getSimpleName());
       }
       mJoust.endClassOrInterface();
  -    mJoust.endFile();
  -  }
  -
  -  private String pickUniqueFieldName(String getter, Set seenNames) {
  -    String baseName;
  -
  -    if (getter.length() > 3 && getter.startsWith("get"))
  -      baseName = Character.toLowerCase(getter.charAt(3)) + getter.substring(4);
  -    else
  -      baseName = "field";
  -
  -    String fieldName = baseName;
  -    if (!NameUtil.isValidJavaIdentifier(fieldName))
  -      fieldName = "_" + fieldName;
  -    for (int i = 1; seenNames.contains(fieldName); i += 1)
  -      fieldName = baseName + i;
  -
  -    seenNames.add(fieldName);
  -    return fieldName;
     }
   
     private void addJavaBeanProperty(String name, String type, String getter, String setter)
  @@ -1349,6 +1386,274 @@
                                              null);
       mJoust.writeAssignmentStatement(propertyField, params[0]);
       mJoust.endMethodOrConstructor();
  +  }
  +
  +  private void printJavaEnum(Scratch scratch, String baseJavaName) throws IOException {
  +    Set seenFieldNames = new HashSet();
  +    XmlAnySimpleType[] enumValues = scratch.getSchemaType().getEnumerationValues();
  +    JaxrpcEnumType enumType = (JaxrpcEnumType) scratch.getBindingType();
  +    JavaTypeName baseType = enumType.getBaseTypeName().getJavaName();
  +    EnumerationPrintHelper enumHelper =
  +            new EnumerationPrintHelper(baseType, mJoust.getExpressionFactory(),
  +                    scratch.getSchemaType().getPrimitiveType().getBuiltinTypeCode());
  +
  +    // figure out what import statements we need
  +    boolean useArrays = enumHelper.isArray() || enumHelper.isBinary();
  +    if (useArrays) {
  +      mJoust.writeImportStatement("java.util.Arrays");
  +    }
  +    mJoust.writeImportStatement("java.util.HashMap");
  +    mJoust.writeImportStatement("java.util.Map");
  +    if (enumHelper.isArray()) {
  +      mJoust.writeImportStatement("java.util.StringTokenizer");
  +    }
  +    mJoust.writeImportStatement("org.apache.xmlbeans.impl.util.XsTypeConverter");
  +    mJoust.startClass(Modifier.PUBLIC, baseJavaName, null);
  +
  +    // Assign appropriate names to the fields we use
  +    boolean matchOk = true;
  +    String[] fieldNames = new String[enumValues.length];
  +    String instanceVarName = "value";
  +    String instanceMapName = "map";
  +    boolean isQName = enumValues.length > 0 && enumValues[0] instanceof XmlQName;
  +    for (int i = 0; i < enumValues.length; i++) {
  +      String tentativeName = NameUtil.lowerCamelCase(isQName ?
  +              ((XmlQName) enumValues[i]).getQNameValue().getLocalPart() :
  +              enumValues[i].getStringValue(),
  +              true, false);
  +      if (!NameUtil.isValidJavaIdentifier(tentativeName)) {
  +        matchOk = false;
  +        break;
  +      }
  +      if (seenFieldNames.contains(tentativeName)) {
  +        matchOk = false;
  +        break;
  +      }
  +      // If we got here, we found a suitable name for this constant
  +      seenFieldNames.add(tentativeName);
  +      fieldNames[i] = tentativeName;
  +    }
  +    if (!matchOk) {
  +      // One or more values could not map to a valid Java identifier
  +      // As per JAX-RPC, rename all identifiers to 'value1', 'value2', etc
  +      for (int i = 0; i < enumValues.length; i++) {
  +        String name = "value" + (i + 1);
  +        fieldNames[i] = name;
  +      }
  +    }
  +    else {
  +      // Check if the instance var name collides with any of the constants
  +      while (seenFieldNames.contains(instanceVarName))
  +        instanceVarName = "x" + instanceVarName;
  +      // Check the map var name
  +      while (seenFieldNames.contains(instanceMapName))
  +        instanceMapName = "x" + instanceMapName;
  +    }
  +
  +    // We have the names, generate the class!
  +    // ======================================
  +    // Private fields and constructor
  +    Variable instanceVar =
  +            mJoust.writeField(Modifier.PRIVATE,
  +                    baseType.toString(),
  +                    instanceVarName,
  +                    null);
  +    Variable instanceMap =
  +            mJoust.writeField(Modifier.PRIVATE | Modifier.STATIC,
  +                    "Map",
  +                    instanceMapName,
  +                    mJoust.getExpressionFactory().createVerbatim("new HashMap()"));
  +    Variable[] params = mJoust.startConstructor(Modifier.PROTECTED,
  +            new String[] {baseType.toString()},
  +            new String[] {"value"},
  +            null);
  +    mJoust.writeAssignmentStatement(instanceVar, params[0]);
  +    mJoust.endMethodOrConstructor();
  +    Variable[] constants = new Variable[enumValues.length];
  +
  +    // Constants of the enumeration base type
  +    for (int i = 0; i < enumValues.length; i++) {
  +      XmlAnySimpleType enumValue = enumValues[i];
  +      constants[i] =
  +              mJoust.writeField(Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL,
  +                      baseType.toString(),
  +                      "_" + fieldNames[i],
  +                      enumHelper.getInitExpr(enumValue));
  +    }
  +
  +    // Constants of enumeration type
  +    String shortClassName = scratch.getJavaName().getShortClassName();
  +    for (int i = 0; i < enumValues.length; i++) {
  +      mJoust.writeField(Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL,
  +              shortClassName,
  +              fieldNames[i],
  +              mJoust.getExpressionFactory().createVerbatim("new " + shortClassName + "(_" + fieldNames[i] + ")"));
  +    }
  +
  +    // Implementation of the getValue() method
  +    mJoust.startMethod(Modifier.PUBLIC,
  +            baseType.toString(),
  +            enumType.getGetValueMethod().getSimpleName(),
  +            null, null, null);
  +    mJoust.writeReturnStatement(instanceVar);
  +    mJoust.endMethodOrConstructor();
  +
  +    // Implementation of the fromValue() method
  +    mJoust.startMethod(Modifier.PUBLIC | Modifier.STATIC,
  +            shortClassName,
  +            enumType.getFromValueMethod().getSimpleName(),
  +            new String[] {baseType.toString()},
  +            new String[] {"value"},
  +            null);
  +    mJoust.writeStatement("if (" + instanceMapName + ".containsKey(" +
  +            (useArrays ? "new " + shortClassName + "(value)" : enumHelper.getObjectVersion("value")) +
  +            ")) return (" + shortClassName + ") " +
  +            instanceMapName + ".get(" + enumHelper.getObjectVersion("value") + ")");
  +    mJoust.writeStatement("else throw new IllegalArgumentException()");
  +    mJoust.endMethodOrConstructor();
  +
  +    // Implementation of the fromString() method
  +    params = mJoust.startMethod(Modifier.PUBLIC | Modifier.STATIC,
  +            shortClassName,
  +            enumType.getFromStringMethod().getSimpleName(),
  +            new String[] {"String"},
  +            new String[] {"value"},
  +            null);
  +    if (enumHelper.isArray()) {
  +      // Here we have to tokenize the input string, then build up an array
  +      // of the enumeration's type and then call fromValue()
  +      final String STRING_PARTS = "parts";
  +      final String BASETYPE_ARRAY = "array";
  +      mJoust.writeStatement("String[] " + STRING_PARTS + "= org.apache.xmlbeans.impl.values.XmlListImpl.split_list(value)");
  +      mJoust.writeStatement(baseType.toString() + " " + BASETYPE_ARRAY + " = new " +
  +        baseType.getArrayItemType(1).toString() + "[" + STRING_PARTS + ".length" + "]");
  +      mJoust.writeStatement("for (int i = 0; i < " + BASETYPE_ARRAY + ".length; i++) " +
  +        BASETYPE_ARRAY + "[i] = " +
  +        enumHelper.getFromStringExpr(params[0]).getMemento().toString());
  +      mJoust.writeReturnStatement(mJoust.getExpressionFactory().createVerbatim(
  +            enumType.getFromValueMethod().getSimpleName() + "(" + BASETYPE_ARRAY + ")"));
  +    }
  +    else
  +    {
  +      mJoust.writeReturnStatement(mJoust.getExpressionFactory().createVerbatim(
  +            enumType.getFromValueMethod().getSimpleName() + "(" +
  +            enumHelper.getFromStringExpr(params[0]).getMemento().toString() +
  +            ")"));
  +    }
  +    mJoust.endMethodOrConstructor();
  +
  +    // Implementation of the toXml() method
  +    mJoust.startMethod(Modifier.PUBLIC,
  +            "String",
  +            enumType.getToXMLMethod().getSimpleName(),
  +            null, null, null);
  +    if (enumHelper.isArray()) {
  +      final String STRING_LIST = "list";
  +      mJoust.writeStatement("StringBuffer " + STRING_LIST + " = new StringBuffer()");
  +      mJoust.writeStatement("for (int i = 0; i < " + instanceVarName + ".length; i++) " +
  +        STRING_LIST + ".append(" + enumHelper.getToXmlString(instanceVar, "i") +
  +        ").append(' ')");
  +      mJoust.writeReturnStatement(mJoust.getExpressionFactory().createVerbatim(STRING_LIST));
  +    }
  +    else {
  +      mJoust.writeReturnStatement(enumHelper.getToXmlExpr(instanceVar));
  +    }
  +    mJoust.endMethodOrConstructor();
  +
  +    // Implementation of the toString() method
  +    mJoust.startMethod(Modifier.PUBLIC,
  +            "String",
  +            "toString",
  +            null, null, null);
  +    if (enumHelper.isArray()) {
  +      final String STRING_LIST = "list";
  +      mJoust.writeStatement("StringBuffer " + STRING_LIST + " = new StringBuffer()");
  +      mJoust.writeStatement("for (int i = 0; i < " + instanceVarName + ".length; i++) " +
  +        STRING_LIST + ".append(String.valueOf(" +
  +        instanceVarName + "[i]" +
  +        ")).append(' ')");
  +      mJoust.writeReturnStatement(mJoust.getExpressionFactory().createVerbatim(STRING_LIST));
  +    }
  +    else {
  +      mJoust.writeReturnStatement(mJoust.getExpressionFactory().createVerbatim(
  +            "String.valueOf(" + instanceVarName + ")"));
  +    }
  +    mJoust.endMethodOrConstructor();
  +
  +    // Implementation of the equals() method
  +    params = mJoust.startMethod(Modifier.PUBLIC,
  +            "boolean",
  +            "equals",
  +            new String[] {"Object"},
  +            new String[] {"obj"},
  +            null);
  +    mJoust.writeStatement("if (this == obj) return true");
  +    mJoust.writeStatement("if (!(obj instanceof " + shortClassName + ")) return false");
  +    mJoust.writeStatement("final " + shortClassName + " x = (" + shortClassName + ") obj");
  +    if (enumHelper.isArray() && enumHelper.isBinary()) {
  +      mJoust.writeStatement("if (x." + instanceVarName + ".length != " + instanceVarName +
  +        ".length) return false");
  +      mJoust.writeStatement("boolean b = true");
  +      mJoust.writeStatement("for (int i = 0; i < " + instanceVarName + ".length && b; i++) " +
  +        "b &= Arrays.equals(x." + instanceVarName + "[i], " + instanceVarName + "[i])");
  +      mJoust.writeStatement("return b");
  +    }
  +    else if (enumHelper.isArray())
  +      mJoust.writeStatement("if (Arrays.equals(x." + instanceVarName + ", " + instanceVarName + ")) return true");
  +    else
  +      mJoust.writeStatement("if (" + enumHelper.getEquals("x." + instanceVarName, instanceVarName) + ") return true");
  +    mJoust.writeReturnStatement(mJoust.getExpressionFactory().createBoolean(false));
  +    mJoust.endMethodOrConstructor();
  +
  +    // Implementation of the hashCode() method
  +    mJoust.startMethod(Modifier.PUBLIC,
  +            "int",
  +            "hashCode",
  +            null, null, null);
  +    if (enumHelper.isArray()) {
  +      mJoust.writeStatement("int val = 0;");
  +      mJoust.writeStatement("for (int i = 0; i < " + instanceVarName + ".length; i++) " +
  +        "{ val *= 19; val =+ " +
  +        enumHelper.getHashCode(instanceVarName).getMemento().toString() +
  +        "; }");
  +      mJoust.writeStatement("return val");
  +    }
  +    else
  +      mJoust.writeReturnStatement(enumHelper.getHashCode(instanceVarName));
  +    mJoust.endMethodOrConstructor();
  +
  +    // Static class code
  +    mJoust.startStaticInitializer();
  +    for (int i = 0; i < fieldNames.length; i++) {
  +      String fieldName = fieldNames[i];
  +      if (useArrays)
  +        mJoust.writeStatement(instanceMapName + ".put(" +
  +              fieldName + ", " + fieldName + ")");
  +      else
  +        mJoust.writeStatement(instanceMapName + ".put(" +
  +              enumHelper.getObjectVersion("_" + fieldName) + ", " +
  +              fieldName + ")");
  +    }
  +    mJoust.endMethodOrConstructor();
  +    mJoust.endClassOrInterface();
  +  }
  +
  +  private String pickUniqueFieldName(String getter, Set seenNames) {
  +    String baseName;
  +
  +    if (getter.length() > 3 && getter.startsWith("get"))
  +      baseName = Character.toLowerCase(getter.charAt(3)) + getter.substring(4);
  +    else
  +      baseName = "field";
  +
  +    String fieldName = baseName;
  +    if (!NameUtil.isValidJavaIdentifier(fieldName))
  +      fieldName = "_" + fieldName;
  +    for (int i = 1; seenNames.contains(fieldName); i += 1)
  +      fieldName = baseName + i;
  +
  +    seenNames.add(fieldName);
  +    return fieldName;
     }
   
   
  
  
  
  1.1                  xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/compile/EnumerationPrintHelper.java
  
  Index: EnumerationPrintHelper.java
  ===================================================================
  /*   Copyright 2004 The Apache Software Foundation
   *
   *   Licensed under the Apache License, Version 2.0 (the "License");
   *   you may not use this file except in compliance with the License.
   *   You may obtain a copy of the License at
   *
   *       http://www.apache.org/licenses/LICENSE-2.0
   *
   *   Unless required by applicable law or agreed to in writing, software
   *   distributed under the License is distributed on an "AS IS" BASIS,
   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *   See the License for the specific language governing permissions and
   *  limitations under the License.
   */
  
  package org.apache.xmlbeans.impl.binding.compile;
  
  import org.apache.xmlbeans.SchemaStringEnumEntry;
  import org.apache.xmlbeans.SchemaType;
  import org.apache.xmlbeans.XmlAnySimpleType;
  import org.apache.xmlbeans.XmlBase64Binary;
  import org.apache.xmlbeans.XmlBoolean;
  import org.apache.xmlbeans.XmlByte;
  import org.apache.xmlbeans.XmlDouble;
  import org.apache.xmlbeans.XmlFloat;
  import org.apache.xmlbeans.XmlHexBinary;
  import org.apache.xmlbeans.XmlInt;
  import org.apache.xmlbeans.XmlInteger;
  import org.apache.xmlbeans.XmlLong;
  import org.apache.xmlbeans.XmlQName;
  import org.apache.xmlbeans.XmlShort;
  import org.apache.xmlbeans.impl.binding.bts.JavaTypeName;
  import org.apache.xmlbeans.impl.binding.joust.Expression;
  import org.apache.xmlbeans.impl.binding.joust.ExpressionFactory;
  import org.apache.xmlbeans.impl.binding.joust.Variable;
  import org.apache.xmlbeans.impl.util.XsTypeConverter;
  import org.apache.xmlbeans.impl.values.XmlListImpl;
  
  import javax.xml.namespace.NamespaceContext;
  import javax.xml.namespace.QName;
  import java.util.Iterator;
  
  /**
   * Helper class for generating a JAX-RPC enumeration type
   */
  public class EnumerationPrintHelper
  {
    // ===================================================
    // Member variables
  
    private boolean mArray;
    private ExpressionFactory mExprFactory;
    private int mTypeCode;
    private int mSchemaTypeCode;
    private boolean mPrimitive;
  
    // ====================================================
    // Constants
  
    // For each of the "simple" (from JAX-RPC perspective) Java types, we
    // have to have custom code that deals with generating the right strings
    // to use in the generated file. Tedious.
    // This is the list of currently supported Java types
  
    private static final int T_STRING        =  1; // java.lang.String
    private static final int T_GDURATION     =  2; // org.apache.xmlbeans.GDuration
    private static final int T_CALENDAR      =  3; // java.util.Calendar
    private static final int T_BOOLEAN       =  4; // boolean
    private static final int T_FLOAT         =  5; // float
    private static final int T_DOUBLE        =  6; // double
    private static final int T_BIGDECIMAL    =  7; // java.math.BigDecimal
    private static final int T_BIGINTEGER    =  8; // java.math.BigInteger
    private static final int T_LONG          =  9; // long
    private static final int T_INT           = 10; // int
    private static final int T_SHORT         = 11; // short
    private static final int T_BYTE          = 12; // byte
    private static final int T_QNAME         = 13; // javax.xml.namespace.QName
    private static final int T_FLOAT_CLASS   = 14; // java.lang.Float
    private static final int T_DOUBLE_CLASS  = 15; // java.lang.Double
    private static final int T_LONG_CLASS    = 16; // java.lang.Long
    private static final int T_INT_CLASS     = 17; // java.lang.Integer
    private static final int T_SHORT_CLASS   = 18; // java.lang.Short
    private static final int T_BYTE_CLASS    = 19; // java.lang.Byte
    private static final int T_BOOLEAN_CLASS = 20; // java.lang.Boolean
    private static final int T_URI           = 21; // java.net.URI
    private static final int T_BYTE_ARRAY    = 22; // byte[]
  
    EnumerationPrintHelper(JavaTypeName typeName, ExpressionFactory exprFactory, int schemaTypeCode)
    {
      if (schemaTypeCode == SchemaType.BTC_BASE_64_BINARY ||
        schemaTypeCode == SchemaType.BTC_HEX_BINARY)
        mArray = typeName.getArrayDepth() > 1;
      else
        mArray = typeName.getArrayDepth() > 0;
  
      mExprFactory = exprFactory;
      String t;
      if (mArray)
        t = typeName.getArrayItemType(1).toString();
      else
        t = typeName.toString();
      mSchemaTypeCode = schemaTypeCode;
      switch (t.charAt(0))
      {
        case 'j':
        if (t.equals("java.lang.String"))
          mTypeCode = T_STRING;
        else if (t.equals("java.util.Calendar"))
          mTypeCode = T_CALENDAR;
        else if (t.equals("java.math.BigDecimal"))
          mTypeCode = T_BIGDECIMAL;
        else if (t.equals("java.math.BigInteger"))
          mTypeCode = T_BIGINTEGER;
        else if (t.equals("javax.xml.namespace.QName"))
          mTypeCode = T_QNAME;
        else if (t.equals("java.net.URI"))
          mTypeCode = T_URI;
        else if (t.equals(Float.class.getName()))
          mTypeCode = T_FLOAT_CLASS;
        else if (t.equals(Double.class.getName()))
          mTypeCode = T_DOUBLE_CLASS;
        else if (t.equals(Long.class.getName()))
          mTypeCode = T_LONG_CLASS;
        else if (t.equals(Integer.class.getName()))
          mTypeCode = T_INT_CLASS;
        else if (t.equals(Short.class.getName()))
          mTypeCode = T_SHORT_CLASS;
        else if (t.equals(Byte.class.getName()))
          mTypeCode = T_BYTE_CLASS;
        else if (t.equals(Boolean.class.getName()))
          mTypeCode = T_BOOLEAN_CLASS;
        else
          throw new IllegalArgumentException("Unknown simple type: " + t);
        break;
        case 'b':
        if (t.equals("boolean"))
          mTypeCode = T_BOOLEAN;
        else if (t.equals("byte"))
          mTypeCode = T_BYTE;
        else if (t.equals("byte[]"))
          mTypeCode = T_BYTE_ARRAY;
        else
          throw new IllegalArgumentException("Unknown simple type: " + t);
        break;
        case 'f':
        if (t.equals("float"))
          mTypeCode = T_FLOAT;
        else
          throw new IllegalArgumentException("Unknown simple type: " + t);
        break;
        case 'd':
        if (t.equals("double"))
          mTypeCode = T_DOUBLE;
        else
          throw new IllegalArgumentException("Unknown simple type: " + t);
        break;
        case 'l':
        if (t.equals("long"))
          mTypeCode = T_LONG;
        else
          throw new IllegalArgumentException("Unknown simple type: " + t);
        break;
        case 'i':
        if (t.equals("int"))
          mTypeCode = T_INT;
        else
          throw new IllegalArgumentException("Unknown simple type: " + t);
        break;
        case 's':
        if (t.equals("short"))
          mTypeCode = T_SHORT;
        else
          throw new IllegalArgumentException("Unknown simple type: " + t);
        break;
        case 'o':
        if (t.equals("org.apache.xmlbeans.GDuration"))
          mTypeCode = T_GDURATION;
        break;
        default:
          throw new IllegalArgumentException("Unknown simple type: " + t);
      }
      switch (mTypeCode)
      {
        case T_BOOLEAN:
        case T_BYTE:
        case T_FLOAT:
        case T_DOUBLE:
        case T_LONG:
        case T_INT:
        case T_SHORT:
          mPrimitive = true;
      }
    }
  
    /**
     * Returns the init expression corresponding to a value of type mJavaType
     * and XML representation value
     * Ex value="abc" --> "abc"
     *    value="123" --> 123
     *    value="456" --> new Float(456)
     */
    public Expression getInitExpr(XmlAnySimpleType value) {
      if (mArray)
      {
        StringBuffer arrayInit = new StringBuffer();
        arrayInit.append('{');
        for (Iterator it = ((XmlListImpl) value).xlistValue().iterator(); it.hasNext(); )
        {
          Expression exprInit = getInitExprHelper((XmlAnySimpleType) it.next());
          arrayInit.append(exprInit.getMemento().toString()).append(',').append(' ');
        }
        arrayInit.append('}');
        return mExprFactory.createVerbatim(arrayInit.toString());
      }
      else
        return getInitExprHelper(value);
    }
  
    private Expression getInitExprHelper(XmlAnySimpleType value) {
      Expression result = null;
      switch (mTypeCode)
      {
        case T_STRING:
          result = mExprFactory.createVerbatim("\"" + value.getStringValue() + "\"");
          break;
        case T_CALENDAR:
          result = mExprFactory.createVerbatim("XsTypeConverter.lexDateTime(\"" + value.getStringValue() + "\")");
          break;
        case T_BIGDECIMAL:
          result = mExprFactory.createVerbatim("XsTypeConverter.lexDecimal(\"" + value.getStringValue() + "\")");
          break;
        case T_BIGINTEGER:
          result = mExprFactory.createVerbatim("XsTypeConverter.lexInteger(\"" + value.getStringValue() + "\")");
          break;
        case T_QNAME:
          QName qName = ((XmlQName) value).getQNameValue();
          result = mExprFactory.createVerbatim("new javax.xml.namespace.QName(\"" +
                  qName.getNamespaceURI() + "\", \"" +
                  qName.getLocalPart() + "\", \"" +
                  qName.getPrefix() + "\")");
          break;
        case T_URI:
          result = mExprFactory.createVerbatim("new java.net.URI(\"" + value.getStringValue() + "\")");
          break;
        case T_FLOAT_CLASS:
          result = mExprFactory.createVerbatim("new Float(" + ((XmlFloat) value).getFloatValue() + ")");
          break;
        case T_DOUBLE_CLASS:
          result = mExprFactory.createVerbatim("new Double(" + ((XmlDouble) value).getDoubleValue() + ")");
          break;
        case T_LONG_CLASS:
          result = mExprFactory.createVerbatim("new Long(" + ((XmlLong) value).getLongValue() + ")");
          break;
        case T_INT_CLASS:
          result = mExprFactory.createVerbatim("new Integer(" + ((XmlInt) value).getIntValue() + ")");
          break;
        case T_SHORT_CLASS:
          result = mExprFactory.createVerbatim("new Short(" + ((XmlShort) value).getShortValue() + ")");
          break;
        case T_BYTE_CLASS:
          result = mExprFactory.createVerbatim("new Byte(" + ((XmlByte) value).getByteValue() + ")");
          break;
        case T_BOOLEAN_CLASS:
          result = mExprFactory.createVerbatim("new Boolean(" + ((XmlBoolean) value).getBooleanValue() + ")");
          break;
        case T_BOOLEAN:
          result = mExprFactory.createVerbatim(String.valueOf(((XmlBoolean) value).getBooleanValue()));
          break;
        case T_BYTE:
          result = mExprFactory.createVerbatim(String.valueOf(((XmlByte) value).getByteValue()));
          break;
        case T_FLOAT:
          result = mExprFactory.createVerbatim("(float)" + String.valueOf(((XmlFloat) value).getFloatValue()));
          break;
        case T_DOUBLE:
          result = mExprFactory.createVerbatim(String.valueOf(((XmlDouble) value).getDoubleValue()));
          break;
        case T_LONG:
          result = mExprFactory.createVerbatim(String.valueOf(((XmlLong) value).getLongValue()));
          break;
        case T_INT:
          result = mExprFactory.createVerbatim(String.valueOf(((XmlInt) value).getIntValue()));
          break;
        case T_SHORT:
          result = mExprFactory.createVerbatim(String.valueOf(((XmlShort) value).getShortValue()));
          break;
        case T_BYTE_ARRAY:
          {
            StringBuffer arrayInit = new StringBuffer();
            arrayInit.append('{');
            byte[] bytes = null;
            if (mSchemaTypeCode == SchemaType.BTC_BASE_64_BINARY)
              bytes = ((XmlBase64Binary) value).getByteArrayValue();
            else if (mSchemaTypeCode == SchemaType.BTC_HEX_BINARY)
              bytes = ((XmlHexBinary) value).getByteArrayValue();
            if (bytes == null)
              break;
            for (int i = 0; i < bytes.length; i++)
              arrayInit.append(bytes[i]).append(',').append(' ');
            arrayInit.append('}');
            result = mExprFactory.createVerbatim(arrayInit.toString());
          }
          break;
        case T_GDURATION:
          result = mExprFactory.createVerbatim("new org.apache.xmlbeans.GDuration(\"" + value.getStringValue() + "\")");
          break;
        default:
      }
      if (result == null)
        throw new IllegalStateException();
      return result;
    }
  
    public Expression getFromStringExpr(Variable param) {
      Expression result = null;
      String s = param.getMemento().toString();
      switch (mTypeCode)
      {
        case T_STRING:
          result = param;
          break;
        case T_CALENDAR:
          result = mExprFactory.createVerbatim("XsTypeConverter.lexDateTime(" + s + ")");
          break;
        case T_BIGDECIMAL:
          result = mExprFactory.createVerbatim("XsTypeConverter.lexDecimal(" + s + ")");
          break;
        case T_BIGINTEGER:
          result = mExprFactory.createVerbatim("XsTypeConverter.lexInteger(" + s + ")");
          break;
        case T_QNAME:
          result = mExprFactory.createVerbatim("XsTypeConverter.lexQName(" + s +
                  ", new javax.xml.namespace.NamespaceContext() {public String getNamespaceURI(String p) {return \"\";} public String getPrefix(String u) {return null;} public java.util.Iterator getPrefixes(String s) {return null;}})");
          break;
        case T_URI:
          result = mExprFactory.createVerbatim("new java.net.URI(" + s + ")");
          break;
        case T_FLOAT_CLASS:
          result = mExprFactory.createVerbatim("new Float(XsTypeConverter.lexFloat(" + s + "))");
          break;
        case T_DOUBLE_CLASS:
          result = mExprFactory.createVerbatim("new Double(XsTypeConverter.lexDouble(" + s + "))");
          break;
        case T_LONG_CLASS:
          result = mExprFactory.createVerbatim("new Long(XsTypeConverter.lexLong(" + s + "))");
          break;
        case T_INT_CLASS:
          result = mExprFactory.createVerbatim("new Integer(XsTypeConverter.lexInt(" + s + "))");
          break;
        case T_SHORT_CLASS:
          result = mExprFactory.createVerbatim("new Short(XsTypeConverter.lexShort(" + s + "))");
          break;
        case T_BYTE_CLASS:
          result = mExprFactory.createVerbatim("new Byte(XsTypeConverter.lexByte(" + s + "))");
          break;
        case T_BOOLEAN_CLASS:
          result = mExprFactory.createVerbatim("new Boolean(XsTypeConverter.lexBoolean(" + s + "))");
          break;
        case T_BOOLEAN:
          result = mExprFactory.createVerbatim("XsTypeConverter.lexBoolean(" + s + ")");
          break;
        case T_BYTE:
          result = mExprFactory.createVerbatim("XsTypeConverter.lexByte(" + s + ")");
          break;
        case T_FLOAT:
          result = mExprFactory.createVerbatim("XsTypeConverter.lexFloat(" + s + ")");
          break;
        case T_DOUBLE:
          result = mExprFactory.createVerbatim("XsTypeConverter.lexDouble(" + s + ")");
          break;
        case T_LONG:
          result = mExprFactory.createVerbatim("XsTypeConverter.lexLong(" + s + ")");
          break;
        case T_INT:
          result = mExprFactory.createVerbatim("XsTypeConverter.lexInt(" + s + ")");
          break;
        case T_SHORT:
          result = mExprFactory.createVerbatim("XsTypeConverter.lexShort(" + s + ")");
          break;
        case T_GDURATION:
          result = mExprFactory.createVerbatim("new org.apache.xmlbeans.GDuration(" + s + ")");
        case T_BYTE_ARRAY:
          if (mSchemaTypeCode == SchemaType.BTC_BASE_64_BINARY)
            result = mExprFactory.createVerbatim("org.apache.xmlbeans.impl.util.Base64.encode("+
              s + ".getBytes())");
          else if (mSchemaTypeCode == SchemaType.BTC_HEX_BINARY)
            result = mExprFactory.createVerbatim("org.apache.xmlbeans.impl.util.HexBin.stringToBytes(" +
              s + ")");
          break;
        default:
      }
      if (result == null)
        throw new IllegalStateException();
      return result;
    }
  
    public String getToXmlString(Variable var, String index) {
      assert mArray : "This method can only be called for a list enumeration";
      return getToXmlExpr(mExprFactory.createVerbatim(var.getMemento().toString() +
          "[" + index + "]")).getMemento().toString();
    }
  
    public Expression getToXmlExpr(Expression var) {
      Expression result = null;
      String s = var.getMemento().toString();
      switch (mTypeCode)
      {
        case T_STRING:
          result = mExprFactory.createVerbatim("XsTypeConverter.printString(" + s + ").toString()");
          break;
        case T_CALENDAR:
          result = mExprFactory.createVerbatim("XsTypeConverter.printDateTime(" + s + ", " + mSchemaTypeCode +").toString()");
          break;
        case T_BIGDECIMAL:
          result = mExprFactory.createVerbatim("XsTypeConverter.printDecimal(" + s + ").toString()");
          break;
        case T_BIGINTEGER:
          result = mExprFactory.createVerbatim("XsTypeConverter.printInteger(" + s + ").toString()");
          break;
        case T_QNAME:
          result = mExprFactory.createVerbatim("XsTypeConverter.getQNameString(" + s +
                  ".getNamespaceURI(), " + s + ".getLocalPart(), " + s + ".getPrefix())");
          break;
        case T_URI:
          result = mExprFactory.createVerbatim(s + ".toString()");
          break;
        case T_FLOAT_CLASS:
          result = mExprFactory.createVerbatim("XsTypeConverter.printFloat(" + s + ".floatValue()).toString()");
          break;
        case T_DOUBLE_CLASS:
          result = mExprFactory.createVerbatim("XsTypeConverter.printDouble(" + s + ".doubleValue()).toString()");
          break;
        case T_LONG_CLASS:
          result = mExprFactory.createVerbatim("XsTypeConverter.printLong(" + s + ".longValue()).toString()");
          break;
        case T_INT_CLASS:
          result = mExprFactory.createVerbatim("XsTypeConverter.printInt(" + s + ".intValue()).toString()");
          break;
        case T_SHORT_CLASS:
          result = mExprFactory.createVerbatim("XsTypeConverter.printShort(" + s + ".shortValue()).toString()");
          break;
        case T_BYTE_CLASS:
          result = mExprFactory.createVerbatim("XsTypeConverter.printByte(" + s + ".byteValue()).toString()");
          break;
        case T_BOOLEAN_CLASS:
          result = mExprFactory.createVerbatim("XsTypeConverter.printBoolean(" + s + ".booleanValue()).toString()");
          break;
        case T_BOOLEAN:
          result = mExprFactory.createVerbatim("XsTypeConverter.printBoolean(" + s + ").toString()");
          break;
        case T_BYTE:
          result = mExprFactory.createVerbatim("XsTypeConverter.printByte(" + s + ").toString()");
          break;
        case T_FLOAT:
          result = mExprFactory.createVerbatim("XsTypeConverter.printFloat(" + s + ").toString()");
          break;
        case T_DOUBLE:
          result = mExprFactory.createVerbatim("XsTypeConverter.printDouble(" + s + ").toString()");
          break;
        case T_LONG:
          result = mExprFactory.createVerbatim("XsTypeConverter.printLong(" + s + ").toString()");
          break;
        case T_INT:
          result = mExprFactory.createVerbatim("XsTypeConverter.printInt(" + s + ").toString()");
          break;
        case T_SHORT:
          result = mExprFactory.createVerbatim("XsTypeConverter.printShort(" + s + ").toString()");
          break;
        case T_GDURATION:
          result = mExprFactory.createVerbatim(s + ".toString()");
          break;
        case T_BYTE_ARRAY:
          if (mSchemaTypeCode == SchemaType.BTC_BASE_64_BINARY)
            result = mExprFactory.createVerbatim("XsTypeConverter.printBase64Binary(" + s + ").toString()");
          else if (mSchemaTypeCode == SchemaType.BTC_HEX_BINARY)
            result = mExprFactory.createVerbatim("XsTypeConverter.printHexBinary(" + s + ").toString()");
          break;
        default:
      }
      if (result == null)
        throw new IllegalStateException();
      else
        return result;
    }
  
    public String getObjectVersion(String var) {
      String result = null;
      if (mPrimitive) {
      switch (mTypeCode) {
        case T_BOOLEAN:
        result = "new Boolean(" + var + ")";
        break;
        case T_BYTE:
        result = "new Byte(" + var + ")";
        break;
        case T_FLOAT:
        result = "new Float(" + var + ")";
        break;
        case T_DOUBLE:
        result = "new Double(" + var + ")";
        break;
        case T_LONG:
        result = "new Long(" + var + ")";
        break;
        case T_INT:
        result = "new Integer(" + var + ")";
        break;
        case T_SHORT:
        result = "new Short(" + var + ")";
        break;
      }}
      else
        result = var;
      if (result == null)
        throw new IllegalStateException();
      else
        return result;
    }
  
    public String getEquals(String var1, String var2) {
      String result = null;
      if (mTypeCode == T_BYTE_ARRAY)
        result = "java.util.Arrays.equals(" + var1 + ", " + var2 + ")";
      else if (mPrimitive)
        result = var1 + " == " + var2;
      else
        result = var1 + ".equals(" + var2 + ")";
      return result;
    }
  
    public Expression getHashCode(String var) {
      Expression result = null;
      if (mPrimitive) {
      switch (mTypeCode) {
        case T_BOOLEAN:
        result = mExprFactory.createVerbatim(var + " ? 1 : 0");
        break;
        case T_BYTE:
        result = mExprFactory.createVerbatim("(int) " + var);
        break;
        case T_FLOAT:
        result = mExprFactory.createVerbatim("Float.floatToIntBits(" + var + ")");
        break;
        case T_DOUBLE:
        result = mExprFactory.createVerbatim("(int) (Double.doubleToLongBits(" + var + ") ^ (Double.doubleYoLongBits(" + var + ") >>> 32))");
        break;
        case T_LONG:
        result = mExprFactory.createVerbatim("(int) (" + var + " ^ (" + var + " >>> 32))");
        break;
        case T_INT:
        result = mExprFactory.createVerbatim(var);
        break;
        case T_SHORT:
        result = mExprFactory.createVerbatim("(int) " + var);
        break;
        }
      }
      else if (mTypeCode == T_BYTE_ARRAY)
        result = mExprFactory.createVerbatim(var + ".length > 0 ? (int)" + var + "[0] : 0");
      else
        result = mExprFactory.createVerbatim(var + ".hashCode()");
      if (result == null)
        throw new IllegalStateException();
      else
        return result;
    }
  
    public boolean isQName()
    { return mTypeCode == T_QNAME; }
  
    public boolean isArray()
    { return mArray; }
  
    public boolean isBinary()
    {
      return mSchemaTypeCode == SchemaType.BTC_BASE_64_BINARY ||
             mSchemaTypeCode == SchemaType.BTC_HEX_BINARY;
    }
  }
  
  
  
  1.3       +5 -0      xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/joust/ExpressionFactory.java
  
  Index: ExpressionFactory.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/joust/ExpressionFactory.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ExpressionFactory.java	12 Feb 2004 20:06:07 -0000	1.2
  +++ ExpressionFactory.java	21 Mar 2004 01:58:27 -0000	1.3
  @@ -47,4 +47,9 @@
      */
     public Expression createNull();
   
  +  /**
  +   * Returns an expresion whose text representation is the given string.
  +   */
  +  public Expression createVerbatim(String value);
  +
   }
  
  
  
  1.6       +33 -0     xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/joust/JavaOutputStream.java
  
  Index: JavaOutputStream.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/joust/JavaOutputStream.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- JavaOutputStream.java	25 Feb 2004 18:56:48 -0000	1.5
  +++ JavaOutputStream.java	21 Mar 2004 01:58:27 -0000	1.6
  @@ -87,6 +87,13 @@
             throws IOException;
   
     /**
  +   * Instructs the stream to begin writing a static initialization block.
  +   * @throws IllegalStateException if the current position is not valid for
  +   *         starting a static initialization block.
  +   */
  +  public void startStaticInitializer() throws IOException;
  +
  +  /**
      * Instructs the stream to begin writing a new interface.
      *
      * @param extendsInterfaceNames Array of interface names, one
  @@ -243,6 +250,32 @@
      * @return An ExpressionFactory.  Must never return null.
      */
     public Annotation createAnnotation(String type);
  +
  +  /**
  +   * Writes out an empty line, used for code formatting.
  +   */
  +  public void writeEmptyLine() throws IOException;
  +
  +  /**
  +   * Writes out a Java statement represented by the given string.
  +   *
  +   * @param statement The string representation of the given statement
  +   *
  +   * @throws IllegalArgumentException if the statement string is null.
  +   */
  +  public void writeStatement(String statement) throws IOException;
  +
  +  /**
  +   * Writes out an import statement of the given class.
  +   *
  +   * @param className The name of the class to be imported. Can contain '*'
  +   *                  for package imports.
  +   *
  +   * @throws IllegalArgumentException if className is null.
  +   * @throws IllegalStateException if the current stream state does not allow
  +   *         an import statement, ie the class definition has already started.
  +   */
  +  public void writeImportStatement(String className) throws IOException;
   
     /**
      * Writes out a return statement for the current method that returns
  
  
  
  1.7       +50 -1     xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/joust/SourceJavaOutputStream.java
  
  Index: SourceJavaOutputStream.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/joust/SourceJavaOutputStream.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- SourceJavaOutputStream.java	12 Feb 2004 20:06:07 -0000	1.6
  +++ SourceJavaOutputStream.java	21 Mar 2004 01:58:27 -0000	1.7
  @@ -94,7 +94,9 @@
     private String mClassOrInterfaceName = null;
     private WriterFactory mWriterFactory;
     private StringWriter mCommentBuffer = null;
  +  private StringWriter mImportBuffer = null;
     private PrintWriter mCommentPrinter = null;
  +  private PrintWriter mImportPrinter = null;
   
     // ========================================================================
     // Constructors
  @@ -144,6 +146,13 @@
       mClassOrInterfaceName = makeI18nSafe(classOrInterfaceName);
     }
   
  +  public void startStaticInitializer() throws IOException {
  +    checkStateForWrite();
  +    printIndents();
  +    mOut.println("static {");
  +    increaseIndent();
  +  }
  +
     public void startClass(int modifiers,
                            String extendsClassName,
                            String[] interfaceNames)
  @@ -157,6 +166,7 @@
       // We need to write up the actual class declaration and save it until
       // after the imports have been written
       //FIXME we should format this code more nicely
  +    printImportsIfNeeded();
       mOut.print(Modifier.toString(modifiers));
       mOut.print(" class ");
       mOut.print(mClassOrInterfaceName);
  @@ -186,6 +196,7 @@
       // We need to write up the actual class declaration and save it until
       // after the imports have been written
       //FIXME we should format this code more nicely
  +    printImportsIfNeeded();
       mOut.print("public interface ");
       mOut.print(mClassOrInterfaceName);
       if (extendsInterfaceNames != null && extendsInterfaceNames.length > 0) {
  @@ -286,6 +297,15 @@
       getCommentPrinter().println(comment);
     }
   
  +  public void writeImportStatement(String className) throws IOException {
  +    getImportPrinter().println("import " + makeI18nSafe(className) + ";");
  +  }
  +
  +  public void writeEmptyLine() throws IOException {
  +    checkStateForWrite();
  +    mOut.println();
  +  }
  +
     public void writeAnnotation(Annotation ann) throws IOException {
       //FIXME haven't really thought much about how to write annotations
       //as javadoc - this is more just proof-of-concept at this point.
  @@ -304,6 +324,14 @@
       }
     }
   
  +  public void writeStatement(String statement) throws IOException {
  +    checkStateForWrite();
  +    printCommentsIfNeeded();
  +    printIndents();
  +    mOut.print(statement);
  +    mOut.println(";");
  +  }
  +
     public void writeReturnStatement(Expression expression) throws IOException {
       mLogger.logVerbose("return");
       checkStateForWrite();
  @@ -369,7 +397,7 @@
     // ExpressionFactory implementation
   
     private static final Expression TRUE = newExp("true");
  -  private static final Expression FALSE = newExp("true");
  +  private static final Expression FALSE = newExp("false");
     private static final Expression NULL = newExp("null");
   
     public Expression createBoolean(boolean value) {
  @@ -388,6 +416,10 @@
       return NULL;
     }
   
  +  public Expression createVerbatim(String value) {
  +    return newExp(makeI18nSafe(value));
  +  }
  +
     // ========================================================================
     // Private methods
   
  @@ -416,6 +448,23 @@
       mOut.println(" */");
       mCommentBuffer = null;
       mCommentPrinter = null;
  +  }
  +
  +  private PrintWriter getImportPrinter() {
  +    if (mImportPrinter == null) {
  +      mImportBuffer = new StringWriter();
  +      mImportPrinter = new PrintWriter(mImportBuffer);
  +    }
  +    return mImportPrinter;
  +  }
  +
  +  private void printImportsIfNeeded() {
  +    if (mImportBuffer == null) return;
  +    checkStateForWrite();
  +    String imports = mImportBuffer.toString();
  +    mOut.println(imports);
  +    mImportBuffer = null;
  +    mImportPrinter = null;
     }
   
     private void checkStateForWrite() {
  
  
  
  1.5       +18 -2     xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/joust/ValidatingJavaOutputStream.java
  
  Index: ValidatingJavaOutputStream.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/joust/ValidatingJavaOutputStream.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ValidatingJavaOutputStream.java	12 Feb 2004 20:06:07 -0000	1.4
  +++ ValidatingJavaOutputStream.java	21 Mar 2004 01:58:27 -0000	1.5
  @@ -29,12 +29,12 @@
    * <pre>
    *   +<-----endClass<-----+ +<-------------------------------+
    *   |                    | |                                |
  - * BEGIN-->startClass--->CLASS-------->writeImport---------->+
  + * BEGIN-->startClass--->CLASS---->writeImportStatement----->+
    *                         |                                 |
    *                         +------->writeMemberVariable----->+
    *                         |                                 |
    *                         +--->startMethod--->METHOD--->endMethod
  - *                               (or ctor)      | ^      (or ctor)
  + *                          (or ctor or static)   | ^   (or ctor or static)
    *                                              V |
    *                                       write...Statement
    * </pre>
  @@ -90,6 +90,10 @@
       return mDest.writeField(modifiers, typeName, fieldName, defaultValue);
     }
   
  +  public void startStaticInitializer() throws IOException {
  +    mDest.startStaticInitializer();
  +  }
  +
     public Variable[] startConstructor(int modifiers,
                                        String[] paramTypeNames,
                                        String[] paramNames,
  @@ -110,6 +114,10 @@
                                paramTypeNames, paramNames, exceptionClassNames);
     }
   
  +  public void writeEmptyLine() throws IOException {
  +    mDest.writeEmptyLine();
  +  }
  +
     public void writeComment(String comment) throws IOException {
       mDest.writeComment(comment);
     }
  @@ -120,6 +128,14 @@
   
     public Annotation createAnnotation(String type) {
       return mDest.createAnnotation(type);
  +  }
  +
  +  public void writeImportStatement(String className) throws IOException {
  +    mDest.writeImportStatement(className);
  +  }
  +
  +  public void writeStatement(String statement) throws IOException {
  +    mDest.writeStatement(statement);
     }
   
     public void writeReturnStatement(Expression expression) throws IOException {
  
  
  
  1.6       +5 -4      xml-xmlbeans/v2/src/common/org/apache/xmlbeans/impl/common/NameUtil.java
  
  Index: NameUtil.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/common/org/apache/xmlbeans/impl/common/NameUtil.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- NameUtil.java	13 Mar 2004 00:33:22 -0000	1.5
  +++ NameUtil.java	21 Mar 2004 01:58:27 -0000	1.6
  @@ -493,7 +493,7 @@
           StringBuffer buf = new StringBuffer();
           for (Iterator it = result.iterator(); it.hasNext(); )
           {
  -            buf.append(nonJavaKeyword(lowerCamelCase((String)it.next(), useJaxRpcRules)));
  +            buf.append(nonJavaKeyword(lowerCamelCase((String)it.next(), useJaxRpcRules, true)));
               buf.append('.');
           }
           return buf.substring(0, buf.length() - 1); // chop off extra dot
  @@ -585,13 +585,14 @@
        */
       public static String lowerCamelCase(String xml_name)
       {
  -        return lowerCamelCase(xml_name, false);
  +        return lowerCamelCase(xml_name, false, true);
       }
   
       /**
        * Returns a camel-cased string using the JAXB or JAX-RPC rules
        */
  -    public static String lowerCamelCase(String xml_name, boolean useJaxRpcRules)
  +    public static String lowerCamelCase(String xml_name, boolean useJaxRpcRules,
  +                                        boolean fixGeneratedName)
       {
           StringBuffer buf = new StringBuffer();
           List words = splitWords(xml_name, useJaxRpcRules);
  @@ -600,7 +601,7 @@
           {
               String first = ((String)words.get(0)).toLowerCase();
               char f = first.charAt(0);
  -            if (!Character.isJavaIdentifierStart(f))
  +            if (!Character.isJavaIdentifierStart(f) && fixGeneratedName)
                   buf.append("x");
               buf.append(first);
   
  
  
  
  1.5       +6 -1      xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/util/XsTypeConverter.java
  
  Index: XsTypeConverter.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/util/XsTypeConverter.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- XsTypeConverter.java	2 Mar 2004 04:52:13 -0000	1.4
  +++ XsTypeConverter.java	21 Mar 2004 01:58:27 -0000	1.5
  @@ -511,7 +511,12 @@
   
       public static String printDateTime(Calendar c)
       {
  -        GDate value = getGDateValue(c, SchemaType.BTC_DATE_TIME);
  +        return printDateTime(c, SchemaType.BTC_DATE_TIME);
  +    }
  +
  +    public static String printDateTime(Calendar c, int type_code)
  +    {
  +        GDate value = getGDateValue(c, type_code);
           return value.canonicalString();
       }
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xmlbeans-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-cvs-help@xml.apache.org


Mime
View raw message