KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > saaj > NodeImpl


1 /*
2  * Copyright 2004,2005 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 package org.apache.axis2.saaj;
17
18 import org.apache.axis2.om.*;
19 import org.apache.axis2.util.Dom2OmUtils;
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.NodeList JavaDoc;
24
25 import javax.xml.soap.Node JavaDoc;
26 import javax.xml.soap.SOAPElement JavaDoc;
27 import javax.xml.soap.SOAPException JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 /**
31  * Class NodeImpl
32  *
33  * @author Ashutosh Shahi
34  * ashutosh.shahi@gmail.com
35  */

36 public class NodeImpl implements Node JavaDoc {
37
38     /**
39      * Field omNode
40      */

41     protected org.apache.axis2.om.OMNode omNode;
42     /**
43      * field document
44      */

45     protected org.w3c.dom.Document JavaDoc document;
46
47     //protected CharacterData textRep = null;
48

49     /**
50      * Constructor NodeImpl
51      *
52      */

53     public NodeImpl(){
54
55     }
56
57     /**
58      * Constructor NodeImpl
59      *
60      * @param node
61      */

62     public NodeImpl(OMNode node){
63         this.omNode = node;
64     }
65     /**
66      * Constructor NodeImpl
67      *
68      * @param attrib
69      */

70     public NodeImpl(OMAttribute attrib){
71         //TODO
72
// To be implemented
73
// Find out a way to construct OMNode from a OMAttribute
74
// as OMAttributes are immutable
75
}
76
77     /**
78      * Constructor NodeImpl
79      *
80      * @param ns
81      */

82     public NodeImpl(OMNamespace ns){
83         //TODO
84
// To be implemented
85
// Find out a way to construct OMNode from OMNamespace
86
// OMNamespace is immutable
87
}
88
89      /**
90      * constructor which adopts the name and NS of the char data, and its text
91      * @param text
92      */

93 /* public NodeImpl(CharacterData text) {
94
95     }
96 */

97
98     public OMNode getOMNode(){
99         return omNode;
100     }
101
102     /**
103      * Method getValue
104      *
105      * @see javax.xml.soap.Node#getValue()
106      */

107     public String JavaDoc getValue() {
108
109         if(omNode.getType() == OMNode.TEXT_NODE)
110             return ((OMText)omNode).getText();
111         else if(omNode.getType() == OMNode.ELEMENT_NODE)
112             return new NodeImpl(((OMElement)omNode).getFirstChild()).getValue();
113         return null;
114     }
115
116     /**
117      * Method setParentElement
118      * @param parent
119      *
120      * @see javax.xml.soap.Node#setParentElement(javax.xml.soap.SOAPElement)
121      */

122     public void setParentElement(SOAPElement JavaDoc parent) throws SOAPException JavaDoc {
123
124         OMElement omParent = ((SOAPElementImpl)parent).getOMElement();
125         omNode.setParent(omParent);
126     }
127
128     /**
129      * Method getParentElement
130      * @see javax.xml.soap.Node#getParentElement()
131      */

132     public SOAPElement JavaDoc getParentElement() {
133
134         OMElement omParent = (OMElement)omNode.getParent();
135         return new SOAPElementImpl(omParent);
136     }
137
138     /**
139      * Method detachNode
140      * @see javax.xml.soap.Node#detachNode()
141      */

142     public void detachNode() {
143
144         omNode.detach();
145     }
146
147     /**
148      * Method recycleNode
149      * @see javax.xml.soap.Node#recycleNode()
150      */

151     public void recycleNode() {
152         // No corresponding implementation in OM
153
// There is no implementation in Axis 1.2 also
154

155     }
156
157     /**
158      * Method setValue
159      * @param value
160      *
161      * @see javax.xml.soap.Node#setValue(java.lang.String)
162      */

163     public void setValue(String JavaDoc value) {
164
165         if(omNode.getType() == OMNode.TEXT_NODE){
166             OMElement parent = (OMElement)omNode.getParent();
167             ((OMText)omNode).discard();
168             omNode = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createText(parent, value);
169         } else if(omNode.getType() == OMNode.ELEMENT_NODE){
170             OMNode firstChild = ((OMElement)omNode).getFirstChild();
171             if(firstChild == null ){
172                 firstChild = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createText((OMElement)omNode, value);
173             }
174             else if(firstChild.getType() == OMNode.TEXT_NODE){
175                 ((OMText)firstChild).discard();
176                 firstChild = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createText((OMElement)omNode, value);
177             }
178         } else{
179             throw new IllegalStateException JavaDoc();
180         }
181     }
182
183     /**
184      * Method getNodeType
185      * @see org.w3c.dom.Node#getNodeType()
186      */

187     public short getNodeType() {
188
189         return (short)omNode.getType();
190     }
191
192     /**
193      * Method normalize
194      * @see org.w3c.dom.Node#normalize()
195      */

196     public void normalize() {
197         // No corresponding function in OM
198
//Axis 1.2 also doesn't have any implementation for this
199

200     }
201
202     /**
203      * Method hasAttributes
204      * @see org.w3c.dom.Node#hasAttributes()
205      */

206     public boolean hasAttributes() {
207         if(omNode instanceof OMElement){
208             Iterator JavaDoc iter = ((OMElement)omNode).getAttributes();
209             return (iter.hasNext() ? true : false);
210         }
211         return false;
212     }
213
214     /**
215      * Method hasChildNodes
216      * @see org.w3c.dom.Node#hasChildNodes()
217      */

218     public boolean hasChildNodes() {
219         if(omNode instanceof OMElement){
220             Iterator JavaDoc iter = ((OMElement)omNode).getChildren();
221             return (iter.hasNext() ? true : false);
222         }
223         return false;
224     }
225
226     /**
227      * Method getLocalName
228      * @see org.w3c.dom.Node#getLocalName()
229      */

230     public String JavaDoc getLocalName() {
231         if(omNode.getType() == ELEMENT_NODE || omNode.getType()
232                 == ATTRIBUTE_NODE)
233             return ((OMElement)omNode).getLocalName();
234         // TODO: else if(omNode.getType() == ATTRIBUTE_NODE)
235
// return some
236
return null;
237     }
238
239     /**
240      * Method getNamespaceURI
241      * @see org.w3c.dom.Node#getNamespaceURI()
242      */

243     public String JavaDoc getNamespaceURI() {
244
245         return ((OMElement)omNode).getNamespace().getName();
246     }
247
248     /**
249      * Method getNodeName
250      * @see org.w3c.dom.Node#getNodeName()
251      */

252     public String JavaDoc getNodeName() {
253
254         if(omNode.getType() == OMNode.ELEMENT_NODE )
255             return ((OMElement)omNode).getLocalName();
256         else if(omNode.getType() == OMNode.COMMENT_NODE)
257             return "#comment";
258         else if(omNode.getType() == OMNode.CDATA_SECTION_NODE)
259             return "#cdata-section";
260         else if(omNode.getType() == OMNode.TEXT_NODE)
261             return "#text";
262         //TODO else if Attribute Node so something
263
// return attribute name
264
return null;
265     }
266
267     /**
268      * Method getNodeValue
269      * @see org.w3c.dom.Node#getNodeValue()
270      */

271     public String JavaDoc getNodeValue() throws DOMException JavaDoc {
272         // Returns text for a TEXT_NODE, null otherwise
273
if(omNode.getType() == OMNode.TEXT_NODE)
274             return ((OMText)omNode).getText();
275         //TODO else if(omNode.getType() == Attribute)
276
else
277             return null;
278     }
279
280     /**
281      * Method getPrefix
282      * @see org.w3c.dom.Node#getPrefix()
283      */

284     public String JavaDoc getPrefix() {
285         if(omNode.getType() == OMNode.ELEMENT_NODE)
286             return ((OMElement)omNode).getNamespace().getPrefix();
287         return null;
288     }
289
290     /**
291      * Method setNodeValue
292      * @see org.w3c.dom.Node#setNodeValue(java.lang.String)
293      */

294     public void setNodeValue(String JavaDoc value) throws DOMException JavaDoc {
295
296         if(omNode.getType() == OMNode.TEXT_NODE){
297             OMElement parent = (OMElement)omNode.getParent();
298             ((OMText)omNode).discard();
299             omNode = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createText(parent, value);
300         }
301     }
302
303     /**
304      * Method setPrefix
305      * @see org.w3c.dom.Node#setPrefix(java.lang.String)
306      */

307     public void setPrefix(String JavaDoc prefix) throws DOMException JavaDoc {
308         //TODO - Do for attribute Node
309
if(omNode.getType() == OMNode.ELEMENT_NODE /*|| Attribute Node*/){
310             OMNamespace ns = ((OMElement)omNode).getNamespace();
311             String JavaDoc uri = ns.getName();
312             OMNamespace newNs = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMNamespace(uri, prefix);
313             ((OMElement)omNode).setNamespace(newNs);
314         }
315
316     }
317
318     /**
319      * Method setOwnerDocument
320      * @param doc
321      */

322     public void setOwnerDocument(Document JavaDoc doc){
323         // method not part of org.w3c.dom.Node, created to set the document
324
this.document = doc;
325     }
326
327     /**
328      * Method getOwnerDocument
329      * @see org.w3c.dom.Node#getOwnerDocument()
330      */

331     public Document JavaDoc getOwnerDocument() {
332         // return the set document
333
return document;
334     }
335
336     /**
337      * Method getAttributes
338      * @see org.w3c.dom.Node#getAttributes()
339      */

340     public NamedNodeMap JavaDoc getAttributes() {
341         // Will have to provide an implementation of NamedNodeMap
342
// Dropping for now
343
// TODO
344
Iterator JavaDoc iter = ((OMElement)omNode).getAttributes();
345
346         return null;
347     }
348
349     /**
350      * Method getFirstChild
351      * @see org.w3c.dom.Node#getFirstChild()
352      */

353     public org.w3c.dom.Node JavaDoc getFirstChild() {
354         //
355
OMNode child = ((OMElement)omNode).getFirstChild();
356         return new NodeImpl(child);
357     }
358
359     /**
360      * Method getLastChild
361      * @see org.w3c.dom.Node#getLastChild()
362      */

363     public org.w3c.dom.Node JavaDoc getLastChild() {
364
365         Iterator JavaDoc children = ((OMElement)omNode).getChildren();
366         Object JavaDoc child = null;
367         while(children.hasNext()){
368             child = children.next();
369         }
370         if(child instanceof OMNode){
371             return new NodeImpl((OMNode)child);
372         }
373         return null;
374     }
375
376     /**
377      * dom Node method
378      */

379     public org.w3c.dom.Node JavaDoc getNextSibling() {
380
381         OMNode sibling = omNode.getNextSibling();
382         return new NodeImpl(sibling);
383     }
384
385
386     public org.w3c.dom.Node JavaDoc getParentNode() {
387
388         OMElement parent = (OMElement)omNode.getParent();
389         return new NodeImpl(parent);
390     }
391
392     /**
393      * dom Node method
394      */

395     public org.w3c.dom.Node JavaDoc getPreviousSibling() {
396
397         OMNode prevSibling = omNode.getPreviousSibling();
398         return new NodeImpl(prevSibling);
399     }
400
401     /* (non-Javadoc)
402      * @see org.w3c.dom.Node#cloneNode(boolean)
403      */

404     public org.w3c.dom.Node JavaDoc cloneNode(boolean deep) {
405         //TODO
406
return null;
407     }
408
409     /**
410      * DOM Node method
411      */

412     public NodeList JavaDoc getChildNodes() {
413         Iterator JavaDoc iter = ((OMElement)omNode).getChildren();
414         NodeListImpl list = new NodeListImpl();
415         while(iter.hasNext()){
416             OMNode omChild = (OMNode)iter.next();
417             Node JavaDoc child = new NodeImpl(omChild);
418             list.addNode(child);
419         }
420         return list;
421     }
422
423     /* (non-Javadoc)
424      * @see org.w3c.dom.Node#isSupported(java.lang.String, java.lang.String)
425      */

426     public boolean isSupported(String JavaDoc arg0, String JavaDoc arg1) {
427         //TODO: Not implemented in 1.2 as well
428
return false;
429     }
430
431     /* (non-Javadoc)
432      * @see org.w3c.dom.Node#appendChild(org.w3c.dom.Node)
433      */

434     public org.w3c.dom.Node JavaDoc appendChild(org.w3c.dom.Node JavaDoc node)
435             throws DOMException JavaDoc {
436
437         OMNode child = Dom2OmUtils.toOM(node);
438         if(omNode.getType() == OMNode.ELEMENT_NODE)
439             ((OMElement)omNode).addChild(child);
440         return null;
441     }
442
443     /* (non-Javadoc)
444      * @see org.w3c.dom.Node#removeChild(org.w3c.dom.Node)
445      */

446     public org.w3c.dom.Node JavaDoc removeChild(org.w3c.dom.Node JavaDoc oldChild)
447             throws DOMException JavaDoc {
448         //Check if equals method has been removed from OMNode
449
OMNode child = Dom2OmUtils.toOM(oldChild);
450         if(omNode.getType() == OMNode.ELEMENT_NODE){
451             Iterator JavaDoc iter = ((OMElement)omNode).getChildren();
452             while(iter.hasNext()){
453                 Object JavaDoc nextChild = iter.next();
454                 if(nextChild instanceof OMNode && nextChild.equals(child)){
455                     ((OMElement)nextChild).discard();
456                     return oldChild;
457                 }
458             }
459         }
460
461         return null;
462     }
463
464     /**
465      * @see org.w3c.dom.Node#insertBefore(org.w3c.dom.Node, org.w3c.dom.Node)
466      */

467     public org.w3c.dom.Node JavaDoc insertBefore(org.w3c.dom.Node JavaDoc arg0,
468             org.w3c.dom.Node JavaDoc arg1) throws DOMException JavaDoc {
469
470         return null;
471     }
472
473     /**
474      * @see org.w3c.dom.Node#replaceChild(org.w3c.dom.Node, org.w3c.dom.Node)
475      */

476     public org.w3c.dom.Node JavaDoc replaceChild(org.w3c.dom.Node JavaDoc newChild,
477             org.w3c.dom.Node JavaDoc refChild) throws DOMException JavaDoc {
478         OMNode newOmChild = Dom2OmUtils.toOM(newChild);
479         OMNode refOmChild = Dom2OmUtils.toOM(refChild);
480         if(omNode.getType() == OMNode.ELEMENT_NODE){
481             Iterator JavaDoc iter = ((OMElement)omNode).getChildren();
482             while(iter.hasNext()){
483                 Object JavaDoc nextChild = iter.next();
484                 if(nextChild instanceof OMNode && nextChild.equals(refOmChild)){
485
486                 }
487             }
488         }
489         return null;
490     }
491
492     public boolean equals(Object JavaDoc o){
493         if(o instanceof NodeImpl){
494             if(this.omNode.equals(((NodeImpl)o).omNode))
495                     return true;
496         }
497         return false;
498     }
499
500 }
501
Popular Tags