dlr 2005/05/16 15:41:15
Modified: src/java/org/apache/xmlrpc Tag: XMLRPC_1_2_BRANCH
XmlWriter.java
src/test/org/apache/xmlrpc Tag: XMLRPC_1_2_BRANCH
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: 1.2
Revision Changes Path
No revision
No revision
1.6.2.3 +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.6.2.2
retrieving revision 1.6.2.3
diff -u -u -r1.6.2.2 -r1.6.2.3
--- XmlWriter.java 16 May 2005 21:31:09 -0000 1.6.2.2
+++ XmlWriter.java 16 May 2005 22:41:15 -0000 1.6.2.3
@@ -90,6 +90,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.
*/
@@ -189,14 +198,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.
No revision
No revision
1.3.2.4 +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.3.2.3
retrieving revision 1.3.2.4
diff -u -u -r1.3.2.3 -r1.3.2.4
--- XmlWriterTest.java 16 May 2005 21:31:09 -0000 1.3.2.3
+++ XmlWriterTest.java 16 May 2005 22:41:15 -0000 1.3.2.4
@@ -55,6 +55,7 @@
*/
import java.io.ByteArrayOutputStream;
+import java.io.IOException;
import java.util.Hashtable;
import junit.framework.Test;
@@ -130,6 +131,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
{
@@ -137,12 +169,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();