You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by di...@apache.org on 2004/10/15 16:44:34 UTC

cvs commit: ws-axis/java/src/org/apache/axis/encoding/ser/castor AxisContentHandler.java CastorSerializer.java

dims        2004/10/15 07:44:34

  Modified:    java/src/org/apache/axis/encoding/ser/castor
                        CastorSerializer.java
  Added:       java/src/org/apache/axis/encoding/ser/castor
                        AxisContentHandler.java
  Log:
  Fix for AXIS-1556 - better castor serializer/deserializer
  from Fabien Nisol (fabien.nisol@advalvas.be)
  
  Revision  Changes    Path
  1.7       +70 -37    ws-axis/java/src/org/apache/axis/encoding/ser/castor/CastorSerializer.java
  
  Index: CastorSerializer.java
  ===================================================================
  RCS file: /home/cvs/ws-axis/java/src/org/apache/axis/encoding/ser/castor/CastorSerializer.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- CastorSerializer.java	25 Feb 2004 14:02:39 -0000	1.6
  +++ CastorSerializer.java	15 Oct 2004 14:44:34 -0000	1.7
  @@ -1,17 +1,56 @@
   /*
  - * Copyright 2002-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.
  + * The Apache Software License, Version 1.1
  + *
  + *
  + * Copyright (c) 2002 The Apache Software Foundation.  All rights
  + * reserved.
  + *
  + * Redistribution and use in source and binary forms, with or without
  + * modification, are permitted provided that the following conditions
  + * are met:
  + *
  + * 1. Redistributions of source code must retain the above copyright
  + *    notice, this list of conditions and the following disclaimer.
  + *
  + * 2. Redistributions in binary form must reproduce the above copyright
  + *    notice, this list of conditions and the following disclaimer in
  + *    the documentation and/or other materials provided with the
  + *    distribution.
  + *
  + * 3. The end-user documentation included with the redistribution,
  + *    if any, must include the following acknowledgment:
  + *       "This product includes software developed by the
  + *        Apache Software Foundation (http://www.apache.org/)."
  + *    Alternately, this acknowledgment may appear in the software itself,
  + *    if and wherever such third-party acknowledgments normally appear.
  + *
  + * 4. The names "Axis" and "Apache Software Foundation" must
  + *    not be used to endorse or promote products derived from this
  + *    software without prior written permission. For written
  + *    permission, please contact apache@apache.org.
  + *
  + * 5. Products derived from this software may not be called "Apache",
  + *    nor may "Apache" appear in their name, without prior written
  + *    permission of the Apache Software Foundation.
  + *
  + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  + * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  + * SUCH DAMAGE.
  + * ====================================================================
  + *
  + * This software consists of voluntary contributions made by many
  + * individuals on behalf of the Apache Software Foundation.  For more
  + * information on the Apache Software Foundation, please see
  + * <http://www.apache.org/>.
    */
   
   package org.apache.axis.encoding.ser.castor;
  @@ -31,11 +70,10 @@
   
   import javax.xml.namespace.QName;
   import java.io.IOException;
  -import java.io.StringWriter;
   
   /**
    * Castor serializer
  - * 
  + *
    * @author Olivier Brand (olivier.brand@vodafone.com)
    * @author Steve Loughran
    * @version 1.0
  @@ -47,45 +85,40 @@
   
       /**
        * Serialize a Castor object.
  -     * 
  -     * @param name       
  -     * @param attributes 
  +     *
  +     * @param name
  +     * @param attributes
        * @param value      this must be a castor object for marshalling
  -     * @param context    
  +     * @param context
        * @throws IOException for XML schema noncompliance, bad object type, and any IO
        *                     trouble.
        */
  -    public void serialize(
  -            QName name,
  -            Attributes attributes,
  -            Object value,
  -            SerializationContext context)
  +    public void serialize(QName name,
  +                          Attributes attributes,
  +                          Object value,
  +                          SerializationContext context)
               throws IOException {
  -        String fdate;
  -        StringWriter writer = null;
  -
           try {
  -            writer = new StringWriter();
  -
  -            // Create a Castor Marshaller initialized with the output stream
  -            Marshaller marshaller = new Marshaller(writer);
  +            AxisContentHandler hand = new AxisContentHandler(context);
  +            Marshaller marshaller = new Marshaller(hand);
   
               // Don't include the DOCTYPE, otherwise an exception occurs due to
               //2 DOCTYPE defined in the document. The XML fragment is included in
               //an XML document containing already a DOCTYPE
               marshaller.setMarshalAsDocument(false);
  -
  +            marshaller.setRootElement(name.getLocalPart());
  +            
               // Marshall the Castor object into the stream (sink)
               marshaller.marshal(value);
  -
  -            context.writeString(writer.toString());
           } catch (MarshalException me) {
               log.error(Messages.getMessage("castorMarshalException00"), me);
  -            throw new IOException(Messages.getMessage("castorMarshalException00")
  +            throw new IOException(Messages.getMessage(
  +                    "castorMarshalException00")
                       + me.getLocalizedMessage());
           } catch (ValidationException ve) {
               log.error(Messages.getMessage("castorValidationException00"), ve);
  -            throw new IOException(Messages.getMessage("castorValidationException00")
  +            throw new IOException(Messages.getMessage(
  +                    "castorValidationException00")
                       + ve.getLocalizedMessage());
           }
       }
  @@ -98,7 +131,7 @@
        * Return XML schema for the specified type, suitable for insertion into
        * the &lt;types&gt; element of a WSDL document, or underneath an
        * &lt;element&gt; or &lt;attribute&gt; declaration.
  -     * 
  +     *
        * @param javaType the Java Class we're writing out schema for
        * @param types    the Java2WSDL Types object which holds the context
        *                 for the WSDL being generated.
  
  
  
  1.1                  ws-axis/java/src/org/apache/axis/encoding/ser/castor/AxisContentHandler.java
  
  Index: AxisContentHandler.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Axis" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.axis.encoding.ser.castor;
  
  import org.apache.axis.encoding.SerializationContext;
  import org.xml.sax.Attributes;
  import org.xml.sax.SAXException;
  import org.xml.sax.helpers.DefaultHandler;
  
  import javax.xml.namespace.QName;
  import java.io.IOException;
  
  /**
   * This ContentHandler delegates all serialization to an axis SerializationContext
   *
   * @author <a href="mailto:fabien.nisol@advalvas.be">Fabien Nisol</a>
   * @version $Revision: 1.1 $ $Name:  $
   */
  public class AxisContentHandler extends DefaultHandler {
      /**
       * serialization context to delegate to
       */
      private SerializationContext context;
  
      /**
       * Creates a contentHandler delegate
       *
       * @param context : axis context to delegate to
       */
      public AxisContentHandler(SerializationContext context) {
          super();
          setContext(context);
      }
  
      /**
       * Getter for property context.
       *
       * @return Value of property context.
       */
      public SerializationContext getContext() {
          return context;
      }
  
      /**
       * Setter for property context.
       *
       * @param context New value of property context.
       */
      public void setContext(SerializationContext context) {
          this.context = context;
      }
  
      /**
       * delegates to the serialization context
       */
      public void startElement(String uri, String localName, String qName,
                               Attributes attributes) throws SAXException {
          try {
              context.startElement(new QName(uri, localName), attributes);
          } catch (IOException ioe) {
              throw new SAXException(ioe);
          }
      }
  
      /**
       * delegates to the serialization context
       */
      public void endElement(String uri, String localName, String qName)
              throws SAXException {
          try {
              context.endElement();
          } catch (IOException ioe) {
              throw new SAXException(ioe);
          }
      }
  
      /**
       * delegates to the serialization context
       */
      public void characters(char[] ch, int start, int length)
              throws org.xml.sax.SAXException {
          try {
              context.writeChars(ch, start, length);
          } catch (IOException ioe) {
              throw new SAXException(ioe);
          }
      }
  }