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 Tue, 16 Dec 2003 05:59:47 GMT
zieg        2003/12/15 21:59:47

  Modified:    v2       build.xml
               v2/src/marshal/org/apache/xmlbeans/impl/marshal
                        BindingContextImpl.java MarshallerImpl.java
                        UnmarshallerImpl.java
               v2/src/xmlpublic/org/apache/xmlbeans Marshaller.java
                        Unmarshaller.java
               v2/test/src/drt/drtcases MarshalTests.java
  Log:
  update marshal apis to take input/output streams and encodings.
  
  Revision  Changes    Path
  1.25      +2 -2      xml-xmlbeans/v2/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/build.xml,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- build.xml	15 Dec 2003 05:03:30 -0000	1.24
  +++ build.xml	16 Dec 2003 05:59:47 -0000	1.25
  @@ -1106,12 +1106,11 @@
       </target>
   
       <!-- temporary build utils ... zieg =============
  -
       <target name='binding-test' depends='declare-tasks, jam.classes'>
           <mkdir dir='build/private/classes/marshal'/>
           <java2schema
               srcdir='test/cases/marshal'
  -            includes='com/mytest/*.java'
  +            includes='com/mytest/**/*.java'
               destdir='build/private/classes/marshal'>
           </java2schema>
       </target>
  @@ -1133,6 +1132,7 @@
           </taskdef>
       </target>
       -->
  +
   
   
   </project>
  
  
  
  1.5       +13 -25    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.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- BindingContextImpl.java	15 Dec 2003 05:03:30 -0000	1.4
  +++ BindingContextImpl.java	16 Dec 2003 05:59:47 -0000	1.5
  @@ -87,41 +87,29 @@
   
       public Unmarshaller createUnmarshaller(XmlOptions options)
           throws XmlException
  -
       {
  -        Collection errors = extractErrorHandler(options);
  +        if (options == null) {
  +            throw new IllegalArgumentException("options must not be null");
  +        }
   
  -        final int prev = errors.size();
  -        final UnmarshallerImpl unmarshalContext =
  -            new UnmarshallerImpl(bindingLoader, typeTable, errors);
  -        checkErrors(prev, errors, "error creating UnmarshalContext");
  -        return unmarshalContext;
  +        return new UnmarshallerImpl(bindingLoader, typeTable, options);
       }
   
   
       public Marshaller createMarshaller(XmlOptions options)
           throws XmlException
       {
  -        Collection errors = extractErrorHandler(options);
  -
  -        final int prev = errors.size();
  -        final MarshallerImpl mc = new MarshallerImpl(EmptyNamespaceContext.getInstance(),
  -                                                     bindingLoader,
  -                                                     typeTable, errors);
  -        checkErrors(prev, errors, "error creating Marshaller");
  -        return mc;
  -    }
  -
  -
  -    static void checkErrors(int prev_size, Collection errors, String err_msg)
  -        throws XmlException
  -    {
  -        if (errors.size() <= prev_size) return;
  -        throw new XmlException(err_msg, null, errors);
  +        if (options == null) {
  +            throw new IllegalArgumentException("options must not be null");
  +        }
  +
  +        return new MarshallerImpl(EmptyNamespaceContext.getInstance(),
  +                                  bindingLoader,
  +                                  typeTable,
  +                                  options);
       }
   
  -
  -    private static Collection extractErrorHandler(XmlOptions options)
  +    static Collection extractErrorHandler(XmlOptions options)
       {
           Collection underlying = (Collection)options.get(XmlOptions.ERROR_LISTENER);
           Collection errors = new XmlErrorWatcher(underlying);
  
  
  
  1.9       +60 -12    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.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- MarshallerImpl.java	15 Dec 2003 05:03:30 -0000	1.8
  +++ MarshallerImpl.java	16 Dec 2003 05:59:47 -0000	1.9
  @@ -58,6 +58,7 @@
   
   import org.apache.xmlbeans.Marshaller;
   import org.apache.xmlbeans.XmlException;
  +import org.apache.xmlbeans.XmlOptions;
   import org.apache.xmlbeans.XmlRuntimeException;
   import org.apache.xmlbeans.impl.binding.bts.BindingLoader;
   import org.apache.xmlbeans.impl.binding.bts.BindingType;
  @@ -68,37 +69,41 @@
   
   import javax.xml.namespace.NamespaceContext;
   import javax.xml.namespace.QName;
  +import javax.xml.stream.XMLOutputFactory;
   import javax.xml.stream.XMLStreamException;
   import javax.xml.stream.XMLStreamReader;
   import javax.xml.stream.XMLStreamWriter;
  -import java.util.ArrayList;
  +import java.io.OutputStream;
   import java.util.Collection;
   
   final class MarshallerImpl
       implements Marshaller
   {
  -    private final Collection errors;
       private final BindingLoader loader;
       private final RuntimeBindingTypeTable typeTable;
       private final ScopedNamespaceContext namespaceContext;
       private final RuntimeTypeFactory runtimeTypeFactory =
           new RuntimeTypeFactory();
  +    private final Collection errors;
  +    private final XmlOptions options;
   
       private int prefixCnt = 0;
   
       private static final String NSPREFIX = "n";
  +    private static final String XML_VERSION = "1.0";
   
       MarshallerImpl(NamespaceContext root_nsctx,
                      BindingLoader loader,
                      RuntimeBindingTypeTable typeTable,
  -                   Collection errors)
  +                   XmlOptions options)
       {
           this.namespaceContext = new ScopedNamespaceContext(root_nsctx);
           this.loader = loader;
           this.typeTable = typeTable;
  -        this.errors = errors;
  +        this.errors = BindingContextImpl.extractErrorHandler(options);
  +        this.options = options;
   
  -        namespaceContext.openScope(); //TODO: verify this
  +        namespaceContext.openScope();
       }
   
       public XMLStreamReader marshall(Object obj,
  @@ -120,15 +125,18 @@
   
           //get type for this element/object pair
           final BindingTypeName type_name = loader.lookupTypeFor(jname);
  +        if (type_name == null) {
  +            String msg = "failed to lookup type for " + jname;
  +            throw new XmlException(msg);
  +        }
           final BindingType btype = loader.getBindingType(type_name);
  +        if (btype == null) {
  +            String msg = "failed to load type " + type_name;
  +            throw new XmlException(msg);
  +        }
   
           RuntimeGlobalProperty prop = new RuntimeGlobalProperty(btype, elem_qn);
  -
  -        final ArrayList errors = new ArrayList();
  -        MarshallerImpl ctx = new MarshallerImpl(nscontext, loader,
  -                                                typeTable, errors);
  -
  -        return new MarshalResult(prop, obj, ctx);
  +        return new MarshalResult(prop, obj, this);
       }
   
       public void marshall(XMLStreamWriter writer, Object obj)
  @@ -143,6 +151,45 @@
           }
       }
   
  +    public void marshall(OutputStream out, Object obj)
  +        throws XmlException
  +    {
  +        String encoding = (String)options.get(XmlOptions.CHARACTER_ENCODING);
  +        if (encoding != null) {
  +            marshall(out, obj, encoding);
  +        } else {
  +            try {
  +                final XMLOutputFactory xof = XMLOutputFactory.newInstance();
  +                final XMLStreamWriter writer = xof.createXMLStreamWriter(out);
  +                marshall(writer, obj);
  +            }
  +            catch (XMLStreamException e) {
  +                throw new XmlException(e);
  +            }
  +        }
  +    }
  +
  +    public void marshall(OutputStream out, Object obj, String encoding)
  +        throws XmlException
  +    {
  +        if (encoding == null) {
  +            throw new IllegalArgumentException("null encoding");
  +        }
  +
  +        try {
  +            XMLOutputFactory output_factory = XMLOutputFactory.newInstance();
  +            XMLStreamWriter writer =
  +                output_factory.createXMLStreamWriter(out, encoding);
  +            writer.writeStartDocument(encoding, XML_VERSION);
  +            marshall(writer, obj);
  +            writer.writeEndDocument();
  +            writer.close();
  +        }
  +        catch (XMLStreamException e) {
  +            throw new XmlException(e);
  +        }
  +    }
  +
       public XMLStreamReader marshalType(Object obj,
                                          QName elementName,
                                          QName schemaType,
  @@ -268,7 +315,8 @@
           do {
               prefix = NSPREFIX + (++prefixCnt);
               testuri = namespaceContext.getNamespaceURI(prefix);
  -        } while (testuri != null);
  +        }
  +        while (testuri != null);
           assert prefix != null;
           namespaceContext.bindNamespace(prefix, uri);
           return prefix;
  
  
  
  1.11      +23 -2     xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/UnmarshallerImpl.java
  
  Index: UnmarshallerImpl.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/UnmarshallerImpl.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- UnmarshallerImpl.java	15 Dec 2003 05:03:30 -0000	1.10
  +++ UnmarshallerImpl.java	16 Dec 2003 05:59:47 -0000	1.11
  @@ -62,6 +62,7 @@
   import org.apache.xmlbeans.Unmarshaller;
   import org.apache.xmlbeans.XmlCalendar;
   import org.apache.xmlbeans.XmlException;
  +import org.apache.xmlbeans.XmlOptions;
   import org.apache.xmlbeans.XmlRuntimeException;
   import org.apache.xmlbeans.impl.binding.bts.BindingLoader;
   import org.apache.xmlbeans.impl.binding.bts.BindingType;
  @@ -74,6 +75,7 @@
   
   import javax.xml.namespace.QName;
   import javax.xml.stream.Location;
  +import javax.xml.stream.XMLInputFactory;
   import javax.xml.stream.XMLStreamException;
   import javax.xml.stream.XMLStreamReader;
   import java.io.InputStream;
  @@ -98,6 +100,7 @@
       private final BindingLoader bindingLoader;
       private final RuntimeBindingTypeTable typeTable;
       private final Collection errors;
  +    private final XmlOptions options;
       private final XsiAttributeHolder xsiAttributeHolder =
           new XsiAttributeHolder();
   
  @@ -110,11 +113,12 @@
   
       UnmarshallerImpl(BindingLoader bindingLoader,
                        RuntimeBindingTypeTable typeTable,
  -                     Collection errors)
  +                     XmlOptions options)
       {
           this.bindingLoader = bindingLoader;
  -        this.errors = errors;
           this.typeTable = typeTable;
  +        this.errors = BindingContextImpl.extractErrorHandler(options);
  +        this.options = options;
       }
   
   
  @@ -195,6 +199,23 @@
           advanceToFirstItemOfInterest();
           BindingType bindingType = determineRootType();
           return unmarshalBindingType(bindingType);
  +    }
  +
  +    public Object unmarshal(InputStream doc)
  +        throws XmlException
  +    {
  +        String encoding = (String)options.get(XmlOptions.CHARACTER_ENCODING);
  +
  +        try {
  +            final XMLInputFactory xif = XMLInputFactory.newInstance();
  +            final XMLStreamReader reader =
  +                encoding == null ? xif.createXMLStreamReader(doc) :
  +                xif.createXMLStreamReader(doc, encoding);
  +            return unmarshal(reader);
  +        }
  +        catch (XMLStreamException e) {
  +            throw new XmlException(e);
  +        }
       }
   
   
  
  
  
  1.5       +37 -0     xml-xmlbeans/v2/src/xmlpublic/org/apache/xmlbeans/Marshaller.java
  
  Index: Marshaller.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/xmlpublic/org/apache/xmlbeans/Marshaller.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Marshaller.java	15 Dec 2003 05:03:32 -0000	1.4
  +++ Marshaller.java	16 Dec 2003 05:59:47 -0000	1.5
  @@ -60,6 +60,7 @@
   import javax.xml.namespace.QName;
   import javax.xml.stream.XMLStreamReader;
   import javax.xml.stream.XMLStreamWriter;
  +import java.io.OutputStream;
   
   /**
    * A Marshaller object is used to convert Java objects to XML documents.
  @@ -106,6 +107,42 @@
        * @throws XmlException
        */
       void marshall(XMLStreamWriter writer, Object obj)
  +        throws XmlException;
  +
  +
  +    /**
  +     * Write an XML representation of the Java object to the provided writer
  +     * as a complete xml document using the default encoding
  +     *
  +     * The object is expected to correspond to a global element in a schema.
  +     * The first matching global element will be used as the root element.
  +     *
  +     * An XML Declaration will be written declaring the encoding if one was
  +     * set via XmlOptions
  +     *
  +     * @param out
  +     * @param obj
  +     * @throws XmlException
  +     */
  +    void marshall(OutputStream out, Object obj)
  +        throws XmlException;
  +
  +
  +    /**
  +     * Write an XML representation of the Java object to the provided writer
  +     * as a complete xml document
  +     *
  +     * The object is expected to correspond to a global element in a schema.
  +     * The first matching global element will be used as the root element.
  +     *
  +     * An XML Declaration will be written declaring the encoding.
  +     *
  +     * @param out
  +     * @param obj
  +     * @param encoding      encoding used when writing the document
  +     * @throws XmlException
  +     */
  +    void marshall(OutputStream out, Object obj, String encoding)
           throws XmlException;
   
   
  
  
  
  1.4       +16 -4     xml-xmlbeans/v2/src/xmlpublic/org/apache/xmlbeans/Unmarshaller.java
  
  Index: Unmarshaller.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/xmlpublic/org/apache/xmlbeans/Unmarshaller.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Unmarshaller.java	15 Dec 2003 05:03:32 -0000	1.3
  +++ Unmarshaller.java	16 Dec 2003 05:59:47 -0000	1.4
  @@ -58,6 +58,7 @@
   
   import javax.xml.namespace.QName;
   import javax.xml.stream.XMLStreamReader;
  +import java.io.InputStream;
   
   /**
    * An Unmarshaller is used to unmarshal xml documents into Java objects.
  @@ -69,21 +70,32 @@
   {
   
       /**
  -     * unmarshall an entire xml document.
  +     *  unmarshall an entire xml document.
        *
        * PRECONDITIONS:
  -     * reader (taken from context) must be positioned at or before the root
  +     * reader must be positioned at or before the root
        * start element of the document.
        *
        * POSTCONDITIONS:
  -     * reader (taken from context) will be positioned immediately after
  +     * reader will be positioned immediately after
        * the end element corresponding to the start element from the precondition
        *
  +     * @param reader
  +     * @return
  +     * @throws XmlException
  +     */
  +    Object unmarshal(XMLStreamReader reader)
  +        throws XmlException;
  +
  +    /**
  +     * unmarshall an entire xml document.  The encoding to use is determined
  +     * according to the heuristic specified in the XML 1.0 recommendation.
        *
  +     * @param doc
        * @return
        * @throws org.apache.xmlbeans.XmlException
        */
  -    Object unmarshal(XMLStreamReader reader)
  +    Object unmarshal(InputStream doc)
           throws XmlException;
   
       /**
  
  
  
  1.21      +75 -0     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.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- MarshalTests.java	15 Dec 2003 05:03:32 -0000	1.20
  +++ MarshalTests.java	16 Dec 2003 05:59:47 -0000	1.21
  @@ -33,6 +33,9 @@
   import java.io.StringReader;
   import java.io.StringWriter;
   import java.io.Writer;
  +import java.io.FileInputStream;
  +import java.io.ByteArrayOutputStream;
  +import java.io.ByteArrayInputStream;
   import java.math.BigDecimal;
   import java.math.BigInteger;
   import java.util.ArrayList;
  @@ -307,6 +310,49 @@
       }
   
   
  +    public void testByNameDocMarshalViaOutputStream()
  +        throws Exception
  +    {
  +        com.mytest.MyClass mc = new com.mytest.MyClass();
  +        mc.setMyatt("attval");
  +        com.mytest.YourClass myelt = new com.mytest.YourClass();
  +        myelt.setAttrib(99999.777f);
  +        myelt.setMyFloat(5555.4444f);
  +//        myelt.setMyClass(new com.mytest.MyClass());
  +        myelt.setMyClass(null);
  +        mc.setMyelt(myelt);
  +
  +        myelt.setStringArray(new String[]{"one", "two", "three"});
  +
  +
  +        final File bcdoc = getBindingConfigDocument();
  +
  +        BindingContext bindingContext =
  +            BindingContextFactory.newInstance().createBindingContext(bcdoc);
  +
  +        ByteArrayOutputStream baos = new ByteArrayOutputStream();
  +
  +        final XmlOptions options = new XmlOptions();
  +        Collection errors = new LinkedList();
  +        options.setErrorListener(errors);
  +        Marshaller ctx =
  +            bindingContext.createMarshaller(options);
  +
  +        final String encoding = "UTF-16";
  +        ctx.marshall(baos, mc, encoding);
  +        baos.close();
  +        final byte[] buf = baos.toByteArray();
  +        System.out.println("Doc="+new String(buf, encoding));
  +
  +        //now unmarshall from String and compare objects...
  +        Unmarshaller umctx = bindingContext.createUnmarshaller((new XmlOptions()));
  +        final ByteArrayInputStream bais = new ByteArrayInputStream(buf);
  +        Object out_obj = umctx.unmarshal(bais);
  +        Assert.assertEquals(mc, out_obj);
  +        Assert.assertTrue(errors.isEmpty());
  +    }
  +
  +
       public void testRoundtripPerf()
           throws Exception
       {
  @@ -497,6 +543,35 @@
           Unmarshaller um_ctx =
               bindingContext.createUnmarshaller(options);
           Object obj = um_ctx.unmarshal(xrdr);
  +
  +        System.out.println("doc2-obj = " + obj);
  +
  +        for (Iterator itr = errors.iterator(); itr.hasNext();) {
  +            XmlError xmlError = (XmlError)itr.next();
  +            System.out.println("doc2-ERROR: " + xmlError);
  +        }
  +
  +        Assert.assertTrue(errors.isEmpty());
  +
  +    }
  +
  +    public void testByNameBeanUnmarshalFromInputStream()
  +        throws Exception
  +    {
  +        File bcdoc = getBindingConfigDocument();
  +
  +        BindingContext bindingContext =
  +            BindingContextFactory.newInstance().createBindingContext(bcdoc);
  +
  +        File doc = TestEnv.xbeanCase("marshal/doc2.xml");
  +
  +        final XmlOptions options = new XmlOptions();
  +        final LinkedList errors = new LinkedList();
  +        options.setErrorListener(errors);
  +
  +        Unmarshaller um_ctx =
  +            bindingContext.createUnmarshaller(options);
  +        Object obj = um_ctx.unmarshal(new FileInputStream(doc));
   
           System.out.println("doc2-obj = " + obj);
   
  
  
  

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