You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlbeans.apache.org by zi...@apache.org on 2004/04/08 01:30:57 UTC

cvs commit: xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/util XsTypeConverter.java

zieg        2004/04/07 16:30:57

  Modified:    v2/src/binding/org/apache/xmlbeans/impl/binding/bts
                        BuiltinBindingLoader.java
                        DefaultBuiltinBindingLoader.java
                        JaxRpcBuiltinBindingLoader.java
               v2/src/marshal/org/apache/xmlbeans/impl/marshal
                        RuntimeBindingTypeTable.java
               v2/src/typeimpl/org/apache/xmlbeans/impl/util
                        XsTypeConverter.java
  Added:       v2/src/marshal/org/apache/xmlbeans/impl/marshal
                        IntDateTypeConverter.java
                        JavaCalendarTypeConverter.java
                        JavaDateTypeConverter.java
  Removed:     v2/src/marshal/org/apache/xmlbeans/impl/marshal
                        DateTimeTypeConverter.java
  Log:
  Finish support for all schema builtin date/time types with all
  conversions in both default and jax-rpc styles.
  
  Revision  Changes    Path
  1.15      +0 -2      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.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- BuiltinBindingLoader.java	13 Mar 2004 00:47:30 -0000	1.14
  +++ BuiltinBindingLoader.java	7 Apr 2004 23:30:56 -0000	1.15
  @@ -15,8 +15,6 @@
   
   package org.apache.xmlbeans.impl.binding.bts;
   
  -import org.apache.xmlbeans.impl.binding.bts.BindingType;
  -
   import javax.xml.namespace.QName;
   
   /**
  
  
  
  1.2       +1 -6      xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/bts/DefaultBuiltinBindingLoader.java
  
  Index: DefaultBuiltinBindingLoader.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/bts/DefaultBuiltinBindingLoader.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DefaultBuiltinBindingLoader.java	13 Mar 2004 00:33:21 -0000	1.1
  +++ DefaultBuiltinBindingLoader.java	7 Apr 2004 23:30:56 -0000	1.2
  @@ -15,9 +15,6 @@
   
   package org.apache.xmlbeans.impl.binding.bts;
   
  -import org.apache.xmlbeans.impl.binding.bts.BindingType;
  -
  -import javax.xml.namespace.QName;
   
   /**
    * Represents builtin bindings.
  @@ -26,9 +23,7 @@
   
     // ========================================================================
     // Constants
  -
  -  private static final String xsns = "http://www.w3.org/2001/XMLSchema";
  -  private static final BuiltinBindingLoader INSTANCE = new DefaultBuiltinBindingLoader();
  +    private static final BuiltinBindingLoader INSTANCE = new DefaultBuiltinBindingLoader();
   
     // ========================================================================
     // Factory
  
  
  
  1.2       +1 -5      xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/bts/JaxRpcBuiltinBindingLoader.java
  
  Index: JaxRpcBuiltinBindingLoader.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/bts/JaxRpcBuiltinBindingLoader.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- JaxRpcBuiltinBindingLoader.java	13 Mar 2004 00:33:21 -0000	1.1
  +++ JaxRpcBuiltinBindingLoader.java	7 Apr 2004 23:30:56 -0000	1.2
  @@ -15,9 +15,7 @@
   
   package org.apache.xmlbeans.impl.binding.bts;
   
  -import org.apache.xmlbeans.impl.binding.bts.BindingType;
   
  -import javax.xml.namespace.QName;
   
   /**
    * Represents builtin bindings in JAX-RPC style
  @@ -26,9 +24,7 @@
   
     // ========================================================================
     // Constants
  -
  -  private static final String xsns = "http://www.w3.org/2001/XMLSchema";
  -  private static final BuiltinBindingLoader INSTANCE = new JaxRpcBuiltinBindingLoader();
  +    private static final BuiltinBindingLoader INSTANCE = new JaxRpcBuiltinBindingLoader();
   
     // ========================================================================
     // Factory
  
  
  
  1.25      +68 -9     xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/RuntimeBindingTypeTable.java
  
  Index: RuntimeBindingTypeTable.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/RuntimeBindingTypeTable.java,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- RuntimeBindingTypeTable.java	6 Apr 2004 00:53:09 -0000	1.24
  +++ RuntimeBindingTypeTable.java	7 Apr 2004 23:30:56 -0000	1.25
  @@ -15,6 +15,7 @@
   
   package org.apache.xmlbeans.impl.marshal;
   
  +import org.apache.xmlbeans.SchemaType;
   import org.apache.xmlbeans.XmlException;
   import org.apache.xmlbeans.impl.binding.bts.BindingLoader;
   import org.apache.xmlbeans.impl.binding.bts.BindingType;
  @@ -24,20 +25,19 @@
   import org.apache.xmlbeans.impl.binding.bts.BuiltinBindingType;
   import org.apache.xmlbeans.impl.binding.bts.ByNameBean;
   import org.apache.xmlbeans.impl.binding.bts.JavaTypeName;
  +import org.apache.xmlbeans.impl.binding.bts.JaxrpcEnumType;
   import org.apache.xmlbeans.impl.binding.bts.ListArrayType;
   import org.apache.xmlbeans.impl.binding.bts.SimpleBindingType;
   import org.apache.xmlbeans.impl.binding.bts.SimpleContentBean;
   import org.apache.xmlbeans.impl.binding.bts.SimpleDocumentBinding;
   import org.apache.xmlbeans.impl.binding.bts.WrappedArrayType;
   import org.apache.xmlbeans.impl.binding.bts.XmlTypeName;
  -import org.apache.xmlbeans.impl.binding.bts.JaxrpcEnumType;
   import org.apache.xmlbeans.impl.common.ConcurrentReaderHashMap;
   import org.apache.xmlbeans.impl.common.XmlWhitespace;
   
   import javax.xml.namespace.QName;
   import java.math.BigDecimal;
   import java.math.BigInteger;
  -import java.util.Calendar;
   import java.util.Map;
   
   /**
  @@ -167,14 +167,18 @@
                                  JavaTypeName jName,
                                  TypeConverter converter)
       {
  -        // todo(radup) save the "jaxRpc" setting in the binding file and
  -        // load it from there
  -        final BindingLoader bindingLoader = BuiltinBindingLoader.getBuiltinBindingLoader(false);
  +        final BindingLoader default_builtin_loader =
  +            BuiltinBindingLoader.getBuiltinBindingLoader(false);
   
           QName xml_type = new QName(XSD_NS, xsdType);
           XmlTypeName xName = XmlTypeName.forTypeNamed(xml_type);
  -        BindingType btype =
  -            bindingLoader.getBindingType(BindingTypeName.forPair(jName, xName));
  +        final BindingTypeName btname = BindingTypeName.forPair(jName, xName);
  +        BindingType btype = default_builtin_loader.getBindingType(btname);
  +        if (btype == null) {
  +            final BindingLoader jaxrpc_builtin_loader =
  +                BuiltinBindingLoader.getBuiltinBindingLoader(true);
  +            btype = jaxrpc_builtin_loader.getBindingType(btname);
  +        }
           if (btype == null) {
               throw new AssertionError("failed to find builtin for java:" + jName +
                                        " - xsd:" + xName);
  @@ -255,6 +259,12 @@
           addXsdBuiltin("IDREF", str, string_conv);
           addXsdBuiltin("ENTITY", str, string_conv);
   
  +        addXsdBuiltin("gDay", str, string_conv);
  +        addXsdBuiltin("gMonth", str, string_conv);
  +        addXsdBuiltin("gMonthDay", str, string_conv);
  +        addXsdBuiltin("gYear", str, string_conv);
  +        addXsdBuiltin("gYearMonth", str, string_conv);
  +
           addXsdBuiltin("anyURI",
                         str,
                         new AnyUriToStringTypeConverter());
  @@ -267,9 +277,58 @@
           addXsdBuiltin("NMTOKENS", str_array,
                         new StringListArrayConverter());
   
  +
  +        addXsdBuiltin("dateTime",
  +                      java.util.Calendar.class,
  +                      new JavaCalendarTypeConverter(SchemaType.BTC_DATE_TIME));
  +
           addXsdBuiltin("dateTime",
  -                      Calendar.class,
  -                      new DateTimeTypeConverter());
  +                      java.util.Date.class,
  +                      new JavaDateTypeConverter(SchemaType.BTC_DATE_TIME));
  +
  +        addXsdBuiltin("time",
  +                      java.util.Calendar.class,
  +                      new JavaCalendarTypeConverter(SchemaType.BTC_TIME));
  +
  +        addXsdBuiltin("date",
  +                      java.util.Calendar.class,
  +                      new JavaCalendarTypeConverter(SchemaType.BTC_DATE));
  +
  +        addXsdBuiltin("date",
  +                      java.util.Date.class,
  +                      new JavaDateTypeConverter(SchemaType.BTC_DATE));
  +
  +        addXsdBuiltin("gDay",
  +                      java.util.Calendar.class,
  +                      new JavaCalendarTypeConverter(SchemaType.BTC_G_DAY));
  +
  +        addXsdBuiltin("gMonth",
  +                      java.util.Calendar.class,
  +                      new JavaCalendarTypeConverter(SchemaType.BTC_G_MONTH));
  +
  +        addXsdBuiltin("gMonthDay",
  +                      java.util.Calendar.class,
  +                      new JavaCalendarTypeConverter(SchemaType.BTC_G_MONTH_DAY));
  +
  +        addXsdBuiltin("gYear",
  +                      java.util.Calendar.class,
  +                      new JavaCalendarTypeConverter(SchemaType.BTC_G_YEAR));
  +
  +        addXsdBuiltin("gYearMonth",
  +                      java.util.Calendar.class,
  +                      new JavaCalendarTypeConverter(SchemaType.BTC_G_YEAR_MONTH));
  +
  +
  +        addXsdBuiltin("gDay",
  +                      int.class,
  +                      new IntDateTypeConverter(SchemaType.BTC_G_DAY));
  +        addXsdBuiltin("gMonth",
  +                      int.class,
  +                      new IntDateTypeConverter(SchemaType.BTC_G_MONTH));
  +        addXsdBuiltin("gYear",
  +                      int.class,
  +                      new IntDateTypeConverter(SchemaType.BTC_G_YEAR));
  +
   
           addXsdBuiltin("QName",
                         QName.class,
  
  
  
  1.1                  xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/IntDateTypeConverter.java
  
  Index: IntDateTypeConverter.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.marshal;
  
  import org.apache.xmlbeans.GDate;
  import org.apache.xmlbeans.GDateBuilder;
  import org.apache.xmlbeans.GDateSpecification;
  import org.apache.xmlbeans.SchemaType;
  import org.apache.xmlbeans.XmlException;
  import org.apache.xmlbeans.impl.common.InvalidLexicalValueException;
  import org.apache.xmlbeans.impl.util.XsTypeConverter;
  
  
  /**
   * convert between schema date/time types and java.util.Date
   */
  final class IntDateTypeConverter
      extends BaseSimpleTypeConverter
  {
      private final int schemaType;
  
      /**
       *
       * @param schemaType  use codes from SchemaType
       */
      IntDateTypeConverter(int schemaType)
      {
          this.schemaType = schemaType;
      }
  
      protected Object getObject(UnmarshalResult context) throws XmlException
      {
          final GDate gdate = context.getGDateValue();
          return extractIntValue(gdate);
      }
  
      private Object extractIntValue(final GDateSpecification gdate)
      {
          final int val;
          //REVIEW: consider subclasses to avoid this switch
          switch (schemaType) {
              case SchemaType.BTC_G_DAY:
                  val = gdate.getDay();
                  break;
              case SchemaType.BTC_G_MONTH:
                  val = gdate.getMonth();
                  break;
              case SchemaType.BTC_G_YEAR:
                  val = gdate.getYear();
                  break;
              default:
                  throw new AssertionError("inapplicable type: " + schemaType);
          }
          return new Integer(val);
      }
  
      public Object unmarshalAttribute(UnmarshalResult context) throws XmlException
      {
          final GDate gdate = context.getAttributeGDateValue();
          return extractIntValue(gdate);
      }
  
      public Object unmarshalAttribute(CharSequence lexical_value,
                                       UnmarshalResult result)
          throws XmlException
      {
          try {
              GDateSpecification gd =
                  XsTypeConverter.getGDateValue(lexical_value, schemaType);
              return extractIntValue(gd);
          }
          catch (IllegalArgumentException e) {
              throw new InvalidLexicalValueException(e, result.getLocation());
          }
  
      }
  
      public CharSequence print(Object value, MarshalResult result)
          throws XmlException
      {
          final int i = ((Integer)value).intValue();
          //REVIEW: consider subclasses to avoid this switch
          try {
              GDateBuilder b = new GDateBuilder();
              switch (schemaType) {
                  case SchemaType.BTC_G_DAY:
                      b.setDay(i);
                      break;
                  case SchemaType.BTC_G_MONTH:
                      b.setMonth(i);
                      break;
                  case SchemaType.BTC_G_YEAR:
                      b.setYear(i);
                      break;
                  default:
                      throw new AssertionError("inapplicable type: " + schemaType);
              }
              b.setBuiltinTypeCode(schemaType);
              return b.toString();
          }
          catch (IllegalArgumentException iae) {
              throw new XmlException(iae);
          }
      }
  }
  
  
  
  1.1                  xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/JavaCalendarTypeConverter.java
  
  Index: JavaCalendarTypeConverter.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.marshal;
  
  import org.apache.xmlbeans.GDateSpecification;
  import org.apache.xmlbeans.XmlException;
  import org.apache.xmlbeans.impl.common.InvalidLexicalValueException;
  import org.apache.xmlbeans.impl.util.XsTypeConverter;
  
  import java.util.Calendar;
  
  
  /**
   * convert between schema date/time types and java.util.Date
   */
  final class JavaCalendarTypeConverter
      extends BaseSimpleTypeConverter
  {
      private final int schemaType;
  
      /**
       *
       * @param schemaType  use codes from SchemaType
       */
      JavaCalendarTypeConverter(int schemaType)
      {
          this.schemaType = schemaType;
      }
  
      protected Object getObject(UnmarshalResult context) throws XmlException
      {
          return context.getCalendarValue();
      }
  
      public Object unmarshalAttribute(UnmarshalResult context) throws XmlException
      {
          return context.getAttributeCalendarValue();
      }
  
      public Object unmarshalAttribute(CharSequence lexical_value,
                                       UnmarshalResult result)
          throws XmlException
      {
          try {
              GDateSpecification gd =
                  XsTypeConverter.getGDateValue(lexical_value, schemaType);
              return gd.getCalendar();
          }
          catch (IllegalArgumentException e) {
              throw new InvalidLexicalValueException(e, result.getLocation());
          }
  
      }
  
      public CharSequence print(Object value, MarshalResult result)
      {
          Calendar cal = (Calendar)value;
          GDateSpecification gd = XsTypeConverter.getGDateValue(cal, schemaType);
          return gd.toString();
      }
  }
  
  
  
  1.1                  xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/JavaDateTypeConverter.java
  
  Index: JavaDateTypeConverter.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.marshal;
  
  import org.apache.xmlbeans.GDateSpecification;
  import org.apache.xmlbeans.XmlException;
  import org.apache.xmlbeans.impl.common.InvalidLexicalValueException;
  import org.apache.xmlbeans.impl.util.XsTypeConverter;
  
  import java.util.Date;
  
  
  /**
   * convert between schema date/time types and java.util.Date
   */
  final class JavaDateTypeConverter
      extends BaseSimpleTypeConverter
  {
      private final int schemaType;
  
      /**
       *
       * @param schemaType  use codes from SchemaType
       */
      JavaDateTypeConverter(int schemaType)
      {
          this.schemaType = schemaType;
      }
  
      protected Object getObject(UnmarshalResult context) throws XmlException
      {
          return context.getDateValue();
      }
  
      public Object unmarshalAttribute(UnmarshalResult context) throws XmlException
      {
          return context.getAttributeDateValue();
      }
  
      public Object unmarshalAttribute(CharSequence lexical_value,
                                       UnmarshalResult result)
          throws XmlException
      {
          try {
              GDateSpecification gd =
                  XsTypeConverter.getGDateValue(lexical_value, schemaType);
              return gd.getDate();
          }
          catch (IllegalArgumentException e) {
              throw new InvalidLexicalValueException(e, result.getLocation());
          }
  
      }
  
      public CharSequence print(Object value, MarshalResult result)
      {
          Date d = (Date)value;
          GDateSpecification gd = XsTypeConverter.getGDateValue(d, schemaType);
          return gd.toString();
      }
  }
  
  
  
  1.7       +32 -12    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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- XsTypeConverter.java	6 Apr 2004 00:53:09 -0000	1.6
  +++ XsTypeConverter.java	7 Apr 2004 23:30:56 -0000	1.7
  @@ -20,6 +20,7 @@
   import org.apache.xmlbeans.GDateBuilder;
   import org.apache.xmlbeans.XmlCalendar;
   import org.apache.xmlbeans.SchemaType;
  +import org.apache.xmlbeans.GDateSpecification;
   import org.apache.xmlbeans.impl.common.InvalidLexicalValueException;
   
   import javax.xml.namespace.QName;
  @@ -510,7 +511,7 @@
       // ======================== dateTime ========================
       public static XmlCalendar lexDateTime(CharSequence v)
       {
  -        GDate value = getGDateValue(v, SchemaType.BTC_DATE_TIME);
  +        GDateSpecification value = getGDateValue(v, SchemaType.BTC_DATE_TIME);
           return value.getCalendar();
       }
   
  @@ -520,16 +521,32 @@
           return printDateTime(c, SchemaType.BTC_DATE_TIME);
       }
   
  +    public static String printTime(Calendar c)
  +    {
  +        return printDateTime(c, SchemaType.BTC_TIME);
  +    }
  +
  +    public static String printDate(Calendar c)
  +    {
  +        return printDateTime(c, SchemaType.BTC_DATE);
  +    }
  +
  +    public static String printDate(Date d)
  +    {
  +        GDateSpecification value = getGDateValue(d, SchemaType.BTC_DATE);
  +        return value.toString();
  +    }
  +
       public static String printDateTime(Calendar c, int type_code)
       {
  -        GDate value = getGDateValue(c, type_code);
  -        return value.canonicalString();
  +        GDateSpecification value = getGDateValue(c, type_code);
  +        return value.toString();
       }
   
       public static String printDateTime(Date c)
       {
  -        GDate value = getGDateValue(c, SchemaType.BTC_DATE_TIME);
  -        return value.canonicalString();
  +        GDateSpecification value = getGDateValue(c, SchemaType.BTC_DATE_TIME);
  +        return value.toString();
       }
   
   
  @@ -548,25 +565,28 @@
       }
   
   
  -
  -    // private utils
  -    private static GDate getGDateValue(Calendar c, int builtin_type_code)
  +    // date utils
  +    public static GDateSpecification getGDateValue(Date d,
  +                                                   int builtin_type_code)
       {
  -        GDateBuilder gDateBuilder = new GDateBuilder(c);
  +        GDateBuilder gDateBuilder = new GDateBuilder(d);
           gDateBuilder.setBuiltinTypeCode(builtin_type_code);
           GDate value = gDateBuilder.toGDate();
           return value;
       }
   
  -    private static GDate getGDateValue(Date d, int builtin_type_code)
  +
  +    public static GDateSpecification getGDateValue(Calendar c,
  +                                                   int builtin_type_code)
       {
  -        GDateBuilder gDateBuilder = new GDateBuilder(d);
  +        GDateBuilder gDateBuilder = new GDateBuilder(c);
           gDateBuilder.setBuiltinTypeCode(builtin_type_code);
           GDate value = gDateBuilder.toGDate();
           return value;
       }
   
  -    private static GDate getGDateValue(CharSequence v, int builtin_type_code)
  +    public static GDateSpecification getGDateValue(CharSequence v,
  +                                                   int builtin_type_code)
       {
           GDateBuilder gDateBuilder = new GDateBuilder(v);
           gDateBuilder.setBuiltinTypeCode(builtin_type_code);
  
  
  

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