Source code: com/eireneh/util/XMLUtil.java
1
2 package com.eireneh.util;
3
4 import java.io.*;
5
6 import org.xml.sax.*;
7 import org.w3c.dom.*;
8
9 import com.eireneh.util.StringUtil;
10
11 /**
12 * The XMLUtil class does general stuff that I need in various places
13 * to do with XML.
14 *
15 * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
16 * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
17 * Distribution Licence:<br />
18 * Project B is free software; you can redistribute it
19 * and/or modify it under the terms of the GNU General Public License,
20 * version 2 as published by the Free Software Foundation.<br />
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * General Public License for more details.<br />
25 * The License is available on the internet
26 * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
27 * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
29 * The copyright to this program is held by it's authors.
30 * </font></td></tr></table>
31 * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
32 * @see docs.Licence
33 * @author Joe Walker
34 * @version D0.I0.T0
35 */
36 public class XMLUtil
37 {
38 /**
39 * Basic constructor
40 */
41 private XMLUtil()
42 {
43 }
44
45 /**
46 * Display a Document in debug mode to the specified writer
47 * @param doc The Document to write
48 * @param out The stream to write to
49 */
50 public static void logDocument(Document doc, int level)
51 {
52 Node node = doc.getDocumentElement();
53
54 recurseNodes(node, null, level, 0);
55 }
56
57 /**
58 * Display a Document in debug mode to the specified writer
59 * @param doc The Document to write
60 * @param out The stream to write to
61 */
62 public static void logDocument(Element start, int level)
63 {
64 recurseNodes(start, null, level, 0);
65 }
66
67 /**
68 * Display a Document in debug mode to the specified writer
69 * @param doc The Document to write
70 * @param out The stream to write to
71 */
72 public static void printDocument(Document doc, PrintWriter out)
73 {
74 Node node = doc.getDocumentElement();
75
76 recurseNodes(node, out, -1, 0);
77 }
78
79 /**
80 * Display a Document in debug mode to the specified writer
81 * @param start The Element to start writing at
82 * @param out The stream to write to
83 */
84 public static void printDocument(Element start, PrintWriter out)
85 {
86 recurseNodes(start, out, -1, 0);
87 }
88
89 /**
90 * Recurse down a Doument node tree
91 * @param node The node to dig into
92 * @param out The place to write the text that we find
93 * @param depth How far down have we gone?
94 */
95 private static void recurseNodes(Node node, PrintWriter out, int level, int depth)
96 {
97 StringBuffer buff = new StringBuffer();
98
99 switch (node.getNodeType())
100 {
101 case Node.TEXT_NODE:
102 String text = node.getNodeValue().trim();
103 if (text.length() != 0)
104 {
105 buff.append(StringUtil.chain(depth*2, '.'));
106 buff.append(text);
107 }
108 break;
109
110 case Node.CDATA_SECTION_NODE:
111 buff.append(StringUtil.chain(depth*2, '.'));
112 buff.append("<![CDATA[");
113 buff.append(node.getNodeValue());
114 buff.append("]]>");
115 break;
116
117 case Node.COMMENT_NODE:
118 buff.append(StringUtil.chain(depth*2, '.'));
119 buff.append("<!-- ");
120 buff.append(node.getNodeValue());
121 buff.append(" -->");
122 break;
123
124 case Node.ELEMENT_NODE:
125 buff.append(StringUtil.chain(depth*2, '.'));
126 buff.append("<");
127 buff.append(node.getNodeName());
128
129 // The attributes
130 NamedNodeMap map = node.getAttributes();
131 if (map != null)
132 {
133 for (int i=0; i<map.getLength(); i++)
134 {
135 buff.append(" ");
136 buff.append(map.item(i).getNodeName());
137 buff.append("='");
138 buff.append(map.item(i).getNodeValue());
139 buff.append("'");
140 }
141 }
142
143 // Children
144 NodeList list = node.getChildNodes();
145
146 if (list == null || list.getLength() == 0)
147 {
148 buff.append("/>");
149 buff.append(StringUtil.getNewline());
150 }
151 else
152 {
153 buff.append(">");
154 buff.append(StringUtil.getNewline());
155
156 for (int i=0; i<list.getLength(); i++)
157 {
158 recurseNodes(list.item(i), out, level, depth+1);
159 }
160
161 buff.append(StringUtil.chain(depth*2, '.'));
162 buff.append("</");
163 buff.append(node.getNodeName());
164 buff.append(">");
165 buff.append(StringUtil.getNewline());
166 }
167 break;
168
169 default:
170 buff.append(StringUtil.chain(depth*2, ' '));
171 buff.append("Not sure what to do with node of type ");
172 buff.append(node.getNodeType());
173 }
174
175 if (out != null)
176 {
177 out.println(buff.toString());
178 out.flush();
179 }
180 else
181 {
182 log.log(level, buff.toString());
183 }
184 }
185
186 /** The log stream */
187 protected static Logger log = Logger.getLogger("util.xml");
188 }