You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ni...@apache.org on 2010/10/05 19:56:05 UTC

svn commit: r1004737 - in /commons/proper/io/trunk/src: main/java/org/apache/commons/io/output/XmlStreamWriter.java test/java/org/apache/commons/io/output/XmlStreamWriterTest.java

Author: niallp
Date: Tue Oct  5 17:56:04 2010
New Revision: 1004737

URL: http://svn.apache.org/viewvc?rev=1004737&view=rev
Log:
IO-162 Allow the default encoding to be configurable

Modified:
    commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/XmlStreamWriter.java
    commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/XmlStreamWriterTest.java

Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/XmlStreamWriter.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/XmlStreamWriter.java?rev=1004737&r1=1004736&r2=1004737&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/XmlStreamWriter.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/XmlStreamWriter.java Tue Oct  5 17:56:04 2010
@@ -41,32 +41,61 @@ import org.apache.commons.io.input.XmlSt
 public class XmlStreamWriter extends Writer {
     private static final int BUFFER_SIZE = 4096;
 
-    private StringWriter xmlPrologWriter = new StringWriter(BUFFER_SIZE);
+    private final OutputStream out;
+
+    private final String defaultEncoding;
 
-    private OutputStream out;
+    private StringWriter xmlPrologWriter = new StringWriter(BUFFER_SIZE);
 
     private Writer writer;
 
     private String encoding;
 
     /**
-     * Construct an new XML stream writer for the specified output stream.
+     * Construct an new XML stream writer for the specified output stream
+     * with a default encoding of UTF-8.
      *
      * @param out The output stream
      */
     public XmlStreamWriter(OutputStream out) {
+        this(out, null);
+    }
+
+    /**
+     * Construct an new XML stream writer for the specified output stream
+     * with the specified default encoding.
+     *
+     * @param out The output stream
+     * @param defaultEncoding The default encoding if not encoding could be detected
+     */
+    public XmlStreamWriter(OutputStream out, String defaultEncoding) {
         this.out = out;
+        this.defaultEncoding = (defaultEncoding != null ? defaultEncoding : "UTF-8");
     }
 
     /**
-     * Construct an new XML stream writer for the specified file.
+     * Construct an new XML stream writer for the specified file
+     * with a default encoding of UTF-8.
      * 
      * @param file The file to write to
      * @throws FileNotFoundException if there is an error creating or
      * opening the file
      */
     public XmlStreamWriter(File file) throws FileNotFoundException {
-        this(new FileOutputStream(file));
+        this(file, null);
+    }
+
+    /**
+     * Construct an new XML stream writer for the specified file
+     * with the specified default encoding.
+     * 
+     * @param file The file to write to
+     * @param defaultEncoding The default encoding if not encoding could be detected
+     * @throws FileNotFoundException if there is an error creating or
+     * opening the file
+     */
+    public XmlStreamWriter(File file, String defaultEncoding) throws FileNotFoundException {
+        this(new FileOutputStream(file), defaultEncoding);
     }
 
     /**
@@ -79,6 +108,15 @@ public class XmlStreamWriter extends Wri
     }
 
     /**
+     * Return the default encoding.
+     *
+     * @return the default encoding
+     */
+    public String getDefaultEncoding() {
+        return defaultEncoding;
+    }
+
+    /**
      * Close the underlying writer.
      *
      * @throws IOException if an error occurs closing the underlying writer
@@ -86,7 +124,7 @@ public class XmlStreamWriter extends Wri
     @Override
     public void close() throws IOException {
         if (writer == null) {
-            encoding = "UTF-8";
+            encoding = defaultEncoding;
             writer = new OutputStreamWriter(out, encoding);
             writer.write(xmlPrologWriter.toString());
         }
@@ -137,18 +175,18 @@ public class XmlStreamWriter extends Wri
                     } else {
                         // no encoding found in XML prolog: using default
                         // encoding
-                        encoding = "UTF-8";
+                        encoding = defaultEncoding;
                     }
                 } else {
                     if (xmlProlog.length() >= BUFFER_SIZE) {
                         // no encoding found in first characters: using default
                         // encoding
-                        encoding = "UTF-8";
+                        encoding = defaultEncoding;
                     }
                 }
             } else {
                 // no XML prolog: using default encoding
-                encoding = "UTF-8";
+                encoding = defaultEncoding;
             }
             if (encoding != null) {
                 // encoding has been chosen: let's do it

Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/XmlStreamWriterTest.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/XmlStreamWriterTest.java?rev=1004737&r1=1004736&r2=1004737&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/XmlStreamWriterTest.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/XmlStreamWriterTest.java Tue Oct  5 17:56:04 2010
@@ -18,6 +18,7 @@ package org.apache.commons.io.output;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.util.Arrays;
 
 import junit.framework.TestCase;
 
@@ -47,27 +48,36 @@ public class XmlStreamWriterTest extends
         return xml;
     }
 
-    private static void checkXmlContent(String xml, String encoding)
+    private static void checkXmlContent(String xml, String encoding, String defaultEncoding)
             throws IOException {
         ByteArrayOutputStream out = new ByteArrayOutputStream();
-        XmlStreamWriter writer = new XmlStreamWriter(out);
+        XmlStreamWriter writer = new XmlStreamWriter(out, defaultEncoding);
         writer.write(xml);
         writer.close();
         byte[] xmlContent = out.toByteArray();
-        String result = new String(xmlContent, encoding);
-        assertEquals(xml, result);
+        assertEquals(encoding, writer.getEncoding());
+        assertTrue(Arrays.equals(xml.getBytes(encoding), xmlContent));
+
     }
 
     private static void checkXmlWriter(String text, String encoding)
             throws IOException {
+        checkXmlWriter(text, encoding, null);
+    }
+
+    private static void checkXmlWriter(String text, String encoding, String defaultEncoding)
+            throws IOException {
         String xml = createXmlContent(text, encoding);
-        String effectiveEncoding = (encoding == null) ? "UTF-8" : encoding;
-        checkXmlContent(xml, effectiveEncoding);
+        String effectiveEncoding = encoding;
+        if (effectiveEncoding == null) {
+            effectiveEncoding = (defaultEncoding == null ? "UTF-8" : defaultEncoding);
+        }
+        checkXmlContent(xml, effectiveEncoding, defaultEncoding);
     }
 
     public void testNoXmlHeader() throws IOException {
         String xml = "<text>text with no XML header</text>";
-        checkXmlContent(xml, "UTF-8");
+        checkXmlContent(xml, "UTF-8", null);
     }
 
     public void testEmpty() throws IOException {
@@ -82,7 +92,11 @@ public class XmlStreamWriterTest extends
     }
 
     public void testDefaultEncoding() throws IOException {
-        checkXmlWriter(TEXT_UNICODE, null);
+        checkXmlWriter(TEXT_UNICODE, null, null);
+        checkXmlWriter(TEXT_UNICODE, null, "UTF-8");
+        checkXmlWriter(TEXT_UNICODE, null, "UTF-16");
+        checkXmlWriter(TEXT_UNICODE, null, "UTF-16BE");
+        checkXmlWriter(TEXT_UNICODE, null, "ISO-8859-1");
     }
 
     public void testUTF8Encoding() throws IOException {