KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > webdav > XMLUtility


1 /*
2  * (C) Copyright IBM Corp. 2000 All rights reserved.
3  *
4  * The program is provided "AS IS" without any warranty express or
5  * implied, including the warranty of non-infringement and the implied
6  * warranties of merchantibility and fitness for a particular purpose.
7  * IBM will not be liable for any damages suffered by you as a result
8  * of using the Program. In no event will IBM be liable for any
9  * special, indirect or consequential damages or lost profits even if
10  * IBM has been advised of the possibility of their occurrence. IBM
11  * will not be liable for any third party claims against you.
12  *
13  * Portions Copyright (C) Simulacra Media Ltd, 2004.
14  */

15
16 package com.ibm.webdav;
17
18 import org.w3c.dom.*;
19
20
21 /**
22  * Utility class for serialising DOM nodes.
23  *
24  * @author Michael Bell
25  * @version $Revision: 1.1 $
26  *
27  */

28 public class XMLUtility {
29     public XMLUtility() {
30     }
31
32     public static String JavaDoc printNode(Node node) {
33         StringBuffer JavaDoc sBuffer = new StringBuffer JavaDoc();
34
35         printNode(sBuffer, node);
36
37         return sBuffer.toString();
38     }
39
40     /** Takes XML node and prints to String.
41     *
42     * @param node Element to print to String
43     * @return XML node as String
44     */

45     private static void printNode(StringBuffer JavaDoc sBuffer, Node node) {
46         if (node.getNodeType() == Node.TEXT_NODE) {
47             sBuffer.append(encodeXMLText(node.getNodeValue().trim()));
48         } else if (node.getNodeType() == Node.CDATA_SECTION_NODE) {
49             sBuffer.append("<![CDATA[");
50             sBuffer.append(node.getNodeValue());
51             sBuffer.append("]]>");
52         } else if (node.getNodeType() == Node.ELEMENT_NODE) {
53             Element el = (Element) node;
54
55             sBuffer.append("<").append(el.getTagName());
56
57             NamedNodeMap attribs = el.getAttributes();
58
59             for (int i = 0; i < attribs.getLength(); i++) {
60                 Attr nextAtt = (Attr) attribs.item(i);
61                 sBuffer.append(" ").append(nextAtt.getName()).append("=\"")
62                        .append(nextAtt.getValue()).append("\"");
63             }
64
65             NodeList nodes = node.getChildNodes();
66
67             if (nodes.getLength() == 0) {
68                 sBuffer.append("/>");
69             } else {
70                 sBuffer.append(">");
71
72                 for (int i = 0; i < nodes.getLength(); i++) {
73                     printNode(sBuffer, nodes.item(i));
74                 }
75
76                 sBuffer.append("</").append(el.getTagName()).append(">");
77             }
78         }
79     }
80     
81     /**
82      * Handles XML encoding of text, e.g. & to &amp;
83      *
84      * @param sText Text to XML encode
85      * @return XML Encoded text
86      */

87     public static String JavaDoc encodeXMLText(String JavaDoc sText) {
88         StringBuffer JavaDoc sBuff2 = new StringBuffer JavaDoc(sText);
89         StringBuffer JavaDoc sNewBuff = new StringBuffer JavaDoc();
90
91         for (int i = 0; i < sBuff2.length(); i++) {
92             char currChar = sBuff2.charAt(i);
93             Character JavaDoc currCharObj = new Character JavaDoc(sBuff2.charAt(i));
94             if (currChar == '&') {
95                 if ((sBuff2.length() - 1 - i) >= 4
96                     && sBuff2.charAt(i + 1) == 'a'
97                     && sBuff2.charAt(i + 2) == 'm'
98                     && sBuff2.charAt(i + 3) == 'p'
99                     && sBuff2.charAt(i + 4) == ';') {
100                     i = i + 4;
101                     sNewBuff.append("&amp;");
102                 } else {
103                     sNewBuff.append("&amp;");
104                 }
105             } else if (currChar == '>') {
106                 sNewBuff.append("&gt;");
107             } else if (currChar == '<') {
108                 sNewBuff.append("&lt;");
109             } else {
110                 sNewBuff.append(currChar);
111             }
112         }
113
114         return sNewBuff.toString();
115
116     }
117 }
Popular Tags