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 Sun, 09 May 2004 23:52:16 GMT
zieg        2004/05/09 16:52:16

  Modified:    v2/src/marshal/org/apache/xmlbeans/impl/marshal
                        MarshallerImpl.java PushMarshalResult.java
                        Soap11MarshalResult.java
                        Soap11RefdObjectVisitor.java
                        SoapMarshallerImpl.java
               v2/src/xmlpublic/org/apache/xmlbeans SoapMarshaller.java
               v2/test/src/drt/drtcases MarshalTests.java
  Added:       v2/src/marshal/org/apache/xmlbeans/impl/marshal
                        LiteralPushMarshalResult.java
                        PullSoapMarshalResult.java
                        PushSoap11MarshalResult.java
                        PushSoapMarshalResult.java
  Removed:     v2/src/marshal/org/apache/xmlbeans/impl/marshal
                        SoapMarshalResult.java
  Log:
  push based soap marshalling support
  
  Revision  Changes    Path
  1.32      +1 -1      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.31
  retrieving revision 1.32
  diff -u -r1.31 -r1.32
  --- MarshallerImpl.java	7 May 2004 09:05:50 -0000	1.31
  +++ MarshallerImpl.java	9 May 2004 23:52:16 -0000	1.32
  @@ -317,7 +317,7 @@
               new RuntimeGlobalProperty(elementName, runtime_type);
   
           final PushMarshalResult pmr =
  -            new PushMarshalResult(loader, typeTable, writer, null);
  +            new LiteralPushMarshalResult(loader, typeTable, writer, null);
   
           pmr.marshalType(obj, prop);
       }
  
  
  
  1.2       +71 -30    xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/PushMarshalResult.java
  
  Index: PushMarshalResult.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/PushMarshalResult.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- PushMarshalResult.java	6 May 2004 17:47:13 -0000	1.1
  +++ PushMarshalResult.java	9 May 2004 23:52:16 -0000	1.2
  @@ -26,7 +26,7 @@
   import javax.xml.stream.XMLStreamWriter;
   import java.util.Iterator;
   
  -class PushMarshalResult
  +abstract class PushMarshalResult
       extends MarshalResult
       implements RuntimeTypeVisitor
   {
  @@ -35,10 +35,10 @@
       private Object currObject;
       private RuntimeBindingProperty currProp;
   
  -    public PushMarshalResult(BindingLoader bindingLoader,
  -                             RuntimeBindingTypeTable typeTable,
  -                             XMLStreamWriter writer,
  -                             XmlOptions options)
  +    PushMarshalResult(BindingLoader bindingLoader,
  +                      RuntimeBindingTypeTable typeTable,
  +                      XMLStreamWriter writer,
  +                      XmlOptions options)
           throws XmlException
       {
           super(bindingLoader,
  @@ -47,34 +47,84 @@
           this.writer = writer;
       }
   
  +
  +    final void marshalType(final Object obj,
  +                           final RuntimeBindingProperty prop)
  +        throws XmlException
  +    {
  +        final RuntimeBindingType actual_rtt =
  +            prop.getActualRuntimeType(obj, this);
  +        marshalType(obj, prop, actual_rtt);
  +    }
  +
       //this method can be called recursively
       private void marshalType(final Object obj,
                                final RuntimeBindingProperty prop,
                                final RuntimeBindingType actual_rtt)
           throws XmlException
       {
  +        marshalTypeWithName(obj, prop, actual_rtt, prop.getName());
  +    }
  +
  +
  +    //this method can be called recursively
  +    private void marshalTypeWithName(final Object obj,
  +                                     final RuntimeBindingProperty prop,
  +                                     final RuntimeBindingType actual_rtt,
  +                                     QName name)
  +        throws XmlException
  +    {
           try {
  -            writeStartElement(prop);
  -            if (obj == null) {
  -                addXsiNilAttribute();
  -            } else if (actual_rtt != prop.getRuntimeBindingType()) {
  -                addXsiTypeAttribute(actual_rtt);
  -            }
  -            currObject = obj;
  -            currProp = prop;
  -            actual_rtt.accept(this);
  -            writer.writeEndElement();
  +            writeStartElement(name);
  +            writeXsiAttributes(obj, actual_rtt, prop);
  +            updateState(obj, prop);
  +            writeContents(actual_rtt);
  +            writeEndElement();
           }
           catch (XMLStreamException e) {
               throw new XmlException(e);
           }
       }
   
  -    private void writeStartElement(final RuntimeBindingProperty prop)
  +    protected final void writeEndElement() throws XMLStreamException
  +    {
  +        writer.writeEndElement();
  +    }
  +
  +    protected final void updateState(final Object obj,
  +                                     final RuntimeBindingProperty prop)
  +    {
  +        currObject = obj;
  +        currProp = prop;
  +    }
  +
  +    protected final void writeXsiAttributes(final Object obj,
  +                                            final RuntimeBindingType actual_rtt,
  +                                            final RuntimeBindingProperty prop)
  +        throws XmlException
  +    {
  +        if (obj == null) {
  +            addXsiNilAttribute();
  +        } else if (actual_rtt != prop.getRuntimeBindingType()) {
  +            addXsiTypeAttribute(actual_rtt);
  +        }
  +    }
  +
  +    protected void writeContents(final RuntimeBindingType actual_rtt)
  +        throws XmlException
  +    {
  +        actual_rtt.accept(this);
  +    }
  +
  +    protected final Object getCurrObject()
  +    {
  +        return currObject;
  +    }
  +
  +    protected void writeStartElement(QName name)
           throws XMLStreamException, XmlException
       {
  -        final QName prop_name = prop.getName();
  -        final String uri = prop_name.getNamespaceURI();
  +        final String uri = name.getNamespaceURI();
           if (uri.length() > 0) {
               String prefix = getNamespaceContext().getPrefix(uri);
               final String new_prefix;
  @@ -86,23 +136,14 @@
               }
               assert prefix != null;
               writer.writeStartElement(prefix,
  -                                     prop_name.getLocalPart(),
  -                                     prop_name.getNamespaceURI());
  +                                     name.getLocalPart(),
  +                                     name.getNamespaceURI());
               if (new_prefix != null) {
                   bindNamespace(new_prefix, uri);
               }
           } else {
  -            writer.writeStartElement(prop_name.getLocalPart());
  +            writer.writeStartElement(name.getLocalPart());
           }
  -    }
  -
  -    void marshalType(final Object obj,
  -                     final RuntimeBindingProperty prop)
  -        throws XmlException
  -    {
  -        final RuntimeBindingType actual_rtt =
  -            prop.getActualRuntimeType(obj, this);
  -        marshalType(obj, prop, actual_rtt);
       }
   
   
  
  
  
  1.2       +1 -1      xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/Soap11MarshalResult.java
  
  Index: Soap11MarshalResult.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/Soap11MarshalResult.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Soap11MarshalResult.java	30 Apr 2004 21:31:48 -0000	1.1
  +++ Soap11MarshalResult.java	9 May 2004 23:52:16 -0000	1.2
  @@ -23,7 +23,7 @@
   import javax.xml.namespace.QName;
   
   final class Soap11MarshalResult
  -    extends SoapMarshalResult
  +    extends PullSoapMarshalResult
   {
   
       private static final QName ID_NAME = new QName("id");
  
  
  
  1.3       +1 -1      xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/Soap11RefdObjectVisitor.java
  
  Index: Soap11RefdObjectVisitor.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/Soap11RefdObjectVisitor.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Soap11RefdObjectVisitor.java	6 May 2004 17:47:13 -0000	1.2
  +++ Soap11RefdObjectVisitor.java	9 May 2004 23:52:16 -0000	1.3
  @@ -24,7 +24,7 @@
   {
   
       private static final QName HREF_NAME = new QName("href");
  -    private static final String REF_PREFIX = '#' + SoapMarshalResult.ID_PREFIX;
  +    private static final String REF_PREFIX = '#' + PullSoapMarshalResult.ID_PREFIX;
   
       public Soap11RefdObjectVisitor(RuntimeBindingProperty property,
                                      Object obj,
  
  
  
  1.7       +90 -35    xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/SoapMarshallerImpl.java
  
  Index: SoapMarshallerImpl.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/SoapMarshallerImpl.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- SoapMarshallerImpl.java	6 May 2004 17:47:13 -0000	1.6
  +++ SoapMarshallerImpl.java	9 May 2004 23:52:16 -0000	1.7
  @@ -28,10 +28,12 @@
   import javax.xml.namespace.NamespaceContext;
   import javax.xml.namespace.QName;
   import javax.xml.stream.XMLStreamReader;
  +import javax.xml.stream.XMLStreamWriter;
   import java.util.Iterator;
   
   //this class is not thread safe and doesn't have to be per javadocs
  -class SoapMarshallerImpl
  +
  +final class SoapMarshallerImpl
       implements SoapMarshaller
   {
       //per binding context constants
  @@ -52,15 +54,34 @@
           this.encodingStyle = encodingStyle;
       }
   
  -    public XMLStreamReader marshalType(Object obj,
  -                                       QName elementName,
  -                                       QName schemaType,
  -                                       String javaType,
  -                                       XmlOptions options)
  +
  +    public void marshalType(XMLStreamWriter writer,
  +                            Object obj,
  +                            QName elementName,
  +                            QName schemaType,
  +                            String javaType,
  +                            XmlOptions options)
           throws XmlException
       {
  -        final NamespaceContext nscontext =
  -            MarshallerImpl.getNamespaceContextFromOptions(options);
  +        final RuntimeGlobalProperty prop =
  +            createGlobalProperty(schemaType, javaType, elementName, obj);
  +
  +        if (prop.getRuntimeBindingType().hasElementChildren()) {
  +            objectRefTable = new ObjectRefTable();
  +        }
  +
  +        final PushSoapMarshalResult result =
  +            createPushSoapResult(writer, options);
  +
  +        addObjectGraphToRefTable(obj, prop.getRuntimeBindingType(),
  +                                 prop, result);
  +
  +        result.marshalType(obj, prop);
  +
  +    }
  +
  +    private RuntimeGlobalProperty createGlobalProperty(QName schemaType, String javaType,
QName elementName, Object obj) throws XmlException
  +    {
           final BindingType btype =
               MarshallerImpl.lookupBindingType(schemaType, javaType,
                                                elementName, obj, loader);
  @@ -74,28 +95,73 @@
   
           RuntimeGlobalProperty prop =
               new RuntimeGlobalProperty(elementName, runtime_type);
  +        return prop;
  +    }
   
  -        if (runtime_type.hasElementChildren()) {
  -            objectRefTable = new ObjectRefTable();
  +    private PushSoapMarshalResult createPushSoapResult(XMLStreamWriter writer,
  +                                                       XmlOptions options)
  +        throws XmlException
  +    {
  +        final PushSoapMarshalResult result;
  +        if (EncodingStyle.SOAP11.equals(encodingStyle)) {
  +            result = new PushSoap11MarshalResult(loader,
  +                                                 typeTable,
  +                                                 writer,
  +                                                 options,
  +                                                 objectRefTable);
  +        } else if (EncodingStyle.SOAP12.equals(encodingStyle)) {
  +            throw new AssertionError("UNIMP");
  +        } else {
  +            throw new AssertionError("UNKNOWN ENCODING: " + encodingStyle);
           }
  +        return result;
  +    }
   
  +    public void marshalReferenced(XMLStreamWriter writer,
  +                                  XmlOptions options)
  +        throws XmlException
  +    {
  +        if (objectRefTable == null || !objectRefTable.hasMultiplyRefdObjects())
  +            return;
   
  -        final SoapMarshalResult retval =
  -            createMarshalResult(nscontext, prop, obj, options, false);
  +        final PushSoapMarshalResult result =
  +            createPushSoapResult(writer, options);
  +        result.writeIdParts();
  +    }
   
  -        addObjectGraphToRefTable(obj, runtime_type, prop, retval);
  +    public XMLStreamReader marshalType(Object obj,
  +                                       QName elementName,
  +                                       QName schemaType,
  +                                       String javaType,
  +                                       XmlOptions options)
  +        throws XmlException
  +    {
  +        final RuntimeGlobalProperty prop =
  +            createGlobalProperty(schemaType, javaType, elementName, obj);
  +
  +        if (prop.getRuntimeBindingType().hasElementChildren()) {
  +            objectRefTable = new ObjectRefTable();
  +        }
  +
  +        final NamespaceContext nscontext =
  +            MarshallerImpl.getNamespaceContextFromOptions(options);
  +        final PullSoapMarshalResult retval =
  +            createPullMarshalResult(nscontext, prop, obj, options, false);
  +
  +        addObjectGraphToRefTable(obj, prop.getRuntimeBindingType(),
  +                                 prop, retval);
   
           return retval;
       }
   
  -    private SoapMarshalResult createMarshalResult(final NamespaceContext nscontext,
  -                                                  RuntimeBindingProperty prop,
  -                                                  Object obj,
  -                                                  XmlOptions options,
  -                                                  boolean doing_id_parts)
  +    private PullSoapMarshalResult createPullMarshalResult(NamespaceContext nscontext,
  +                                                          RuntimeBindingProperty prop,
  +                                                          Object obj,
  +                                                          XmlOptions options,
  +                                                          boolean doing_id_parts)
           throws XmlException
       {
  -        final SoapMarshalResult retval;
  +        final PullSoapMarshalResult retval;
   
           if (EncodingStyle.SOAP11.equals(encodingStyle)) {
               retval = new Soap11MarshalResult(loader, typeTable,
  @@ -121,7 +187,6 @@
           instanceVisitor.setCurrObject(obj, prop);
           instanceVisitor.setMarshalResult(result);
           runtime_type.accept(instanceVisitor);
  -
       }
   
       public Iterator marshalReferenced(XmlOptions options)
  @@ -139,7 +204,7 @@
           private final XmlOptions options;
           private final NamespaceContext nscontext;
           private final Iterator tblItr = objectRefTable.getMultipleRefTableEntries();
  -        private SoapMarshalResult marshalResult;
  +        private PullSoapMarshalResult marshalResult;
   
           public ReaderIterator(XmlOptions options)
           {
  @@ -160,10 +225,10 @@
   
               try {
                   if (marshalResult == null) {
  -                    marshalResult = createMarshalResult(nscontext,
  -                                                        cur_val.getProp(),
  -                                                        cur_val.object,
  -                                                        options, true);
  +                    marshalResult = createPullMarshalResult(nscontext,
  +                                                            cur_val.getProp(),
  +                                                            cur_val.object,
  +                                                            options, true);
                   } else {
                       marshalResult.reset(cur_val.getProp(), cur_val.object, true);
                   }
  @@ -192,21 +257,11 @@
           private RuntimeBindingProperty currProp;
           private MarshalResult marshalResult;
   
  -        public Object getCurrObject()
  -        {
  -            return currObject;
  -        }
  -
           public void setCurrObject(Object obj,
                                     RuntimeBindingProperty prop)
           {
               this.currObject = obj;
               this.currProp = prop;
  -        }
  -
  -        public MarshalResult getMarshalResult()
  -        {
  -            return marshalResult;
           }
   
           public void setMarshalResult(MarshalResult marshalResult)
  
  
  
  1.1                  xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/LiteralPushMarshalResult.java
  
  Index: LiteralPushMarshalResult.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.XmlException;
  import org.apache.xmlbeans.XmlOptions;
  import org.apache.xmlbeans.impl.binding.bts.BindingLoader;
  import org.apache.xmlbeans.impl.marshal.util.ArrayUtils;
  
  import javax.xml.namespace.NamespaceContext;
  import javax.xml.namespace.QName;
  import javax.xml.stream.XMLStreamException;
  import javax.xml.stream.XMLStreamWriter;
  import java.util.Iterator;
  
  final class LiteralPushMarshalResult
      extends PushMarshalResult
  {
      LiteralPushMarshalResult(BindingLoader bindingLoader,
                               RuntimeBindingTypeTable typeTable,
                               XMLStreamWriter writer,
                               XmlOptions options)
          throws XmlException
      {
          super(bindingLoader,
                typeTable,
                writer,
                options);
      }
  
  }
  
  
  
  1.1                  xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/PullSoapMarshalResult.java
  
  Index: PullSoapMarshalResult.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.XmlException;
  import org.apache.xmlbeans.XmlOptions;
  import org.apache.xmlbeans.impl.binding.bts.BindingLoader;
  
  import javax.xml.namespace.NamespaceContext;
  import javax.xml.namespace.QName;
  
  abstract class PullSoapMarshalResult
      extends PullMarshalResult
  {
      private final ObjectRefTable objectRefTable;
      private final boolean writingMultiRefdObjs;
  
      protected static final String ID_PREFIX = "i";
  
  
      //TODO: REVIEW: consider ways to reduce the number of parameters here
      PullSoapMarshalResult(BindingLoader loader,
                        RuntimeBindingTypeTable tbl,
                        NamespaceContext root_nsctx,
                        RuntimeBindingProperty property,
                        Object obj,
                        XmlOptions options,
                        ObjectRefTable object_ref_table,
                        boolean writingMultiRefdObjs)
          throws XmlException
      {
          super(loader, tbl, root_nsctx, property, obj, options);
  
          this.objectRefTable = object_ref_table;
          this.writingMultiRefdObjs = writingMultiRefdObjs;
          if (writingMultiRefdObjs) topLevelIdInit(obj);
      }
  
  
      protected void reset(RuntimeBindingProperty property,
                           Object obj,
                           boolean writingMultiRefdObjs)
          throws XmlException
      {
          reset(property, obj);
          if (writingMultiRefdObjs) topLevelIdInit(obj);
      }
  
      private void topLevelIdInit(Object top_lvl_obj)
          throws XmlException
      {
          initAttributes();
  
          if (objectRefTable != null) {
              int id = objectRefTable.getId(top_lvl_obj);
              if (id >= 0) {
                  fillAndAddAttribute(getIdAttributeName(), ID_PREFIX + id);
              }
          }
  
      }
  
  
      //somewhat ugly way to "un-override" our createVisitor override, since
      //we don't want the special behavior when writing top lvl id parts.
      protected XmlTypeVisitor createInitialVisitor(RuntimeBindingProperty property,
                                                    Object obj)
          throws XmlException
      {
          if (writingMultiRefdObjs)
              return super.createVisitor(property, obj);
          else
              return createVisitor(property, obj);
      }
  
  
      protected XmlTypeVisitor createVisitor(RuntimeBindingProperty property,
                                             Object obj)
          throws XmlException
      {
          if (objectRefTable != null) {
              final int id = objectRefTable.getId(obj);
              if (id >= 0) {
                  return createRefdObjectVisitor(property, obj, id);
              }
          }
  
          return super.createVisitor(property, obj);
  
      }
  
      protected abstract XmlTypeVisitor createRefdObjectVisitor(RuntimeBindingProperty property,
                                                                Object obj,
                                                                int id)
          throws XmlException;
  
      protected abstract QName getIdAttributeName();
  }
  
  
  
  1.1                  xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/PushSoap11MarshalResult.java
  
  Index: PushSoap11MarshalResult.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.impl.binding.bts.BindingLoader;
  import org.apache.xmlbeans.XmlOptions;
  import org.apache.xmlbeans.XmlException;
  
  import javax.xml.namespace.QName;
  import javax.xml.stream.XMLStreamWriter;
  
  final class PushSoap11MarshalResult
      extends PushSoapMarshalResult
  {
  
      private static final QName ID_NAME = new QName("id");
      private static final QName REF_NAME = new QName("href");
      private static final String REF_PREFIX =
          '#' + PullSoapMarshalResult.ID_PREFIX;
  
  
      PushSoap11MarshalResult(BindingLoader bindingLoader,
                              RuntimeBindingTypeTable typeTable,
                              XMLStreamWriter writer,
                              XmlOptions options,
                              ObjectRefTable objectRefTable)
          throws XmlException
      {
          super(bindingLoader, typeTable, writer, options, objectRefTable);
      }
  
      protected QName getRefQName()
      {
          return REF_NAME;
      }
  
      protected String getRefValue(int id)
      {
          return REF_PREFIX + Integer.toString(id);
      }
  
      protected QName getIdQName()
      {
          return ID_NAME;
      }
  }
  
  
  
  1.1                  xml-xmlbeans/v2/src/marshal/org/apache/xmlbeans/impl/marshal/PushSoapMarshalResult.java
  
  Index: PushSoapMarshalResult.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.XmlException;
  import org.apache.xmlbeans.XmlOptions;
  import org.apache.xmlbeans.impl.binding.bts.BindingLoader;
  
  import javax.xml.namespace.QName;
  import javax.xml.stream.XMLStreamException;
  import javax.xml.stream.XMLStreamWriter;
  import java.util.Iterator;
  
  abstract class PushSoapMarshalResult
      extends PushMarshalResult
  {
      private final ObjectRefTable objectRefTable;
  
      PushSoapMarshalResult(BindingLoader bindingLoader,
                            RuntimeBindingTypeTable typeTable,
                            XMLStreamWriter writer,
                            XmlOptions options,
                            ObjectRefTable objectRefTable)
          throws XmlException
      {
          super(bindingLoader, typeTable, writer, options);
          this.objectRefTable = objectRefTable;
      }
  
      final void writeIdParts()
          throws XmlException
      {
          final Iterator itr = objectRefTable.getMultipleRefTableEntries();
  
          while (itr.hasNext()) {
              final ObjectRefTable.Value cur_val =
                  (ObjectRefTable.Value)itr.next();
              marshalTypeWithId(cur_val);
          }
      }
  
      private void marshalTypeWithId(ObjectRefTable.Value val)
          throws XmlException
      {
          final RuntimeBindingProperty prop = val.getProp();
          final Object obj = val.object;
  
          final RuntimeBindingType actual_rtt =
              prop.getActualRuntimeType(obj, this);
  
          try {
              writeStartElement(actual_rtt.getSchemaTypeName());
              fillAndAddAttribute(getIdQName(), getIdValue(val.getId()));
              writeXsiAttributes(obj, actual_rtt, prop);
              updateState(obj, prop);
              writeContents(actual_rtt);
              writeEndElement();
          }
          catch (XMLStreamException e) {
              throw new XmlException(e);
          }
      }
  
  
      protected final void writeContents(final RuntimeBindingType actual_rtt)
          throws XmlException
      {
          final int id =
              objectRefTable == null ? -1 : objectRefTable.getId(getCurrObject());
  
          if (id < 0) {
              super.writeContents(actual_rtt);
          } else {
              fillAndAddAttribute(getRefQName(), getRefValue(id));
          }
      }
  
  
      protected abstract QName getRefQName();
  
      protected abstract String getRefValue(int id);
  
      protected abstract QName getIdQName();
  
      private static String getIdValue(int id)
      {
          return PullSoapMarshalResult.ID_PREFIX + id;
      }
  }
  
  
  
  1.4       +52 -3     xml-xmlbeans/v2/src/xmlpublic/org/apache/xmlbeans/SoapMarshaller.java
  
  Index: SoapMarshaller.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/xmlpublic/org/apache/xmlbeans/SoapMarshaller.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- SoapMarshaller.java	30 Apr 2004 21:31:48 -0000	1.3
  +++ SoapMarshaller.java	9 May 2004 23:52:16 -0000	1.4
  @@ -17,6 +17,7 @@
   
   import javax.xml.namespace.QName;
   import javax.xml.stream.XMLStreamReader;
  +import javax.xml.stream.XMLStreamWriter;
   import java.util.Iterator;
   
   /**
  @@ -27,6 +28,54 @@
    */
   public interface SoapMarshaller
   {
  +    /**
  +     * Write an XML representation of the Java object to the provided output.
  +     *
  +     * It is the responsibility of the caller to ensure that
  +     * obj is an instanceof javaType
  +     *
  +     * As of this writing (11/22/2003), this method will NOT write
  +     * a START_DOCUMENT or END_DOCUMENT element.
  +     * The first event written will be a START_ELEMENT event.
  +     *
  +     * <ul>
  +     * <li>A collection instance that should be used as an error listener during
  +     * compilation, as described in {@link XmlOptions#setErrorListener}.</li>
  +     * </ul>
  +     *
  +     * @param writer
  +     * @param obj
  +     * @param elementName
  +     * @param schemaType
  +     * @param javaType the java type in the format returned by Class.getName()
  +     * @throws XmlException
  +     */
  +    void marshalType(XMLStreamWriter writer,
  +                     Object obj,
  +                     QName elementName,
  +                     QName schemaType,
  +                     String javaType,
  +                     XmlOptions options)
  +        throws XmlException;
  +
  +
  +
  +    /**
  +     * Get the multiply referenced objects, usually written after the other parts.
  +     *
  +     * <ul>
  +     * <li>A collection instance that should be used as an error listener during
  +     * compilation, as described in {@link XmlOptions#setErrorListener}.</li>
  +     * </ul>
  +     *
  +     * @param options
  +     * @throws XmlException
  +     */
  +    void marshalReferenced(XMLStreamWriter writer,
  +                           XmlOptions options)
  +        throws XmlException;
  +
  +
   
       /**
        * Get an XMLStreamReader object that represents the given java type.
  @@ -34,8 +83,8 @@
        * It is the responsibility of the caller to ensure that
        * obj is an instanceof javaType
   
  -     * As of this writing (11/22/2003), the returned reader will NOT contain
  -     * a START_DOCUMENT or END_DOCUMENT element.
  +     * The returned reader will NOT contain a
  +     * START_DOCUMENT or END_DOCUMENT element.
        * The reader's first event is a START_ELEMENT event.
        *
        * <ul>
  @@ -62,7 +111,7 @@
       /**
        * Get the multiply referenced objects, usually written after the other parts.
        *
  -     * Returns an Iterator of XMLStreamReader objects, each reader 
  +     * Returns an Iterator of XMLStreamReader objects, each reader
        * represting an object that has been referred to more than once.
        *
        *
  
  
  
  1.61      +142 -4    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.60
  retrieving revision 1.61
  diff -u -r1.60 -r1.61
  --- MarshalTests.java	7 May 2004 09:05:50 -0000	1.60
  +++ MarshalTests.java	9 May 2004 23:52:16 -0000	1.61
  @@ -91,6 +91,7 @@
       //must be in sync with binding config file
       private static final BigInteger DEFAULT_BIG_INT =
           new BigInteger("876587658765876587658765876587658765");
  +    private static final String XSD_URI = "http://www.w3.org/2001/XMLSchema";
   
       public MarshalTests(String name)
       {
  @@ -115,6 +116,51 @@
           dumpReader(reader);
       }
   
  +    //does not test any xmlbeans code, but rather a quick sanity check
  +    //of the current jsr 173 impl
  +    public void testAStreamWriter()
  +        throws Exception
  +    {
  +        StringWriter sw = new StringWriter();
  +        final XMLStreamWriter writer =
  +            XMLOutputFactory.newInstance().createXMLStreamWriter(sw);
  +
  +        writer.writeStartDocument();
  +        writer.writeStartElement("dummy");
  +        final String uri = "uri";
  +        final String prefix = "prefix";
  +        final String lname = "lname";
  +
  +        writer.writeStartElement(prefix, lname, uri);
  +        if (writer.getPrefix(uri) == null) {
  +            writer.writeNamespace(prefix, uri);
  +        }
  +        writer.writeEndElement();
  +
  +        writer.writeStartElement(prefix, lname, uri);
  +        if (writer.getPrefix(uri) == null) {
  +            writer.writeNamespace(prefix, uri);
  +        }
  +        writer.writeEndElement();
  +
  +        writer.writeEndElement(); //dummy
  +
  +        writer.writeEndDocument();
  +        writer.close();
  +        sw.close();
  +
  +        final String DOC = sw.getBuffer().toString();
  +        inform("DOC W: " + DOC);
  +
  +
  +        StringReader sr = new StringReader(DOC);
  +        final XMLStreamReader reader =
  +            XMLInputFactory.newInstance().createXMLStreamReader(sr);
  +
  +        //uncomment when stax bug is fixed
  +        //dumpReader(reader, true);
  +    }
  +
       public void testManySimpleTypesUnmarshall()
           throws Exception
       {
  @@ -298,7 +344,7 @@
               ctx.marshalType(orig,
   //                           new QName("uri", "lname"),
                               new QName("lname"),
  -                            new QName("http://www.w3.org/2001/XMLSchema", xsd_type),
  +                            new QName(XSD_URI, xsd_type),
                               java_type, options);
   
   
  @@ -326,7 +372,7 @@
   
           String our_obj = "hello";
   
  -        final QName schemaType = new QName("http://www.w3.org/2001/XMLSchema", "anyType");
  +        final QName schemaType = new QName(XSD_URI, "anyType");
           final String javaType = Object.class.getName();
           final XMLStreamReader reader =
               ctx.marshalType(our_obj,
  @@ -501,6 +547,98 @@
       }
   
   
  +    public void testByNameMarshalSoapViaWriter()
  +        throws Exception
  +    {
  +        final boolean verbose = false;
  +
  +        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);
  +
  +        QName qn = new QName("foo", "bar");
  +        myelt.setQn(qn);
  +        myelt.setQn2(qn);
  +
  +        myelt.setWrappedArrayOne(new String[]{"a", "a", "b"});
  +
  +        MySubClass sub = new MySubClass();
  +        sub.setBigInt(new BigInteger("23522352235223522352"));
  +
  +        myelt.setMySubClass(sub);
  +        myelt.setMyClass(sub);
  +        sub.setMyelt(myelt);  //cycle
  +
  +        myelt.setMyBoss(myelt); //cycle: self reference
  +
  +        SimpleContentExample se = new SimpleContentExample();
  +        se.setFloatAttOne(44.33f);
  +        se.setSimpleContent("someSimpleContentOkay");
  +        myelt.setSimpleContentExample(se);
  +
  +        myelt.setModeEnum(ModeEnum.On);
  +
  +        mc.setMyelt(myelt);
  +
  +        myelt.setStringArray(new String[]{"a", "b", "c"});
  +
  +        myelt.setMyClassArray(new MyClass[]{sub, new MyClass(),
  +                                            sub});
  +
  +
  +        BindingContext bindingContext = getBindingContext(getBindingConfigDocument());
  +
  +        final XmlOptions options = new XmlOptions();
  +        Collection errors = new LinkedList();
  +        options.setErrorListener(errors);
  +
  +        final SoapMarshaller ctx =
  +            bindingContext.createSoapMarshaller(EncodingStyle.SOAP11);
  +
  +        Assert.assertNotNull(ctx);
  +
  +        {
  +
  +            StringWriter sw = new StringWriter();
  +            XMLStreamWriter xml_out =
  +                XMLOutputFactory.newInstance().createXMLStreamWriter(sw);
  +
  +            ctx.marshalType(xml_out, mc, new QName("java:com.mytest", "load"),
  +                            new QName("java:com.mytest", "MyClass"),
  +                            mc.getClass().getName(), options);
  +//        ctx.marshalType(xml_out, "TEST1", new QName("someuri", "str"),
  +//                        new QName(XSD_URI, "string"),
  +//                        String.class.getName(), options);
  +            xml_out.close();
  +            sw.close();
  +
  +            inform("=======SOAPOUT-XML:\n" +
  +                   PrettyPrinter.indent(sw.getBuffer().toString()));
  +            reportErrors(errors, "byname-marshal-soap-writer");
  +            Assert.assertTrue(errors.isEmpty());
  +        }
  +
  +        {
  +            StringWriter sw2 = new StringWriter();
  +            XMLStreamWriter xml_out2 =
  +                XMLOutputFactory.newInstance().createXMLStreamWriter(sw2);
  +            xml_out2.writeStartDocument();
  +            xml_out2.writeStartElement("ID_DUMMY");
  +            ctx.marshalReferenced(xml_out2, options);
  +            xml_out2.writeEndElement();
  +            xml_out2.writeEndDocument();
  +            xml_out2.close();
  +            sw2.close();
  +
  +            inform("=======2SOAPOUT-XML:\n" + sw2.getBuffer().toString());
  +            reportErrors(errors, "byname-marshal-soap-writer");
  +            Assert.assertTrue(errors.isEmpty());
  +        }
  +    }
  +
  +
       public void testWrappedArray()
           throws Exception
       {
  @@ -790,8 +928,8 @@
               bindingContext.createMarshaller();
           Assert.assertNotNull(ctx);
   
  -        ctx.marshal(fos, mc);
  -//        ctx.marshal(fos, mc, options);
  +//        ctx.marshal(fos, mc);
  +        ctx.marshal(fos, mc, options);
           fos.close();
   
           //now unmarshall from file and compare objects...
  
  
  

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