You are viewing a plain text version of this content. The canonical link for it is here.
Posted to axis-cvs@ws.apache.org by di...@apache.org on 2005/02/10 16:36:07 UTC

cvs commit: ws-axis/java/src/org/apache/axis/utils XMLUtils.java

dims        2005/02/10 07:36:07

  Modified:    java/src/org/apache/axis Message.java SOAPPart.java
               java/test/saaj PackageTests.java
               java/src/org/apache/axis/message SAXOutputter.java
               java/src/org/apache/axis/encoding SerializationContext.java
                        TextSerializationContext.java
               java/src/org/apache/axis/utils XMLUtils.java
  Added:       java/test/saaj TestMessageProperty.java
  Log:
  Fix for AXIS-1518 - XML Declaration missing
  from  Jongjin Choi
  
  URL: http://issues.apache.org/jira/browse/AXIS-1518
  
  Revision  Changes    Path
  1.118     +2 -6      ws-axis/java/src/org/apache/axis/Message.java
  
  Index: Message.java
  ===================================================================
  RCS file: /home/cvs/ws-axis/java/src/org/apache/axis/Message.java,v
  retrieving revision 1.117
  retrieving revision 1.118
  diff -u -r1.117 -r1.118
  --- Message.java	7 Sep 2004 09:30:45 -0000	1.117
  +++ Message.java	10 Feb 2005 15:36:06 -0000	1.118
  @@ -515,12 +515,8 @@
           if (mAttachments == null || 0 == mAttachments.getAttachmentCount()) {
               try {
                   String charEncoding = XMLUtils.getEncoding(this, msgContext);;
  -                // write the xml declaration header
  -                String incXMLDecl = (String)getProperty(SOAPMessage.WRITE_XML_DECLARATION);
  -                if(incXMLDecl == null){
  -                    incXMLDecl = "false";
  -                }
  -                mSOAPPart.writeTo(os, charEncoding, incXMLDecl);
  +                mSOAPPart.setEncoding(charEncoding);
  +                mSOAPPart.writeTo(os);
               } catch (java.io.IOException e) {
                   log.error(Messages.getMessage("javaIOException00"), e);
               }
  
  
  
  1.76      +30 -28    ws-axis/java/src/org/apache/axis/SOAPPart.java
  
  Index: SOAPPart.java
  ===================================================================
  RCS file: /home/cvs/ws-axis/java/src/org/apache/axis/SOAPPart.java,v
  retrieving revision 1.75
  retrieving revision 1.76
  diff -u -r1.75 -r1.76
  --- SOAPPart.java	3 Feb 2005 21:57:33 -0000	1.75
  +++ SOAPPart.java	10 Feb 2005 15:36:06 -0000	1.76
  @@ -49,6 +49,7 @@
   import org.xml.sax.SAXException;
   
   import javax.xml.soap.SOAPException;
  +import javax.xml.soap.SOAPMessage;
   import javax.xml.transform.Source;
   import javax.xml.transform.dom.DOMSource;
   import javax.xml.transform.stream.StreamSource;
  @@ -256,34 +257,15 @@
        *
        * @param os  the <code>java.io.OutputStream</code> to write to
        * @param charEncoding  
  -     * @param incXMLDecl  
        */
  -    public void writeTo(java.io.OutputStream os, String charEncoding,
  -                         String incXMLDecl) throws IOException {
  +    public void writeTo(java.io.OutputStream os) throws IOException {
           if ( currentForm == FORM_BYTES ) {
  -            if(incXMLDecl.equalsIgnoreCase("true")){
  -                os.write(("<?xml version=\"1.0\" encoding=\"" + charEncoding +
  -                        "\"?>").getBytes());
  -            }
               os.write((byte[])currentMessage);
           } else if ( currentForm == FORM_OPTIMIZED ) {
  -            // If the message already has XML declaration, don't write it again.
  -            // TODO: FIXME. how do we do this better?
  -            //ByteArray array = (ByteArray) currentMessage;
  -            //String content = new String(array.toByteArray(), charEncoding);
  -            //if (!content.startsWith("<?xml")) {
  -                if (incXMLDecl.equalsIgnoreCase("true")) {
  -                    os.write(("<?xml version=\"1.0\" encoding=\"" + charEncoding + "\"?>").getBytes());
  -                }
  -            //}
               ((ByteArray) currentMessage).writeTo(os);
           } else {
  -            Writer writer = new OutputStreamWriter(os,charEncoding);
  +            Writer writer = new OutputStreamWriter(os, currentEncoding);
               writer = new BufferedWriter(new PrintWriter(writer));
  -    
  -            if(incXMLDecl.equalsIgnoreCase("true")){
  -                writer.write("<?xml version=\"1.0\" encoding=\"" + charEncoding +"\"?>");
  -            }
               writeTo(writer);
               writer.flush();
           }
  @@ -295,11 +277,28 @@
        * @param writer  the <code>Writer</code> to write to
        */
       public void writeTo(Writer writer) throws IOException {
  -
  +        boolean inclXmlDecl = false;         
  +        
  +        if (msgObject.getMessageContext() != null) {    // if we have message context (JAX-RPC), write xml decl always. 
  +            inclXmlDecl = true;            
  +        } else {    // if we have no message context (SAAJ), write xml decl according to property.
  +            try {
  +                String xmlDecl = (String)msgObject.getProperty(SOAPMessage.WRITE_XML_DECLARATION);
  +                if (xmlDecl != null && xmlDecl.equals("true")) {
  +                    inclXmlDecl = true;                    
  +                }                
  +            } catch (SOAPException e) {
  +                throw new IOException(e.getMessage());
  +            }
  +        }
  +        
           if ( currentForm == FORM_FAULT ) {
               AxisFault env = (AxisFault)currentMessage;
               try {
  -                env.output(new SerializationContext(writer, getMessage().getMessageContext()));
  +                SerializationContext serContext = new SerializationContext(writer, getMessage().getMessageContext()); 
  +                serContext.setSendDecl(inclXmlDecl);
  +                serContext.setEncoding(currentEncoding);
  +                env.output(serContext);
               } catch (Exception e) {
                   log.error(Messages.getMessage("exception00"), e);
                   throw env;
  @@ -310,7 +309,10 @@
           if ( currentForm == FORM_SOAPENVELOPE ) {
               SOAPEnvelope env = (SOAPEnvelope)currentMessage;
               try {
  -                env.output(new SerializationContext(writer, getMessage().getMessageContext()));
  +                SerializationContext serContext = new SerializationContext(writer, getMessage().getMessageContext());
  +                serContext.setSendDecl(inclXmlDecl);
  +                serContext.setEncoding(currentEncoding);
  +                env.output(serContext);
               } catch (Exception e) {
                   throw AxisFault.makeFault(e);
               }
  @@ -466,7 +468,7 @@
               ByteArrayOutputStream baos = new ByteArrayOutputStream();
               BufferedOutputStream os = new BufferedOutputStream(baos);
               try {
  -                this.writeTo(os, currentEncoding, "false");
  +                this.writeTo(os);
                   os.flush();
               } catch (Exception e) {
                   throw AxisFault.makeFault(e);
  @@ -518,7 +520,7 @@
               currentEncoding = XMLUtils.getEncoding(msgObject, null);
               ByteArray array = new ByteArray();
               try {
  -                this.writeTo(array, currentEncoding, "false");
  +                this.writeTo(array);
                   array.flush();
               } catch (Exception e) {
                   throw AxisFault.makeFault(e);
  @@ -1110,12 +1112,12 @@
   
       public String getEncoding()
       {
  -        throw new UnsupportedOperationException("Not yet implemented.69");
  +        return currentEncoding;
       }
   
       public  void setEncoding(String s)
       {
  -        throw new UnsupportedOperationException("Not yet implemented.70");
  +        currentEncoding = s;
       }
   
       public  boolean getStandalone()
  
  
  
  1.13      +1 -0      ws-axis/java/test/saaj/PackageTests.java
  
  Index: PackageTests.java
  ===================================================================
  RCS file: /home/cvs/ws-axis/java/test/saaj/PackageTests.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- PackageTests.java	7 Nov 2004 11:26:37 -0000	1.12
  +++ PackageTests.java	10 Feb 2005 15:36:06 -0000	1.13
  @@ -27,6 +27,7 @@
           suite.addTestSuite(test.saaj.TestDOM.class);
           suite.addTestSuite(test.saaj.TestSOAPElement.class);
           suite.addTestSuite(test.saaj.TestText.class);
  +        suite.addTestSuite(test.saaj.TestMessageProperty.class);
           return suite;
       }
   }
  
  
  
  1.1                  ws-axis/java/test/saaj/TestMessageProperty.java
  
  Index: TestMessageProperty.java
  ===================================================================
  package test.saaj;
  
  import java.io.ByteArrayInputStream;
  import java.io.ByteArrayOutputStream;
  import java.io.InputStream;
  
  import javax.xml.soap.MessageFactory;
  import javax.xml.soap.MimeHeaders;
  import javax.xml.soap.SOAPBody;
  import javax.xml.soap.SOAPElement;
  import javax.xml.soap.SOAPMessage;
  import javax.xml.soap.Text;
  
  public class TestMessageProperty extends junit.framework.TestCase {
      public TestMessageProperty(String name) {
          super(name);
      }
  
      private static final String textValue = "\uc548\ub155\ud558\uc138\uc694";
      
      private SOAPMessage createTestMessage() throws Exception {
          MessageFactory mf = MessageFactory.newInstance();
          SOAPMessage msg = mf.createMessage();
          SOAPBody sb = msg.getSOAPBody();
          SOAPElement se1 = sb.addChildElement("echoString", "ns1", "http://tempuri.org");
          SOAPElement se2 = se1.addChildElement("string");
          se2.addTextNode(textValue);
          
          return msg;
      }
      
      
      public void testWriteXmlDeclPropertyTrue() throws Exception {
          testXmlDecl("true", "<?xml");        
      }
      
      public void testWriteXmlDeclPropertyFalse() throws Exception {
          testXmlDecl("false", "<soapenv:Envelope");
      }    
      
      public void testEncodingPropertyUTF16() throws Exception {
          testEncoding("UTF-16");               
      }        
          
      public void testEncodingPropertyUTF8() throws Exception {
          testEncoding("UTF-8");                
      }        
      
      private void testXmlDecl(String xmlDecl, String expected) throws Exception {
          SOAPMessage msg = createTestMessage();
          
          msg.setProperty(SOAPMessage.WRITE_XML_DECLARATION, xmlDecl);
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          msg.writeTo(baos);
          String msgString = new String(baos.toByteArray(), "UTF-8");
          System.out.println("msgString =" + msgString);
          assertTrue(msgString.startsWith(expected));       
      }
      
      private void testEncoding(String encoding) throws Exception {
          SOAPMessage msg = createTestMessage();
          
          msg.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
          msg.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, encoding);
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          msg.writeTo(baos);
          
          String msgString = new String(baos.toByteArray(), encoding);
          System.out.println("msgString (" + encoding + ")=" + msgString);
          assertTrue(msgString.startsWith("<?xml version=\"1.0\" encoding=\"" + encoding + "\""));
  
          ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
          SOAPMessage msg1 = createMessageFromInputStream(bais);
          SOAPElement se1 = (SOAPElement) msg1.getSOAPBody().getChildElements().next();
          SOAPElement se2 = (SOAPElement) se1.getChildElements().next();
          Text text = (Text)se2.getChildElements().next();
          
  	assertEquals(textValue, text.getValue());
      }
  
      private SOAPMessage createMessageFromInputStream(InputStream is) throws Exception {
          MessageFactory mf = MessageFactory.newInstance();
          return mf.createMessage(new MimeHeaders(), is);
      }
  }
  
  
  
  1.26      +1 -9      ws-axis/java/src/org/apache/axis/message/SAXOutputter.java
  
  Index: SAXOutputter.java
  ===================================================================
  RCS file: /home/cvs/ws-axis/java/src/org/apache/axis/message/SAXOutputter.java,v
  retrieving revision 1.25
  retrieving revision 1.26
  diff -u -r1.25 -r1.26
  --- SAXOutputter.java	18 May 2004 08:04:56 -0000	1.25
  +++ SAXOutputter.java	10 Feb 2005 15:36:07 -0000	1.26
  @@ -41,15 +41,7 @@
       }
       
       public void startDocument() throws SAXException {
  -        try {
  -			context.writeString("<?xml version=\"1.0\" encoding=\"");
  -	        String encoding = XMLUtils.getEncoding(context.getMessageContext());
  -	        context.writeString(encoding);
  -	        context.writeString("\"?>\n");
  -	        context.setSendDecl(false);
  -		} catch (IOException e) {
  -			throw new SAXException(e);
  -		}
  +        context.setSendDecl(true);
       }
       
       public void endDocument() throws SAXException {
  
  
  
  1.103     +51 -1     ws-axis/java/src/org/apache/axis/encoding/SerializationContext.java
  
  Index: SerializationContext.java
  ===================================================================
  RCS file: /home/cvs/ws-axis/java/src/org/apache/axis/encoding/SerializationContext.java,v
  retrieving revision 1.102
  retrieving revision 1.103
  diff -u -r1.102 -r1.103
  --- SerializationContext.java	8 Feb 2005 18:44:37 -0000	1.102
  +++ SerializationContext.java	10 Feb 2005 15:36:07 -0000	1.103
  @@ -41,6 +41,7 @@
   import org.apache.axis.client.Call;
   import org.apache.axis.components.logger.LogFactory;
   import org.apache.axis.components.encoding.XMLEncoder;
  +import org.apache.axis.components.encoding.XMLEncoderFactory;
   import org.apache.axis.description.OperationDesc;
   import org.apache.axis.description.TypeDesc;
   import org.apache.axis.encoding.ser.BaseSerializerFactory;
  @@ -151,6 +152,12 @@
       private QName writeXMLType;
       private XMLEncoder encoder = null;
       
  +    /** The flag whether the XML decl should be written */
  +    protected boolean startOfDocument = true;
  + 
  +    /** The encoding to serialize */
  +    private String encoding = XMLEncoderFactory.DEFAULT_ENCODING;
  +    
       class MultiRefItem {
           String id;
           QName xmlType;
  @@ -979,6 +986,13 @@
           secondLevelObjects = null;
       }
   
  +    public void writeXMLDeclaration() throws IOException {
  +        writer.write("<?xml version=\"1.0\" encoding=\"");        
  +        writer.write(encoding);
  +        writer.write("\"?>");
  +        startOfDocument = false;        
  +    }
  +    
       /**
        * Writes (using the Writer) the start tag for element QName along with the
        * indicated attributes and namespace mappings.
  @@ -994,6 +1008,10 @@
                       "[" + qName.getNamespaceURI() + "]:" + qName.getLocalPart()));
           }
   
  +        if (startOfDocument && sendXMLDecl) {
  +            writeXMLDeclaration();
  +        }
  +
           if (writingStartTag) {
               writer.write('>');
               if (pretty) writer.write('\n');
  @@ -1129,6 +1147,10 @@
       public void writeChars(char [] p1, int p2, int p3)
           throws IOException
       {
  +        if (startOfDocument && sendXMLDecl) {
  +            writeXMLDeclaration();
  +        }
  +        
           if (writingStartTag) {
               writer.write('>');
               writingStartTag = false;
  @@ -1144,6 +1166,10 @@
       public void writeString(String string)
           throws IOException
       {
  +        if (startOfDocument && sendXMLDecl) {
  +            writeXMLDeclaration();
  +        }
  +        
           if (writingStartTag) {
               writer.write('>');
               writingStartTag = false;
  @@ -1160,6 +1186,10 @@
       public void writeSafeString(String string)
           throws IOException
       {
  +        if (startOfDocument && sendXMLDecl) {
  +            writeXMLDeclaration();
  +        }
  +        
           if (writingStartTag) {
               writer.write('>');
               writingStartTag = false;
  @@ -1176,6 +1206,10 @@
       public void writeDOMElement(Element el)
           throws IOException
       {
  +        if (startOfDocument && sendXMLDecl) {
  +            writeXMLDeclaration();
  +        }
  +        
           // If el is a Text element, write the text and exit
           if (el instanceof org.apache.axis.message.Text) {            
               writeSafeString(((Text)el).getData());
  @@ -1508,8 +1542,24 @@
   
       public XMLEncoder getEncoder() {
           if(encoder == null) {
  -            encoder = XMLUtils.getXMLEncoder(msgContext);
  +            encoder = XMLUtils.getXMLEncoder(encoding);
           }
           return encoder;
       }
  +
  +    /**
  +     * get the encoding for the serialization
  +     * @return
  +     */
  +    public String getEncoding() {
  +        return encoding;
  +    }
  +
  +    /**
  +     * set the encoding for the serialization
  +     * @return
  +     */
  +    public void setEncoding(String encoding) {
  +        this.encoding = encoding;
  +    }
   }
  
  
  
  1.2       +2 -0      ws-axis/java/src/org/apache/axis/encoding/TextSerializationContext.java
  
  Index: TextSerializationContext.java
  ===================================================================
  RCS file: /home/cvs/ws-axis/java/src/org/apache/axis/encoding/TextSerializationContext.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TextSerializationContext.java	11 Nov 2004 00:00:49 -0000	1.1
  +++ TextSerializationContext.java	10 Feb 2005 15:36:07 -0000	1.2
  @@ -41,11 +41,13 @@
       public TextSerializationContext(Writer writer)
       {
           super(writer);
  +        startOfDocument = false;    // prevent XML decl for text
       }
   
       public TextSerializationContext(Writer writer, MessageContext msgContext)
       {
           super(writer, msgContext);
  +        startOfDocument = false;    // prevent XML decl for text
       }
   
       public void serialize(QName elemQName,
  
  
  
  1.102     +9 -2      ws-axis/java/src/org/apache/axis/utils/XMLUtils.java
  
  Index: XMLUtils.java
  ===================================================================
  RCS file: /home/cvs/ws-axis/java/src/org/apache/axis/utils/XMLUtils.java,v
  retrieving revision 1.101
  retrieving revision 1.102
  diff -u -r1.101 -r1.102
  --- XMLUtils.java	9 Dec 2004 13:56:38 -0000	1.101
  +++ XMLUtils.java	10 Feb 2005 15:36:07 -0000	1.102
  @@ -135,9 +135,16 @@
        * Get the current XMLEncoder
        * @return XMLEncoder
        */
  -    public static XMLEncoder getXMLEncoder(MessageContext msgContext) {
  +    public static XMLEncoder getXMLEncoder(MessageContext msgContext) {        
  +        return getXMLEncoder(getEncoding(null, msgContext));
  +    }
  +    
  +    /**
  +     * Get the XMLEncoder for specific encoding
  +     * @return XMLEncoder
  +     */
  +    public static XMLEncoder getXMLEncoder(String encoding) {
           XMLEncoder encoder = null;
  -        String encoding = getEncoding(null, msgContext);
           try {
               encoder = XMLEncoderFactory.getEncoder(encoding);
           } catch (Exception e) {