KickJava   Java API By Example, From Geeks To Geeks.

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


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.w3c.dom.Attr JavaDoc;
19 import org.w3c.dom.DOMException JavaDoc;
20 import org.w3c.dom.NodeList JavaDoc;
21
22 import javax.xml.namespace.QName JavaDoc;
23 import javax.xml.soap.Name JavaDoc;
24 import javax.xml.soap.SOAPElement JavaDoc;
25 import javax.xml.soap.SOAPException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 /**
29  * Class SOAPElementImpl
30  *
31  * @author Jayachandra
32  * jayachandra@gmail.com
33  */

34 public class SOAPElementImpl extends NodeImpl implements SOAPElement JavaDoc {
35     /**
36      * Field omElement
37      * The corresponding OM object for SOAPElement is OMElement, so we would
38      * have a datamember of type OMElement in this class
39      */

40     protected org.apache.axis2.om.OMElement omElement;
41
42     /**
43      * Constructor SOAPElementImpl
44      * The standard constructor for being able to create SOAPElement given a omElement
45      * @param omElement
46      */

47     public SOAPElementImpl(org.apache.axis2.om.OMElement omElement)
48     {
49         super(omElement);
50         this.omElement = omElement;
51     }
52
53     /**
54      * Constructor SOAPElementImpl
55      * The empty constructor
56      */

57     public SOAPElementImpl() {
58         super();
59     }
60
61     /**
62      * Method getOMElement
63      * getter method on the data member omElement
64      * @return
65      */

66     public org.apache.axis2.om.OMElement getOMElement() {
67         return this.omElement;
68     }
69
70     /**
71      * Method addChildElement
72      * @param name
73      * @return SOAPElement
74      * @throws SOAPException
75      * @see javax.xml.soap.SOAPElement#addChildElement(javax.xml.soap.Name)
76      */

77     public SOAPElement JavaDoc addChildElement(Name JavaDoc name) throws SOAPException JavaDoc {
78         //We will create a new OMElement and add that as a child to the OMElement datamember that
79
//we are carrying along. And return back a wrapped SOAPElement corresponding to the
80
//created OMElement
81

82         //Since a <code>Name</code> object is given as parameter we should try to create an OMElement
83
//and register it with the contents of the <code>name</code> element
84
org.apache.axis2.om.OMElement newOMElement = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMElement(new QName JavaDoc(name.getURI(), name.getLocalName(), name.getPrefix()), omElement);
85         omElement.addChild(newOMElement);
86         return new SOAPElementImpl(newOMElement);
87     }
88
89     /**
90      * Method addChildElement
91      * @param localName
92      * @return
93      * @throws SOAPException
94      * @see javax.xml.soap.SOAPElement#addChildElement(java.lang.String)
95      */

96     public SOAPElement JavaDoc addChildElement(String JavaDoc localName) throws SOAPException JavaDoc {
97         //We will create a new OMElement and add that as a child to the OMElement datamember that
98
//we are carrying along. And return back a wrapped SOAPElement corresponding to the
99
//created OMElement
100
org.apache.axis2.om.OMElement newOMElement = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMElement(new QName JavaDoc(localName), omElement);
101         omElement.addChild(newOMElement);
102         return new SOAPElementImpl(newOMElement);
103     }
104
105     /**
106      * Method addChildElement
107      * @param localName
108      * @param prefix
109      * @return
110      * @throws SOAPException
111      * @see javax.xml.soap.SOAPElement#addChildElement(java.lang.String, java.lang.String)
112      */

113     public SOAPElement JavaDoc addChildElement(String JavaDoc localName, String JavaDoc prefix)
114             throws SOAPException JavaDoc {
115         org.apache.axis2.om.OMElement newOMElement = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMElement(new QName JavaDoc(null,localName,prefix), omElement);
116         omElement.addChild(newOMElement);
117         return new SOAPElementImpl(newOMElement);
118     }
119
120     /**
121      * Method addChildElement
122      * @param localName
123      * @param prefix
124      * @return
125      * @throws SOAPException
126      * @see javax.xml.soap.SOAPElement#addChildElement(java.lang.String, java.lang.String, java.lang.String)
127      */

128     public SOAPElement JavaDoc addChildElement(String JavaDoc localName, String JavaDoc prefix,
129             String JavaDoc uri) throws SOAPException JavaDoc {
130         org.apache.axis2.om.OMElement newOMElement = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMElement(new QName JavaDoc(uri,localName,prefix), omElement);
131         omElement.addChild(newOMElement);
132         return new SOAPElementImpl(newOMElement);
133     }
134
135     /**
136      * Method addChildElement
137      * @param element
138      * @return
139      * @throws SOAPException
140      * @see javax.xml.soap.SOAPElement#addChildElement(javax.xml.soap.SOAPElement)
141      */

142     public SOAPElement JavaDoc addChildElement(SOAPElement JavaDoc element)
143             throws SOAPException JavaDoc {
144         //TODO:
145
//The fragment rooted in element is either added as a whole or not at all, if there was an error.
146
//The fragment rooted in element cannot contain elements named “Envelope”, “Header” or “Body”
147
//and in the SOAP namespace. Any namespace prefixes present in the fragment should be fully
148
//resolved using appropriate namespace declarations within the fragment itself.
149

150         org.apache.axis2.om.OMElement omElementToAdd = ((SOAPElementImpl)element).getOMElement();
151         omElementToAdd.setParent(omElement);
152         omElement.addChild(omElementToAdd);
153         return new SOAPElementImpl(omElementToAdd);
154     }
155
156     /**
157      * Method addTextNode
158      * @param text
159      * @return
160      * @throws SOAPException
161      * @see javax.xml.soap.SOAPElement#addTextNode(java.lang.String)
162      */

163     public SOAPElement JavaDoc addTextNode(String JavaDoc text) throws SOAPException JavaDoc {
164         //We need to create an OMText node and add that to
165
//the omElement delegate member that we have with us. All this OMElement's setText() does
166
omElement.setText(text);
167         return this;
168     }
169
170     /**
171      * Method addAttribute
172      * This method adds an attribute to the underlying omElement datamember and returns ourselves
173      * @param name
174      * @param value
175      * @return ourself
176      * @throws SOAPException
177      * @see javax.xml.soap.SOAPElement#addAttribute(javax.xml.soap.Name, java.lang.String)
178      */

179     public SOAPElement JavaDoc addAttribute(Name JavaDoc name, String JavaDoc value)
180             throws SOAPException JavaDoc {
181         org.apache.axis2.om.OMNamespace omNS = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMNamespace(name.getURI(), name.getPrefix());
182         //TODO:
183
//The namespace of the attribute must be within the scope of the SOAPElement
184
//That check should be performed here.
185
omElement.addAttribute(name.getLocalName(), value, omNS);
186         return this;
187     }
188
189     /**
190      * Method addNamespaceDeclaration
191      * @param prefix
192      * @param uri
193      * @return
194      * @throws SOAPException
195      * @see javax.xml.soap.SOAPElement#addNamespaceDeclaration(java.lang.String, java.lang.String)
196      */

197     public SOAPElement JavaDoc addNamespaceDeclaration(String JavaDoc prefix, String JavaDoc uri)
198             throws SOAPException JavaDoc {
199         omElement.declareNamespace(uri, prefix);
200         return this;
201     }
202
203     /**
204      * Method getAttributeValue
205      * @param name
206      * @return
207      * @see javax.xml.soap.SOAPElement#getAttributeValue(javax.xml.soap.Name)
208      */

209     public String JavaDoc getAttributeValue(Name JavaDoc name) {
210         //This method is waiting on the finalization of the name for a method
211
//in OMElement that returns a OMAttribute from an input QName
212
return omElement.getFirstAttribute( new QName JavaDoc(name.getURI(),name.getLocalName(),name.getPrefix())).getValue();
213     }
214
215     /**
216      * Method getAllAttributes
217      * @return
218      * @see javax.xml.soap.SOAPElement#getAllAttributes()
219      */

220     public Iterator JavaDoc getAllAttributes() {
221         Iterator JavaDoc attrIter = omElement.getAttributes();
222         ArrayList JavaDoc arrayList = new ArrayList JavaDoc();
223         while (attrIter.hasNext()) {
224             Object JavaDoc o = attrIter.next();
225             if (o instanceof org.apache.axis2.om.OMAttribute) {
226                 //we need to create a SOAPNode for this and add to the arrayList
227
javax.xml.soap.Node JavaDoc soapNode = new NodeImpl((org.apache.axis2.om.OMAttribute)o);
228                 arrayList.add(soapNode);
229             }
230         }
231         return arrayList.iterator();
232     }
233
234     /**
235      * Method getNamespaceURI
236      * @param prefix
237      * @return
238      * @see javax.xml.soap.SOAPElement#getNamespaceURI(java.lang.String)
239      */

240     public String JavaDoc getNamespaceURI(String JavaDoc prefix) {
241         //Lets get all the inscope namespaces of this SOAPElement and iterate over them,
242
//whenever the prefix mathces break and return the corresponding URI.
243
Iterator JavaDoc nsIter = omElement.getAllDeclaredNamespaces();
244
245         //loop over to see a prefix matching namespace.
246
while(nsIter.hasNext()) {
247             Object JavaDoc o = nsIter.next();
248             if (o instanceof org.apache.axis2.om.OMNamespace) {
249                 org.apache.axis2.om.OMNamespace ns = (org.apache.axis2.om.OMNamespace)o;
250                 if( ns.getPrefix().equalsIgnoreCase(prefix))
251                     return ns.getName();
252             }
253         }
254         return null;
255     }
256
257     /**
258      * method getNamespacePrefixes
259      * This method returns an iterator over all the declared namespaces prefix names.
260      * @return Iterator
261      * @see javax.xml.soap.SOAPElement#getNamespacePrefixes()
262      */

263     public Iterator JavaDoc getNamespacePrefixes() {
264         //Get all declared namespace, make a list of their prefixes and return an iterator over that list
265
ArrayList JavaDoc prefixList = new ArrayList JavaDoc();
266         Iterator JavaDoc nsIter = omElement.getAllDeclaredNamespaces();
267         while(nsIter.hasNext()) {
268             Object JavaDoc o = nsIter.next();
269             if (o instanceof org.apache.axis2.om.OMNamespace) {
270                 org.apache.axis2.om.OMNamespace ns = (org.apache.axis2.om.OMNamespace)o;
271                 prefixList.add(ns.getPrefix());
272             }
273         }
274         return prefixList.iterator();
275     }
276
277     /**
278      * Method getElementName
279      * @return
280      * @see javax.xml.soap.SOAPElement#getElementName()
281      */

282     public Name JavaDoc getElementName() {
283         QName JavaDoc qName = omElement.getQName();
284         return (Name JavaDoc)(new PrefixedQName(qName.getNamespaceURI(), qName.getLocalPart(), qName.getPrefix()));
285     }
286
287     /**
288      * method removeAttribute
289      * This method removes an attribute with the specified name from the element.
290      * Returns true if the attribute was removed successfully; false if it was not
291      * @param name
292      * @return boolean
293      * @see javax.xml.soap.SOAPElement#removeAttribute(javax.xml.soap.Name)
294      */

295     public boolean removeAttribute(Name JavaDoc name) {
296         //get the OMAttribute with the given Name first, and call a removeAttribute(OMAttribute)
297
//method on the omElement datamember this SOAPElement has in it.
298
org.apache.axis2.om.OMAttribute attr = omElement.getFirstAttribute(new QName JavaDoc(name.getURI(),name.getLocalName(),name.getPrefix()));
299         if (attr != null) {
300             omElement.removeAttribute(attr);
301             return true;
302         }
303         return false;
304     }
305
306     /**
307      * method removeNamespaceDeclaration
308      * @param prefix
309      * @return
310      * @see javax.xml.soap.SOAPElement#removeNamespaceDeclaration(java.lang.String)
311      */

312     public boolean removeNamespaceDeclaration(String JavaDoc prefix) {
313         //TODO:
314
//I'm waiting on a removeNamespace method to be added to OMElement API
315
return false;
316     }
317
318     /**
319      * method getChildElements
320      * @return
321      * @see javax.xml.soap.SOAPElement#getChildElements()
322      */

323     public Iterator JavaDoc getChildElements() {
324         //Actually all the children are being treated as OMNodes and are being
325
//wrapped accordingly to a single type (SOAPElement) and being returned in an iterator.
326
//Text nodes and element nodes are all being treated alike here. Is that a serious issue???
327
Iterator JavaDoc childIter = omElement.getChildren();
328         ArrayList JavaDoc arrayList = new ArrayList JavaDoc();
329         while(childIter.hasNext()) {
330             Object JavaDoc o = childIter.next();
331             if (o instanceof org.apache.axis2.om.OMNode) {
332                 if(o instanceof org.apache.axis2.om.OMText){
333                     javax.xml.soap.Text JavaDoc childText = new TextImpl(((org.apache.axis2.om.OMText)o).getText());
334                     arrayList.add(childText);
335                 }else{
336                 SOAPElement JavaDoc childElement = new SOAPElementImpl((org.apache.axis2.om.OMElement)o);
337                 arrayList.add(childElement);
338                 }
339                 //javax.xml.soap.Node childElement = new NodeImpl((org.apache.axis2.om.OMNode)o);
340

341             }
342         }
343         return arrayList.iterator();
344     }
345
346     /**
347      * method getChildElements
348      * @param name
349      * @return
350      * @see javax.xml.soap.SOAPElement#getChildElements(javax.xml.soap.Name)
351      */

352     public Iterator JavaDoc getChildElements(Name JavaDoc name) {
353         QName JavaDoc qName = new QName JavaDoc(name.getURI(),name.getLocalName());
354         Iterator JavaDoc childIter = omElement.getChildrenWithName(qName);
355         ArrayList JavaDoc arrayList = new ArrayList JavaDoc();
356         while(childIter.hasNext()) {
357             Object JavaDoc o = childIter.next();
358             if (o instanceof org.apache.axis2.om.OMNode) {
359                 SOAPElement JavaDoc childElement = new SOAPElementImpl((org.apache.axis2.om.OMElement)o);
360                 arrayList.add(childElement);
361             }
362         }
363         return arrayList.iterator();
364     }
365
366     /**
367      * method setEncodingStyle
368      * @param encodingStyle
369      * @see javax.xml.soap.SOAPElement#setEncodingStyle(java.lang.String)
370      */

371     public void setEncodingStyle(String JavaDoc encodingStyle) throws SOAPException JavaDoc {
372
373         //TODO:
374
//Donno how to tackle this right now.
375
//Couldn't figure out corresponding functionality in OM
376
//Should re-visit
377

378     }
379
380     /**
381      * method getEncodingStyle
382      * @return
383      * @see javax.xml.soap.SOAPElement#getEncodingStyle()
384      */

385     public String JavaDoc getEncodingStyle() {
386         //TODO:
387
//This is incomplete, needs to be revisited later
388
return null;
389     }
390
391     /**
392      * method removeContents
393      * @see javax.xml.soap.SOAPElement#removeContents()
394      */

395     public void removeContents() {
396         //We will get all the children and iteratively call the detach() on all of 'em.
397
Iterator JavaDoc childIter = omElement.getChildren();
398
399         while(childIter.hasNext()) {
400             Object JavaDoc o = childIter.next();
401             if(o instanceof org.apache.axis2.om.OMNode)
402                 ((org.apache.axis2.om.OMNode)o).detach();
403         }
404     }
405
406     /**
407      * method getVisibleNamespacePrefixes
408      * @return
409      * @see javax.xml.soap.SOAPElement#getVisibleNamespacePrefixes()
410      */

411     public Iterator JavaDoc getVisibleNamespacePrefixes() {
412         //I'll recursively return all the declared namespaces till this node, including its parents etc.
413
Iterator JavaDoc namespacesIter = omElement.getAllDeclaredNamespaces();
414         ArrayList JavaDoc returnList = new ArrayList JavaDoc();
415         while(namespacesIter.hasNext()) {
416             Object JavaDoc o = namespacesIter.next();
417             if (o instanceof org.apache.axis2.om.OMNamespace) {
418                 javax.xml.soap.Node JavaDoc soapNode = new NodeImpl((org.apache.axis2.om.OMNamespace)o);
419                 returnList.add(soapNode);
420             }
421         }//taken care of adding namespaces of this node.
422
//now we have to take care of adding the namespaces that are in the scope till the level of
423
//this nodes' parent.
424
org.apache.axis2.om.OMContainer parent = omElement.getParent();
425         if (parent!=null && parent instanceof org.apache.axis2.om.OMElement) {
426             Iterator JavaDoc parentScopeNamespacesIter = ((org.apache.axis2.om.OMElement)parent).getAllDeclaredNamespaces();
427             while(parentScopeNamespacesIter.hasNext()) {
428                 Object JavaDoc o = parentScopeNamespacesIter.next();
429                 if (o instanceof org.apache.axis2.om.OMNamespace) {
430                     javax.xml.soap.Node JavaDoc soapNode = new NodeImpl((org.apache.axis2.om.OMNamespace)o);
431                     returnList.add(soapNode);
432                 }
433             }
434         }
435         return returnList.iterator();
436     }
437
438     /**
439      * method getTagName
440      * @return
441      * @see org.w3c.dom.Element#getTagName()
442      */

443     public String JavaDoc getTagName() {
444         return this.getLocalName();
445     }
446
447     /**
448      * method removeAttribute
449      * @param arg0
450      * @see org.w3c.dom.Element#removeAttribute(java.lang.String)
451      */

452     public void removeAttribute(String JavaDoc localName) throws DOMException JavaDoc {
453         //just got a localName, so assuming the namespace to be that of element
454
Name JavaDoc elementQualifiedName = this.getElementName();
455         //now try to remove that Attribute from this SOAPElement
456
this.removeAttribute(new PrefixedQName(elementQualifiedName.getURI(), localName, elementQualifiedName.getPrefix()));
457     }
458
459     /**
460      * method hasAttribute
461      * This method returns true when an attribute with a given name is specified
462      * on this element, false otherwise.
463      * @param localName
464      * @return
465      * @see org.w3c.dom.Element#hasAttribute(java.lang.String)
466      */

467     public boolean hasAttribute(String JavaDoc localName) {
468         Iterator JavaDoc attrIter = omElement.getAttributes();
469         while(attrIter.hasNext()) {
470             org.apache.axis2.om.OMAttribute omAttr = (org.apache.axis2.om.OMAttribute)(attrIter.next());
471             if (omAttr.getLocalName().equals(localName)) {
472                 return true;
473             }
474         }
475         return false;
476     }
477
478     /**
479      * method getAttribute
480      * This method retrieves the value of an attribute having specified localname.
481      * In case of an element having multiple attributes with same localname but declared
482      * in different namespaces, use of this method is unadvised.
483      * @param name
484      * @return String
485      * @see org.w3c.dom.Element#getAttribute(java.lang.String)
486      */

487     public String JavaDoc getAttribute(String JavaDoc name) {
488         Iterator JavaDoc attrIter = omElement.getAttributes();
489         while(attrIter.hasNext()) {
490             org.apache.axis2.om.OMAttribute omAttr = (org.apache.axis2.om.OMAttribute)(attrIter.next());
491             if (omAttr.getLocalName().equals(name)) {
492                 return omAttr.getValue();
493             }
494         }
495         return null;
496     }
497
498     /* (non-Javadoc)
499      * @see org.w3c.dom.Element#removeAttributeNS(java.lang.String, java.lang.String)
500      */

501     public void removeAttributeNS(String JavaDoc namespaceURI, String JavaDoc localName) throws DOMException JavaDoc {
502         Name JavaDoc name = new PrefixedQName(namespaceURI, localName, null);
503         this.removeAttribute(name);
504     }
505
506     /**
507      * Method setAttribute
508      * This method creates and adds an attribute with the given localName and value
509      * into the underlying OM. It uses the namespace of omElement datamember of this SOAPElement for the
510      * newly added attribute.
511      * @param localName
512      * @param value
513      * @return
514      * @see org.w3c.dom.Element#setAttribute(java.lang.String, java.lang.String)
515      */

516     public void setAttribute(String JavaDoc localName, String JavaDoc value) throws DOMException JavaDoc {
517         //We will create a OMAttribute for the given input params, add it
518
//to the omElement datamemeber
519
org.apache.axis2.om.OMAttribute omAttr = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMAttribute(localName, omElement.getNamespace(), value);
520         omElement.addAttribute(omAttr);
521     }
522
523     /**
524      * method hasAttributeNS
525      * This method returns true when an attribute with a given local name and
526      * namespace URI is specified on this element or has a default value, false
527      * otherwise.
528      * @param namespaceURI
529      * @param localName
530      * @return boolean
531      * @see org.w3c.dom.Element#hasAttributeNS(java.lang.String, java.lang.String)
532      */

533     public boolean hasAttributeNS(String JavaDoc namespaceURI, String JavaDoc localName) {
534         Iterator JavaDoc attrIter = omElement.getAttributes();
535         while(attrIter.hasNext()) {
536             org.apache.axis2.om.OMAttribute omAttr = (org.apache.axis2.om.OMAttribute)(attrIter.next());
537             if (omAttr.getLocalName().equals(localName) && omAttr.getNamespace().getName().equals(namespaceURI)) {
538                 return true;
539             }
540         }
541         return false;
542     }
543
544     /**
545      * method getAttributeNode
546      * This method retrieves an attribute node by the specified localname
547      * @param name
548      * @returns Attr
549      * @see org.w3c.dom.Element#getAttributeNode(java.lang.String)
550      */

551     public Attr JavaDoc getAttributeNode(String JavaDoc localName) {
552         Iterator JavaDoc attrIter = omElement.getAttributes();
553         while(attrIter.hasNext()) {
554             org.apache.axis2.om.OMAttribute omAttr = (org.apache.axis2.om.OMAttribute)(attrIter.next());
555             if (omAttr.getLocalName().equals(localName)) {
556                 //So we have the right OMAttribute in hand.
557
//wrap it into a org.w3c.dom.Attr object and return
558
return (new org.apache.axis2.saaj.AttrImpl(omAttr, this));
559             }
560         }
561         return null;
562     }
563
564     /**
565      * method removeAttributeNode
566      * This method removes the specified attribute node from this element.
567      * @param Attr
568      * The attribute node that should be removed.
569      * @return Attr
570      * The removed attribute node
571      * @see org.w3c.dom.Element#removeAttributeNode(org.w3c.dom.Attr)
572      */

573     public Attr JavaDoc removeAttributeNode(Attr JavaDoc oldAttr) throws DOMException JavaDoc {
574         //create a OMAttribute with the input object's localName, namespace (URI + prefix), value
575
//remove from underlying omElement such an attribute
576
//wrap the OMAttribute used for removing, into a dom Attr object and return.
577
org.apache.axis2.om.OMNamespace oldAttrNS = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMNamespace(oldAttr.getNamespaceURI(),oldAttr.getPrefix());
578         org.apache.axis2.om.OMAttribute omAttr = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMAttribute(oldAttr.getName(), oldAttrNS, oldAttr.getValue());
579         omElement.removeAttribute( omAttr );
580         return ( new org.apache.axis2.saaj.AttrImpl(omAttr, this));
581     }
582
583     /**
584      * Method setAttributeNode
585      * This method creates and adds an attribute corresponding to the supplied <code>Attr</code>
586      * object into the underlying OM. The attribute that gets added to OM is created against this.omElement's namespace
587      * @param attr - a dom Attr object
588      * @return Attr - a dom Attr object corresponding to the added attribute.
589      * @see org.w3c.dom.Element#setAttributeNode(org.w3c.dom.Attr)
590      */

591     public Attr JavaDoc setAttributeNode(Attr JavaDoc attr) throws DOMException JavaDoc {
592         //Create a OMAttribute out of the supplied Attr, add this to the
593
//omElement and now wrap the created OMAttribute into a Attr and return
594
org.apache.axis2.om.OMAttribute omAttr = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMAttribute(attr.getName(), omElement.getNamespace(), attr.getValue());
595         omElement.addAttribute(omAttr);
596         return (new org.apache.axis2.saaj.AttrImpl(omAttr, this));
597     }
598
599     /**
600      * Method setAttributeNode
601      * This method creates and adds an attribute corresponding to the supplied <code>Attr</code>
602      * object into the underlying OM. The attribute added is created against it's own namespace
603      * @param attr - a dom Attr object
604      * @return Attr - a dom Attr object corresponding to the added attribute.
605      * @see org.w3c.dom.Element#setAttributeNodeNS(org.w3c.dom.Attr)
606      */

607     public Attr JavaDoc setAttributeNodeNS(Attr JavaDoc attr) throws DOMException JavaDoc {
608         org.apache.axis2.om.OMNamespace attrNS = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMNamespace(attr.getNamespaceURI(), attr.getPrefix());
609         org.apache.axis2.om.OMAttribute omAttr = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMAttribute(attr.getName(), attrNS, attr.getValue());
610         omElement.addAttribute(omAttr);
611         return (new org.apache.axis2.saaj.AttrImpl(omAttr, this));
612     }
613
614     /**
615      * Method getElementsByTagName
616      * Returns a NodeList of all the descendant Elements with the given local
617      * name, in the order in which they are encountered in a preorder traversal
618      * of this Element tree.
619      * Current SOAPElement MAY not feature in the returned NodeList, only
620      * the descendant elements matching the criterion should be added.
621      * @param localName
622      * @return NodeList
623      * @see org.w3c.dom.Element#getElementsByTagName(java.lang.String)
624      */

625     public NodeList JavaDoc getElementsByTagName(String JavaDoc localName) {
626         Iterator JavaDoc childIter = this.getChildElements();
627         NodeListImpl returnList;
628         if (childIter==null)
629             return null;
630         else {
631             returnList = new NodeListImpl();
632             while(childIter.hasNext()) {
633                 NodeList JavaDoc list = getElementsByTagNamePreOrder((SOAPElement JavaDoc)childIter.next(), localName);
634                 //should *append* this list to the existing list. Remember, we are doing preorder
635
returnList.addNodeList(list);
636             }
637         }
638         return (NodeList JavaDoc)returnList;
639     }
640
641     private NodeList JavaDoc getElementsByTagNamePreOrder(SOAPElement JavaDoc child, String JavaDoc localName) {
642         NodeListImpl returnList = new NodeListImpl();
643         //We are doing preorder, so see if root itself is a match and place it first in the order
644
if(child.getLocalName().equals(localName)) {
645             //so this must be first in the returnList
646
returnList.addNode((org.w3c.dom.Node JavaDoc)child);
647         }
648         returnList.addNodeList(child.getElementsByTagName(localName));
649         return returnList;
650     }
651
652     /**
653      * method getAttributeNS
654      * This method retrieves the value of the attribute matching the specified namespaceURI, and localName
655      * @param namespaceURI
656      * @param localName
657      * @return String
658      * @see org.w3c.dom.Element#getAttributeNS(java.lang.String, java.lang.String)
659      */

660     public String JavaDoc getAttributeNS(String JavaDoc namespaceURI, String JavaDoc localName) {
661         Iterator JavaDoc attrIter = omElement.getAttributes();
662         while(attrIter.hasNext()) {
663             org.apache.axis2.om.OMAttribute omAttr = (org.apache.axis2.om.OMAttribute)(attrIter.next());
664             if (omAttr.getLocalName().equals(localName) && omAttr.getNamespace().getName().equals(namespaceURI)) {
665                 return omAttr.getValue();
666             }
667         }
668         return null;
669     }
670
671     /**
672      * Method setAttributeNS
673      * This method creates and adds an attribute with the given namespaceURI, localName and value
674      * into the underlying OM.
675      * @param localName
676      * @param value
677      * @return
678      * @see org.w3c.dom.Element#setAttributeNS(java.lang.String, java.lang.String, java.lang.String)
679      */

680     public void setAttributeNS(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc value)
681             throws DOMException JavaDoc {
682         //since no prefix is given, we create a OMNamespace with given URI and null prefix. How good is it???
683
org.apache.axis2.om.OMNamespace attrNS = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMNamespace(namespaceURI,null);
684         org.apache.axis2.om.OMAttribute omAttr = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMAttribute(localName, attrNS, value);
685         omElement.addAttribute( omAttr );
686     }
687
688     /**
689      * method getAttributeNodeNS
690      * This method retrieves an org.w3c.dom.Attr node matching the specified namespaceURI and localName
691      * @param namespaceURI
692      * @param localName
693      * @return Attr
694      * @see org.w3c.dom.Element#getAttributeNodeNS(java.lang.String, java.lang.String)
695      */

696     public Attr JavaDoc getAttributeNodeNS(String JavaDoc namespaceURI, String JavaDoc localName) {
697         Iterator JavaDoc attrIter = omElement.getAttributes();
698         while(attrIter.hasNext()) {
699             org.apache.axis2.om.OMAttribute omAttr = (org.apache.axis2.om.OMAttribute)(attrIter.next());
700             if (omAttr.getLocalName().equals(localName) && omAttr.getNamespace().getName().equals(namespaceURI)) {
701                 return (new org.apache.axis2.saaj.AttrImpl(omAttr, this));
702             }
703         }
704         return null;
705     }
706
707     /**
708      * getElementsByTagNameNS
709      * Returns a NodeList of all the descendant Elements with a given local
710      * name and namespace URI in the order in which they are encountered in a
711      * preorder traversal of this Element tree.
712      * Current SOAPElement MAY not feature in the returned NodeList, only
713      * the descendant elements matching the criterion should be added.
714      * @param namespaceURI
715      * @param localName
716      * @return NodeList
717      * @see org.w3c.dom.Element#getElementsByTagNameNS(java.lang.String, java.lang.String)
718      */

719     public NodeList JavaDoc getElementsByTagNameNS(String JavaDoc namespaceURI, String JavaDoc localName) {
720         Iterator JavaDoc childIter = this.getChildElements();
721         NodeListImpl returnList;
722         if (childIter==null)
723             return null;
724         else {
725             returnList = new NodeListImpl();
726             while(childIter.hasNext()) {
727                 NodeList JavaDoc list = getElementsByTagNameNSPreOrder((SOAPElement JavaDoc)childIter.next(), namespaceURI, localName);
728                 //should *append* this list to the existing list. Remember, we are doing preorder
729
returnList.addNodeList(list);
730             }
731         }
732         return (NodeList JavaDoc)returnList;
733     }
734
735     private NodeList JavaDoc getElementsByTagNameNSPreOrder(SOAPElement JavaDoc child, String JavaDoc namespaceURI, String JavaDoc localName) {
736         NodeListImpl returnList = new NodeListImpl();
737         //We are doing preorder, so see if root itself is a match and place it first in the order
738
if(child.getNamespaceURI().equals(namespaceURI) && child.getLocalName().equals(localName)) {
739             //so this must be first in the returnList
740
returnList.addNode((org.w3c.dom.Node JavaDoc)child);
741         }
742         returnList.addNodeList(child.getElementsByTagNameNS(namespaceURI, localName));
743         return returnList;
744     }
745
746 }
747
Popular Tags