001: /*
002: * (C) Copyright IBM Corp. 2000 All rights reserved.
003: *
004: * The program is provided "AS IS" without any warranty express or
005: * implied, including the warranty of non-infringement and the implied
006: * warranties of merchantibility and fitness for a particular purpose.
007: * IBM will not be liable for any damages suffered by you as a result
008: * of using the Program. In no event will IBM be liable for any
009: * special, indirect or consequential damages or lost profits even if
010: * IBM has been advised of the possibility of their occurrence. IBM
011: * will not be liable for any third party claims against you.
012: *
013: * Portions Copyright (C) Simulacra Media Ltd, 2004.
014: */
015:
016: package com.ibm.webdav;
017:
018: import org.w3c.dom.*;
019:
020: /**
021: * Utility class for serialising DOM nodes.
022: *
023: * @author Michael Bell
024: * @version $Revision: 1.1 $
025: *
026: */
027: public class XMLUtility {
028: public XMLUtility() {
029: }
030:
031: public static String printNode(Node node) {
032: StringBuffer sBuffer = new StringBuffer();
033:
034: printNode(sBuffer, node);
035:
036: return sBuffer.toString();
037: }
038:
039: /** Takes XML node and prints to String.
040: *
041: * @param node Element to print to String
042: * @return XML node as String
043: */
044: private static void printNode(StringBuffer sBuffer, Node node) {
045: if (node.getNodeType() == Node.TEXT_NODE) {
046: sBuffer.append(encodeXMLText(node.getNodeValue().trim()));
047: } else if (node.getNodeType() == Node.CDATA_SECTION_NODE) {
048: sBuffer.append("<![CDATA[");
049: sBuffer.append(node.getNodeValue());
050: sBuffer.append("]]>");
051: } else if (node.getNodeType() == Node.ELEMENT_NODE) {
052: Element el = (Element) node;
053:
054: sBuffer.append("<").append(el.getTagName());
055:
056: NamedNodeMap attribs = el.getAttributes();
057:
058: for (int i = 0; i < attribs.getLength(); i++) {
059: Attr nextAtt = (Attr) attribs.item(i);
060: sBuffer.append(" ").append(nextAtt.getName()).append(
061: "=\"").append(nextAtt.getValue()).append("\"");
062: }
063:
064: NodeList nodes = node.getChildNodes();
065:
066: if (nodes.getLength() == 0) {
067: sBuffer.append("/>");
068: } else {
069: sBuffer.append(">");
070:
071: for (int i = 0; i < nodes.getLength(); i++) {
072: printNode(sBuffer, nodes.item(i));
073: }
074:
075: sBuffer.append("</").append(el.getTagName())
076: .append(">");
077: }
078: }
079: }
080:
081: /**
082: * Handles XML encoding of text, e.g. & to &
083: *
084: * @param sText Text to XML encode
085: * @return XML Encoded text
086: */
087: public static String encodeXMLText(String sText) {
088: StringBuffer sBuff2 = new StringBuffer(sText);
089: StringBuffer sNewBuff = new StringBuffer();
090:
091: for (int i = 0; i < sBuff2.length(); i++) {
092: char currChar = sBuff2.charAt(i);
093: Character currCharObj = new Character(sBuff2.charAt(i));
094: if (currChar == '&') {
095: if ((sBuff2.length() - 1 - i) >= 4
096: && sBuff2.charAt(i + 1) == 'a'
097: && sBuff2.charAt(i + 2) == 'm'
098: && sBuff2.charAt(i + 3) == 'p'
099: && sBuff2.charAt(i + 4) == ';') {
100: i = i + 4;
101: sNewBuff.append("&");
102: } else {
103: sNewBuff.append("&");
104: }
105: } else if (currChar == '>') {
106: sNewBuff.append(">");
107: } else if (currChar == '<') {
108: sNewBuff.append("<");
109: } else {
110: sNewBuff.append(currChar);
111: }
112: }
113:
114: return sNewBuff.toString();
115:
116: }
117: }
|