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.OMContainer;
022: import org.apache.axiom.om.OMElement;
023: import org.apache.axiom.om.OMException;
024: import org.apache.axiom.om.OMFactory;
025: import org.apache.axiom.om.OMNode;
026: import org.apache.axiom.om.impl.dom.ElementImpl;
027: import org.apache.axiom.om.impl.dom.NodeImpl;
028: import org.apache.axiom.soap.impl.dom.SOAPBodyImpl;
029: import org.apache.axiom.soap.impl.dom.SOAPEnvelopeImpl;
030: import org.w3c.dom.Attr;
031: import org.w3c.dom.Comment;
032: import org.w3c.dom.DOMException;
033: import org.w3c.dom.Text;
034: import org.w3c.dom.TypeInfo;
035:
036: import javax.xml.soap.Node;
037: import javax.xml.soap.SOAPElement;
038: import javax.xml.soap.SOAPException;
039:
040: /**
041: * A representation of a node (element) in a DOM representation of an XML document that provides
042: * some tree manipulation methods. This interface provides methods for getting the value of a node,
043: * for getting and setting the parent of a node, and for removing a node.
044: */
045: public abstract class NodeImplEx extends NodeImpl implements Node {
046:
047: /** @param factory */
048: protected NodeImplEx(OMFactory factory) {
049: super (factory);
050: }
051:
052: protected SOAPElement parentElement;
053: static final String SAAJ_NODE = "saaj.node";
054:
055: /**
056: * Removes this <code>Node</code> object from the tree. Once removed, this node can be garbage
057: * collected if there are no application references to it.
058: */
059: public void detachNode() {
060: this .detach();
061: }
062:
063: public OMNode detach() {
064: parentElement = null;
065: return null;
066: }
067:
068: /**
069: * Removes this <code>Node</code> object from the tree. Once removed, this node can be garbage
070: * collected if there are no application references to it.
071: */
072: public SOAPElement getParentElement() {
073: return this .parentElement;
074: }
075:
076: public OMContainer getParent() {
077: return (OMContainer) this .parentElement;
078: }
079:
080: /* public OMNode getOMNode() {
081: return omNode;
082: }*/
083:
084: /**
085: * Returns the the value of the immediate child of this <code>Node</code> object if a child
086: * exists and its valu e is text.
087: *
088: * @return a <code>String</code> with the text of the immediate child of this <code>Node</code>
089: * object if (1) there is a child and (2) the child is a <code>Text</code> object;
090: * <code>null</code> otherwise
091: */
092: public String getValue() {
093: if (this .getNodeType() == Node.TEXT_NODE) {
094: return this .getNodeValue();
095: } else if (this .getNodeType() == Node.ELEMENT_NODE) {
096: return ((NodeImplEx) (((OMElement) this ).getFirstOMChild()))
097: .getValue();
098: }
099: return null;
100: }
101:
102: /**
103: * Notifies the implementation that this <code>Node</code> object is no longer being used by the
104: * application and that the implementation is free to reuse this object for nodes that may be
105: * created later.
106: * <p/>
107: * Calling the method <code>recycleNode</code> implies that the method <code>detachNode</code>
108: * has been called previously.
109: */
110: public void recycleNode() {
111: // No corresponding implementation in OM
112: // There is no implementation in Axis 1.2 also
113: }
114:
115: /**
116: * Sets the parent of this <code>Node</code> object to the given <code>SOAPElement</code>
117: * object.
118: *
119: * @param parent the <code>SOAPElement</code> object to be set as the parent of this
120: * <code>Node</code> object
121: * @throws SOAPException if there is a problem in setting the parent to the given element
122: * @see #getParentElement() getParentElement()
123: */
124: public void setParentElement(SOAPElement parent)
125: throws SOAPException {
126: this .parentElement = parent;
127: }
128:
129: public void setType(int nodeType) throws OMException {
130: throw new UnsupportedOperationException("TODO");
131: }
132:
133: public int getType() {
134: return this .getNodeType();
135: }
136:
137: public TypeInfo getSchemaTypeInfo() {
138: // TODO - Fixme.
139: throw new UnsupportedOperationException("TODO");
140: }
141:
142: public void setIdAttribute(String name, boolean isId)
143: throws DOMException {
144: // TODO - Fixme.
145: throw new UnsupportedOperationException("TODO");
146: }
147:
148: public void setIdAttributeNS(String namespaceURI, String localName,
149: boolean isId) throws DOMException {
150: // TODO - Fixme.
151: throw new UnsupportedOperationException("TODO");
152: }
153:
154: public void setIdAttributeNode(Attr idAttr, boolean isId)
155: throws DOMException {
156: // TODO - Fixme.
157: throw new UnsupportedOperationException("TODO");
158: }
159:
160: /**
161: * Converts or extracts the SAAJ node from the given DOM Node (domNode)
162: *
163: * @param domNode
164: * @return the SAAJ Node corresponding to the domNode
165: */
166: javax.xml.soap.Node toSAAJNode(org.w3c.dom.Node domNode) {
167: if (domNode == null) {
168: return null;
169: }
170: if (domNode instanceof org.w3c.dom.Text
171: || domNode instanceof org.w3c.dom.Comment) {
172: org.w3c.dom.Node prevSiblingDOMNode = domNode
173: .getPreviousSibling();
174: org.w3c.dom.Node nextSiblingDOMNode = domNode
175: .getNextSibling();
176:
177: TextImplEx saajTextNode = (TextImplEx) ((NodeImpl) domNode)
178: .getUserData(SAAJ_NODE);
179: if (saajTextNode == null) {
180: // if SAAJ node has not been set in userData, try to construct it
181: return toSAAJNode2(domNode);
182: }
183: saajTextNode.setPreviousSibling(prevSiblingDOMNode);
184: saajTextNode.setNextSibling(nextSiblingDOMNode);
185: return saajTextNode;
186: }
187: Node saajNode = (Node) ((NodeImpl) domNode)
188: .getUserData(SAAJ_NODE);
189: if (saajNode == null) { // if SAAJ node has not been set in userData, try to construct it
190: return toSAAJNode2(domNode);
191: }
192: return saajNode;
193: }
194:
195: private javax.xml.soap.Node toSAAJNode2(org.w3c.dom.Node domNode) {
196: if (domNode == null) {
197: return null;
198: }
199: if (domNode instanceof org.w3c.dom.Text) {
200: Text text = (Text) domNode;
201: org.w3c.dom.Node prevSiblingDOMNode = text
202: .getPreviousSibling();
203: org.w3c.dom.Node nextSiblingDOMNode = text.getNextSibling();
204: SOAPElementImpl parent = new SOAPElementImpl(
205: (ElementImpl) domNode.getParentNode());
206: TextImplEx saajTextNode = new TextImplEx(text.getData(),
207: parent, prevSiblingDOMNode, nextSiblingDOMNode);
208: ((NodeImpl) domNode).setUserData(SAAJ_NODE, saajTextNode,
209: null);
210: return saajTextNode;
211: } else if (domNode instanceof org.w3c.dom.Comment) {
212: Comment comment = (Comment) domNode;
213: org.w3c.dom.Node prevSiblingDOMNode = comment
214: .getPreviousSibling();
215: org.w3c.dom.Node nextSiblingDOMNode = comment
216: .getNextSibling();
217: SOAPElementImpl parent = new SOAPElementImpl(
218: (ElementImpl) domNode.getParentNode());
219: TextImplEx saajTextNode = new TextImplEx("<!--"
220: + comment.getData() + "-->", parent,
221: prevSiblingDOMNode, nextSiblingDOMNode);
222: ((NodeImpl) domNode).setUserData(SAAJ_NODE, saajTextNode,
223: null);
224: return saajTextNode;
225: } else if (domNode instanceof org.apache.axiom.soap.impl.dom.SOAPBodyImpl) {
226: org.apache.axiom.soap.impl.dom.SOAPBodyImpl doomSOAPBody = (SOAPBodyImpl) domNode;
227: javax.xml.soap.SOAPBody saajSOAPBody = new org.apache.axis2.saaj.SOAPBodyImpl(
228: doomSOAPBody);
229: doomSOAPBody.setUserData(SAAJ_NODE, saajSOAPBody, null);
230: return saajSOAPBody;
231: } else if (domNode instanceof org.apache.axiom.soap.impl.dom.SOAPEnvelopeImpl) {
232: org.apache.axiom.soap.impl.dom.SOAPEnvelopeImpl doomSOAPEnv = (SOAPEnvelopeImpl) domNode;
233: javax.xml.soap.SOAPEnvelope saajEnvelope = new org.apache.axis2.saaj.SOAPEnvelopeImpl(
234: doomSOAPEnv);
235: doomSOAPEnv.setUserData(SAAJ_NODE, saajEnvelope, null);
236: return saajEnvelope;
237: } else if (domNode instanceof org.apache.axiom.soap.impl.dom.SOAPFaultNodeImpl) {
238: org.apache.axiom.soap.impl.dom.SOAPFaultNodeImpl doomSOAPFaultNode = (org.apache.axiom.soap.impl.dom.SOAPFaultNodeImpl) domNode;
239: javax.xml.soap.SOAPFaultElement saajSOAPFaultEle = new org.apache.axis2.saaj.SOAPFaultElementImpl(
240: doomSOAPFaultNode);
241: doomSOAPFaultNode.setUserData(SAAJ_NODE, saajSOAPFaultEle,
242: null);
243: return saajSOAPFaultEle;
244: } else if (domNode instanceof org.apache.axiom.soap.impl.dom.SOAPFaultDetailImpl) {
245: org.apache.axiom.soap.impl.dom.SOAPFaultDetailImpl doomSOAPFaultDetail = (org.apache.axiom.soap.impl.dom.SOAPFaultDetailImpl) domNode;
246: javax.xml.soap.Detail saajDetail = new org.apache.axis2.saaj.DetailImpl(
247: doomSOAPFaultDetail);
248: doomSOAPFaultDetail
249: .setUserData(SAAJ_NODE, saajDetail, null);
250: return saajDetail;
251: } else if (domNode instanceof org.apache.axiom.soap.impl.dom.SOAPFaultImpl) {
252: org.apache.axiom.soap.impl.dom.SOAPFaultImpl doomSOAPFault = (org.apache.axiom.soap.impl.dom.SOAPFaultImpl) domNode;
253: javax.xml.soap.SOAPFault saajSOAPFault = new org.apache.axis2.saaj.SOAPFaultImpl(
254: doomSOAPFault);
255: doomSOAPFault.setUserData(SAAJ_NODE, saajSOAPFault, null);
256: return saajSOAPFault;
257: } else if (domNode instanceof org.apache.axiom.soap.impl.dom.SOAPHeaderBlockImpl) {
258: org.apache.axiom.soap.impl.dom.SOAPHeaderBlockImpl doomSOAPHeaderBlock = (org.apache.axiom.soap.impl.dom.SOAPHeaderBlockImpl) domNode;
259: javax.xml.soap.SOAPHeaderElement saajSOAPHeaderEle = new org.apache.axis2.saaj.SOAPHeaderElementImpl(
260: doomSOAPHeaderBlock);
261: doomSOAPHeaderBlock.setUserData(SAAJ_NODE,
262: saajSOAPHeaderEle, null);
263: return saajSOAPHeaderEle;
264: } else if (domNode instanceof org.apache.axiom.soap.impl.dom.SOAPHeaderImpl) {
265: org.apache.axiom.soap.impl.dom.SOAPHeaderImpl doomSOAPHeader = (org.apache.axiom.soap.impl.dom.SOAPHeaderImpl) domNode;
266: javax.xml.soap.SOAPHeader saajSOAPHeader = new org.apache.axis2.saaj.SOAPHeaderImpl(
267: doomSOAPHeader);
268: doomSOAPHeader.setUserData(SAAJ_NODE, saajSOAPHeader, null);
269: return saajSOAPHeader;
270: } else { // instanceof org.apache.axis2.om.impl.dom.ElementImpl
271: ElementImpl doomElement = (ElementImpl) domNode;
272: SOAPElementImpl saajSOAPElement = new SOAPElementImpl(
273: doomElement);
274: doomElement.setUserData(SAAJ_NODE, saajSOAPElement, null);
275: return saajSOAPElement;
276: }
277: }
278: }
|