1 /*------------------------------------------------------------------------------
2 Name: XmlUtility.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: Demo code for a svg client using batik
6 Version: $Id: XmlUtility.java 16476 2007-09-06 22:36:52Z laghi $
7 ------------------------------------------------------------------------------*/
8 package javaclients.svg.batik;
9
10 import java.io.Writer;
11 import java.io.PrintWriter;
12 import java.io.StringWriter;
13 import java.io.IOException;
14
15 import javax.xml.parsers.DocumentBuilderFactory;
16 import javax.xml.parsers.DocumentBuilder;
17 import javax.xml.parsers.ParserConfigurationException;
18
19 import org.w3c.dom.Document;
20 import org.w3c.dom.Node;
21 import org.w3c.dom.Attr;
22 import org.w3c.dom.NamedNodeMap;
23 import org.xml.sax.InputSource;
24 import org.xml.sax.SAXException;
25
26
27 /**
28 * Used to retrieve a default Document (of the underling implementation)
29 * @author $Author: laghi $ (michele@laghi.eu)
30 */
31 public class XmlUtility {
32
33 private static final boolean fCanonical = false;
34
35 public static Document getDocument () throws ParserConfigurationException
36 {
37 return
38 DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
39 }
40
41
42 public static Document parse(InputSource inputSource)
43 throws ParserConfigurationException, IOException, SAXException
44 {
45 return
46 DocumentBuilderFactory.newInstance().
47 newDocumentBuilder().parse(inputSource);
48 }
49
50
51 public static void write (Node node, Writer writer)
52 {
53 PrintWriter printWriter = new PrintWriter(writer);
54 write(node, printWriter);
55 }
56
57
58 public static String write (Node node)
59 {
60 StringWriter writer = new StringWriter();
61 write(node, writer);
62 return writer.toString();
63 }
64
65
66 public static void write (Node node, PrintWriter fOut)
67 {
68 // is there anything to do?
69 if (node == null) {
70 return;
71 }
72
73 short type = node.getNodeType();
74 switch (type) {
75 case Node.DOCUMENT_NODE: {
76 if (!fCanonical) {
77 fOut.println("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>");
78 fOut.flush();
79 }
80
81 Document document = (Document)node;
82 write(document.getDocumentElement(), fOut);
83 break;
84 }
85
86 case Node.ELEMENT_NODE: {
87 fOut.print('<');
88 fOut.print(node.getNodeName());
89 Attr attrs[] = sortAttributes(node.getAttributes());
90 for (int i = 0; i < attrs.length; i++) {
91 Attr attr = attrs[i];
92 fOut.print(' ');
93 fOut.print(attr.getNodeName());
94 fOut.print("=\"");
95 normalizeAndPrint(attr.getNodeValue(), fOut);
96 fOut.print('"');
97 }
98 fOut.print('>');
99 fOut.flush();
100
101 Node child = node.getFirstChild();
102 while (child != null) {
103 write(child, fOut);
104 child = child.getNextSibling();
105 }
106 break;
107 }
108
109 case Node.ENTITY_REFERENCE_NODE: {
110 if (fCanonical) {
111 Node child = node.getFirstChild();
112 while (child != null) {
113 write(child, fOut);
114 child = child.getNextSibling();
115 }
116 }
117 else {
118 fOut.print('&');
119 fOut.print(node.getNodeName());
120 fOut.print(';');
121 fOut.flush();
122 }
123 break;
124 }
125
126 case Node.CDATA_SECTION_NODE: {
127 if (fCanonical) {
128 normalizeAndPrint(node.getNodeValue(), fOut);
129 }
130 else {
131 fOut.print("<![CDATA[");
132 fOut.print(node.getNodeValue());
133 fOut.print("]]>");
134 }
135 fOut.flush();
136 break;
137 }
138
139 case Node.TEXT_NODE: {
140 normalizeAndPrint(node.getNodeValue(),fOut);
141 fOut.flush();
142 break;
143 }
144
145 case Node.PROCESSING_INSTRUCTION_NODE: {
146 fOut.print("<?");
147 fOut.print(node.getNodeName());
148 String data = node.getNodeValue();
149 if (data != null && data.length() > 0) {
150 fOut.print(' ');
151 fOut.print(data);
152 }
153 fOut.println("?>");
154 fOut.flush();
155 break;
156 }
157 }
158
159 if (type == Node.ELEMENT_NODE) {
160 fOut.print("</");
161 fOut.print(node.getNodeName());
162 fOut.print('>');
163 fOut.flush();
164 }
165
166 } // write(Node)
167
168
169 /** Returns a sorted list of attributes. */
170 protected static Attr[] sortAttributes(NamedNodeMap attrs) {
171
172 int len = (attrs != null) ? attrs.getLength() : 0;
173 Attr array[] = new Attr[len];
174 for (int i = 0; i < len; i++) {
175 array[i] = (Attr)attrs.item(i);
176 }
177 for (int i = 0; i < len - 1; i++) {
178 String name = array[i].getNodeName();
179 int index = i;
180 for (int j = i + 1; j < len; j++) {
181 String curName = array[j].getNodeName();
182 if (curName.compareTo(name) < 0) {
183 name = curName;
184 index = j;
185 }
186 }
187 if (index != i) {
188 Attr temp = array[i];
189 array[i] = array[index];
190 array[index] = temp;
191 }
192 }
193
194 return array;
195
196 } // sortAttributes(NamedNodeMap):Attr[]
197
198 //
199 // Protected methods
200 //
201
202 /** Normalizes and prints the given string. */
203 protected static void normalizeAndPrint(String s, PrintWriter fOut) {
204
205 int len = (s != null) ? s.length() : 0;
206 for (int i = 0; i < len; i++) {
207 char c = s.charAt(i);
208 normalizeAndPrint(c, fOut);
209 }
210
211 } // normalizeAndPrint(String)
212
213 /** Normalizes and print the given character. */
214 protected static void normalizeAndPrint(char c, PrintWriter fOut) {
215
216 switch (c) {
217 case '<': {
218 fOut.print("<");
219 break;
220 }
221 case '>': {
222 fOut.print(">");
223 break;
224 }
225 case '&': {
226 fOut.print("&");
227 break;
228 }
229 case '"': {
230 fOut.print(""");
231 break;
232 }
233 case '\r':
234 case '\n': {
235 if (fCanonical) {
236 fOut.print("&#");
237 fOut.print(Integer.toString(c));
238 fOut.print(';');
239 break;
240 }
241 // else, default print char
242 }
243 default: {
244 fOut.print(c);
245 }
246 }
247
248 } // normalizeAndPrint(char)
249
250 }
syntax highlighted by Code2HTML, v. 0.9.1