1 17 18 19 20 package org.apache.lenya.xml; 21 22 import java.io.BufferedWriter ; 23 import java.io.FileNotFoundException ; 24 import java.io.OutputStream ; 25 import java.io.OutputStreamWriter ; 26 import java.io.PrintWriter ; 27 import java.util.Vector ; 28 29 import org.apache.log4j.Category; 30 import org.w3c.dom.Document ; 31 import org.w3c.dom.Element ; 32 import org.w3c.dom.NamedNodeMap ; 33 import org.w3c.dom.Node ; 34 import org.w3c.dom.NodeList ; 35 36 37 41 public class DOMWriter { 42 static Category log = Category.getInstance(DOMWriter.class); 43 PrintWriter out = null; 44 String encoding = null; 45 46 51 public DOMWriter(PrintWriter out) { 52 this.out = out; 53 } 54 55 61 public DOMWriter(PrintWriter out, String encoding) { 62 this(out); 63 this.encoding = encoding; 64 } 65 66 73 public DOMWriter(OutputStream os) throws Exception { 74 this(os, "utf-8"); 75 } 76 77 85 public DOMWriter(OutputStream os, String encoding) 86 throws Exception { 87 out = new PrintWriter (new BufferedWriter ( 88 new OutputStreamWriter (os, XMLEncToJavaEnc.getJava(encoding)))); 89 this.encoding = encoding; 90 } 91 92 97 public static void main(String [] args) { 98 if (args.length != 1) { 99 System.err.println("Usage: java org.apache.lenya.xml.DOMWriter \"file.xml\""); 100 System.err.println("Description: Reads \"file.xml\" and writes it to standard output"); 101 102 return; 103 } 104 105 DOMParserFactory dpf = new DOMParserFactory(); 106 Document document = null; 107 108 try { 109 document = dpf.getDocument(args[0]); 110 } catch (FileNotFoundException e) { 111 System.err.println("No such file: " + e.getMessage()); 112 113 return; 114 } catch (Exception e) { 115 System.err.println(e.getMessage()); 116 117 return; 118 } 119 120 try { 121 new DOMWriter(System.out, "iso-8859-1").printWithoutFormatting(document); 122 } catch (Exception e) { 123 System.err.println(e.getMessage()); 124 125 return; 126 } 127 128 log.fatal("\n"); 129 130 log.fatal(".main(): System.exit(0)"); 131 System.exit(0); 132 133 new DOMWriter(new PrintWriter (System.out)).print(document); 134 System.out.print("\n"); 135 136 XPointerFactory xpf = new XPointerFactory(); 137 138 try { 139 Vector nodes = xpf.select(document.getDocumentElement(), 140 "xpointer(/Example/People/Person/City)"); 141 String [] values = xpf.getNodeValues(nodes); 142 143 for (int i = 0; i < values.length; i++) { 144 System.out.println(values[i]); 145 } 146 147 Document doc = dpf.getDocument(); 148 Element root = dpf.newElementNode(doc, "Root"); 149 150 for (int i = 0; i < values.length; i++) { 152 root.appendChild(dpf.newTextNode(doc, values[i])); 153 } 154 155 doc.appendChild(root); 156 new DOMWriter(new PrintWriter (System.out)).print(doc); 157 System.out.print("\n"); 158 } catch (Exception e) { 159 System.err.println(e); 160 } 161 } 162 163 168 public void print(Node node) { 169 if (node == null) { 170 return; 171 } 172 173 short type = node.getNodeType(); 174 175 switch (type) { 176 case Node.DOCUMENT_NODE: { 177 out.print("<?xml version=\"1.0\""); 178 179 if (encoding != null) { 180 out.print(" encoding=\"" + encoding + "\""); 181 } 182 183 out.print("?>\n\n"); 184 print(((Document ) node).getDocumentElement()); 185 out.flush(); 186 187 break; 188 } 189 190 case Node.ELEMENT_NODE: { 191 out.print("<" + node.getNodeName()); 192 193 NamedNodeMap attributes = node.getAttributes(); 194 195 for (int i = 0; i < attributes.getLength(); i++) { 196 Node attribute = attributes.item(i); 197 out.print(" " + attribute.getNodeName() + "=\"" + 198 Normalize.normalize(attribute.getNodeValue()) + "\""); 199 } 200 201 if (node.hasChildNodes()) { 202 out.print(">"); 203 204 NodeList children = node.getChildNodes(); 205 206 for (int i = 0; i < children.getLength(); i++) { 207 print(children.item(i)); 208 } 209 210 out.print("</" + node.getNodeName() + ">"); 211 } else { 212 out.print("/>"); 213 } 214 215 break; 216 } 217 218 case Node.TEXT_NODE: { 219 out.print(Normalize.normalize(node.getNodeValue())); 220 221 break; 222 } 223 224 case Node.COMMENT_NODE: { 225 out.print("<!--" + node.getNodeValue() + "-->"); 226 227 break; 228 } 229 230 default: { 231 System.err.println(this.getClass().getName() + ".print(): Node type not implemented: " + 232 type); 233 234 break; 235 } 236 } 237 } 238 239 244 public void printWithoutFormatting(Node node) { 245 if (node == null) { 246 return; 247 } 248 249 short type = node.getNodeType(); 250 251 switch (type) { 252 case Node.DOCUMENT_NODE: { 253 out.print("<?xml version=\"1.0\""); 254 255 if (encoding != null) { 256 out.print(" encoding=\"" + encoding + "\""); 257 } 258 259 out.print("?>\n\n"); 260 261 Element root = ((Document ) node).getDocumentElement(); 262 root.setAttribute("xmlns:xlink", XLink.XLINK_NAMESPACE); 263 printWithoutFormatting(root); 264 out.flush(); 265 266 break; 267 } 268 269 case Node.ELEMENT_NODE: { 270 out.print("<" + node.getNodeName()); 271 272 NamedNodeMap attributes = node.getAttributes(); 273 274 for (int i = 0; i < attributes.getLength(); i++) { 275 Node attribute = attributes.item(i); 276 out.print(" " + attribute.getNodeName() + "=\"" + 277 replaceSpecialCharacters(attribute.getNodeValue()) + "\""); 278 } 279 280 if (node.hasChildNodes()) { 281 out.print(">"); 282 283 NodeList children = node.getChildNodes(); 284 285 for (int i = 0; i < children.getLength(); i++) { 286 printWithoutFormatting(children.item(i)); 287 } 288 289 out.print("</" + node.getNodeName() + ">"); 290 } else { 291 out.print("/>"); 292 } 293 294 break; 295 } 296 297 case Node.TEXT_NODE: { 298 out.print(replaceSpecialCharacters(node.getNodeValue())); 299 300 break; 301 } 302 303 case Node.COMMENT_NODE: { 304 out.print("<!--" + node.getNodeValue() + "-->"); 305 306 break; 307 } 308 309 default: { 310 System.err.println(this.getClass().getName() + ".print(): Node type not implemented: " + 311 type); 312 313 break; 314 } 315 } 316 } 317 318 325 public String replaceSpecialCharacters(String s) { 326 StringBuffer str = new StringBuffer (); 327 328 int len = (s != null) ? s.length() : 0; 329 330 for (int i = 0; i < len; i++) { 331 char ch = s.charAt(i); 332 333 switch (ch) { 334 case '<': { 335 str.append("<"); 336 337 break; 338 } 339 340 case '>': { 341 str.append(">"); 342 343 break; 344 } 345 346 case '&': { 347 str.append("&"); 348 349 break; 350 } 351 352 default: 353 str.append(ch); 354 } 355 } 356 357 return (str.toString()); 358 } 359 } 360 | Popular Tags |