[ http://issues.apache.org/jira/browse/JAXME-47?page=comments#action_57714 
]
     
Nacho G. Mac Dowell commented on JAXME-47:
------------------------------------------

If we make use of DataTypeConverter then we could easily provide a utility 
class that would gives us a String representation of the constructed default 
value. In other words, if runtimeType.getName() is BigInteger and pDefaultValue 
is "1" we want : javax.xml.bind.DatatypeConverter.parseInteger("1")

Let's take a look at a possible candidate:

import java.lang.reflect.Method;
import java.util.Hashtable;
import javax.xml.bind.DatatypeConverter;
import org.apache.ws.jaxme.js.JavaQName;

public class DataTypeConverterUtil {

  /**
   * Cache converters so we don't have to use reflection all the time.
   */
  private static final Hashtable cache = new Hashtable();

  /**
   * Returns a String representation of how to convert from a String
   * (pDefaultValue) to the provided JavaQName type. eg: it will return:
   * 
   * <code>javax.xml.bind.DatatypeConverter.parseInteger("1")</code>
   * 
   * if runtimeType.getName() is BigInteger and pDefaultValue is "1".
   * 
   * @param pRuntimeType
   *            the type
   * @param pDefaultValue
   *            the default value
   * @return a String representing the conversion
   */
  public static String getDataTypeConverter(JavaQName pRuntimeType,
      String pDefaultValue) {
    try {
      return getDataTypeConverter(Class.forName(pRuntimeType
          .getPackageName()
          + "." + pRuntimeType.getClassName()), pDefaultValue);
    } catch (ClassNotFoundException e) {
      //it is a simple type (or at least we hope so...)
      return pDefaultValue;
    }
  }

  /**
   * @see DataTypeConverterUtil#getDataTypeConverter(JavaQName, String)
   */
  public static String getDataTypeConverter(Class pClass, String pDefaultValue) 
{
    String dataTypeConverter = (String) cache.get(pClass);
    if (dataTypeConverter == null) {
      final Method[] methods = DatatypeConverter.class.getMethods();
      for (int i = 0; i < methods.length; i++) {
        if (methods[i].getName().startsWith("parse")
            && methods[i].getReturnType().equals(pClass)) {
          dataTypeConverter = DatatypeConverter.class.getName() + "."
              + methods[i].getName() + "(\"" + pDefaultValue
              + "\")";
        }
      }
      if (dataTypeConverter == null) {
        //it is most probably just an Object, let it be a String
        dataTypeConverter = "\"" + pDefaultValue + "\"";
      }
      cache.put(pClass.getName(), dataTypeConverter);
    }
    return dataTypeConverter;
  }
}

What do you think? Another good thing would be that the defaultValue is 
returned whenever the requested variable is null. This means that all vars are 
initialized to null and is the getter that checks if it is null. If it is, then 
return the cast. This is how RI works. This would require changing the 
signature of getXMLGetMethod and getXMLField in TypeSGChain so it is 
getXMLGetMethod which receives the default value.


> When using default values initialization of vars doesn't work for all types
> ---------------------------------------------------------------------------
>
>          Key: JAXME-47
>          URL: http://issues.apache.org/jira/browse/JAXME-47
>      Project: JaxMe
>         Type: Bug
>   Components: JaxMe Core
>     Reporter: Nacho G. Mac Dowell

>
> Consider the definition of the following element:
>     <element name="elt">
>         <complexType>
>             <attribute name="a" default="1" type="decimal" />
>             <attribute name="b" default="0" type="hexBinary" />
>             <attribute name="c" default="none">
>                 <simpleType>
>                     <union>
>                         <simpleType>
>                             <restriction base="NMTOKEN"/>
>                         </simpleType>
>                         <simpleType>
>                             <restriction base="anyURI" />
>                         </simpleType>
>                     </union>
>                 </simpleType>
>             </attribute>
>             <attribute name="d" default="1" type="integer" />
>             <attribute name="e" default="-1" type="nonPositiveInteger" />
>             <attribute name="f" default="1" type="positiveInteger" />
>             <attribute name="g" default="1" type="unsignedLong" />
>         </complexType>
>     </element>
> When compiling the schema we get the following result:
>   private java.math.BigDecimal A = 1;
>   private byte[] B = 01;
>   private java.lang.Object C = none;
>   private java.math.BigInteger D = 1;
>   private java.math.BigInteger E = -1;
>   private java.math.BigInteger F = 1;
>   private java.math.BigInteger G = 1;
> having multiple compile errors.
> Further investigation is required to determine exactly all cases.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
If you want more information on JIRA, or have a bug to report see:
   http://www.atlassian.com/software/jira


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to