You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by di...@apache.org on 2004/12/14 13:58:57 UTC
cvs commit: ws-axis/java/test/saaj TestDOM.java
dims 2004/12/14 04:58:57
Modified: java/src/org/apache/axis/message NodeImpl.java
java/test/saaj TestDOM.java
Log:
Fix for (AXIS-1713) SAAJ 1.2 (DOM) node trees are invalid - missing parents
from Mark Hansen
URL: http://nagoya.apache.org/jira/browse/AXIS-1713
Revision Changes Path
1.11 +47 -23 ws-axis/java/src/org/apache/axis/message/NodeImpl.java
Index: NodeImpl.java
===================================================================
RCS file: /home/cvs/ws-axis/java/src/org/apache/axis/message/NodeImpl.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- NodeImpl.java 18 Nov 2004 23:34:05 -0000 1.10
+++ NodeImpl.java 14 Dec 2004 12:58:56 -0000 1.11
@@ -191,7 +191,11 @@
* platform.
*/
public String getNodeValue() throws DOMException {
- return textRep.getData();
+ if (textRep == null) {
+ return null;
+ } else {
+ return textRep.getData();
+ }
}
/**
@@ -375,7 +379,7 @@
* <code>null</code>.
*/
public Node getParentNode() {
- return parent;
+ return (Node) getParent();
}
/**
@@ -384,12 +388,19 @@
*/
public Node getPreviousSibling() {
SOAPElement parent = getParentElement();
- Iterator iter = parent.getChildElements();
+ if (parent == null) {
+ return null;
+ }
+ NodeList nl = parent.getChildNodes();
+ int len = nl.getLength();
+ int i = 0;
Node previousSibling = null;
- while (iter.hasNext()) {
- if (iter.next() == this) {
+ while (i < len) {
+ if (nl.item(i) == this) {
return previousSibling;
}
+ previousSibling = nl.item(i);
+ i++;
}
return previousSibling; // should be null.
}
@@ -472,10 +483,21 @@
* from a different document than the one that created this node.
* <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly or
* if the previous parent of the node being inserted is readonly.
+ *
*/
public Node appendChild(Node newChild) throws DOMException {
+ if (newChild == null) {
+ throw new DOMException
+ (DOMException.HIERARCHY_REQUEST_ERR,
+ "Can't append a null node.");
+ }
initializeChildren();
+ // per DOM spec - must remove from tree. If newChild.parent == null,
+ // detachNode() does nothing. So this shouldn't hurt performace of
+ // serializers.
+ ((NodeImpl) newChild).detachNode();
children.add(newChild);
+ ((NodeImpl) newChild).parent = this;
return newChild;
}
@@ -490,25 +512,26 @@
* this node.
*/
public Node removeChild(Node oldChild) throws DOMException {
- if (children == null) {
- return null;
+ if (removeNodeFromChildList((NodeImpl) oldChild)) {
+ setDirty(true);
+ return oldChild;
}
+ throw new DOMException(DOMException.NOT_FOUND_ERR,
+ "NodeImpl Not found");
+ }
+
+ private boolean removeNodeFromChildList(NodeImpl n) {
boolean removed = false;
+ initializeChildren();
final Iterator itr = children.iterator();
while (itr.hasNext()) {
- final Node node = (Node) itr.next();
- if (node == oldChild) {
+ final NodeImpl node = (NodeImpl) itr.next();
+ if (node == n) {
removed = true;
itr.remove();
}
}
- if (!removed) {
- throw new DOMException(DOMException.NOT_FOUND_ERR,
- "NodeImpl Not found");
- } else {
- setDirty(removed);
- }
- return oldChild;
+ return removed;
}
/**
@@ -754,7 +777,8 @@
}
/**
- * set the parent node
+ * Set the parent node and invoke appendChild(this) to
+ * add this node to the parent's list of children.
* @param parent
* @throws SOAPException
*/
@@ -799,7 +823,9 @@
* get the dirty bit
* @return
*/
- public boolean isDirty() { return _isDirty; }
+ public boolean isDirty() {
+ return _isDirty;
+ }
/**
* set the dirty bit. will also set our parent as dirty, if there is one.
@@ -807,12 +833,10 @@
* @param dirty new value of the dirty bit
*/
public void setDirty(boolean dirty)
- {
- _isDirty = dirty;
+ {
+ _isDirty = dirty;
if (_isDirty && parent != null) {
- ((NodeImpl)parent).setDirty(true);
+ ((NodeImpl) parent).setDirty(true);
}
}
-
-
}
1.5 +32 -0 ws-axis/java/test/saaj/TestDOM.java
Index: TestDOM.java
===================================================================
RCS file: /home/cvs/ws-axis/java/test/saaj/TestDOM.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- TestDOM.java 8 Jul 2004 14:13:29 -0000 1.4
+++ TestDOM.java 14 Dec 2004 12:58:57 -0000 1.5
@@ -166,6 +166,38 @@
assertXMLEqual(xml, soapBodyElt.toString());
}
+ public void testForParent() throws Exception {
+ String NL = System.getProperty("line.separator");
+ String SOAP_STR =
+ "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">"+NL+
+ " <soapenv:Body>"+NL+
+ " <rtnElement>"+NL+
+ " <USAddress>"+NL+
+ " <name>Robert Smith</name>"+NL+
+ " </USAddress>"+NL+
+ " </rtnElement>"+NL+
+ " </soapenv:Body>"+NL+
+ "</soapenv:Envelope>";
+ java.io.InputStream is =
+ new java.io.ByteArrayInputStream(SOAP_STR.getBytes());
+ org.apache.axis.Message msg = new org.apache.axis.Message
+ (is, false, "text/xml;charset=utf-8", null);
+ // get the SOAPEnvelope (a SAAJ instance)
+ javax.xml.soap.SOAPEnvelope senv = msg.getSOAPEnvelope();
+ javax.xml.soap.SOAPBody sbody = senv.getBody();
+ javax.xml.soap.SOAPElement rtnElement =
+ (javax.xml.soap.SOAPElement) sbody.getChildElements().next();
+ javax.xml.soap.SOAPElement addrElement =
+ (javax.xml.soap.SOAPElement) rtnElement.getChildElements().next();
+ javax.xml.soap.SOAPElement nameElement =
+ (javax.xml.soap.SOAPElement) addrElement.getChildElements().next();
+ javax.xml.soap.Node textNode =
+ (javax.xml.soap.Node) nameElement.getChildElements().next();
+ assertNotNull
+ ("A DOM node parent (within a SOAPElement) should never be null.",
+ (org.w3c.dom.Node) textNode.getParentNode());
+ }
+
private String messageToString(SOAPMessage message) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
message.writeTo(baos);