001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.saaj;
020:
021: import org.apache.axiom.om.OMAttribute;
022: import org.apache.axiom.om.OMContainer;
023: import org.apache.axiom.om.OMException;
024: import org.apache.axiom.om.OMNamespace;
025: import org.apache.axiom.om.OMNode;
026: import org.apache.axiom.om.impl.OMNamespaceImpl;
027: import org.apache.axiom.om.impl.dom.DocumentImpl;
028: import org.apache.axiom.om.impl.dom.ElementImpl;
029: import org.apache.axiom.om.impl.dom.NodeImpl;
030: import org.apache.axiom.om.impl.dom.TextImpl;
031: import org.apache.axiom.soap.SOAP11Constants;
032: import org.apache.axiom.soap.SOAP12Constants;
033: import org.apache.axiom.soap.impl.dom.soap11.SOAP11Factory;
034: import org.apache.axiom.soap.impl.dom.soap12.SOAP12Factory;
035: import org.w3c.dom.Attr;
036: import org.w3c.dom.DOMException;
037: import org.w3c.dom.Document;
038: import org.w3c.dom.NamedNodeMap;
039: import org.w3c.dom.Node;
040: import org.w3c.dom.NodeList;
041: import org.w3c.dom.Text;
042:
043: import javax.xml.namespace.QName;
044: import javax.xml.soap.Detail;
045: import javax.xml.soap.Name;
046: import javax.xml.soap.SOAPBody;
047: import javax.xml.soap.SOAPConstants;
048: import javax.xml.soap.SOAPElement;
049: import javax.xml.soap.SOAPEnvelope;
050: import javax.xml.soap.SOAPException;
051: import javax.xml.soap.SOAPFault;
052: import javax.xml.soap.SOAPFaultElement;
053: import javax.xml.soap.SOAPHeader;
054: import javax.xml.stream.XMLStreamException;
055: import java.net.URI;
056: import java.net.URISyntaxException;
057: import java.util.ArrayList;
058: import java.util.Collection;
059: import java.util.Iterator;
060:
061: public class SOAPElementImpl extends NodeImplEx implements SOAPElement {
062:
063: /**
064: * Using a delegate because we can't extend from org.apache.axiom.om.impl.dom.ElementImpl since
065: * this class must extend SNodeImpl
066: */
067: protected ElementImpl element;
068: private String encodingStyle;
069:
070: public SOAPElementImpl(ElementImpl element) {
071: super (element.getOMFactory());
072: this .element = element;
073: }
074:
075: /* (non-Javadoc)
076: * @see org.apache.axiom.om.OMNode#discard()
077: */
078: public void discard() throws OMException {
079: element.discard();
080: }
081:
082: /* (non-Javadoc)
083: */
084: public void internalSerialize(
085: javax.xml.stream.XMLStreamWriter writer)
086: throws XMLStreamException {
087: element.internalSerialize(writer);
088: }
089:
090: /* (non-Javadoc)
091: * @see org.apache.axiom.om.OMNode#serializeAndConsume(org.apache.axiom.om.impl.OMOutputImpl)
092: */
093: public void internalSerializeAndConsume(
094: javax.xml.stream.XMLStreamWriter writer)
095: throws XMLStreamException {
096: element.internalSerializeAndConsume(writer);
097: }
098:
099: /**
100: * Adds an attribute with the specified name and value to this <code>SOAPElement</code> object.
101: * <p/>
102: *
103: * @param name a <code>Name</code> object with the name of the attribute
104: * @param value a <code>String</code> giving the value of the attribute
105: * @return the <code>SOAPElement</code> object into which the attribute was inserted
106: * @throws SOAPException if there is an error in creating the Attribute
107: */
108: public SOAPElement addAttribute(Name name, String value)
109: throws SOAPException {
110: if (name.getURI() == null || name.getURI().trim().length() == 0) {
111: element.setAttribute(name.getLocalName(), value);
112: } else {
113: element.setAttributeNS(name.getURI(), name.getPrefix()
114: + ":" + name.getLocalName(), value);
115: }
116: return this ;
117: }
118:
119: /* (non-Javadoc)
120: * @see javax.xml.soap.SOAPElement#addChildElement(javax.xml.soap.Name)
121: */
122: public SOAPElement addChildElement(Name name) throws SOAPException {
123: String prefix = name.getPrefix();
124: return addChildElement(name.getLocalName(),
125: "".equals(prefix) ? null : prefix, name.getURI());
126: }
127:
128: /* (non-Javadoc)
129: * @see javax.xml.soap.SOAPElement#addChildElement(javax.xml.soap.SOAPElement)
130: */
131: public SOAPElement addChildElement(SOAPElement soapElement)
132: throws SOAPException {
133: String namespaceURI = soapElement.getNamespaceURI();
134: String prefix = soapElement.getPrefix();
135: String localName = soapElement.getLocalName();
136:
137: SOAPElementImpl childEle;
138: if (namespaceURI == null || namespaceURI.trim().length() == 0) {
139: childEle = new SOAPElementImpl(
140: (ElementImpl) getOwnerDocument().createElement(
141: localName));
142: } else {
143: element.declareNamespace(namespaceURI, prefix);
144: childEle = new SOAPElementImpl(
145: (ElementImpl) getOwnerDocument().createElementNS(
146: namespaceURI, localName));
147: }
148:
149: for (Iterator iter = soapElement.getAllAttributes(); iter
150: .hasNext();) {
151: Name name = (Name) iter.next();
152: childEle.addAttribute(name, soapElement
153: .getAttributeValue(name));
154: }
155:
156: for (Iterator iter = soapElement.getChildElements(); iter
157: .hasNext();) {
158: Object o = iter.next();
159: if (o instanceof Text) {
160: childEle.addTextNode(((Text) o).getData());
161: } else {
162: childEle.addChildElement((SOAPElement) o);
163: }
164: }
165:
166: childEle.element.setUserData(SAAJ_NODE, childEle, null);
167: if (namespaceURI != null && namespaceURI.trim().length() > 0) {
168: childEle.element.setNamespace(childEle.element
169: .declareNamespace(namespaceURI, prefix));
170: }
171: element.appendChild(childEle.element);
172: ((NodeImpl) childEle.element.getParentNode()).setUserData(
173: SAAJ_NODE, this , null);
174: childEle.setParentElement(this );
175: return childEle;
176: }
177:
178: public String getLocalName() {
179: return element.getLocalName();
180: }
181:
182: public String getNamespaceURI() {
183: return element.getNamespaceURI();
184: }
185:
186: /*
187: * Overidden in ElementImpl and AttrImpl.
188: */
189: public String getPrefix() {
190: return element.getPrefix();
191: }
192:
193: /* (non-Javadoc)
194: * @see javax.xml.soap.SOAPElement#addChildElement(java.lang.String, java.lang.String, java.lang.String)
195: */
196: public SOAPElement addChildElement(String localName, String prefix,
197: String uri) throws SOAPException {
198: OMNamespace omNamespace = element.declareNamespace(uri, prefix);
199: return addChildElement(localName, omNamespace.getPrefix());
200: }
201:
202: /* (non-Javadoc)
203: * @see javax.xml.soap.SOAPElement#addChildElement(java.lang.String, java.lang.String)
204: */
205: public SOAPElement addChildElement(String localName, String prefix)
206: throws SOAPException {
207: String namespaceURI = getNamespaceURI(prefix);
208:
209: if (namespaceURI == null) {
210: throw new SOAPException(
211: "Namespace not declared for the give prefix: "
212: + prefix);
213: }
214:
215: SOAPElementImpl childEle = new SOAPElementImpl(
216: (ElementImpl) getOwnerDocument().createElementNS(
217: namespaceURI, prefix + ":" + localName));
218:
219: childEle.element.setUserData(SAAJ_NODE, childEle, null);
220: childEle.element.setNamespace(childEle.element
221: .declareNamespace(namespaceURI, prefix));
222: element.appendChild(childEle.element);
223: ((NodeImpl) childEle.element.getParentNode()).setUserData(
224: SAAJ_NODE, this , null);
225: childEle.setParentElement(this );
226: return childEle;
227: }
228:
229: /* (non-Javadoc)
230: * @see javax.xml.soap.SOAPElement#addChildElement(java.lang.String)
231: */
232: public SOAPElement addChildElement(String localName)
233: throws SOAPException {
234: SOAPElementImpl childEle = new SOAPElementImpl(
235: (ElementImpl) getOwnerDocument().createElement(
236: localName));
237: childEle.element.setUserData(SAAJ_NODE, childEle, null);
238: element.appendChild(childEle.element);
239: ((NodeImpl) childEle.element.getParentNode()).setUserData(
240: SAAJ_NODE, this , null);
241: childEle.setParentElement(this );
242: return childEle;
243: }
244:
245: /* (non-Javadoc)
246: * @see javax.xml.soap.SOAPElement#addNamespaceDeclaration(java.lang.String, java.lang.String)
247: */
248: public SOAPElement addNamespaceDeclaration(String prefix, String uri)
249: throws SOAPException {
250: element.declareNamespace(uri, prefix);
251: return this ;
252: }
253:
254: /**
255: * Creates a new <code>Text</code> object initialized with the given <code>String</code> and
256: * adds it to this <code>SOAPElement</code> object.
257: *
258: * @param text a <code>String</code> object with the textual content to be added
259: * @return the <code>SOAPElement</code> object into which the new <code>Text</code> object was
260: * inserted
261: * @throws SOAPException if there is an error in creating the new <code>Text</code> object
262: */
263: public SOAPElement addTextNode(String text) throws SOAPException {
264: //OmElement.setText() will remove all the other text nodes that it contains
265: //Therefore create a text node and add it
266: //TODO: May need to address the situation where the prev sibling of the textnode itself is a textnode
267: Text textNode = getOwnerDocument().createTextNode(text);
268: NodeImpl node = ((NodeImpl) element.appendChild(textNode));
269: TextImplEx saajTextNode = new TextImplEx(text, this );
270: node.setUserData(SAAJ_NODE, saajTextNode, null);
271: return this ;
272: }
273:
274: /**
275: * Returns an iterator over all of the attribute names in this <CODE>SOAPElement</CODE> object.
276: * The iterator can be used to get the attribute names, which can then be passed to the method
277: * <CODE>getAttributeValue</CODE> to retrieve the value of each attribute.
278: *
279: * @return an iterator over the names of the attributes
280: */
281: public Iterator getAllAttributes() {
282: final Iterator attribIter = element.getAllAttributes();
283: Collection attribName = new ArrayList();
284: Attr attr;
285: while (attribIter.hasNext()) {
286: attr = (Attr) attribIter.next();
287: PrefixedQName qname;
288: if (attr.getNamespaceURI() == null
289: || attr.getNamespaceURI().trim().length() == 0) {
290: qname = new PrefixedQName(attr.getNamespaceURI(), attr
291: .getName(), attr.getPrefix());
292: } else {
293: qname = new PrefixedQName(attr.getNamespaceURI(), attr
294: .getLocalName(), attr.getPrefix());
295: }
296: attribName.add(qname);
297: }
298: return attribName.iterator();
299: }
300:
301: /* (non-Javadoc)
302: * @see javax.xml.soap.SOAPElement#getAttributeValue(javax.xml.soap.Name)
303: */
304: public String getAttributeValue(Name name) {
305: //This method is waiting on the finalization of the name for a method
306: //in OMElement that returns a OMAttribute from an input QName
307: final OMAttribute attribute = element.getAttribute(new QName(
308: name.getURI(), name.getLocalName(), name.getPrefix()));
309: if (attribute == null) {
310: return null;
311: }
312: return attribute.getAttributeValue();
313: }
314:
315: /**
316: * Returns an iterator over all the immediate content of this element. This includes
317: * <CODE>Text</CODE> objects as well as <CODE>SOAPElement</CODE> objects.
318: *
319: * @return an iterator over <CODE>Text</CODE> and <CODE>SOAPElement</CODE> contained within this
320: * <CODE>SOAPElement</CODE> object
321: */
322: public Iterator getChildElements() {
323: Iterator childIter = element.getChildren();
324: Collection childElements = new ArrayList();
325: while (childIter.hasNext()) {
326: childElements.add(toSAAJNode((org.w3c.dom.Node) childIter
327: .next()));
328: }
329: return childElements.iterator();
330: }
331:
332: /* (non-Javadoc)
333: * @see javax.xml.soap.SOAPElement#getChildElements(javax.xml.soap.Name)
334: */
335: public Iterator getChildElements(Name name) {
336: QName qName = new QName(name.getURI(), name.getLocalName());
337: Iterator childIter = element.getChildrenWithName(qName);
338: Collection childElements = new ArrayList();
339: while (childIter.hasNext()) {
340: childElements.add(toSAAJNode((org.w3c.dom.Node) childIter
341: .next()));
342: }
343: return childElements.iterator();
344: }
345:
346: /* (non-Javadoc)
347: * @see javax.xml.soap.SOAPElement#getElementName()
348: */
349: public Name getElementName() {
350: QName qName = element.getQName();
351: return new PrefixedQName(qName.getNamespaceURI(), qName
352: .getLocalPart(), qName.getPrefix());
353: }
354:
355: /* (non-Javadoc)
356: * @see javax.xml.soap.SOAPElement#getEncodingStyle()
357: */
358: public String getEncodingStyle() {
359: return this .encodingStyle;
360: }
361:
362: /* (non-Javadoc)
363: * @see javax.xml.soap.SOAPElement#getNamespacePrefixes()
364: */
365: public Iterator getNamespacePrefixes() {
366: //Get all declared namespace, make a list of their prefixes and return an iterator over that list
367: ArrayList prefixList = new ArrayList();
368: Iterator nsIter = element.getAllDeclaredNamespaces();
369: while (nsIter.hasNext()) {
370: Object o = nsIter.next();
371: if (o instanceof org.apache.axiom.om.OMNamespace) {
372: org.apache.axiom.om.OMNamespace ns = (org.apache.axiom.om.OMNamespace) o;
373: prefixList.add(ns.getPrefix());
374: }
375: }
376: return prefixList.iterator();
377: }
378:
379: /* (non-Javadoc)
380: * @see javax.xml.soap.SOAPElement#getNamespaceURI(java.lang.String)
381: */
382: public String getNamespaceURI(String prefix) {
383: return element.getNamespaceURI(prefix);
384: }
385:
386: /* (non-Javadoc)
387: * @see javax.xml.soap.SOAPElement#getVisibleNamespacePrefixes()
388: */
389: public Iterator getVisibleNamespacePrefixes() {
390: //I'll recursively return all the declared namespaces till this node, including its parents etc.
391: Iterator namespacesIter = element.getAllDeclaredNamespaces();
392: ArrayList returnList = new ArrayList();
393: while (namespacesIter.hasNext()) {
394: Object o = namespacesIter.next();
395: if (o instanceof OMNamespace) {
396: OMNamespace ns = (OMNamespace) o;
397: if (ns.getPrefix() != null) {
398: returnList.add(ns.getPrefix());
399: }
400: }
401: }
402: //taken care of adding namespaces of this node.
403: //now we have to take care of adding the namespaces that are in the scope till the level of
404: //this nodes' parent.
405: org.apache.axiom.om.OMContainer parent = element.getParent();
406: if (parent != null
407: && parent instanceof org.apache.axiom.om.OMElement) {
408: Iterator parentScopeNamespacesIter = ((org.apache.axiom.om.OMElement) parent)
409: .getAllDeclaredNamespaces();
410: while (parentScopeNamespacesIter.hasNext()) {
411: Object o = parentScopeNamespacesIter.next();
412: if (o instanceof OMNamespace) {
413: OMNamespace ns = (OMNamespace) o;
414: if (ns.getPrefix() != null) {
415: returnList.add(ns.getPrefix());
416: }
417: }
418: }
419: }
420: return returnList.iterator();
421: }
422:
423: public SOAPElement addAttribute(QName qname, String value)
424: throws SOAPException {
425: if (qname.getNamespaceURI() == null
426: || qname.getNamespaceURI().trim().length() == 0) {
427: element.setAttribute(qname.getLocalPart(), value);
428: } else {
429: element.setAttributeNS(qname.getNamespaceURI(), qname
430: .getPrefix()
431: + ":" + qname.getLocalPart(), value);
432: }
433: return this ;
434: }
435:
436: public SOAPElement addChildElement(QName qname)
437: throws SOAPException {
438: String prefix = qname.getPrefix();
439: return addChildElement(qname.getLocalPart(),
440: "".equals(prefix) ? null : prefix, qname
441: .getNamespaceURI());
442: }
443:
444: /**
445: * Creates a QName whose namespace URI is the one associated with the parameter, prefix, in the
446: * context of this SOAPElement. The remaining elements of the new QName are taken directly from
447: * the parameters, localName and prefix.
448: *
449: * @param localName - a String containing the local part of the name. prefix - a String
450: * containing the prefix for the name.
451: * @return a QName with the specified localName and prefix, and with a namespace that is
452: * associated with the prefix in the context of this SOAPElement. This namespace will be
453: * the same as the one that would be returned by getNamespaceURI(String) if it were
454: * given prefix as its parameter.
455: * @throws SOAPException - if the QName cannot be created.
456: * @since SAAJ 1.3
457: */
458: public QName createQName(String localName, String prefix)
459: throws SOAPException {
460: if (this .element.getNamespaceURI(prefix) == null) {
461: throw new SOAPException("Invalid prefix");
462: }
463: QName qname = null;
464: if (this .element.getOMFactory() instanceof SOAP11Factory) {
465: qname = new QName(
466: SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI,
467: localName, prefix);
468: } else if (this .element.getOMFactory() instanceof SOAP12Factory) {
469: qname = new QName(
470: SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI,
471: localName, prefix);
472: }
473: return qname;
474: }
475:
476: public Iterator getAllAttributesAsQNames() {
477: final Iterator attribIter = element.getAllAttributes();
478: Collection attributesAsQNames = new ArrayList();
479: Attr attr;
480: QName qname;
481: while (attribIter.hasNext()) {
482: attr = (Attr) attribIter.next();
483: //Check : attr.getLocalName() | attr.getName()
484: qname = new QName(attr.getNamespaceURI(), attr.getName(),
485: attr.getPrefix());
486: attributesAsQNames.add(qname);
487: }
488: return attributesAsQNames.iterator();
489: }
490:
491: public String getAttributeValue(QName qname) {
492: final OMAttribute attribute = element.getAttribute(qname);
493: if (attribute == null) {
494: return null;
495: }
496: return attribute.getAttributeValue();
497: }
498:
499: public Iterator getChildElements(QName qname) {
500: Iterator childIter = element.getChildrenWithName(qname);
501: Collection childElements = new ArrayList();
502: while (childIter.hasNext()) {
503: childElements.add(toSAAJNode((org.w3c.dom.Node) childIter
504: .next()));
505: }
506: return childElements.iterator();
507: }
508:
509: public QName getElementQName() {
510: return element.getQName();
511: }
512:
513: public boolean removeAttribute(QName qname) {
514: org.apache.axiom.om.OMAttribute attr = element
515: .getAttribute(qname);
516: if (attr != null) {
517: element.removeAttribute(attr);
518: return true;
519: }
520: return false;
521: }
522:
523: public SOAPElement setElementQName(QName newName)
524: throws SOAPException {
525: String localName = this .element.getLocalName();
526: if (org.apache.axiom.soap.SOAPConstants.BODY_LOCAL_NAME
527: .equals(localName)
528: || org.apache.axiom.soap.SOAPConstants.HEADER_LOCAL_NAME
529: .equals(localName)
530: || org.apache.axiom.soap.SOAPConstants.SOAPENVELOPE_LOCAL_NAME
531: .equals(localName)) {
532: throw new SOAPException(
533: "changing this element name is not allowed");
534: }
535: OMNamespace omNamespace = new OMNamespaceImpl(newName
536: .getNamespaceURI(), newName.getPrefix());
537: this .element.setNamespace(omNamespace);
538: this .element.setLocalName(newName.getLocalPart());
539: return this ;
540: }
541:
542: /* (non-Javadoc)
543: * @see javax.xml.soap.SOAPElement#removeAttribute(javax.xml.soap.Name)
544: */
545: public boolean removeAttribute(Name name) {
546: org.apache.axiom.om.OMAttribute attr = element
547: .getAttribute(new QName(name.getURI(), name
548: .getLocalName(), name.getPrefix()));
549: if (attr != null) {
550: element.removeAttribute(attr);
551: return true;
552: }
553: return false;
554: }
555:
556: /* (non-Javadoc)
557: * @see javax.xml.soap.SOAPElement#removeContents()
558: */
559: public void removeContents() {
560: //We will get all the children and iteratively call the detach() on all of 'em.
561: Iterator childIter = element.getChildElements();
562: while (childIter.hasNext()) {
563: Object o = childIter.next();
564: if (o instanceof org.apache.axiom.om.OMNode) {
565: ((org.apache.axiom.om.OMNode) o).detach();
566: }
567: }
568: }
569:
570: /* (non-Javadoc)
571: * @see javax.xml.soap.SOAPElement#removeNamespaceDeclaration(java.lang.String)
572: */
573: public boolean removeNamespaceDeclaration(String prefix) {
574: return element.removeNamespace(prefix);
575: }
576:
577: /**
578: * Sets the encoding style for this SOAPElement object to one specified.
579: *
580: * @param encodingStyle - a String giving the encoding style
581: * @throws IllegalArgumentException
582: * - if there was a problem in the encoding style being set. SOAPException - if setting
583: * the encodingStyle is invalid for this SOAPElement.
584: */
585: public void setEncodingStyle(String encodingStyle)
586: throws SOAPException {
587: if (this .element.getOMFactory() instanceof SOAP11Factory) {
588: try {
589: URI uri = new URI(encodingStyle);
590: if (!(this instanceof SOAPEnvelope)) {
591: if (!encodingStyle
592: .equals(SOAPConstants.URI_NS_SOAP_ENCODING)) {
593: throw new IllegalArgumentException(
594: "Invalid Encoding style : "
595: + encodingStyle);
596: }
597: }
598: this .encodingStyle = encodingStyle;
599: } catch (URISyntaxException e) {
600: throw new IllegalArgumentException(
601: "Invalid Encoding style : " + encodingStyle
602: + ":" + e);
603: }
604: } else if (this .element.getOMFactory() instanceof SOAP12Factory) {
605: if (this instanceof SOAPHeader || this instanceof SOAPBody
606: || this instanceof SOAPFault
607: || this instanceof SOAPFaultElement
608: || this instanceof SOAPEnvelope
609: || this instanceof Detail) {
610: throw new SOAPException(
611: "EncodingStyle attribute cannot appear in : "
612: + this );
613: }
614: }
615: }
616:
617: /* (non-Javadoc)
618: * @see org.apache.axiom.om.impl.OMNodeEx#setParent(org.apache.axiom.om.OMContainer)
619: */
620: public void setParent(OMContainer parentElement) {
621: element.setParent(parentElement);
622: }
623:
624: /* (non-Javadoc)
625: * @see org.w3c.dom.Element#getAttribute(java.lang.String)
626: */
627: public String getAttribute(String name) {
628: return element.getAttribute(name);
629: }
630:
631: /* (non-Javadoc)
632: * @see org.w3c.dom.Element#getAttributeNode(java.lang.String)
633: */
634: public Attr getAttributeNode(String name) {
635: return element.getAttributeNode(name);
636: }
637:
638: /* (non-Javadoc)
639: * @see org.w3c.dom.Element#getAttributeNodeNS(java.lang.String, java.lang.String)
640: */
641: public Attr getAttributeNodeNS(String namespaceURI, String localName) {
642: return element.getAttributeNodeNS(namespaceURI, localName);
643: }
644:
645: /* (non-Javadoc)
646: * @see org.w3c.dom.Element#getAttributeNS(java.lang.String, java.lang.String)
647: */
648: public String getAttributeNS(String namespaceURI, String localName) {
649: return element.getAttributeNS(namespaceURI, localName);
650: }
651:
652: /* (non-Javadoc)
653: * @see org.w3c.dom.Element#getElementsByTagName(java.lang.String)
654: */
655: public NodeList getElementsByTagName(String name) {
656: return element.getElementsByTagName(name);
657: }
658:
659: /* (non-Javadoc)
660: * @see org.w3c.dom.Element#getElementsByTagNameNS(java.lang.String, java.lang.String)
661: */
662: public NodeList getElementsByTagNameNS(String namespaceURI,
663: String localName) {
664: return element.getElementsByTagNameNS(namespaceURI, localName);
665: }
666:
667: /* (non-Javadoc)
668: * @see org.w3c.dom.Element#getTagName()
669: */
670: public String getTagName() {
671: return element.getTagName();
672: }
673:
674: /* (non-Javadoc)
675: * @see org.w3c.dom.Element#hasAttribute(java.lang.String)
676: */
677: public boolean hasAttribute(String name) {
678: return element.hasAttribute(name);
679: }
680:
681: /* (non-Javadoc)
682: * @see org.w3c.dom.Element#hasAttributeNS(java.lang.String, java.lang.String)
683: */
684: public boolean hasAttributeNS(String namespaceURI, String localName) {
685: return element.hasAttributeNS(namespaceURI, localName);
686: }
687:
688: /* (non-Javadoc)
689: * @see org.w3c.dom.Element#removeAttribute(java.lang.String)
690: */
691: public void removeAttribute(String name) throws DOMException {
692: element.removeAttribute(name);
693: }
694:
695: /* (non-Javadoc)
696: * @see org.w3c.dom.Element#removeAttributeNode(org.w3c.dom.Attr)
697: */
698: public Attr removeAttributeNode(Attr attr) throws DOMException {
699: return element.removeAttributeNode(attr);
700: }
701:
702: /* (non-Javadoc)
703: * @see org.w3c.dom.Element#removeAttributeNS(java.lang.String, java.lang.String)
704: */
705: public void removeAttributeNS(String namespaceURI, String localName)
706: throws DOMException {
707: element.removeAttributeNS(namespaceURI, localName);
708: }
709:
710: /* (non-Javadoc)
711: * @see org.w3c.dom.Element#setAttribute(java.lang.String, java.lang.String)
712: */
713: public void setAttribute(String name, String value)
714: throws DOMException {
715: element.setAttribute(name, value);
716: }
717:
718: /* (non-Javadoc)
719: * @see org.w3c.dom.Element#setAttributeNode(org.w3c.dom.Attr)
720: */
721: public Attr setAttributeNode(Attr attr) throws DOMException {
722: return element.setAttributeNode(attr);
723: }
724:
725: /* (non-Javadoc)
726: * @see org.w3c.dom.Element#setAttributeNodeNS(org.w3c.dom.Attr)
727: */
728: public Attr setAttributeNodeNS(Attr attr) throws DOMException {
729: return element.setAttributeNodeNS(attr);
730: }
731:
732: /* (non-Javadoc)
733: * @see org.w3c.dom.Element#setAttributeNS(java.lang.String, java.lang.String, java.lang.String)
734: */
735: public void setAttributeNS(String namespaceURI,
736: String qualifiedName, String value) throws DOMException {
737: element.setAttributeNS(namespaceURI, qualifiedName, value);
738: }
739:
740: /* (non-Javadoc)
741: * @see org.w3c.dom.Node#getNodeName()
742: */
743: public String getNodeName() {
744: return element.getNodeName();
745: }
746:
747: /* (non-Javadoc)
748: * @see org.w3c.dom.Node#getNodeType()
749: */
750: public short getNodeType() {
751: return Node.ELEMENT_NODE;
752: }
753:
754: public ElementImpl getElement() {
755: return element;
756: }
757:
758: /**
759: * Returns the parent element of this <code>Node</code> object. This method can throw an
760: * <code>UnsupportedOperationException</code> if the tree is not kept in memory.
761: *
762: * @return the <code>SOAPElement</code> object that is the parent of this <code>Node</code>
763: * object or <code>null</code> if this <code>Node</code> object is root
764: * @throws UnsupportedOperationException if the whole tree is not kept in memory
765: * @see #setParentElement(javax.xml.soap.SOAPElement) setParentElement(javax.xml.soap.SOAPElement)
766: */
767: public SOAPElement getParentElement() {
768: if (this .parentElement == null) {
769: return (SOAPElement) toSAAJNode(element.getParentNode());
770: }
771: return this .parentElement;
772: }
773:
774: public void setParentElement(SOAPElement parent)
775: throws SOAPException {
776: this .parentElement = parent;
777: this .element.setParent(((SOAPElementImpl) parent).element);
778: }
779:
780: /**
781: * Find the Document that this Node belongs to (the document in whose context the Node was
782: * created). The Node may or may not
783: */
784: public Document getOwnerDocument() {
785: return element.getOwnerDocument();
786: }
787:
788: /**
789: * Returns the the value of the immediate child of this <code>Node</code> object if a child
790: * exists and its value is text.
791: *
792: * @return a <code>String</code> with the text of the immediate child of this <code>Node</code>
793: * object if (1) there is a child and (2) the child is a <code>Text</code> object;
794: * <code>null</code> otherwise
795: */
796: public String getValue() {
797: if (element.getType() == OMNode.TEXT_NODE) {
798: return element.getText();
799: } else if (element.getType() == OMNode.ELEMENT_NODE) {
800: final OMNode firstOMChild = element.getFirstOMChild();
801: if (firstOMChild instanceof TextImpl) {
802: return ((TextImpl) firstOMChild).getData();
803: } else if (firstOMChild instanceof SOAPElementImpl) {
804: return ((SOAPElementImpl) firstOMChild).getValue();
805: }
806: }
807: return null;
808: }
809:
810: public org.w3c.dom.Node getFirstChild() {
811: return toSAAJNode(element.getFirstChild());
812: }
813:
814: /**
815: * Method getLastChild
816: *
817: * @see org.w3c.dom.Node#getLastChild()
818: */
819: public org.w3c.dom.Node getLastChild() {
820: return toSAAJNode(element.getLastChild());
821: }
822:
823: public Node getParentNode() {
824: return getParentElement();
825: }
826:
827: /** dom Node method */
828: public org.w3c.dom.Node getNextSibling() {
829: return toSAAJNode(element.getNextSibling());
830: }
831:
832: public Node getPreviousSibling() {
833: return toSAAJNode(element.getPreviousSibling());
834: }
835:
836: public NodeList getChildNodes() {
837: NodeList childNodes = element.getChildNodes();
838: NodeListImpl nodes = new NodeListImpl();
839:
840: for (int i = 0; i < childNodes.getLength(); i++) {
841: nodes.addNode(toSAAJNode(childNodes.item(i)));
842: }
843: return nodes;
844: }
845:
846: public boolean hasChildNodes() {
847: return element.hasChildNodes();
848: }
849:
850: /**
851: * If this is a Text node then this method will set its value, otherwise it sets the value of
852: * the immediate (Text) child of this node. The value of the immediate child of this node can be
853: * set only if, there is one child node and that node is a Text node, or if there are no
854: * children in which case a child Text node will be created.
855: *
856: * @param value the text to set
857: * @throws IllegalStateException if the node is not a Text node and either has more than one
858: * child node or has a child node that is not a Text node
859: */
860: public void setValue(String value) {
861: OMNode firstChild = element.getFirstOMChild();
862: if (firstChild == null) {
863: try {
864: this .addTextNode(value);
865: } catch (SOAPException e) {
866: throw new RuntimeException("Cannot add text node", e);
867: }
868: } else if (((org.w3c.dom.Node) firstChild).getNodeType() == javax.xml.soap.Node.TEXT_NODE
869: && firstChild.getNextOMSibling() == null) {
870: ((org.w3c.dom.Text) firstChild).setData(value);
871: } else {
872: throw new IllegalStateException(
873: "This node is not a Text node and "
874: + "either has more than one child node or has a child "
875: + "node that is not a Text node");
876: }
877: }
878:
879: public void detachNode() {
880: this .detach();
881: }
882:
883: public OMNode detach() {
884: OMNode omNode = this .element.detach();
885: this .parentElement = null;
886: return omNode;
887: }
888:
889: /**
890: * Returns the collection of attributes associated with this node, or null if none. At this
891: * writing, Element is the only type of node which will ever have attributes.
892: *
893: * @see org.apache.axiom.om.impl.dom.ElementImpl
894: */
895: public NamedNodeMap getAttributes() {
896: return element.getAttributes();
897: }
898: }
|