You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlbeans.apache.org by da...@apache.org on 2004/03/06 22:25:25 UTC

cvs commit: xml-xmlbeans/v2/src/common/org/apache/xmlbeans/impl/common NameUtil.java

daveremy    2004/03/06 13:25:25

  Modified:    v2/src/binding/org/apache/xmlbeans/impl/binding/bts
                        BindingProperty.java BuiltinBindingLoader.java
                        JavaTypeName.java
               v2/src/binding/org/apache/xmlbeans/impl/binding/compile
                        Schema2Java.java
               v2/src/common/org/apache/xmlbeans/impl/common NameUtil.java
  Log:
  Few fixes and improvements to the Schema2Java code.
  Contributed by Radu Preotiuc.
  
  Revision  Changes    Path
  1.11      +9 -0      xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/bts/BindingProperty.java
  
  Index: BindingProperty.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/bts/BindingProperty.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- BindingProperty.java	12 Feb 2004 21:18:15 -0000	1.10
  +++ BindingProperty.java	6 Mar 2004 21:25:25 -0000	1.11
  @@ -36,6 +36,7 @@
     private MethodName issetter;
     private String field;
     private JavaTypeName collection;
  +  private JavaTypeName boxed;
   
     // ========================================================================
     // Constructors
  @@ -153,6 +154,14 @@
   
     public void setCollectionClass(JavaTypeName jName) {
       collection = jName;
  +  }
  +
  +  public JavaTypeName getBoxedClass() {
  +    return boxed;
  +  }
  +
  +  public void setBoxedClass(JavaTypeName jName) {
  +    boxed = jName;
     }
   
     // ========================================================================
  
  
  
  1.11      +17 -8     xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/bts/BuiltinBindingLoader.java
  
  Index: BuiltinBindingLoader.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/bts/BuiltinBindingLoader.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- BuiltinBindingLoader.java	19 Feb 2004 18:43:56 -0000	1.10
  +++ BuiltinBindingLoader.java	6 Mar 2004 21:25:25 -0000	1.11
  @@ -92,24 +92,33 @@
       addPojoXml("Name", "java.lang.String");
       addPojoXml("NCName", "java.lang.String");
       addPojoXml("NMTOKEN", "java.lang.String");
  +    addPojoXml("NMTOKENS", "java.lang.String[]");
       addPojoXml("ID", "java.lang.String");
       addPojoXml("IDREF", "java.lang.String");
  +    addPojoXml("IDREFS", "java.lang.String[]");
       addPojoXml("ENTITY", "java.lang.String");
  +    addPojoXml("ENTITIES", "java.lang.String[]");
   
  -    addPojoTwoWay("duration", "org.apache.xmlbeans.GDuration");
  +    addPojoXml("duration", "java.lang.String");
  +    addPojoJava("duration", "org.apache.xmlbeans.GDuration");
   
       addPojoTwoWay("dateTime", "java.util.Calendar");
       addPojoJava("dateTime", "java.util.Date");
       addPojoXml("time", "java.util.Calendar");
       addPojoXml("date", "java.util.Calendar");
       addPojo("date", "java.util.Date");
  -    addPojoXml("gYearMonth", "java.util.Calendar");
  -    addPojoXml("gYear", "java.util.Calendar");
  +    addPojoXml("gYearMonth", "java.lang.String");
  +    addPojo("gYearMonth", "java.util.Calendar");
  +    addPojoXml("gYear", "java.lang.String");
  +    addPojo("gYear", "java.util.Calendar");
       addPojo("gYear", "int");
  -    addPojoXml("gMonthDay", "java.util.Calendar");
  -    addPojoXml("gMonth", "java.util.Calendar");
  +    addPojoXml("gMonthDay", "java.lang.String");
  +    addPojo("gMonthDay", "java.util.Calendar");
  +    addPojoXml("gMonth", "java.lang.String");
  +    addPojo("gMonth", "java.util.Calendar");
       addPojo("gMonth", "int");
  -    addPojoXml("gDay", "java.util.Calendar");
  +    addPojoXml("gDay", "java.lang.String");
  +    addPojo("gDay", "java.util.Calendar");
       addPojo("gDay", "int");
   
       addPojoTwoWay("boolean", "boolean");
  @@ -133,8 +142,8 @@
       addPojoXml("unsignedInt", "long");
       addPojoXml("unsignedShort", "int");
       addPojoXml("unsignedByte", "short");
  -    addPojoXml("anyURI", "java.lang.String");
  -    addPojoJava("anyURI", "java.net.URI");
  +    addPojoTwoWay("anyURI", "java.lang.String");
  +    addPojo("anyURI", "java.net.URI");
       addPojoTwoWay("QName", "javax.xml.namespace.QName");
       addPojoXml("NOTATION", "java.lang.String");
   
  
  
  
  1.8       +18 -0     xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/bts/JavaTypeName.java
  
  Index: JavaTypeName.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/bts/JavaTypeName.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- JavaTypeName.java	3 Mar 2004 18:53:28 -0000	1.7
  +++ JavaTypeName.java	6 Mar 2004 21:25:25 -0000	1.8
  @@ -49,6 +49,12 @@
     // Constants
   
     private static String XMLOBJECT_CLASSNAME = XmlObject.class.getName();
  +  private static String[] PRIMITIVE_TYPES =
  +  {"int", "boolean", "float", "long", "double", "short", "char"};
  +  private static String[] BOXED_TYPES =
  +  {"java.lang.Integer", "java.lang.Boolean", "java.lang.Float",
  +   "java.lang.Long", "java.lang.Double", "java.lang.Short",
  +   "java.lang.Character"};
   
     // ========================================================================
     // Variables
  @@ -107,6 +113,18 @@
       return forString(itemType.toString() + arrayBrackets);
     }
   
  +
  +  /**
  +   * Builds a JavaTypeName for the boxed type corresponding to the
  +   * given JavaTypeName.
  +   */
  +  public static JavaTypeName forBoxed(JavaTypeName type) {
  +    // We could use a map here and initialize it on first use
  +    for (int i = 0; i < PRIMITIVE_TYPES.length; i++)
  +      if (PRIMITIVE_TYPES[i].equals(type.toString()))
  +        return forString(BOXED_TYPES[i]);
  +    return null;
  +  }
   
   
     /**
  
  
  
  1.15      +146 -31   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.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- Schema2Java.java	18 Feb 2004 21:15:23 -0000	1.14
  +++ Schema2Java.java	6 Mar 2004 21:25:25 -0000	1.15
  @@ -305,9 +305,39 @@
         case Scratch.LITERALARRAY_TYPE:
           {
             SchemaType itemType = getLiteralArrayItemType(scratch.getSchemaType());
  +          boolean nillable = scratch.getSchemaType().
  +              getProperties()[0].hasNillable() != SchemaProperty.NEVER;
             Scratch itemScratch = scratchForSchemaType(itemType);
  -          resolveJavaName(itemScratch);
  -          scratch.setJavaName(JavaTypeName.forArray(itemScratch.getJavaName(), 1));
  +          JavaTypeName itemName = null;
  +          if (itemScratch == null)
  +          {
  +              BindingType bType = mLoader.getBindingType(mLoader.
  +                  lookupPojoFor(XmlTypeName.forSchemaType(itemType)));
  +              if (bType != null)
  +                  itemName = bType.getName().getJavaName();
  +              else if (itemType.isBuiltinType())
  +                  logError("Bultin type " + itemType.getName() + " is not supported",
  +                      null, itemType);
  +              else
  +                  throw new IllegalStateException(itemType.getName().toString()+
  +                      " type is not on mLoader");
  +          }
  +          else
  +          {
  +              resolveJavaName(itemScratch);
  +              itemName = itemScratch.getJavaName();
  +          }
  +          if (itemName != null)
  +              if (nillable)
  +              {
  +                  JavaTypeName boxedName = JavaTypeName.forBoxed(itemName);
  +                  if (boxedName != null)
  +                      scratch.setJavaName(JavaTypeName.forArray(boxedName, 1));
  +                  else
  +                      scratch.setJavaName(JavaTypeName.forArray(itemName, 1));
  +              }
  +              else
  +                  scratch.setJavaName(JavaTypeName.forArray(itemName, 1));
             return;
           }
   
  @@ -336,23 +366,47 @@
           {
             logVerbose("processing element "+scratch.getXmlName());
             SchemaType contentType = scratch.getSchemaType().getProperties()[0].getType();
  +          boolean nillable = scratch.getSchemaType().
  +              getProperties()[0].hasNillable() != SchemaProperty.NEVER;
             logVerbose("content type is "+contentType.getName());
  -          if (contentType.isSimpleType()) {
  -            //REVIEW this is a quick bug fix for the case where an element
  -            //is of a primitive type.  I'm not completely sure it is the right
  -            //thing to do.  pcal
  -            XmlTypeName xtn = XmlTypeName.forSchemaType(contentType);
  -            scratch.setJavaName(mLoader.lookupPojoFor(xtn).getJavaName());
  -            scratch.setAsIf(xtn);
  -            return;
  -          } else {
  -            Scratch contentScratch = scratchForSchemaType(contentType);
  -            logVerbose("content scratch is "+contentScratch);
  -            resolveJavaName(contentScratch);
  -            scratch.setJavaName(contentScratch.getJavaName());
  -            scratch.setAsIf(contentScratch.getXmlName());
  -            return;
  +          JavaTypeName contentName = null;
  +          Scratch contentScratch = scratchForSchemaType(contentType);
  +          logVerbose("content scratch is "+contentScratch);
  +          if (contentScratch == null)
  +          {
  +              XmlTypeName treatAs = XmlTypeName.forSchemaType(contentType);
  +              BindingType bType = mLoader.getBindingType(mLoader.
  +                  lookupPojoFor(treatAs));
  +              if (bType != null)
  +              {
  +                  contentName = bType.getName().getJavaName();
  +                  scratch.setAsIf(treatAs);
  +              }
  +              else if (contentType.isBuiltinType())
  +                  logError("Builtin type " + contentType.getName() + " is not supported",
  +                      null, contentType);
  +              else
  +                  throw new IllegalStateException(contentType.getName().toString()+
  +                      " type is not on mLoader");
  +          }
  +          else
  +          {
  +              resolveJavaName(contentScratch);
  +              contentName = contentScratch.getJavaName();
  +              scratch.setAsIf(contentScratch.getXmlName());
             }
  +          if (contentName != null)
  +              if (nillable)
  +              {
  +                  JavaTypeName boxedName = JavaTypeName.forBoxed(contentName);
  +                  if (boxedName != null)
  +                      scratch.setJavaName(boxedName);
  +                  else
  +                      scratch.setJavaName(contentName);
  +              }
  +              else
  +                  scratch.setJavaName(contentName);
  +          return;
           }
   
         default:
  @@ -424,8 +478,6 @@
     /**
      * Now we resolve the structural aspects (property names) for each
      * scratch.
  -   *
  -   * todo: understand how we want inheritance to work
      */
     private void resolveJavaStructure(Scratch scratch) {
       if (scratch.getCategory() != Scratch.STRUCT_TYPE)
  @@ -436,7 +488,14 @@
   
       scratch.setStructureResolved(true);
   
  -    SchemaType baseType = scratch.getSchemaType().getBaseType();
  +    SchemaType schemaType = scratch.getSchemaType();
  +    SchemaType baseType = schemaType.getBaseType();
  +    int derivationType  = schemaType.getDerivationType();
  +    if (derivationType == SchemaType.DT_RESTRICTION)
  +    {
  +        // Derivation type is restriction, so no new properties may be added
  +        return;
  +    }
       Collection baseProperties = null;
       if (baseType != null)
         baseProperties = extractProperties(baseType);
  @@ -463,27 +522,52 @@
           seenMethodNames.add(prop.getSetterName());
       }
   
  +    if (schemaType.getContentType() == SchemaType.SIMPLE_CONTENT &&
  +        baseType.isSimpleType())
  +    {
  +        // we have to add a '_value' property to hold the value corresponding to the
  +        // content of the XML elem
  +        BindingType bType = extractBindingType(baseType);
  +        if (bType == null)
  +            throw new IllegalStateException("Type " + baseType.getName() +
  +                "not found in type loader");
  +        String propName = "_value";
  +        QNameProperty prop = new QNameProperty();
  +        prop.setQName(null);
  +        prop.setAttribute(false);
  +        prop.setSetterName(MethodName.create("set" + propName,
  +                bType.getName().getJavaName()));
  +        prop.setGetterName(MethodName.create("get" + propName));
  +        prop.setBindingType(bType);
  +        prop.setNillable(false);
  +        prop.setOptional(false);
  +        prop.setMultiple(false);
  +        scratch.addQNameProperty(prop);
  +    }
  +
       // now deal with remaining props
  -    SchemaProperty[] props = scratch.getSchemaType().getProperties();
  +    SchemaProperty[] props = schemaType.getProperties();
       for (int i = 0; i < props.length; i++) {
         QNameProperty prop = (QNameProperty) (props[i].isAttribute() ? seenAttrProps : seenEltProps).get(props[i].getName());
         if (prop != null) {
           // already seen property: verify multiplicity looks cool
           if (prop.isMultiple() != isMultiple(props[i])) {
  -          // todo: signal nicer error
  -          throw new IllegalStateException("Can't change multiplicity");
  +            logError("Could not bind element \"" + props[i].getName() +
  +                "\" because the corresponding element in the base type has a " +
  +                "different 'maxOccurs' value", null, props[i]);
           }
   
           // todo: think about optionality and nillability too
         } else {
           SchemaType sType = props[i].getType();
           BindingType bType = bindingTypeForSchemaType(sType);
  +        if (bType == null)
  +            throw new IllegalStateException("Type " + sType.getName() +
  +                "not found in type loader");
   
           String propName = pickUniquePropertyName(props[i].getName(), seenMethodNames);
           boolean isMultiple = isMultiple(props[i]);
           JavaTypeName collection = null;
  -        if (isMultiple)
  -          collection = JavaTypeName.forArray(bType.getName().getJavaName(), 1);
   
           prop = new QNameProperty();
           prop.setQName(props[i].getName());
  @@ -491,11 +575,20 @@
           prop.setSetterName(MethodName.create("set" + propName,
                                                bType.getName().getJavaName()));
           prop.setGetterName(MethodName.create("get" + propName));
  -        prop.setCollectionClass(collection);
           prop.setBindingType(bType);
           prop.setNillable(props[i].hasNillable() != SchemaProperty.NEVER);
           prop.setOptional(isOptional(props[i]));
           prop.setMultiple(isMultiple);
  +        if (prop.isNillable() || prop.isOptional())
  +            prop.setBoxedClass(JavaTypeName.forBoxed(bType.getName().getJavaName()));
  +        if (prop.isMultiple())
  +        {
  +            if (prop.getBoxedClass() != null)
  +                collection = JavaTypeName.forArray(prop.getBoxedClass(), 1);
  +            else
  +                collection = JavaTypeName.forArray(bType.getName().getJavaName(), 1);
  +        }
  +        prop.setCollectionClass(collection);
         }
         scratch.addQNameProperty(prop);
       }
  @@ -530,10 +623,10 @@
     }
   
     /**
  -   * True if the given SchemaProperty has minOccurs < 1
  +   * True if the given SchemaProperty has minOccurs < 1 and maxOccurs <= 1
      */
     private static boolean isOptional(SchemaProperty prop) {
  -    return (prop.getMinOccurs().signum() == 0);
  +      return (prop.getMinOccurs().signum() == 0 && !isMultiple(prop));
     }
   
     /**
  @@ -563,6 +656,22 @@
     }
   
     /**
  +   * Returns the simple type which the base of a complex Type with simpleContent
  +   */
  +  private BindingType extractBindingType(SchemaType sType) {
  +    // case 1: it's in the current area
  +    Scratch scratch = scratchForSchemaType(sType);
  +    if (scratch != null)
  +      return scratch.getBindingType();
  +
  +    // case 2: it's in the mLoader
  +    BindingType bType = mLoader.getBindingType(mLoader.lookupPojoFor(XmlTypeName.
  +            forSchemaType(sType)));
  +
  +    return bType;
  +  }
  +
  +  /**
      * True for a schema type that is a SOAP array.
      */
     private static boolean isSoapArray(SchemaType sType) {
  @@ -648,10 +757,10 @@
       String baseName = NameUtil.getClassNameFromQName(qname);
       String pickedName = baseName;
   
  -    for (int i = 1; usedNames.contains(pickedName); i += 1)
  +    for (int i = 1; usedNames.contains(pickedName.toLowerCase()); i += 1)
         pickedName = baseName + i;
   
  -    usedNames.add(pickedName);
  +    usedNames.add(pickedName.toLowerCase());
   
       return JavaTypeName.forString(pickedName);
     }
  @@ -993,7 +1102,10 @@
   
       String packageName = javaName.getPackage();
       String shortClassName = javaName.getShortClassName();
  -    BindingType baseType = bindingTypeForSchemaType(scratch.getSchemaType().getBaseType());
  +    BindingType baseType = null;
  +    SchemaType bSchemaType = scratch.getSchemaType().getBaseType();
  +    if (!bSchemaType.isSimpleType())
  +      baseType = bindingTypeForSchemaType(bSchemaType);
       String baseJavaname = null;
       if (baseType != null) {
         baseJavaname = baseType.getName().getJavaName().toString();
  @@ -1019,6 +1131,9 @@
       for (Iterator i = props.iterator(); i.hasNext();) {
         QNameProperty prop = (QNameProperty) i.next();
         JavaTypeName jType = prop.getTypeName().getJavaName();
  +      if (prop.getBoxedClass() != null) {
  +        jType = prop.getBoxedClass();
  +      }
         if (prop.getCollectionClass() != null) {
           jType = prop.getCollectionClass();
         }
  
  
  
  1.3       +0 -1      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.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- NameUtil.java	12 Feb 2004 20:06:08 -0000	1.2
  +++ NameUtil.java	6 Mar 2004 21:25:25 -0000	1.3
  @@ -722,7 +722,6 @@
           return (c == HYPHEN
                 || c == PERIOD
                 || c == COLON
  -              || c == USCORE
                 || c == DOT
                 || c == TELEIA
                 || c == AYAH
  
  
  

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