2
* The Apache Software License, Version 1.1
4
* Copyright (c) 2000 The Apache Software Foundation. All rights
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions
11
* 1. Redistributions of source code must retain the above copyright
12
* notice, this list of conditions and the following disclaimer.
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
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.
26
* 4. The names "The Jakarta Project", "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.
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.
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
47
* ====================================================================
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/>.
55
package org.apache.tools.ant.util;
61
* Writes a DOM tree to a given Writer.
63
* <p>Utility class used by {@link org.apache.tools.ant.XmlLogger
65
* org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter
66
* XMLJUnitResultFormatter}.</p>
68
* @author The original author of XmlLogger
69
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
70
* @author <a href="mailto:sbailliez@imediation.com">Stephane Bailliez</tt>
72
public class DOMElementWriter {
74
private static String lSep = System.getProperty("line.separator");
75
private StringBuffer sb = new StringBuffer();
78
* Don't try to be too smart but at least recognize the predefined
81
protected String[] knownEntities = {"gt", "amp", "lt", "apos", "quot"};
84
* Writes a DOM tree to a stream.
86
* @param element the Root DOM element of the tree
87
* @param out where to send the output
88
* @param indent number of
89
* @param indentWith strings,
90
* that should be used to indent the corresponding tag.
92
public void write(Element element, Writer out, int indent,
96
// Write indent characters
97
for (int i = 0; i < indent; i++) {
98
out.write(indentWith);
103
out.write(element.getTagName());
106
NamedNodeMap attrs = element.getAttributes();
107
for (int i = 0; i < attrs.getLength(); i++) {
108
Attr attr = (Attr) attrs.item(i);
110
out.write(attr.getName());
112
out.write(encode(attr.getValue()));
117
// Write child elements and text
118
boolean hasChildren = false;
119
NodeList children = element.getChildNodes();
120
for (int i = 0; i < children.getLength(); i++) {
121
Node child = children.item(i);
123
switch (child.getNodeType()) {
125
case Node.ELEMENT_NODE:
130
write((Element)child, out, indent + 1, indentWith);
134
out.write(encode(child.getNodeValue()));
137
case Node.CDATA_SECTION_NODE:
138
out.write("<![CDATA[");
139
out.write(((Text)child).getData());
143
case Node.ENTITY_REFERENCE_NODE:
145
out.write(child.getNodeName());
149
case Node.PROCESSING_INSTRUCTION_NODE:
151
out.write(child.getNodeName());
152
String data = child.getNodeValue();
153
if ( data != null && data.length() > 0 ) {
162
// If we had child elements, we need to indent before we close
163
// the element, otherwise we're on the same line and don't need
166
for (int i = 0; i < indent; i++) {
167
out.write(indentWith);
171
// Write element close
173
out.write(element.getTagName());
180
* Escape <, > & ' and " as their entities.
182
public String encode(String value) {
184
for (int i=0; i<value.length(); i++) {
185
char c = value.charAt(i);
200
int nextSemi = value.indexOf(";", i);
202
|| !isReference(value.substring(i, nextSemi+1))) {
213
return sb.toString();
217
* Is the given argument a character or entity reference?
219
public boolean isReference(String ent) {
220
if (!(ent.charAt(0) == '&') || !ent.endsWith(";")) {
224
if (ent.charAt(1) == '#') {
225
if (ent.charAt(2) == 'x') {
227
Integer.parseInt(ent.substring(3, ent.length()-1), 16);
229
} catch (NumberFormatException nfe) {
234
Integer.parseInt(ent.substring(2, ent.length()-1));
236
} catch (NumberFormatException nfe) {
242
String name = ent.substring(1, ent.length() - 1);
243
for (int i=0; i<knownEntities.length; i++) {
244
if (name.equals(knownEntities[i])) {