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 Mon, 29 Mar 2004 07:02:48 GMT
zieg        2004/03/28 23:02:48

  Modified:    v2/src/marshal/org/apache/xmlbeans/impl/marshal
                        UnmarshalResult.java
               v2/src/xmlpublic/org/apache/xmlbeans XmlOptions.java
               v2/test/cases/marshal doc2.xml
               v2/test/cases/marshal/com/mytest YourClass.java
               v2/test/src/drt/drtcases MarshalTests.java
  Added:       v2/src/xmlpublic/org/apache/xmlbeans ObjectFactory.java
  Log:
  better support object factory
  
  Revision  Changes    Path
  1.14      +20 -3     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.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- UnmarshalResult.java	25 Mar 2004 03:19:28 -0000	1.13
  +++ UnmarshalResult.java	29 Mar 2004 07:02:48 -0000	1.14
  @@ -17,6 +17,7 @@
   
   import org.apache.xmlbeans.GDate;
   import org.apache.xmlbeans.GDuration;
  +import org.apache.xmlbeans.ObjectFactory;
   import org.apache.xmlbeans.SchemaType;
   import org.apache.xmlbeans.SchemaTypeLoader;
   import org.apache.xmlbeans.XmlCalendar;
  @@ -28,7 +29,6 @@
   import org.apache.xmlbeans.impl.binding.bts.JavaTypeName;
   import org.apache.xmlbeans.impl.binding.bts.SimpleDocumentBinding;
   import org.apache.xmlbeans.impl.binding.bts.XmlTypeName;
  -import org.apache.xmlbeans.impl.common.InvalidLexicalValueException;
   import org.apache.xmlbeans.impl.richParser.XMLStreamReaderExt;
   import org.apache.xmlbeans.impl.richParser.XMLStreamReaderExtImpl;
   import org.apache.xmlbeans.impl.validator.ValidatingXMLStreamReader;
  @@ -92,7 +92,8 @@
           return typeTable.getRuntimeTypeFactory();
       }
   
  -    RuntimeBindingType getRuntimeType(BindingType type) throws XmlException
  +    RuntimeBindingType getRuntimeType(BindingType type)
  +        throws XmlException
       {
           return typeTable.getRuntimeTypeFactory().createRuntimeType(type,
                                                                      typeTable,
  @@ -178,7 +179,23 @@
   
           this.updateAttributeState();
   
  -        return um.unmarshal(this);
  +        ObjectFactory of = extractObjectFactory();
  +        if (of == null) {
  +            return um.unmarshal(this);
  +        } else {
  +            final RuntimeBindingType rtt = getRuntimeType(bindingType);
  +            final Object initial_obj = of.createObject(rtt.getJavaType());
  +            um.unmarshal(initial_obj, this);
  +            return initial_obj;
  +        }
  +    }
  +
  +    private ObjectFactory extractObjectFactory()
  +    {
  +        if (options == null) return null;
  +
  +        return
  +            (ObjectFactory)options.get(XmlOptions.UNMARSHAL_INITIAL_OBJECT_FACTORY);
       }
   
       Object unmarshalType(XMLStreamReader reader,
  
  
  
  1.7       +12 -0     xml-xmlbeans/v2/src/xmlpublic/org/apache/xmlbeans/XmlOptions.java
  
  Index: XmlOptions.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/xmlpublic/org/apache/xmlbeans/XmlOptions.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- XmlOptions.java	22 Mar 2004 16:51:12 -0000	1.6
  +++ XmlOptions.java	29 Mar 2004 07:02:48 -0000	1.7
  @@ -432,6 +432,15 @@
           return set( UNMARSHAL_VALIDATE );
       }
   
  +
  +    /**
  +     * Provide a factory to be used for the creation of the top level object
  +     * returned by the unmarshal methods.
  +     */
  +    public XmlOptions setUnmarshalInitialObjectFactory(ObjectFactory factory) {
  +        return set( UNMARSHAL_INITIAL_OBJECT_FACTORY, factory );
  +    }
  +
       /**
        * Sets the name of the variable that represents
        * the current node in a query expression.
  @@ -636,6 +645,9 @@
   
       /** @exclude */
       public static final String UNMARSHAL_VALIDATE              =  "UNMARSHAL_VALIDATE";
  +
  +    /** @exclude */
  +    public static final String UNMARSHAL_INITIAL_OBJECT_FACTORY=  "UNMARSHAL_INITIAL_OBJECT_FACTORY";
   
       /** @exclude */
       public static final String XQUERY_CURRENT_NODE_VAR         =  "XQUERY_CURRENT_NODE_VAR";
  
  
  
  1.1                  xml-xmlbeans/v2/src/xmlpublic/org/apache/xmlbeans/ObjectFactory.java
  
  Index: ObjectFactory.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;
  
  /**
   * generic interface to abstract creation of an object
   */
  public interface ObjectFactory
  {
      /**
       * Given the type, create an object of that type.  Note that the return object
       * might be a subclass of type, but should always be an instanceof type.
       *
       * @param type   type of object to create
       * @return  instance
       */
      Object createObject(Class type);
  }
  
  
  
  1.8       +1 -0      xml-xmlbeans/v2/test/cases/marshal/doc2.xml
  
  Index: doc2.xml
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/test/cases/marshal/doc2.xml,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- doc2.xml	17 Feb 2004 17:34:56 -0000	1.7
  +++ doc2.xml	29 Mar 2004 07:02:48 -0000	1.8
  @@ -1,5 +1,6 @@
   <?xml version="1.0" encoding="utf-8"?>
   <n1:load xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  +    xsi:type="n1:MyClass"
       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
       xmlns:n1="java:com.mytest">
   
  
  
  
  1.16      +2 -0      xml-xmlbeans/v2/test/cases/marshal/com/mytest/YourClass.java
  
  Index: YourClass.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/test/cases/marshal/com/mytest/YourClass.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- YourClass.java	25 Mar 2004 03:19:28 -0000	1.15
  +++ YourClass.java	29 Mar 2004 07:02:48 -0000	1.16
  @@ -16,6 +16,7 @@
   package com.mytest;
   
   import org.apache.xmlbeans.impl.marshal.util.ArrayUtils;
  +import org.apache.xmlbeans.ObjectFactory;
   
   import javax.xml.namespace.QName;
   import java.util.ArrayList;
  @@ -24,6 +25,7 @@
   import java.util.Arrays;
   
   public class YourClass
  +    implements ObjectFactory
   {
       private YourClass myBoss = null;
   
  
  
  
  1.48      +4 -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.47
  retrieving revision 1.48
  diff -u -r1.47 -r1.48
  --- MarshalTests.java	25 Mar 2004 07:48:07 -0000	1.47
  +++ MarshalTests.java	29 Mar 2004 07:02:48 -0000	1.48
  @@ -22,6 +22,7 @@
   import com.mytest.MySubClass;
   import com.mytest.MySubSubClass;
   import com.mytest.SimpleContentExample;
  +import com.mytest.YourClass;
   import junit.framework.Assert;
   import junit.framework.Test;
   import junit.framework.TestCase;
  @@ -37,6 +38,7 @@
   import org.apache.xmlbeans.XmlException;
   import org.apache.xmlbeans.XmlObject;
   import org.apache.xmlbeans.XmlOptions;
  +import org.apache.xmlbeans.ObjectFactory;
   import org.apache.xmlbeans.impl.binding.compile.Schema2Java;
   import org.apache.xmlbeans.impl.binding.tylar.TylarConstants;
   import org.apache.xmlbeans.impl.common.XmlReaderToWriter;
  @@ -886,6 +888,8 @@
               xmlInputFactory.createXMLStreamReader(new FileReader(doc));
   
           final XmlOptions options = new XmlOptions();
  +        ObjectFactory of = new YourClass();
  +        options.setUnmarshalInitialObjectFactory(of);
           final LinkedList errors = new LinkedList();
           options.setErrorListener(errors);
   
  
  
  

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