Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 336   Methods: 6
NCLOC: 185   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DOMElementWriter.java 93.5% 81% 83.3% 84.5%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 
 55   
 package org.apache.tools.ant.util;
 56   
 
 57   
 import java.io.IOException;
 58   
 import java.io.OutputStream;
 59   
 import java.io.OutputStreamWriter;
 60   
 import java.io.Writer;
 61   
 import org.w3c.dom.Attr;
 62   
 import org.w3c.dom.Element;
 63   
 import org.w3c.dom.NamedNodeMap;
 64   
 import org.w3c.dom.Node;
 65   
 import org.w3c.dom.NodeList;
 66   
 import org.w3c.dom.Text;
 67   
 
 68   
 /**
 69   
  * Writes a DOM tree to a given Writer.
 70   
  *
 71   
  * <p>Utility class used by {@link org.apache.tools.ant.XmlLogger
 72   
  * XmlLogger} and
 73   
  * org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter
 74   
  * XMLJUnitResultFormatter}.</p>
 75   
  *
 76   
  * @author The original author of XmlLogger
 77   
  * @author Stefan Bodewig
 78   
  * @author <a href="mailto:sbailliez@apache.org">Stephane Bailliez</tt>
 79   
  */
 80   
 public class DOMElementWriter {
 81   
 
 82   
     private static String lSep = System.getProperty("line.separator");
 83   
 
 84   
     /**
 85   
      * Don't try to be too smart but at least recognize the predefined
 86   
      * entities.
 87   
      */
 88   
     protected String[] knownEntities = {"gt", "amp", "lt", "apos", "quot"};
 89   
 
 90   
 
 91   
     /**
 92   
      * Writes a DOM tree to a stream in UTF8 encoding. Note that
 93   
      * it appends the &lt;?xml version='1.0' encoding='UTF-8'?&gt;.
 94   
      * The indent number is set to 0 and a 2-space indent.
 95   
      * @param root the root element of the DOM tree.
 96   
      * @param out the outputstream to write to.
 97   
      * @throws IOException if an error happens while writing to the stream.
 98   
      */
 99  0
     public void write(Element root, OutputStream out) throws IOException {
 100  0
         Writer wri = new OutputStreamWriter(out, "UTF8");
 101  0
         wri.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
 102  0
         write(root, wri, 0, "  ");
 103  0
         wri.flush();
 104   
     }
 105   
 
 106   
     /**
 107   
      * Writes a DOM tree to a stream.
 108   
      * @param element the Root DOM element of the tree
 109   
      * @param out where to send the output
 110   
      * @param indent number of 
 111   
      * @param indentWith string that should be used to indent the corresponding tag.
 112   
      * @throws IOException if an error happens while writing to the stream.
 113   
      */
 114  205
     public void write(Element element, Writer out, int indent, 
 115   
                       String indentWith)
 116   
         throws IOException {
 117   
 
 118   
         // Write indent characters
 119  205
         for (int i = 0; i < indent; i++) {
 120  465
             out.write(indentWith);
 121   
         }
 122   
 
 123   
         // Write element
 124  205
         out.write("<");
 125  205
         out.write(element.getTagName());
 126   
 
 127   
         // Write attributes
 128  205
         NamedNodeMap attrs = element.getAttributes();
 129  205
         for (int i = 0; i < attrs.getLength(); i++) {
 130  402
             Attr attr = (Attr) attrs.item(i);
 131  402
             out.write(" ");
 132  402
             out.write(attr.getName());
 133  402
             out.write("=\"");
 134  402
             out.write(encode(attr.getValue()));
 135  402
             out.write("\"");
 136   
         }
 137  205
         out.write(">");
 138   
 
 139   
         // Write child elements and text
 140  205
         boolean hasChildren = false;
 141  205
         NodeList children = element.getChildNodes();
 142  205
         for (int i = 0; i < children.getLength(); i++) {
 143  355
             Node child = children.item(i);
 144   
 
 145  355
             switch (child.getNodeType()) {
 146   
                 
 147   
             case Node.ELEMENT_NODE:
 148  203
                 if (!hasChildren) {
 149  8
                     out.write(lSep);
 150  8
                     hasChildren = true;
 151   
                 }
 152  203
                 write((Element) child, out, indent + 1, indentWith);
 153  203
                 break;
 154   
                 
 155   
             case Node.TEXT_NODE:
 156  150
                 out.write(encode(child.getNodeValue()));
 157  150
                 break;
 158   
                 
 159   
             case Node.CDATA_SECTION_NODE:
 160  2
                 out.write("<![CDATA[");
 161  2
                 out.write(encodedata(((Text) child).getData()));
 162  2
                 out.write("]]>");
 163  2
                 break;
 164   
 
 165   
             case Node.ENTITY_REFERENCE_NODE:
 166  0
                 out.write('&');
 167  0
                 out.write(child.getNodeName());
 168  0
                 out.write(';');
 169  0
                 break;
 170   
 
 171   
             case Node.PROCESSING_INSTRUCTION_NODE:
 172  0
                 out.write("<?");
 173  0
                 out.write(child.getNodeName());
 174  0
                 String data = child.getNodeValue();
 175  0
                 if (data != null && data.length() > 0) {
 176  0
                     out.write(' ');
 177  0
                     out.write(data);
 178   
                 }
 179  0
                 out.write("?>");
 180  0
                 break;
 181   
             }
 182   
         }
 183   
 
 184   
         // If we had child elements, we need to indent before we close
 185   
         // the element, otherwise we're on the same line and don't need
 186   
         // to indent
 187  205
         if (hasChildren) {
 188  8
             for (int i = 0; i < indent; i++) {
 189  10
                 out.write(indentWith);
 190   
             }
 191   
         }
 192   
 
 193   
         // Write element close
 194  205
         out.write("</");
 195  205
         out.write(element.getTagName());
 196  205
         out.write(">");
 197  205
         out.write(lSep);
 198  205
         out.flush();
 199   
     }
 200   
 
 201   
     /**
 202   
      * Escape &lt;, &gt; &amp; &apos;, &quot; as their entities and
 203   
      * drop characters that are illegal in XML documents.
 204   
      */
 205  567
     public String encode(String value) {
 206  567
         StringBuffer sb = new StringBuffer();
 207  567
         int len = value.length();
 208  567
         for (int i = 0; i < len; i++) {
 209  10939
             char c = value.charAt(i);
 210  10939
             switch (c) {
 211   
             case '<':
 212  1
                 sb.append("&lt;");
 213  1
                 break;
 214   
             case '>':
 215  0
                 sb.append("&gt;");
 216  0
                 break;
 217   
             case '\'':
 218  0
                 sb.append("&apos;");
 219  0
                 break;
 220   
             case '\"':
 221  1
                 sb.append("&quot;");
 222  1
                 break;
 223   
             case '&':
 224  11
                 int nextSemi = value.indexOf(";", i);
 225  11
                 if (nextSemi < 0
 226   
                     || !isReference(value.substring(i, nextSemi + 1))) {
 227  3
                     sb.append("&amp;");
 228   
                 } else {
 229  8
                     sb.append('&');
 230   
                 }
 231  11
                 break;
 232   
             default:
 233  10926
                 if (isLegalCharacter(c)) {
 234  10925
                     sb.append(c);
 235   
                 }
 236  10926
                 break;
 237   
             }
 238   
         }
 239  567
         return sb.substring(0);
 240   
     }
 241   
 
 242   
     /**
 243   
      * Drop characters that are illegal in XML documents.
 244   
      *
 245   
      * <p>Also ensure that we are not including an <code>]]&gt;</code>
 246   
      * marker by replacing that sequence with
 247   
      * <code>&amp;#x5d;&amp;#x5d;&amp;gt;</code>.</p>
 248   
      *
 249   
      * <p>See XML 1.0 2.2 <a
 250   
      * href="http://www.w3.org/TR/1998/REC-xml-19980210#charsets">http://www.w3.org/TR/1998/REC-xml-19980210#charsets</a> and
 251   
      * 2.7 <a
 252   
      * href="http://www.w3.org/TR/1998/REC-xml-19980210#sec-cdata-sect">http://www.w3.org/TR/1998/REC-xml-19980210#sec-cdata-sect</a>.</p>
 253   
 
 254   
      */
 255  11
     public String encodedata(final String value) {
 256  11
         StringBuffer sb = new StringBuffer();
 257  11
         for (int i = 0; i < value.length(); ++i) {
 258  410
             char c = value.charAt(i);
 259  410
             if (isLegalCharacter(c)) {
 260  409
                 sb.append(c);
 261   
             }
 262   
         }
 263   
 
 264  11
         String result = sb.substring(0);
 265  11
         int cdEnd = result.indexOf("]]>");
 266  11
         while (cdEnd != -1) {
 267  6
             sb.setLength(cdEnd);
 268  6
             sb.append("&#x5d;&#x5d;&gt;")
 269   
                 .append(result.substring(cdEnd+3));
 270  6
             result = sb.substring(0);
 271  6
             cdEnd = result.indexOf("]]>");
 272   
         }
 273   
         
 274  11
         return result;
 275   
     }
 276   
 
 277   
     /**
 278   
      * Is the given argument a character or entity reference?
 279   
      */
 280  20
     public boolean isReference(String ent) {
 281  20
         if (!(ent.charAt(0) == '&') || !ent.endsWith(";")) {
 282  2
             return false;
 283   
         }
 284   
 
 285  18
         if (ent.charAt(1) == '#') {
 286  8
             if (ent.charAt(2) == 'x') {
 287  4
                 try {
 288  4
                     Integer.parseInt(ent.substring(3, ent.length() - 1), 16);
 289  4
                     return true;
 290   
                 } catch (NumberFormatException nfe) {
 291  0
                     return false;
 292   
                 }
 293   
             } else {
 294  4
                 try {
 295  4
                     Integer.parseInt(ent.substring(2, ent.length() - 1));
 296  2
                     return true;
 297   
                 } catch (NumberFormatException nfe) {
 298  2
                     return false;
 299   
                 }
 300   
             }
 301   
         }
 302   
 
 303  10
         String name = ent.substring(1, ent.length() - 1);
 304  30
         for (int i = 0; i < knownEntities.length; i++) {
 305  30
             if (name.equals(knownEntities[i])) {
 306  10
                 return true;
 307   
             }
 308   
         }
 309  0
         return false;
 310   
     }
 311   
 
 312   
     /**
 313   
      * Is the given character allowed inside an XML document?
 314   
      *
 315   
      * <p>See XML 1.0 2.2 <a
 316   
      * href="http://www.w3.org/TR/1998/REC-xml-19980210#charsets">
 317   
      * http://www.w3.org/TR/1998/REC-xml-19980210#charsets</a>.</p>
 318   
      *
 319   
      * @since 1.10, Ant 1.5
 320   
      */
 321  11348
     public boolean isLegalCharacter(char c) {
 322  11348
         if (c == 0x9 || c == 0xA || c == 0xD) {
 323  177
             return true;
 324  11171
         } else if (c < 0x20) {
 325  4
             return false;
 326  11167
         } else if (c <= 0xD7FF) {
 327  11162
             return true;
 328  5
         } else if (c < 0xE000) {
 329  2
             return false;
 330  3
         } else if (c <= 0xFFFD) {
 331  2
             return true;
 332   
         }
 333  1
         return false;
 334   
     }
 335   
 }
 336