vgritsenko    2004/02/23 18:16:55

  Modified:    java/src/org/apache/xindice/xml TextWriter.java
  Added:       java/tests/src/org/apache/xindice/xml TextWriterTest.java
  Log:
  Throw IOExceptions where they happen; ignore IOException in case of 
StringWriter.
  Add UnitTest.
  
  Revision  Changes    Path
  1.1                  
xml-xindice/java/tests/src/org/apache/xindice/xml/TextWriterTest.java
  
  Index: TextWriterTest.java
  ===================================================================
  /*
   * Copyright 1999-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.
   *
   * CVS $Id: TextWriterTest.java,v 1.1 2004/02/24 02:16:55 vgritsenko Exp $
   */
  
  package org.apache.xindice.xml;
  
  import org.apache.xindice.xml.dom.DOMParser;
  
  import junit.framework.TestCase;
  import org.w3c.dom.Node;
  import org.w3c.dom.DocumentFragment;
  
  import java.io.StringWriter;
  import java.io.ByteArrayOutputStream;
  
  /**
   * Tests TextWriter class
   * 
   * @version CVS $Revision: 1.1 $, $Date: 2004/02/24 02:16:55 $
   */
  public class TextWriterTest extends TestCase {
  
      private static final String FRAGMENT =
              "<element attr1=\"value1\" attr2=\"value2\">\n" +
              "<!-- Comment Node -->\n" +
              "<empty-element />\n" +
              "<![CDATA[ CDATA Node ]]>\n" +
              "Text &lt;Node&gt; &amp; &apos;Escaped &quot;Text&quot;&apos;" +
              "</element>";
  
      private static final String XML =
              "<?xml version=\"1.0\"?>\n" + FRAGMENT;
  
      private Node dom;
      private TextWriter writer;
  
      public void setUp() throws Exception {
          dom = DOMParser.toDocument(XML);
          writer = new TextWriter(dom);
      }
  
      public void testToString() throws Exception {
          assertEquals(XML, writer.toString());
      }
  
      public void testWriter() throws Exception {
          StringWriter out = new StringWriter();
          writer.write(out);
          assertEquals(XML, out.toString());
      }
  
      public void testOutputStream() throws Exception {
          ByteArrayOutputStream out = new ByteArrayOutputStream();
          writer.write(out);
          assertEquals(XML, out.toString());
      }
  
      public void testWriter1() throws Exception {
          StringWriter out = new StringWriter();
          TextWriter.write(dom, out);
          assertEquals(XML, out.toString());
      }
  
      public void testOutputStream1() throws Exception {
          ByteArrayOutputStream out = new ByteArrayOutputStream();
          TextWriter.write(dom, out);
          assertEquals(XML, out.toString());
      }
  
      public void testFragment() throws Exception {
          ByteArrayOutputStream out = new ByteArrayOutputStream();
          DocumentFragment fragment = 
dom.getOwnerDocument().createDocumentFragment();
          fragment.appendChild(dom.getOwnerDocument().getDocumentElement());
          TextWriter.write(fragment, out);
          assertEquals(FRAGMENT, out.toString());
      }
  }
  
  
  
  1.16      +22 -29    
xml-xindice/java/src/org/apache/xindice/xml/TextWriter.java
  
  Index: TextWriter.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xindice/java/src/org/apache/xindice/xml/TextWriter.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- TextWriter.java   8 Feb 2004 03:50:13 -0000       1.15
  +++ TextWriter.java   24 Feb 2004 02:16:55 -0000      1.16
  @@ -49,7 +49,7 @@
       private Node node = null;
   
   
  -    public TextWriter(Node node) throws DOMException {
  +    public TextWriter(Node node) {
           this.node = node;
       }
   
  @@ -58,7 +58,7 @@
        *
        * @param writer The Writer to write to
        */
  -    public void write(Writer writer) {
  +    public void write(Writer writer) throws IOException {
           write(node, writer);
       }
   
  @@ -67,7 +67,7 @@
        *
        * @param output The OutputStream to write to
        */
  -    public void write(OutputStream output) {
  +    public void write(OutputStream output) throws IOException {
           write(node, output);
       }
   
  @@ -181,7 +181,7 @@
                   writer.write(node.getNodeValue());
                   writer.write("-->");
                   break;
  -                
  +
               default:
                   if (log.isWarnEnabled()) {
                       log.warn("invalid node type : " + node);
  @@ -199,10 +199,11 @@
   
       private static void writeEscapedText(Writer writer, String text) throws 
IOException {
           char[] value = text.toCharArray();
  -        String outval = null;
           int start = 0;
           int len = 0;
  +
           for (int i = 0; i < value.length; i++) {
  +            String outval = null;
               switch (value[i]) {
                   case '&':
                       outval = "&amp;";
  @@ -231,9 +232,9 @@
                   writer.write(outval);
                   start = i + 1;
                   len = 0;
  -                outval = null;
               }
           }
  +
           if (len > 0) {
               writer.write(value, start, len);
           }
  @@ -245,16 +246,10 @@
        * @param node The Node to write
        * @param writer The Writer to write to
        */
  -    public static void write(Node node, Writer writer) {
  -        try {
  -            BufferedWriter buf = new BufferedWriter(writer, 4096);
  -            writeNode(buf, node);
  -            buf.flush();
  -        } catch (Exception e) {
  -            if (log.isWarnEnabled()) {
  -                log.warn("ignored exception", e);
  -            }
  -        }
  +    public static void write(Node node, Writer writer) throws IOException {
  +        BufferedWriter buf = new BufferedWriter(writer, 4096);
  +        writeNode(buf, node);
  +        buf.flush();
       }
   
       /**
  @@ -263,15 +258,9 @@
        * @param node The Node to write
        * @param output The OutputStream to write to
        */
  -    public static void write(Node node, OutputStream output) {
  -        try {
  -            OutputStreamWriter o = new OutputStreamWriter(output, "utf-8");
  -            write(node, o);
  -        } catch (Exception e) {
  -            if (log.isWarnEnabled()) {
  -                log.warn("ignored exception", e);
  -            }
  -        }
  +    public static void write(Node node, OutputStream output) throws 
IOException {
  +        OutputStreamWriter o = new OutputStreamWriter(output, "utf-8");
  +        write(node, o);
       }
   
       /**
  @@ -279,10 +268,14 @@
        *
        * @param node The Node to convert
        * @return The String value
  -     **/
  +     */
       public static String toString(Node node) {
           StringWriter sw = new StringWriter();
  -        write(node, sw);
  +        try {
  +            write(node, sw);
  +        } catch (IOException e) {
  +            // StringWriter does not generate any IOExceptions; it can 
create only OutOfMemoryError
  +        }
           return sw.toString();
       }
   }
  
  
  

Reply via email to