dlr         2005/05/16 15:39:27

  Modified:    src/java/org/apache/xmlrpc XmlWriter.java
               src/test/org/apache/xmlrpc XmlWriterTest.java
  Log:
  A followup to the previous change, correcting XML prolog writing
  behavior for all overloads of Writer.write().
  
  * src/java/org/apache/xmlrpc/XmlWriter.java
    (PROLOG): A char[] representation of the concatenation of
     PROLOG_START and PROLOG_END for efficient output.
  
    (write): Added two new overloads for String and char writing, and
     modified char[] overload to leverage new PROLOG constant.
  
  * src/test/org/apache/xmlrpc/XmlWriterTest.java
    (testProlog): A new test case which exercises the lazy writing of
     the XML prolog using all overloads of Writer.write().
  
    (testBasicResults): Removed test of XML prolog.
  
  Target release: 2.0
  
  Revision  Changes    Path
  1.15      +46 -2     ws-xmlrpc/src/java/org/apache/xmlrpc/XmlWriter.java
  
  Index: XmlWriter.java
  ===================================================================
  RCS file: /home/cvs/ws-xmlrpc/src/java/org/apache/xmlrpc/XmlWriter.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -u -r1.14 -r1.15
  --- XmlWriter.java    16 May 2005 21:23:21 -0000      1.14
  +++ XmlWriter.java    16 May 2005 22:39:27 -0000      1.15
  @@ -54,6 +54,15 @@
       protected static final String GREATER_THAN_ENTITY = ">";
       protected static final String AMPERSAND_ENTITY = "&";
   
  +    private static final char[] PROLOG =
  +        new char[PROLOG_START.length() + PROLOG_END.length()];
  +    static
  +    {
  +        int len = PROLOG_START.length();
  +        PROLOG_START.getChars(0, len, PROLOG, 0);
  +        PROLOG_END.getChars(0, PROLOG_END.length(), PROLOG, len);
  +    }
  +
       /**
        * Java's name for the ISO-8859-1 encoding.
        */
  @@ -161,14 +170,49 @@
       {
           if (!hasWrittenProlog)
           {
  -            super.write(PROLOG_START, 0, PROLOG_START.length());
  -            super.write(PROLOG_END, 0, PROLOG_END.length());
  +            super.write(PROLOG, 0, PROLOG.length);
               hasWrittenProlog = true;
           }
           super.write(cbuf, off, len);
       }
   
       /**
  +     * A mostly pass-through implementation wrapping
  +     * <code>OutputStreamWriter.write()</code> which assures that the
  +     * XML prolog is written before any other data.
  +     *
  +     * @see java.io.OutputStreamWriter.write(char)
  +     */
  +    public void write(char c)
  +        throws IOException
  +    {
  +        if (!hasWrittenProlog)
  +        {
  +            super.write(PROLOG, 0, PROLOG.length);
  +            hasWrittenProlog = true;
  +        }
  +        super.write(c);
  +    }
  +
  +    /**
  +     * A mostly pass-through implementation wrapping
  +     * <code>OutputStreamWriter.write()</code> which assures that the
  +     * XML prolog is written before any other data.
  +     *
  +     * @see java.io.OutputStreamWriter.write(String, int, int)
  +     */
  +    public void write(String str, int off, int len)
  +        throws IOException
  +    {
  +        if (!hasWrittenProlog)
  +        {
  +            super.write(PROLOG, 0, PROLOG.length);
  +            hasWrittenProlog = true;
  +        }
  +        super.write(str, off, len);
  +    }
  +
  +    /**
        * Writes the XML representation of a supported Java object type.
        *
        * @param obj The <code>Object</code> to write.
  
  
  
  1.12      +33 -7     ws-xmlrpc/src/test/org/apache/xmlrpc/XmlWriterTest.java
  
  Index: XmlWriterTest.java
  ===================================================================
  RCS file: /home/cvs/ws-xmlrpc/src/test/org/apache/xmlrpc/XmlWriterTest.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -u -r1.11 -r1.12
  --- XmlWriterTest.java        16 May 2005 21:23:21 -0000      1.11
  +++ XmlWriterTest.java        16 May 2005 22:39:27 -0000      1.12
  @@ -18,6 +18,7 @@
   package org.apache.xmlrpc;
   
   import java.io.ByteArrayOutputStream;
  +import java.io.IOException;
   import java.util.Hashtable;
   
   import junit.framework.Test;
  @@ -93,6 +94,37 @@
                        XmlWriter.UTF16, writer.getEncoding());
       }
   
  +    public void testProlog()
  +        throws IOException
  +    {
  +        final String EXPECTED_PROLOG =
  +            XmlWriter.PROLOG_START + XmlWriter.PROLOG_END;
  +
  +        writer = new XmlWriter(buffer, XmlWriter.UTF8);
  +        writer.write(new char[0], 0, 0);
  +        writer.flush();
  +        assertEquals("Unexpected or missing XML prolog when writing char[]",
  +                     EXPECTED_PROLOG, buffer.toString());
  +        // Append a space using an overload, and assure non-duplication.
  +        writer.write(' ');
  +        writer.flush();
  +        assertEquals("Unexpected or missing XML prolog when writing char",
  +                     EXPECTED_PROLOG + ' ', buffer.toString());
  +
  +        buffer = new ByteArrayOutputStream();
  +        writer = new XmlWriter(buffer, XmlWriter.UTF8);
  +        writer.write("");
  +        writer.flush();
  +        assertEquals("Unexpected or missing XML prolog when writing String",
  +                     EXPECTED_PROLOG, buffer.toString());
  +        // Try again to assure it's not duplicated in the output.
  +        writer.write("");
  +        writer.flush();
  +        assertEquals("Unexpected or missing XML prolog when writing String",
  +                     EXPECTED_PROLOG, buffer.toString());
  +        
  +    }
  +
       public void testBasicResults()
           throws Exception
       {
  @@ -100,12 +132,6 @@
           {
               writer = new XmlWriter(buffer, XmlWriter.UTF8);
   
  -            writer.write(new char[0], 0, 0);
  -            writer.flush();
  -            assertEquals("Unexpected or missing XML prolog",
  -                         XmlWriter.PROLOG_START + XmlWriter.PROLOG_END,
  -                         buffer.toString());
  -
               String foobar = "foobar";
               writer.writeObject(foobar);
               writer.flush();
  
  
  

Reply via email to