xmlbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From z...@apache.org
Subject cvs commit: xml-xmlbeans/v2/test/src/drt/drtcases MarshalTests.java
Date Fri, 23 Apr 2004 18:52:31 GMT
zieg        2004/04/23 11:52:31

  Modified:    v2/src/binding/org/apache/xmlbeans/impl/binding/bts
                        DefaultBuiltinBindingLoader.java
               v2/src/marshal/org/apache/xmlbeans/impl/marshal
                        JavaCalendarTypeConverter.java
                        RuntimeBindingTypeTable.java UnmarshalResult.java
               v2/test/src/drt/drtcases MarshalTests.java
  Added:       v2/src/marshal/org/apache/xmlbeans/impl/marshal
                        DurationTypeConverter.java
  Log:
  better date/time type support
  better error handling for simple types
  
  Revision  Changes    Path
  1.3       +6 -0      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.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- DefaultBuiltinBindingLoader.java	7 Apr 2004 23:30:56 -0000	1.2
  +++ DefaultBuiltinBindingLoader.java	23 Apr 2004 18:52:31 -0000	1.3
  @@ -57,6 +57,7 @@
       addPojoXml("ENTITIES", "java.lang.String[]");
   
       addPojoTwoWay("duration", "org.apache.xmlbeans.GDuration");
  +    addPojo("duration", "java.lang.String");
   
       addPojoTwoWay("dateTime", "java.util.Calendar");
       addPojoJava("dateTime", "java.util.Date");
  @@ -64,13 +65,18 @@
       addPojoXml("date", "java.util.Calendar");
       addPojo("date", "java.util.Date");
       addPojoXml("gYearMonth", "java.util.Calendar");
  +    addPojo("gYearMonth", "java.lang.String");
       addPojoXml("gYear", "java.util.Calendar");
       addPojo("gYear", "int");
  +    addPojo("gYear", "java.lang.String");
       addPojoXml("gMonthDay", "java.util.Calendar");
  +    addPojo("gMonthDay", "java.lang.String");
       addPojoXml("gMonth", "java.util.Calendar");
       addPojo("gMonth", "int");
  +    addPojo("gMonth", "java.lang.String");
       addPojoXml("gDay", "java.util.Calendar");
       addPojo("gDay", "int");
  +    addPojo("gDay", "java.lang.String");
   
       addPojoTwoWay("boolean", "boolean");
       addPojoTwoWay("base64Binary", "byte[]");
  
  
  
  1.2       +1 -1      xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/JavaCalendarTypeConverter.java
  
  Index: JavaCalendarTypeConverter.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/JavaCalendarTypeConverter.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- JavaCalendarTypeConverter.java	7 Apr 2004 23:30:56 -0000	1.1
  +++ JavaCalendarTypeConverter.java	23 Apr 2004 18:52:31 -0000	1.2
  @@ -24,7 +24,7 @@
   
   
   /**
  - * convert between schema date/time types and java.util.Date
  + * convert between schema date/time types and java.util.Calendar
    */
   final class JavaCalendarTypeConverter
       extends BaseSimpleTypeConverter
  
  
  
  1.29      +28 -17    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.28
  retrieving revision 1.29
  diff -u -r1.28 -r1.29
  --- RuntimeBindingTypeTable.java	21 Apr 2004 05:29:40 -0000	1.28
  +++ RuntimeBindingTypeTable.java	23 Apr 2004 18:52:31 -0000	1.29
  @@ -17,6 +17,7 @@
   
   import org.apache.xmlbeans.SchemaType;
   import org.apache.xmlbeans.XmlException;
  +import org.apache.xmlbeans.GDuration;
   import org.apache.xmlbeans.impl.binding.bts.BindingLoader;
   import org.apache.xmlbeans.impl.binding.bts.BindingType;
   import org.apache.xmlbeans.impl.binding.bts.BindingTypeName;
  @@ -245,9 +246,9 @@
                         java.net.URI.class,
                         new AnyUriToUriTypeConverter());
   
  -        final StringTypeConverter string_conv = new StringTypeConverter();
           final Class str = String.class;
           addXsdBuiltin("anySimpleType", str, new AnySimpleTypeConverter());
  +        final StringTypeConverter string_conv = new StringTypeConverter();
           addXsdBuiltin("string", str, string_conv);
           addXsdBuiltin("normalizedString", str, string_conv);
           addXsdBuiltin("token", str, string_conv);
  @@ -259,27 +260,37 @@
           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);
  +        final TypeConverter collapsing_string_conv =
  +            CollapseStringTypeConverter.getInstance();
  +
  +        addXsdBuiltin("duration", str, collapsing_string_conv);
  +        addXsdBuiltin("gDay", str, collapsing_string_conv);
  +        addXsdBuiltin("gMonth", str, collapsing_string_conv);
  +        addXsdBuiltin("gMonthDay", str, collapsing_string_conv);
  +        addXsdBuiltin("gYear", str, collapsing_string_conv);
  +        addXsdBuiltin("gYearMonth", str, collapsing_string_conv);
   
           addXsdBuiltin("anyURI",
                         str,
                         new AnyUriToStringTypeConverter());
   
           final Class str_array = (new String[0]).getClass();
  +        final StringListArrayConverter string_list_array_conv =
  +            new StringListArrayConverter();
           addXsdBuiltin("ENTITIES", str_array,
  -                      new StringListArrayConverter());
  +                      string_list_array_conv);
           addXsdBuiltin("IDREFS", str_array,
  -                      new StringListArrayConverter());
  +                      string_list_array_conv);
           addXsdBuiltin("NMTOKENS", str_array,
  -                      new StringListArrayConverter());
  +                      string_list_array_conv);
   
  +        addXsdBuiltin("duration",
  +                      GDuration.class,
  +                      new DurationTypeConverter());
   
  +        final Class calendar_class = java.util.Calendar.class;
           addXsdBuiltin("dateTime",
  -                      java.util.Calendar.class,
  +                      calendar_class,
                         new JavaCalendarTypeConverter(SchemaType.BTC_DATE_TIME));
   
           addXsdBuiltin("dateTime",
  @@ -287,11 +298,11 @@
                         new JavaDateTypeConverter(SchemaType.BTC_DATE_TIME));
   
           addXsdBuiltin("time",
  -                      java.util.Calendar.class,
  +                      calendar_class,
                         new JavaCalendarTypeConverter(SchemaType.BTC_TIME));
   
           addXsdBuiltin("date",
  -                      java.util.Calendar.class,
  +                      calendar_class,
                         new JavaCalendarTypeConverter(SchemaType.BTC_DATE));
   
           addXsdBuiltin("date",
  @@ -299,23 +310,23 @@
                         new JavaDateTypeConverter(SchemaType.BTC_DATE));
   
           addXsdBuiltin("gDay",
  -                      java.util.Calendar.class,
  +                      calendar_class,
                         new JavaCalendarTypeConverter(SchemaType.BTC_G_DAY));
   
           addXsdBuiltin("gMonth",
  -                      java.util.Calendar.class,
  +                      calendar_class,
                         new JavaCalendarTypeConverter(SchemaType.BTC_G_MONTH));
   
           addXsdBuiltin("gMonthDay",
  -                      java.util.Calendar.class,
  +                      calendar_class,
                         new JavaCalendarTypeConverter(SchemaType.BTC_G_MONTH_DAY));
   
           addXsdBuiltin("gYear",
  -                      java.util.Calendar.class,
  +                      calendar_class,
                         new JavaCalendarTypeConverter(SchemaType.BTC_G_YEAR));
   
           addXsdBuiltin("gYearMonth",
  -                      java.util.Calendar.class,
  +                      calendar_class,
                         new JavaCalendarTypeConverter(SchemaType.BTC_G_YEAR_MONTH));
   
   
  
  
  
  1.19      +21 -12    xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/UnmarshalResult.java
  
  Index: UnmarshalResult.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/UnmarshalResult.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- UnmarshalResult.java	17 Apr 2004 00:06:22 -0000	1.18
  +++ UnmarshalResult.java	23 Apr 2004 18:52:31 -0000	1.19
  @@ -32,6 +32,7 @@
   import org.apache.xmlbeans.impl.richParser.XMLStreamReaderExt;
   import org.apache.xmlbeans.impl.richParser.XMLStreamReaderExtImpl;
   import org.apache.xmlbeans.impl.validator.ValidatingXMLStreamReader;
  +import org.apache.xmlbeans.impl.common.InvalidLexicalValueException;
   
   import javax.xml.namespace.QName;
   import javax.xml.namespace.NamespaceContext;
  @@ -182,20 +183,28 @@
   
           final TypeUnmarshaller um;
           final ObjectFactory of = extractObjectFactory();
  -        if (of == null) {
  -            if (hasXsiNil())
  -                um = NullUnmarshaller.getInstance();
  -            else
  +
  +        try {
  +            if (of == null) {
  +                if (hasXsiNil())
  +                    um = NullUnmarshaller.getInstance();
  +                else
  +                    um = typeTable.getOrCreateTypeUnmarshaller(bindingType,
  +                                                               bindingLoader);
  +                return um.unmarshal(this);
  +            } else {
  +                final RuntimeBindingType rtt = getRuntimeType(bindingType);
  +                final Object initial_obj = of.createObject(rtt.getJavaType());
                   um = typeTable.getOrCreateTypeUnmarshaller(bindingType,
                                                              bindingLoader);
  -            return um.unmarshal(this);
  -        } else {
  -            final RuntimeBindingType rtt = getRuntimeType(bindingType);
  -            final Object initial_obj = of.createObject(rtt.getJavaType());
  -            um = typeTable.getOrCreateTypeUnmarshaller(bindingType,
  -                                                       bindingLoader);
  -            um.unmarshal(initial_obj, this);
  -            return initial_obj;
  +                um.unmarshal(initial_obj, this);
  +                return initial_obj;
  +            }
  +        }
  +        catch (InvalidLexicalValueException ilve) {
  +            //top level simple types can end up here for invalid lexical values
  +            assert !errors.isEmpty();
  +            return null;
           }
       }
   
  
  
  
  1.1                  xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/DurationTypeConverter.java
  
  Index: DurationTypeConverter.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.GDuration;
  import org.apache.xmlbeans.impl.common.InvalidLexicalValueException;
  import org.apache.xmlbeans.impl.util.XsTypeConverter;
  
  import java.util.Calendar;
  
  
  /**
   * convert between schema Duration and GDuration
   */
  final class DurationTypeConverter
      extends BaseSimpleTypeConverter
  {
  
      protected Object getObject(UnmarshalResult context) throws XmlException
      {
          return context.getGDurationValue();
      }
  
      public Object unmarshalAttribute(UnmarshalResult context) throws XmlException
      {
          return context.getAttributeGDurationValue();
      }
  
      public Object unmarshalAttribute(CharSequence lexical_value,
                                       UnmarshalResult result)
          throws XmlException
      {
          try {
              return new GDuration(lexical_value);
          }
          catch (IllegalArgumentException e) {
              throw new InvalidLexicalValueException(e, result.getLocation());
          }
  
      }
  
      public CharSequence print(Object value, MarshalResult result)
      {
          GDuration cal = (GDuration)value;
          return cal.toString();
      }
  }
  
  
  
  1.56      +43 -40    xml-xmlbeans/v2/test/src/drt/drtcases/MarshalTests.java
  
  Index: MarshalTests.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/test/src/drt/drtcases/MarshalTests.java,v
  retrieving revision 1.55
  retrieving revision 1.56
  diff -u -r1.55 -r1.56
  --- MarshalTests.java	17 Apr 2004 00:06:23 -0000	1.55
  +++ MarshalTests.java	23 Apr 2004 18:52:31 -0000	1.56
  @@ -39,6 +39,7 @@
   import org.apache.xmlbeans.XmlException;
   import org.apache.xmlbeans.XmlObject;
   import org.apache.xmlbeans.XmlOptions;
  +import org.apache.xmlbeans.GDuration;
   import org.apache.xmlbeans.impl.binding.compile.Schema2Java;
   import org.apache.xmlbeans.impl.common.XmlReaderToWriter;
   import org.apache.xmlbeans.impl.common.XmlStreamUtils;
  @@ -131,7 +132,8 @@
           testSimpleTypeUnmarshal(new BigDecimal("43434343342.233434342"), "decimal");
           testSimpleTypeUnmarshal(new Float(54.5423f), "float");
           testSimpleTypeUnmarshal(new Double(23432.43234), "double");
  -        testSimpleTypeUnmarshal(new Double(23432.43234), "double");
  +
  +        testSimpleTypeUnmarshal(new GDuration("P1Y2M3DT10H30M"), "duration");
   
           testStringTypeUnmarshal("anySimpleType");
           testStringTypeUnmarshal("string");
  @@ -184,6 +186,8 @@
           testSimpleTypeMarshal(new Double(1231.444), "double");
           testSimpleTypeMarshal(new URI("http://www.apache.org/"), "anyURI");
   
  +        testSimpleTypeMarshal(new GDuration("P1Y2M3DT10H30M"), "duration");
  +
           testSimpleTypeMarshal("some text here", "string");
           testSimpleTypeMarshal("aToken", "token");
           testSimpleTypeMarshal("       ", "string");
  @@ -230,6 +234,8 @@
               bindingContext.createUnmarshaller();
   
           Object obj = umctx.unmarshal(xrdr, options);
  +        reportErrors(errors, "SimpleType error. lexical="+lexval);
  +        Assert.assertTrue(errors.isEmpty());
   
   
           //special case date/time tests.
  @@ -248,12 +254,17 @@
               Assert.assertEquals(expected, obj);
           }
   
  -        Assert.assertTrue(errors.isEmpty());
  -
  -
   //        inform("OK for " + expected);
       }
   
  +    private void reportErrors(Collection errors, final String prefix)
  +    {
  +        for (Iterator itr = errors.iterator(); itr.hasNext();) {
  +            XmlError xmlError = (XmlError)itr.next();
  +            error(prefix + " " + xmlError);
  +        }
  +    }
  +
   
       public void testSimpleTypeMarshal(Object orig, String xsd_type)
           throws Exception
  @@ -289,12 +300,7 @@
           inform("==================OBJ: " + orig);
           dumpReader(reader);
   
  -        if (!errors.isEmpty()) {
  -            for (Iterator itr = errors.iterator(); itr.hasNext();) {
  -                Object err = itr.next();
  -                inform("Error: " + err);
  -            }
  -        }
  +        reportErrors(errors, "simpleTypeMarshal");
   
           Assert.assertTrue(errors.isEmpty());
       }
  @@ -337,13 +343,7 @@
               Object out = um.unmarshalType(reader, schemaType, javaType, options);
               Assert.assertEquals(our_obj, out);
   
  -            if (!errors.isEmpty()) {
  -                for (Iterator itr = errors.iterator(); itr.hasNext();) {
  -                    Object err = itr.next();
  -                    inform("Error: " + err);
  -                }
  -            }
  -
  +            reportErrors(errors, "poly-marshal");
               Assert.assertTrue(errors.isEmpty());
           }
       }
  @@ -415,6 +415,7 @@
           inform("=======IN-OBJA: " + mc);
   
           dumpReader(reader);
  +        reportErrors(errors, "byname-marshal");
           Assert.assertTrue(errors.isEmpty());
       }
   
  @@ -445,10 +446,12 @@
           inform("=======WRAPPED-ARRAY-OBJ: " + strs);
   
   //        dumpReader(reader);
  +        reportErrors(errors, "wrapped-array");
           Assert.assertTrue(errors.isEmpty());
   
           final Unmarshaller um = bindingContext.createUnmarshaller();
           Object retval = um.unmarshalType(reader, schema_type, java_type, options);
  +        reportErrors(errors, "wrapped-array2");
           Assert.assertTrue(errors.isEmpty());
   
           Assert.assertTrue("expected " + ArrayUtils.arrayToString(strs) +
  @@ -494,6 +497,7 @@
   
           inform("=======IN-OBJ: " + mc);
           inform("=======OUT-XML: " + PrettyPrinter.indent(sw.getBuffer().toString()));
  +        reportErrors(errors, "byname-writer");
           Assert.assertTrue(errors.isEmpty());
       }
   
  @@ -531,8 +535,10 @@
               XMLInputFactory.newInstance().createXMLStreamReader(sr);
           Unmarshaller umctx = bindingContext.createUnmarshaller();
           Object out_obj = umctx.unmarshal(rdr, options);
  -        Assert.assertEquals(mc, out_obj);
  +        reportErrors(errors, "byname-doc-writer");
           Assert.assertTrue(errors.isEmpty());
  +        Assert.assertEquals(mc, out_obj);
  +
       }
   
       public void testByNameMarshalElementViaWriter()
  @@ -574,10 +580,13 @@
           XMLStreamReader rdr =
               XMLInputFactory.newInstance().createXMLStreamReader(sr);
           Unmarshaller umctx = bindingContext.createUnmarshaller();
  -        while(!rdr.isStartElement()) {rdr.next();}
  +        while (!rdr.isStartElement()) {
  +            rdr.next();
  +        }
           Object out_obj = umctx.unmarshalElement(rdr, elem_name,
                                                   mc.getClass().getName(),
                                                   options);
  +        reportErrors(errors, "marsh-elem");
           Assert.assertEquals(mc, out_obj);
           Assert.assertTrue(errors.isEmpty());
       }
  @@ -620,6 +629,7 @@
           Unmarshaller umctx = bindingContext.createUnmarshaller();
           final ByteArrayInputStream bais = new ByteArrayInputStream(buf);
           Object out_obj = umctx.unmarshal(bais, options);
  +        reportErrors(errors, "marsh-outstream");
           Assert.assertEquals(mc, out_obj);
           Assert.assertTrue(errors.isEmpty());
       }
  @@ -708,6 +718,7 @@
           final long after_millis = System.currentTimeMillis();
           final long diff = (after_millis - before_millis);
           inform(" perf_out_obj = " + top_obj);
  +        reportErrors(errors, "perf");
           Assert.assertTrue(errors.isEmpty());
           Assert.assertEquals(top_obj, out_obj);
           inform("milliseconds: " + diff + " trials: " + trials);
  @@ -791,6 +802,9 @@
           final long after_millis = System.currentTimeMillis();
           final long diff = (after_millis - before_millis);
   //        inform(" perf_out_obj = " + top_obj);
  +
  +
  +        reportErrors(errors, "thread-perf");
           Assert.assertTrue(errors.isEmpty());
           //Assert.assertEquals(top_obj, out_obj);
           inform("milliseconds: " + diff + " trials: " + trials +
  @@ -941,7 +955,9 @@
           Unmarshaller umctx =
               bindingContext.createUnmarshaller();
           out_obj = umctx.unmarshalType(reader, schemaType, javaType, options);
  +
           inform(" out_obj = " + top_obj);
  +        reportErrors(errors, "j2s2j");
           Assert.assertEquals(top_obj, out_obj);
           Assert.assertTrue(errors.isEmpty());
       }
  @@ -1005,14 +1021,7 @@
           Object obj = um_ctx.unmarshal(xrdr, options);
   
           inform("doc2-obj = " + obj);
  -
  -        for (Iterator itr = errors.iterator(); itr.hasNext();) {
  -            XmlError xmlError = (XmlError)itr.next();
  -
  -            inform("doc2-ERROR: source=" + xmlError.getSourceName() +
  -                   " " + xmlError);
  -        }
  -
  +        reportErrors(errors, "byname-um");
           Assert.assertTrue(errors.isEmpty());
   
       }
  @@ -1066,12 +1075,7 @@
           Object obj = um_ctx.unmarshal(new FileInputStream(doc), options);
   
           inform("doc2-obj = " + obj);
  -
  -        for (Iterator itr = errors.iterator(); itr.hasNext();) {
  -            XmlError xmlError = (XmlError)itr.next();
  -            inform("doc2-ERROR: " + xmlError);
  -        }
  -
  +        reportErrors(errors, "doc2-err");
           Assert.assertTrue(errors.isEmpty());
   
       }
  @@ -1110,13 +1114,7 @@
           MyClass mc = (MyClass)obj;
           MySubClass first = (MySubClass)mc.getMyelt().getMyClassArray()[0];
           Assert.assertEquals(DEFAULT_BIG_INT, first.getBigInt());
  -
  -
  -        for (Iterator itr = errors.iterator(); itr.hasNext();) {
  -            XmlError xmlError = (XmlError)itr.next();
  -            inform("doc-ERROR: " + xmlError);
  -        }
  -
  +        reportErrors(errors, "dco-err");
           Assert.assertTrue(errors.isEmpty());
       }
   
  @@ -1307,5 +1305,10 @@
       private static void inform(String msg)
       {
           if (VERBOSE) System.out.println(msg);
  +    }
  +
  +    private static void error(String msg)
  +    {
  +        System.out.println(msg);
       }
   }
  
  
  

---------------------------------------------------------------------
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