xml-xmlbeans-cvs 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 Thu, 18 Mar 2004 23:25:36 GMT
zieg        2004/03/18 15:25:35

  Modified:    v2/src/binding/org/apache/xmlbeans/impl/binding/tylar
                        ExplodedTylarImpl.java
               v2/src/marshal/org/apache/xmlbeans/impl/marshal
                        BindingContextImpl.java MarshallerImpl.java
                        RuntimeBindingType.java UnmarshalResult.java
               v2/src/repackage/repackage Repackage.java
               v2/test/src/drt/drtcases MarshalTests.java
  Added:       v2/test/cases/marshal example.xsd example_instance.xml
  Log:
  Better support for validation in unmarshalling (added test to MarshalTests)
  
  Revision  Changes    Path
  1.10      +1 -1      xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/tylar/ExplodedTylarImpl.java
  
  Index: ExplodedTylarImpl.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/tylar/ExplodedTylarImpl.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- ExplodedTylarImpl.java	25 Feb 2004 18:56:48 -0000	1.9
  +++ ExplodedTylarImpl.java	18 Mar 2004 23:25:35 -0000	1.10
  @@ -219,7 +219,7 @@
   
     public ClassLoader createClassLoader(ClassLoader parent) {
       try {
  -      return new URLClassLoader(new URL[] {mSourceRoot.toURL()},parent);
  +      return new URLClassLoader(new URL[] {mRootDir.toURL()},parent);
       } catch(MalformedURLException mue){
         throw new RuntimeException(mue); //FIXME this is bad
       }
  
  
  
  1.12      +6 -4      xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/BindingContextImpl.java
  
  Index: BindingContextImpl.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/BindingContextImpl.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- BindingContextImpl.java	16 Mar 2004 00:55:01 -0000	1.11
  +++ BindingContextImpl.java	18 Mar 2004 23:25:35 -0000	1.12
  @@ -44,14 +44,15 @@
           runtimeTypeFactory = new RuntimeTypeFactory();
           this.typeTable =
               RuntimeBindingTypeTable.createTable(runtimeTypeFactory);
  -        this.schemaTypeLoaderProvider  = provider;
  +        this.schemaTypeLoaderProvider = provider;
       }
   
   
       public Unmarshaller createUnmarshaller()
           throws XmlException
       {
  -        return new UnmarshallerImpl(bindingLoader, typeTable,
  +        return new UnmarshallerImpl(bindingLoader,
  +                                    typeTable,
                                       schemaTypeLoaderProvider);
       }
   
  @@ -68,7 +69,9 @@
       public Marshaller createMarshaller()
           throws XmlException
       {
  -        return new MarshallerImpl(bindingLoader, typeTable, runtimeTypeFactory);
  +        return new MarshallerImpl(bindingLoader,
  +                                  typeTable,
  +                                  runtimeTypeFactory);
       }
   
   
  @@ -92,6 +95,5 @@
   
           return FailFastErrorHandler.getInstance();
       }
  -
   
   }
  
  
  
  1.22      +12 -9     xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/MarshallerImpl.java
  
  Index: MarshallerImpl.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/MarshallerImpl.java,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- MarshallerImpl.java	3 Mar 2004 18:53:28 -0000	1.21
  +++ MarshallerImpl.java	18 Mar 2004 23:25:35 -0000	1.22
  @@ -85,17 +85,27 @@
               throw new XmlException(msg);
           }
   
  +        return createMarshalResult(btype, elem_qn, nscontext, obj);
  +    }
  +
  +
  +    private MarshalResult createMarshalResult(final BindingType btype,
  +                                              QName elem_qn,
  +                                              NamespaceContext nscontext,
  +                                              Object obj)
  +        throws XmlException
  +    {
           final RuntimeBindingType runtime_type =
               runtimeTypeFactory.createRuntimeType(btype, typeTable, loader);
   
           RuntimeGlobalProperty prop =
               new RuntimeGlobalProperty(elem_qn, runtime_type);
   
  -        //TODO: review null options param
           return new MarshalResult(runtimeTypeFactory, loader, typeTable,
                                    nscontext, prop, obj, null);
       }
   
  +
       public XMLStreamReader marshal(Object obj,
                                      XmlOptions options)
           throws XmlException
  @@ -253,14 +263,7 @@
               throw new XmlException(msg);
           }
   
  -        final RuntimeBindingType runtime_type =
  -            runtimeTypeFactory.createRuntimeType(type, typeTable, loader);
  -
  -        RuntimeGlobalProperty prop =
  -            new RuntimeGlobalProperty(elementName, runtime_type);
  -
  -        return new MarshalResult(runtimeTypeFactory, loader, typeTable,
  -                                 namespaceContext, prop, obj, null);
  +        return createMarshalResult(type, elementName, namespaceContext, obj);
       }
   
       public void marshalType(XMLStreamWriter writer,
  
  
  
  1.14      +3 -1      xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/RuntimeBindingType.java
  
  Index: RuntimeBindingType.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/RuntimeBindingType.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- RuntimeBindingType.java	16 Mar 2004 00:55:01 -0000	1.13
  +++ RuntimeBindingType.java	18 Mar 2004 23:25:35 -0000	1.14
  @@ -52,8 +52,10 @@
   
           }
           catch (ClassNotFoundException e) {
  +            ClassLoader context_cl =
  +                Thread.currentThread().getContextClassLoader();
               final String msg = "failed to load " +
  -                binding_type.getName().getJavaName();
  +                binding_type.getName().getJavaName() + " from " + context_cl;
               throw new XmlException(msg, e);
           }
   
  
  
  
  1.12      +8 -20     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.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- UnmarshalResult.java	16 Mar 2004 00:55:01 -0000	1.11
  +++ UnmarshalResult.java	18 Mar 2004 23:25:35 -0000	1.12
  @@ -17,11 +17,11 @@
   
   import org.apache.xmlbeans.GDate;
   import org.apache.xmlbeans.GDuration;
  +import org.apache.xmlbeans.SchemaType;
  +import org.apache.xmlbeans.SchemaTypeLoader;
   import org.apache.xmlbeans.XmlCalendar;
   import org.apache.xmlbeans.XmlException;
   import org.apache.xmlbeans.XmlOptions;
  -import org.apache.xmlbeans.SchemaTypeLoader;
  -import org.apache.xmlbeans.SchemaType;
   import org.apache.xmlbeans.impl.binding.bts.BindingLoader;
   import org.apache.xmlbeans.impl.binding.bts.BindingType;
   import org.apache.xmlbeans.impl.binding.bts.BindingTypeName;
  @@ -143,29 +143,17 @@
       Object unmarshalDocument(XMLStreamReader reader)
           throws XmlException
       {
  -        //TODO: turn on validation before looking at root type
  -        MarshalStreamUtils.advanceToFirstItemOfInterest(reader);
  -        enrichXmlStream(reader);
  -        BindingType bindingType = determineRootType();
  -
           if (isValidating()) {
               ValidatingXMLStreamReader vr = new ValidatingXMLStreamReader();
               final SchemaTypeLoader schemaTypeLoader =
                   schemaTypeLoaderProvider.getSchemaTypeLoader();
  -            final XmlTypeName xtype = bindingType.getName().getXmlName();
  -            assert xtype.isSchemaType();
  -            assert xtype.isGlobal();
  -            SchemaType schema_type = schemaTypeLoader.findType(xtype.getQName());
  -            if (schema_type == null) {
  -                throw new XmlException("unable to locate type " + schema_type +
  -                                       " in provided schema type system");
  -            }
  -            //TODO: pass in null instead of schema_type (currently not working)
  -            vr.init(reader, schema_type, schemaTypeLoader, options, errors);
  -            reader = vr; //note changing param
  +            vr.init(reader, null, schemaTypeLoader, options, errors);
  +            enrichXmlStream(vr);
  +        } else {
  +            enrichXmlStream(reader);
           }
  -
  -
  +        advanceToFirstItemOfInterest();
  +        BindingType bindingType = determineRootType();
           return unmarshalBindingType(bindingType);
       }
   
  
  
  
  1.6       +3 -3      xml-xmlbeans/v2/src/repackage/repackage/Repackage.java
  
  Index: Repackage.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/repackage/repackage/Repackage.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Repackage.java	3 Mar 2004 22:56:39 -0000	1.5
  +++ Repackage.java	18 Mar 2004 23:25:35 -0000	1.6
  @@ -342,7 +342,7 @@
           return w.getBuffer();
       }
       
  -    public void copyFile ( File from, File to ) throws IOException
  +    public static void copyFile ( File from, File to ) throws IOException
       {
           to.getParentFile().mkdirs();
           
  @@ -355,7 +355,7 @@
           in.close();
       }
       
  -    public void copy ( InputStream in, OutputStream out ) throws IOException
  +    public static void copy ( InputStream in, OutputStream out ) throws IOException
       {
           byte[] buffer = new byte [ 1024 * 16 ];
   
  @@ -370,7 +370,7 @@
           }
       }
       
  -    public void copy ( Reader r, Writer w ) throws IOException
  +    public static void copy ( Reader r, Writer w ) throws IOException
       {
           char[] buffer = new char [ 1024 * 16 ];
   
  
  
  
  1.1                  xml-xmlbeans/v2/test/cases/marshal/example.xsd
  
  Index: example.xsd
  ===================================================================
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
      targetNamespace="http://nosuch.domain.name"
      xmlns:tns="http://nosuch.domain.name"
      elementFormDefault="qualified">
  
      <xs:element name="address" type="tns:USAddress"/>
  
      <xs:complexType name="USAddress">
          <xs:sequence>
              <xs:element name="name" type="xs:string"/>
              <xs:element name="zip" type="tns:ranged-int"/>
          </xs:sequence>
          <xs:attribute name="country" type="xs:NMTOKEN"/>
      </xs:complexType>
  
  
      <xs:simpleType name="ranged-int">
          <xs:restriction base="xs:int">
              <xs:maxInclusive value="99999"/>
          </xs:restriction>
      </xs:simpleType>
  
  </xs:schema>
  
  
  
  1.1                  xml-xmlbeans/v2/test/cases/marshal/example_instance.xml
  
  Index: example_instance.xml
  ===================================================================
  <tns:address xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:tns="http://nosuch.domain.name"
      xmlns:oth="http://othernosuch.domain.name"
      country="USA">
  
  
      <tns:name>Somewhere</tns:name>
      <tns:zip>54321</tns:zip>
  <!--    <tns:zip>4454321</tns:zip>-->
  
  <!--    <tns:zzzip>54321</tns:zzzip>-->
  
  
  </tns:address>
  
  
  
  
  1.45      +121 -5    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.44
  retrieving revision 1.45
  diff -u -r1.44 -r1.45
  --- MarshalTests.java	16 Mar 2004 00:55:01 -0000	1.44
  +++ MarshalTests.java	18 Mar 2004 23:25:35 -0000	1.45
  @@ -15,13 +15,13 @@
   
   package drtcases;
   
  +import com.mytest.IntEnum;
  +import com.mytest.IntegerEnum;
  +import com.mytest.ModeEnum;
   import com.mytest.MyClass;
   import com.mytest.MySubClass;
   import com.mytest.MySubSubClass;
   import com.mytest.SimpleContentExample;
  -import com.mytest.ModeEnum;
  -import com.mytest.IntegerEnum;
  -import com.mytest.IntEnum;
   import junit.framework.Assert;
   import junit.framework.Test;
   import junit.framework.TestCase;
  @@ -29,11 +29,16 @@
   import org.apache.xmlbeans.BindingContext;
   import org.apache.xmlbeans.BindingContextFactory;
   import org.apache.xmlbeans.Marshaller;
  +import org.apache.xmlbeans.SchemaTypeSystem;
   import org.apache.xmlbeans.Unmarshaller;
  +import org.apache.xmlbeans.XmlBeans;
   import org.apache.xmlbeans.XmlCalendar;
   import org.apache.xmlbeans.XmlError;
   import org.apache.xmlbeans.XmlException;
  +import org.apache.xmlbeans.XmlObject;
   import org.apache.xmlbeans.XmlOptions;
  +import org.apache.xmlbeans.impl.binding.compile.Schema2Java;
  +import org.apache.xmlbeans.impl.binding.tylar.TylarConstants;
   import org.apache.xmlbeans.impl.common.XmlReaderToWriter;
   import org.apache.xmlbeans.impl.common.XmlStreamUtils;
   import org.apache.xmlbeans.impl.marshal.BindingContextFactoryImpl;
  @@ -54,6 +59,7 @@
   import java.io.FileInputStream;
   import java.io.FileReader;
   import java.io.IOException;
  +import java.io.InputStream;
   import java.io.Reader;
   import java.io.StringReader;
   import java.io.StringWriter;
  @@ -65,7 +71,13 @@
   import java.util.Collection;
   import java.util.Iterator;
   import java.util.LinkedList;
  +import java.util.List;
   import java.util.Random;
  +import java.net.URI;
  +import java.net.URLClassLoader;
  +import java.net.URL;
  +
  +import repackage.Repackage;
   
   
   public class MarshalTests extends TestCase
  @@ -190,7 +202,7 @@
       {
           BindingContext bindingContext = getBuiltinBindingContext();
   
  -         String xmldoc = "<a" +
  +        String xmldoc = "<a" +
               " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" +
               " xmlns:xs='http://www.w3.org/2001/XMLSchema' xsi:type='xs:" +
               xsd_type + "' >" + lexval + "</a>";
  @@ -202,7 +214,6 @@
           final XmlOptions options = new XmlOptions();
           Collection errors = new LinkedList();
           options.setErrorListener(errors);
  -        options.setUnmarshalValidate();
   
           Unmarshaller umctx =
               bindingContext.createUnmarshaller();
  @@ -1035,6 +1046,111 @@
           final long diff = (after_millis - before_millis);
           inform("milliseconds: " + diff + " trials: " + trials);
           inform("milliseconds PER trial: " + (diff / (double)trials));
  +    }
  +
  +    public void testUnmarshalValidation()
  +        throws Exception
  +    {
  +        final File schema = TestEnv.xbeanCase("marshal/example.xsd");
  +        final File instance = TestEnv.xbeanCase("marshal/example_instance.xml");
  +
  +        Assert.assertTrue(schema.exists());
  +        Assert.assertTrue(instance.exists());
  +
  +        final XmlObject[] schemas = new XmlObject[]{XmlObject.Factory.parse(schema)};
  +        SchemaTypeSystem sts = XmlBeans.compileXsd(schemas, XmlBeans.getBuiltinTypeSystem(),
new XmlOptions());
  +        Schema2Java s2j = new Schema2Java(sts);
  +        File dest = new File("/tmp/unmarshal-tests-" + System.currentTimeMillis() + "-tylar");
  +        final boolean ok = dest.mkdirs();
  +        Assert.assertTrue("mkdir" + dest + " failed", ok);
  +
  +        s2j.bindAsExplodedTylar(dest);
  +
  +        //workaround bug in schema2java, we need to copy the xsd files by hand
  +        File sdir = new File(dest, TylarConstants.SCHEMA_DIR);
  +        final boolean k = sdir.mkdirs();
  +        Assert.assertTrue("failed to mkdirs: " + sdir, k);
  +        Assert.assertTrue("no such directory: " + sdir, sdir.exists());
  +        File destfile = new File(sdir, schema.getName());
  +        Repackage.copyFile(schema, destfile);
  +        Assert.assertTrue("file copy failed to " + destfile, destfile.exists());
  +
  +        final URI tylar_uri = dest.toURI();
  +        final Thread thread = Thread.currentThread();
  +        final ClassLoader curr_cl = thread.getContextClassLoader();
  +        final URLClassLoader cl =
  +            new URLClassLoader(new URL[]{tylar_uri.toURL()}, curr_cl);
  +        thread.setContextClassLoader(cl);
  +        try {
  +            final BindingContextFactory bcf = BindingContextFactory.newInstance();
  +            final BindingContext binding_context =
  +                bcf.createBindingContext(tylar_uri);
  +            final Unmarshaller um = binding_context.createUnmarshaller();
  +            InputStream is = new FileInputStream(instance);
  +            XmlOptions opts_validation_on = new XmlOptions();
  +            opts_validation_on.setUnmarshalValidate();
  +            final List errors = new ArrayList();
  +            opts_validation_on.setErrorListener(errors);
  +            final Object obj = um.unmarshal(is, opts_validation_on);
  +            Assert.assertNotNull(obj);
  +            inform("address=" + obj);
  +            is.close();
  +
  +            reportErrors(errors);
  +            Assert.assertTrue(errors.isEmpty());
  +
  +            // -- this is currently broken --
  +            //now try unmarshalType...
  +//        final FileInputStream fis = new FileInputStream(instance);
  +//        final XMLStreamReader rdr =
  +//            XMLInputFactory.newInstance().createXMLStreamReader(fis);
  +//        QName schema_type = new QName("http://nosuch.domain.name", "USAddress");
  +//        String java_type = obj.getClass().getName();
  +//
  +//        //not super robust but this should work for valid xml
  +//        while(!rdr.isStartElement()) {
  +//            rdr.next();
  +//        }
  +//
  +//        um.unmarshalType(rdr, schema_type, java_type, opts);
  +//        rdr.close();
  +//        fis.close();
  +//
  +//        reportErrors(errors);
  +//        Assert.assertTrue(errors.isEmpty());
  +
  +
  +            // -- this is currently broken --
  +            //now lets try validating our stream over objects
  +//        final Marshaller marshaller = binding_context.createMarshaller();
  +//        final XmlOptions empty_opts = new XmlOptions();
  +//        final XMLStreamReader obj_rdr =
  +//            marshaller.marshal(obj, empty_opts);
  +//        inform("VALIDATION-OBJ: " + obj);
  +//
  +//        final Object obj2 = um.unmarshal(obj_rdr, opts_validation_on);
  +//        inform("obj2="+obj2);
  +//        obj_rdr.close();
  +//        reportErrors(errors);
  +//        Assert.assertTrue(errors.isEmpty());
  +
  +            // depends on reasonable equals methods which we do not have yet
  +            //Assert.assertEquals(obj, obj2);
  +
  +        }
  +        finally {
  +            thread.setContextClassLoader(curr_cl);
  +        }
  +    }
  +
  +    private static void reportErrors(List errors)
  +    {
  +        if (!errors.isEmpty()) {
  +            for (Iterator itr = errors.iterator(); itr.hasNext();) {
  +                Object err = itr.next();
  +                inform("validation-error: " + err);
  +            }
  +        }
       }
   
       protected static void bufferedStreamCopy(Reader in, Writer out)
  
  
  

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