KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > message > NodeImpl


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.jboss.axis.message;
18
19 import org.jboss.logging.Logger;
20 import org.w3c.dom.DOMException JavaDoc;
21 import org.w3c.dom.Document JavaDoc;
22 import org.w3c.dom.NamedNodeMap JavaDoc;
23 import org.w3c.dom.Node JavaDoc;
24 import org.w3c.dom.NodeList JavaDoc;
25 import org.w3c.dom.UserDataHandler JavaDoc;
26
27 import javax.xml.soap.SOAPElement JavaDoc;
28 import javax.xml.soap.SOAPException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
31
32 /**
33  * An implemenation of the abstract Node.
34  * <p/>
35  * This class should not expose functionality that is not part of
36  * {@link javax.xml.soap.Node}. Client code should use <code>javax.xml.soap.Node</code> instead of this class.
37  * <p/>
38  * When creating a DOM2 tree the objects maintained by the tree are <code>org.w3c.dom.Node</code> objects
39  * and not <code>javax.xml.soap.Node</code> objects.
40  * <p/>
41  * This implementation schields the client from the the underlying DOM2 tree, returning <code>javax.xml.soap.Node</code>
42  * objects.
43  *
44  * @author Thomas Diesler (thomas.diesler@jboss.org)
45  */

46 public class NodeImpl implements javax.xml.soap.Node JavaDoc
47 {
48
49    // provide logging
50
private static Logger log = Logger.getLogger(NodeImpl.class.getName());
51
52    // The parent of this Node
53
protected SOAPElementImpl soapParent;
54    // This org.w3c.dom.Node
55
protected org.w3c.dom.Node JavaDoc domNode;
56    // A list af soap node children
57
private List JavaDoc soapChildren = new ArrayList JavaDoc();
58
59    /**
60     * Construct the Node for a given org.w3c.dom.Node
61     */

62    NodeImpl(org.w3c.dom.Node JavaDoc node)
63    {
64       if (node.getNodeName().startsWith(":"))
65          throw new IllegalArgumentException JavaDoc("Illegal node name: " + node.getNodeName());
66
67       this.domNode = node;
68    }
69
70    // javax.xml.soap.Node *********************************************************************************************
71

72    /**
73     * Removes this Node object from the tree.
74     */

75    public void detachNode()
76    {
77       assertSOAPParent();
78
79       org.w3c.dom.Node JavaDoc parent = domNode.getParentNode();
80       if (parent != null)
81       {
82
83          parent.removeChild(domNode);
84          ((NodeImpl)soapParent).soapChildren.remove(this);
85
86          // remove the Node from the children list maintained by SOAPElementAxisImpl
87
if (soapParent instanceof SOAPElementAxisImpl)
88          {
89             ((SOAPElementAxisImpl)soapParent).removeChild(this);
90          }
91
92          soapParent = null;
93       }
94    }
95
96    /**
97     * Returns the parent node of this Node object.
98     * This method can throw an UnsupportedOperationException if the tree is not kept in memory.
99     *
100     * @return the SOAPElement object that is the parent of this Node object or null if this Node object is root
101     */

102    public SOAPElement JavaDoc getParentElement()
103    {
104       return soapParent;
105    }
106
107    /**
108     * Sets the parent of this Node object to the given SOAPElement object.
109     *
110     * @param parent the SOAPElement object to be set as the parent of this Node object
111     * @throws javax.xml.soap.SOAPException if there is a problem in setting the parent to the given node
112     */

113    public void setParentElement(SOAPElement JavaDoc parent) throws SOAPException JavaDoc
114    {
115
116       // detach from the old parent
117
if (soapParent != null)
118          detachNode();
119
120       soapParent = (SOAPElementImpl)parent;
121    }
122
123    /**
124     * Returns the value of this node if this is a Text node or the value of the immediate child of this node otherwise.
125     * <p/>
126     * If there is an immediate child of this Node that it is a Text node then it's value will be returned.
127     * If there is more than one Text node then the value of the first Text Node will be returned.
128     * Otherwise null is returned.
129     *
130     * @return a String with the text of this node if this is a Text node or the text contained by the first immediate
131     * child of this Node object that is a Text object if such a child exists; null otherwise.
132     */

133    public String JavaDoc getValue()
134    {
135
136       // The Text node should overwrite this.
137
// When we get here this is not a text node
138
if (this instanceof javax.xml.soap.Text JavaDoc)
139          throw new IllegalStateException JavaDoc("javax.xml.soap.Text should take care of this");
140
141       org.w3c.dom.Node JavaDoc child = (org.w3c.dom.Node JavaDoc)getFirstChild();
142       if (child instanceof org.w3c.dom.Text JavaDoc)
143          return ((org.w3c.dom.Text JavaDoc)child).getNodeValue();
144
145       return null;
146    }
147
148    /**
149     * If this is a Text node then this method will set its value, otherwise it sets the value of the immediate (Text) child of this node.
150     * <p/>
151     * The value of the immediate child of this node can be set only if, there is one child node and
152     * that node is a Text node, or if there are no children in which case a child Text node will be created.
153     *
154     * @param value A value string
155     * @throws IllegalStateException if the node is not a Text node and either has more than one child node or has a child node that is not a Text node.
156     */

157    public void setValue(String JavaDoc value)
158    {
159       // The Text node should overwrite this.
160
// When we get here this is not a text node
161
if (this instanceof javax.xml.soap.Text JavaDoc)
162          throw new IllegalStateException JavaDoc("javax.xml.soap.Text should take care of this");
163
164       org.w3c.dom.Node JavaDoc child = (org.w3c.dom.Node JavaDoc)getFirstChild();
165       if (child instanceof org.w3c.dom.Text JavaDoc)
166          ((org.w3c.dom.Text JavaDoc)child).setNodeValue(value);
167
168       if (child == null && value != null)
169       {
170          child = domNode.getOwnerDocument().createTextNode(value);
171          appendChild(new TextImpl(child));
172       }
173    }
174
175    /**
176     * Notifies the implementation that this Node object is no longer being used by the application and that the
177     * implementation is free to reuse this object for nodes that may be created later.
178     * Calling the method recycleNode implies that the method detachNode has been called previously.
179     */

180    public void recycleNode()
181    {
182
183    }
184
185    // org.w3c.dom.Node *******************************************************************************************
186

187    public String JavaDoc getNodeName()
188    {
189       return domNode.getNodeName();
190    }
191
192    public String JavaDoc getNodeValue() throws DOMException JavaDoc
193    {
194       return domNode.getNodeValue();
195    }
196
197    public void setNodeValue(String JavaDoc nodeValue) throws DOMException JavaDoc
198    {
199       domNode.setNodeValue(nodeValue);
200    }
201
202    public short getNodeType()
203    {
204       return domNode.getNodeType();
205    }
206
207    public org.w3c.dom.Node JavaDoc getParentNode()
208    {
209       assertSOAPParent();
210       return domNode.getParentNode();
211    }
212
213    public NodeList JavaDoc getChildNodes()
214    {
215       return domNode.getChildNodes();
216    }
217
218    public org.w3c.dom.Node JavaDoc getFirstChild()
219    {
220       return domNode.getFirstChild();
221    }
222
223    public org.w3c.dom.Node JavaDoc getLastChild()
224    {
225       return domNode.getLastChild();
226    }
227
228    public org.w3c.dom.Node JavaDoc getPreviousSibling()
229    {
230       return domNode.getPreviousSibling();
231    }
232
233    public org.w3c.dom.Node JavaDoc getNextSibling()
234    {
235       return domNode.getNextSibling();
236    }
237
238    public NamedNodeMap JavaDoc getAttributes()
239    {
240       return domNode.getAttributes();
241    }
242
243    public Document JavaDoc getOwnerDocument()
244    {
245       return domNode.getOwnerDocument();
246    }
247
248    public org.w3c.dom.Node JavaDoc insertBefore(org.w3c.dom.Node JavaDoc newChild, org.w3c.dom.Node JavaDoc refChild) throws DOMException JavaDoc
249    {
250       assertSOAPNode(newChild);
251       assertSOAPNode(refChild);
252
253       int index = soapChildren.indexOf(refChild);
254       if (index < 0)
255          throw new IllegalArgumentException JavaDoc("Cannot find refChild in list of javax.xml.soap.Node children");
256
257       NodeImpl soapNewNode = (NodeImpl)newChild;
258       NodeImpl soapRefNode = (NodeImpl)refChild;
259       domNode.insertBefore(soapNewNode.domNode, soapRefNode.domNode);
260       soapChildren.add(index, soapNewNode);
261
262       return newChild;
263    }
264
265    public org.w3c.dom.Node JavaDoc replaceChild(org.w3c.dom.Node JavaDoc newChild, org.w3c.dom.Node JavaDoc oldChild) throws DOMException JavaDoc
266    {
267       assertSOAPNode(newChild);
268       assertSOAPNode(oldChild);
269
270       int index = soapChildren.indexOf(oldChild);
271       if (index < 0)
272          throw new DOMException JavaDoc(DOMException.NOT_FOUND_ERR, "Cannot find oldChild in list of javax.xml.soap.Node children");
273
274       NodeImpl soapNewNode = (NodeImpl)newChild;
275       NodeImpl soapOldNode = (NodeImpl)oldChild;
276       domNode.replaceChild(soapNewNode.domNode, soapOldNode.domNode);
277       soapChildren.remove(index);
278       soapChildren.add(index, soapNewNode);
279
280       return newChild;
281    }
282
283    public org.w3c.dom.Node JavaDoc removeChild(org.w3c.dom.Node JavaDoc oldChild) throws DOMException JavaDoc
284    {
285       assertSOAPNode(oldChild);
286
287       int index = soapChildren.indexOf(oldChild);
288       if (index < 0)
289          throw new DOMException JavaDoc(DOMException.NOT_FOUND_ERR, "Cannot find oldChild in list of javax.xml.soap.Node children");
290
291       NodeImpl soapOldNode = (NodeImpl)oldChild;
292       domNode.removeChild(soapOldNode.domNode);
293       soapChildren.remove(index);
294
295       return oldChild;
296    }
297
298    public org.w3c.dom.Node JavaDoc appendChild(org.w3c.dom.Node JavaDoc newChild) throws DOMException JavaDoc
299    {
300       assertSOAPNode(newChild);
301
302       if ((this instanceof SOAPElementImpl) == false)
303          throw new DOMException JavaDoc(DOMException.INVALID_ACCESS_ERR, "Cannot append child to this node: " + this);
304
305       NodeImpl soapNode = (NodeImpl)newChild;
306       domNode.appendChild(soapNode.domNode);
307
308       soapNode.soapParent = (SOAPElementImpl)this;
309       soapChildren.add(soapNode);
310
311       return newChild;
312    }
313
314    public boolean hasChildNodes()
315    {
316       return domNode.hasChildNodes();
317    }
318
319    public org.w3c.dom.Node JavaDoc cloneNode(boolean deep)
320    {
321
322       NodeImpl soapNode = new NodeImpl(domNode.cloneNode(deep));
323       if (deep == true)
324       {
325          for (int i = 0; i < soapChildren.size(); i++)
326          {
327             NodeImpl node = (NodeImpl)soapChildren.get(i);
328             soapNode.soapChildren.add(node.cloneNode(deep));
329          }
330       }
331
332       return soapNode;
333    }
334
335    public void normalize()
336    {
337       domNode.normalize();
338    }
339
340    public boolean isSupported(String JavaDoc feature, String JavaDoc version)
341    {
342       return domNode.isSupported(feature, version);
343    }
344
345    public String JavaDoc getNamespaceURI()
346    {
347       return domNode.getNamespaceURI();
348    }
349
350    public String JavaDoc getPrefix()
351    {
352       return domNode.getPrefix();
353    }
354
355    public void setPrefix(String JavaDoc prefix) throws DOMException JavaDoc
356    {
357       domNode.setPrefix(prefix);
358    }
359
360    public String JavaDoc getLocalName()
361    {
362       return domNode.getLocalName();
363    }
364
365    public boolean hasAttributes()
366    {
367       return domNode.hasAttributes();
368    }
369
370    public int hashCode()
371    {
372       return domNode.hashCode();
373    }
374
375    public String JavaDoc toString()
376    {
377       return super.toString() + "[" + domNode.toString() + "]";
378    }
379
380    private void assertSOAPNode(org.w3c.dom.Node JavaDoc node)
381    {
382       if ((node instanceof NodeImpl) == false)
383          throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Operation only supported for javax.xml.soap.Node, this is a " + node);
384    }
385
386    private void assertSOAPParent()
387    {
388       org.w3c.dom.Node JavaDoc domParent = domNode.getParentNode();
389       if (domParent != null && soapParent == null)
390          throw new IllegalStateException JavaDoc("Inconsistent node, has a DOM parent but no SOAP parent: " + this);
391       if (domParent == null && soapParent != null)
392          throw new IllegalStateException JavaDoc("Inconsistent node, has a SOAP parent but no DOM parent: " + this);
393    }
394
395    // DOM3-API start ***************************************************************************************************
396
public String JavaDoc getBaseURI()
397    {
398       return null;
399    }
400
401    public short compareDocumentPosition(Node JavaDoc other) throws DOMException JavaDoc
402    {
403       return 0;
404    }
405
406    public String JavaDoc getTextContent() throws DOMException JavaDoc
407    {
408       return null;
409    }
410
411    public void setTextContent(String JavaDoc textContent) throws DOMException JavaDoc
412    {
413
414    }
415
416    public boolean isSameNode(Node JavaDoc other)
417    {
418       return false;
419    }
420
421    public String JavaDoc lookupPrefix(String JavaDoc namespaceURI)
422    {
423       return null;
424    }
425
426    public boolean isDefaultNamespace(String JavaDoc namespaceURI)
427    {
428       return false;
429    }
430
431    public String JavaDoc lookupNamespaceURI(String JavaDoc prefix)
432    {
433       return null;
434    }
435
436    public boolean isEqualNode(Node JavaDoc arg)
437    {
438       return false;
439    }
440
441    public Object JavaDoc getFeature(String JavaDoc feature, String JavaDoc version)
442    {
443       return null;
444    }
445
446    public Object JavaDoc setUserData(String JavaDoc key, Object JavaDoc data, UserDataHandler JavaDoc handler)
447    {
448       return null;
449    }
450
451    public Object JavaDoc getUserData(String JavaDoc key)
452    {
453       return null;
454    }
455    // DOM3-API end *****************************************************************************************************
456
}
457
Popular Tags