Source code: com/tripi/asp/msxml2/DOMNode.java
1 /**
2 * ArrowHead ASP Server
3 * This is a source file for the ArrowHead ASP Server - an 100% Java
4 * VBScript interpreter and ASP server.
5 *
6 * For more information, see http://www.tripi.com/arrowhead
7 *
8 * Copyright (C) 2002 Terence Haddock
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 */
25 package com.tripi.asp.msxml2;
26
27 import java.util.Collection;
28 import java.util.Iterator;
29
30 import org.apache.log4j.Category;
31 import org.jdom.Document;
32 import org.jdom.output.XMLOutputter;
33
34 /**
35 * This class implements msxml2.DOMNode.
36 *
37 * Contributed by Jim Horner <jhorner@arinbe.com>
38 */
39 public class DOMNode {
40
41 /** Debugging category. */
42 Category DBG = Category.getInstance(DOMNode.class);
43
44 // contains the list of attributes for this node. [read-only; see Attributes()]
45 protected DOMNamedNodeMap _Attributes;
46
47 // returns the base name for the name qualified with the namespace [read-only]
48 // private String baseName;
49
50 // contains a node list containing the children
51 // (for nodes that can have children) [read-only; see ChildNodes()]
52 protected DOMNodeList _ChildNodes;
53
54 // specifies the datatpe for this node. [read/write]
55 // public String dataType;
56
57 // private String Definition;
58
59 // return the Uniform Resource Identifier (URI) for the
60 // namespace [read-only; see NamespaceURI()]
61 protected String _NamespaceURI;
62
63 // contains the qualified name of the element, attribute, or entity reference
64 // or a fixed string for other node types [read-only; see NodeName()]
65 protected String _NodeName;
66
67 // specifies teh XML Document Object Model node type, which determines
68 // valid values and whether the node can have child nodes.
69 // [read-only; see NodeType() and NodeTypeString()]
70 protected DOMNodeType _NodeType;
71
72 // contains the text associated with the node [read/write; see NodeValue()]
73 protected Object _NodeValue;
74
75 // returns the root of the document that contains this node.
76 // [read-only; see OwnerDocument()]
77 protected DOMDocument _OwnerDocument;
78
79 // contains the parent node (for nodes that can have parents)
80 // [read-only; see ParentNode()]
81 protected DOMNode _ParentNode;
82
83 // returns the namespace prefix [read-only; see Prefix()]
84 protected String _Prefix;
85
86 public DOMNode() {
87 this._ChildNodes = new DOMNodeList();
88 this._Attributes = null;
89 this._Prefix = "";
90 }
91
92 public DOMNode AppendChild(DOMNode node) {
93
94 node._ParentNode = this;
95 node._OwnerDocument = this._OwnerDocument;
96 this._ChildNodes.AppendNode(node);
97
98 return node;
99 }
100
101 public String Prefix() {
102 return this._Prefix;
103 }
104
105 /**
106 * ASP-Accessible function
107 * @return the attributes for this node
108 */
109 public DOMNamedNodeMap Attributes() {
110
111 // these types should always return null
112 String type = this._NodeType.toString();
113 if (type.equals("attribute")
114 || type.equals("cdatasection")
115 || type.equals("comment")
116 || type.equals("document")
117 || type.equals("documentfragement")
118 || type.equals("entityreference")
119 || type.equals("text")) {
120
121 return null;
122 }
123
124 return this._Attributes;
125 }
126
127 /**
128 * ASP-Accessible function
129 * @return the first node of the node list
130 */
131 public DOMNode FirstChild() {
132 DOMNode result = null;
133 if (this._ChildNodes != null) {
134 result = this._ChildNodes.Item(0);
135 }
136 return result;
137 }
138
139 /**
140 * ASP-Accessible function
141 * @return the last node of the node list
142 */
143 public DOMNode LastChild() {
144 DOMNode result = null;
145 if (this._ChildNodes != null) {
146 result = this._ChildNodes.Item(0);
147 }
148 return result;
149 }
150
151 /**
152 * ASP-Accessible function
153 * @return the node type
154 */
155 public int NodeType() {
156 return this._NodeType.toInteger();
157 }
158
159 /**
160 * ASP-Accessible function
161 * @return the node type string value
162 */
163 public String NodeTypeString() {
164 return this._NodeType.toString();
165 }
166
167 /**
168 * ASP-Accessible function
169 * @return the URI of the namespace
170 */
171 public String NamespaceURI() {
172 return this._NamespaceURI;
173 }
174
175 // contains the text associated with the node [read/write]
176 public Object NodeValue() {
177
178 // check for valid types
179 /* These types contain null and throw an error when setting
180 * NODE_DOCUMENT, NODE_DOCUMENT_TYPE, NODE_DOCUMENT_FRAGMENT,
181 * NODE_ELEMENT, NODE_ENTITY, NODE_ENTITY_REFERENCE, NODE_NOTATION
182 */
183 String type = this._NodeType.toString();
184 if (type.equals("document")
185 || type.equals("documenttype")
186 || type.equals("documentfragement")
187 || type.equals("element")
188 || type.equals("entity")
189 || type.equals("entityreference")
190 || type.equals("notation")) {
191
192 return null;
193 }
194
195 return this._NodeValue;
196 }
197
198 public void NodeValue(Object value) throws Exception {
199
200 // check for valid types
201 if (this._NodeType == null) {
202 throw new Exception("Must set NodeType before setting NodeValue.");
203 }
204
205 /* These types contain null and throw an error when setting
206 * NODE_DOCUMENT, NODE_DOCUMENT_TYPE, NODE_DOCUMENT_FRAGMENT,
207 * NODE_ELEMENT, NODE_ENTITY, NODE_ENTITY_REFERENCE, NODE_NOTATION
208 */
209 String type = this._NodeType.toString();
210 if (type.equals("document")
211 || type.equals("documenttype")
212 || type.equals("documentfragement")
213 || type.equals("element")
214 || type.equals("entity")
215 || type.equals("entityreference")
216 || type.equals("notation")) {
217 // bland, general, "plagarized" error from IIS error
218 // don't won't anyone spoiled with too much information
219 throw new Exception(
220 "This operation can not be performed "
221 + "with a Node of type "
222 + type.toUpperCase()
223 + ".");
224 }
225
226 this._NodeValue = value;
227 }
228
229 // contains the text content of the node and its subtrees. [read/write]
230 public String Text() {
231
232 String result = "";
233
234 String type = this._NodeType.toString();
235 if (type.equals("text") || type.equals("attribute")) {
236 result = this.NodeValue().toString();
237 }
238 else {
239 Collection children = _ChildNodes.Items();
240 Iterator it = children.iterator();
241 while (it.hasNext()) {
242
243 // check for a text child
244 DOMNode child = (DOMNode) it.next();
245 result = result + child.Text();
246 }
247 }
248
249 return result;
250 }
251
252 public void Text(String text) throws Exception {
253
254 String result = "";
255
256 if (this._NodeType == null) {
257 throw new Exception("Must set NodeType before setting Text.");
258 }
259
260 String type = this._NodeType.toString();
261 if (type.equals("text") || type.equals("attribute")) {
262 this.NodeValue(text);
263 }
264 else {
265 DOMNode child = new DOMNode();
266 child._NodeType = new DOMNodeType("text");
267 child.Text(text);
268 this.AppendChild(child);
269 }
270
271 }
272
273 public String Xml() {
274
275 XMLOutputter xmlDbg = new XMLOutputter();
276 xmlDbg.setIndent(" ");
277 xmlDbg.setNewlines(true);
278 Document xmldoc = new Document(DOMUtils.convertToJdom(this));
279 String result = xmlDbg.outputString(xmldoc);
280
281 return result;
282 }
283 }