Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 250   Methods: 9
NCLOC: 142   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
TextWriter.java 37.5% 70.7% 100% 68.4%
coverage coverage
 1    /*
 2    * Licensed to the Apache Software Foundation (ASF) under one or more
 3    * contributor license agreements. See the NOTICE file distributed with
 4    * this work for additional information regarding copyright ownership.
 5    * The ASF licenses this file to You under the Apache License, Version 2.0
 6    * (the "License"); you may not use this file except in compliance with
 7    * the License. You may obtain a copy of the License at
 8    *
 9    * http://www.apache.org/licenses/LICENSE-2.0
 10    *
 11    * Unless required by applicable law or agreed to in writing, software
 12    * distributed under the License is distributed on an "AS IS" BASIS,
 13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14    * See the License for the specific language governing permissions and
 15    * limitations under the License.
 16    *
 17    * $Id: TextWriter.java 636598 2008-03-13 01:32:03Z natalia $
 18    */
 19   
 20    package org.apache.xindice.xml;
 21   
 22    import org.apache.commons.logging.Log;
 23    import org.apache.commons.logging.LogFactory;
 24    import org.apache.xindice.util.XMLUtilities;
 25   
 26    import org.w3c.dom.Attr;
 27    import org.w3c.dom.Element;
 28    import org.w3c.dom.NamedNodeMap;
 29    import org.w3c.dom.Node;
 30    import org.w3c.dom.NodeList;
 31    import org.w3c.dom.ProcessingInstruction;
 32   
 33    import java.io.BufferedWriter;
 34    import java.io.IOException;
 35    import java.io.OutputStream;
 36    import java.io.OutputStreamWriter;
 37    import java.io.StringWriter;
 38    import java.io.Writer;
 39   
 40    /**
 41    * TextWriter takes a Document, DocumentFragment, or Element and
 42    * streams it as text into an {@link OutputStream}, {@link Writer},
 43    * or a {@link String}.
 44    *
 45    * UTF-8 output encoding is assumed.
 46    *
 47    * @version $Revision: 636598 $, $Date: 2008-03-13 01:32:03 +0000 (Thu, 13 Mar 2008) $
 48    */
 49    public final class TextWriter {
 50   
 51    private static final Log log = LogFactory.getLog(TextWriter.class);
 52   
 53    private Node node;
 54   
 55   
 56  6 public TextWriter(Node node) {
 57  6 this.node = node;
 58    }
 59   
 60    /**
 61    * write writes the node to the writer as text.
 62    *
 63    * @param writer The Writer to write to
 64    * @throws IOException If input/output exception occured
 65    */
 66  1 public void write(Writer writer) throws IOException {
 67  1 write(node, writer);
 68    }
 69   
 70    /**
 71    * write writes the node to the OutputStream as text.
 72    *
 73    * @param output The OutputStream to write to
 74    * @throws IOException If input/output exception occured
 75    */
 76  1 public void write(OutputStream output) throws IOException {
 77  1 write(node, output);
 78    }
 79   
 80    /**
 81    * toString returns the node as a String.
 82    *
 83    * @return The String value
 84    */
 85  1 public String toString() {
 86  1 return toString(node);
 87    }
 88   
 89  68161 private static void writeNode(Writer writer, Node node) throws IOException {
 90  68161 short type = node.getNodeType();
 91  68160 switch (type) {
 92   
 93  3439 case Node.DOCUMENT_NODE:
 94  3439 writer.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
 95  3439 writeChildren(writer, node);
 96  3439 break;
 97   
 98  1 case Node.DOCUMENT_FRAGMENT_NODE:
 99  1 writeChildren(writer, node);
 100  1 break;
 101   
 102  0 case Node.DOCUMENT_TYPE_NODE:
 103    // TODO Implement
 104  0 if (log.isErrorEnabled()) {
 105  0 log.error("can't serialize doctype yet");
 106    }
 107    /*
 108    DocumentType d = (DocumentType)node;
 109    writer.write("<!DOCTYPE ");
 110    writer.write(node.getOwnerDocument().getDocumentElement().getNodeName());
 111    writer.write('>');
 112    */
 113  0 break;
 114   
 115  25127 case Node.ELEMENT_NODE:
 116  25127 Element e = (Element) node;
 117  25127 String n = e.getTagName();
 118   
 119  25127 writer.write('<');
 120  25127 writer.write(n);
 121   
 122  25127 NamedNodeMap a = e.getAttributes();
 123  25127 int size = a.getLength();
 124  25127 for (int i = 0; i < size; i++) {
 125  25967 Attr att = (Attr) a.item(i);
 126  25967 writer.write(' ');
 127  25967 writeNode(writer, att);
 128    }
 129   
 130  25127 if (e.hasChildNodes()) {
 131  16360 writer.write('>');
 132  16360 writeChildren(writer, node);
 133  16360 writer.write("</");
 134  16360 writer.write(n);
 135  16360 writer.write('>');
 136    } else {
 137  8767 writer.write(" />");
 138    }
 139  25127 break;
 140   
 141  25967 case Node.ATTRIBUTE_NODE:
 142  25967 Attr att = (Attr) node;
 143  25967 writer.write(att.getName());
 144  25967 writer.write("=\"");
 145  25967 writer.write(XMLUtilities.escape(att.getValue()));
 146  25967 writer.write("\"");
 147  25967 break;
 148   
 149  0 case Node.ENTITY_REFERENCE_NODE:
 150    // TODO Implement
 151  0 if (log.isErrorEnabled()) {
 152  0 log.error("can't serialize reference ref yet");
 153    }
 154  0 break;
 155   
 156  0 case Node.ENTITY_NODE:
 157    // TODO Implement
 158  0 if (log.isErrorEnabled()) {
 159  0 log.error("can't serialize entity yet");
 160    }
 161  0 break;
 162   
 163  0 case Node.NOTATION_NODE:
 164    // TODO Implement
 165  0 if (log.isErrorEnabled()) {
 166  0 log.error("can't serialize notation yet");
 167    }
 168  0 break;
 169   
 170  0 case Node.PROCESSING_INSTRUCTION_NODE:
 171  0 ProcessingInstruction pi = (ProcessingInstruction) node;
 172  0 writer.write("<?");
 173  0 writer.write(pi.getTarget());
 174  0 writer.write(" ");
 175  0 writer.write(pi.getData());
 176  0 writer.write("?>\n");
 177  0 break;
 178   
 179  13431 case Node.TEXT_NODE:
 180  13431 writer.write(XMLUtilities.escape(node.getNodeValue()));
 181  13431 break;
 182   
 183  6 case Node.CDATA_SECTION_NODE:
 184  6 writer.write("<![CDATA[");
 185  6 writer.write(node.getNodeValue());
 186  6 writer.write("]]>");
 187  6 break;
 188   
 189  189 case Node.COMMENT_NODE:
 190  189 writer.write("<!--");
 191  189 writer.write(node.getNodeValue());
 192  189 writer.write("-->");
 193  189 break;
 194   
 195  0 default:
 196  0 if (log.isWarnEnabled()) {
 197  0 log.warn("invalid node type : " + node);
 198    }
 199    }
 200    }
 201   
 202  19800 private static void writeChildren(Writer writer, Node node) throws IOException {
 203  19800 NodeList l = node.getChildNodes();
 204  19800 int size = l.getLength();
 205  19800 for (int i = 0; i < size; i++) {
 206  38108 writeNode(writer, l.item(i));
 207    }
 208    }
 209   
 210    /**
 211    * write writes the specified node to the writer as text.
 212    *
 213    * @param node The Node to write
 214    * @param writer The Writer to write to
 215    * @throws IOException If input/output exception occured
 216    */
 217  4086 public static void write(Node node, Writer writer) throws IOException {
 218  4086 BufferedWriter buf = new BufferedWriter(writer, 4096);
 219  4086 writeNode(buf, node);
 220  4085 buf.flush();
 221    }
 222   
 223    /**
 224    * write writes the specified node to the OutputStream as text.
 225    *
 226    * @param node The Node to write
 227    * @param output The OutputStream to write to
 228    * @throws IOException If input/output exception occured
 229    */
 230  3 public static void write(Node node, OutputStream output) throws IOException {
 231  3 OutputStreamWriter o = new OutputStreamWriter(output, "utf-8");
 232  3 write(node, o);
 233    }
 234   
 235    /**
 236    * toString returns the node as a String.
 237    *
 238    * @param node The Node to convert
 239    * @return The String value
 240    */
 241  4081 public static String toString(Node node) {
 242  4081 StringWriter sw = new StringWriter();
 243  4081 try {
 244  4081 write(node, sw);
 245    } catch (IOException e) {
 246    // StringWriter does not generate any IOExceptions; it can create only OutOfMemoryError
 247    }
 248  4080 return sw.toString();
 249    }
 250    }