KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > simplexml > DOMNodeUtil


1 /*
2  * Copyright (c) 1998-2004 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Charles Reich
28  */

29
30 package com.caucho.quercus.lib.simplexml;
31
32 import org.w3c.dom.NamedNodeMap JavaDoc;
33 import org.w3c.dom.Node JavaDoc;
34 import org.w3c.dom.NodeList JavaDoc;
35
36 // XXX: replace with use of XmlPrinter as done in DOMDocument
37
public class DOMNodeUtil {
38
39   public static StringBuilder JavaDoc asXML(Node JavaDoc node)
40   {
41     return asXMLVersion(node, "1.0");
42   }
43
44   public static StringBuilder JavaDoc asXMLVersion(Node JavaDoc node,
45                                            String JavaDoc version)
46   {
47     StringBuilder JavaDoc result = new StringBuilder JavaDoc();
48
49     result.append("<?xml version=\"").append(version).append("\"?>\n");
50     generateXML(node, result);
51
52     return result;
53   }
54
55   public static StringBuilder JavaDoc asXMLVersionEncoding(Node JavaDoc node,
56                                                    String JavaDoc version,
57                                                    String JavaDoc encoding)
58   {
59     StringBuilder JavaDoc result = new StringBuilder JavaDoc();
60
61     result.append("<?xml version=\"").append(version).append("\"");
62
63     if (!"".equals(encoding)) {
64       result.append(" encoding=\"").append(encoding).append("\"");
65     }
66
67     result.append("?>\n");
68     generateXML(node, result);
69
70     return result;
71   }
72
73   /**
74    * recursive helper function for asXML
75    * @return XML in string buffer
76    */

77   public static StringBuilder JavaDoc generateXML(Node JavaDoc node,
78                                           StringBuilder JavaDoc sb)
79   {
80     if (node == null)
81         return sb;
82
83     // If this is a text node, then just return the text
84
if (node.getNodeType() == Node.TEXT_NODE) {
85       sb.append(node.getNodeValue());
86       return sb;
87     }
88
89     // not a text node
90
sb.append("<");
91
92     sb.append(node.getNodeName());
93
94     // add attributes, if any
95
NamedNodeMap JavaDoc attrs = node.getAttributes();
96     int attrLength = attrs.getLength();
97
98     for (int i=0; i < attrLength; i++) {
99       Node JavaDoc attribute = attrs.item(i);
100       sb.append(" ")
101         .append(attribute.getNodeName())
102         .append("=\"")
103         .append(attribute.getNodeValue())
104         .append("\"");
105     }
106
107     // recurse through children, if any
108
NodeList JavaDoc children = node.getChildNodes();
109     int nodeLength = children.getLength();
110
111     if (nodeLength == 0) {
112       sb.append(" />");
113       return sb;
114     }
115
116     sb.append(">");
117
118     // there are children
119
for (int i=0; i < nodeLength; i++) {
120       Node JavaDoc child = children.item(i);
121
122       if (child.getNodeType() == Node.TEXT_NODE) {
123         sb.append(child.getNodeValue());
124         continue;
125       }
126       generateXML(child, sb);
127     }
128
129     // add closing tag
130
sb.append("</").append(node.getNodeName()).append(">");
131
132     return sb;
133   }
134 }
135
Popular Tags