You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commons-dev@ws.apache.org by ve...@apache.org on 2010/08/24 21:29:19 UTC
svn commit: r988681 - in
/webservices/commons/trunk/modules/axiom/modules/axiom-dom/src:
main/java/org/apache/axiom/om/impl/dom/
test/java/org/apache/axiom/om/impl/dom/
Author: veithen
Date: Tue Aug 24 19:29:19 2010
New Revision: 988681
URL: http://svn.apache.org/viewvc?rev=988681&view=rev
Log:
WSCOMMONS-557: Fixed the implementation of the hasAttributes and getAttributes methods in ElementImpl. Thanks to Amila Jayasekara for reporting and analyzing this issue.
Modified:
webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java
webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/test/java/org/apache/axiom/om/impl/dom/DOMTestUtil.java
webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/test/java/org/apache/axiom/om/impl/dom/ElementImplTest.java
Modified: webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java?rev=988681&r1=988680&r2=988681&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java Tue Aug 24 19:29:19 2010
@@ -609,25 +609,10 @@ public class ElementImpl extends ParentN
/** Returns whether this element contains any attribute or not. */
public boolean hasAttributes() {
-
- boolean flag;
-
- // TODO : Review. This code doesn't make any sense since it always gets overwritten. WTF?
-// if (this.attributes != null) {
-// flag = (this.attributes.getLength() > 0);
-// }
-
- //The namespaces
- flag = this.namespace != null;
-
- if (!flag) {
- if (this.namespaces != null) {
- flag = !this.namespaces.isEmpty();
- }
- }
-
-
- return flag;
+ // DOM represents namespace declarations as attributes; therefore
+ // we need to check both "attributes" and "namespaces"
+ return attributes != null && attributes.getLength() > 0
+ || namespaces != null && !namespaces.isEmpty();
}
/*
@@ -1310,39 +1295,19 @@ public class ElementImpl extends ParentN
Iterator nsDecls = this.namespaces.keySet().iterator();
while (nsDecls.hasNext()) {
String prefix = (String) nsDecls.next();
- if (prefix != null && !"".equals(prefix)
- && !prefix.equals(OMConstants.XMLNS_NS_PREFIX)) {
- OMNamespace ns = (OMNamespace) this.namespaces.get(prefix);
- AttrImpl attr = new AttrImpl(this.ownerNode, prefix, ns
- .getNamespaceURI(), this.factory);
- attr.setOMNamespace(new NamespaceImpl(
- OMConstants.XMLNS_NS_URI,
- OMConstants.XMLNS_NS_PREFIX));
- attributeMap.addItem(attr);
- }
- }
+ if (prefix != null){
- // Set the default NS attr if any
- if (this.namespace != null
- && (this.namespace.getPrefix() == null || ""
- .equals(this.namespace.getPrefix()))
- && this.namespace.getNamespaceURI() != null) {
-
- // check if the parent of this element has the same namespace
- // as the default and if NOT add the attr
- boolean parentHasSameDefaultNS = this.parentNode != null &&
- this.parentNode.getNamespaceURI() != null &&
- this.parentNode.getNamespaceURI().equals(this.getNamespaceURI()) &&
- (this.parentNode.getPrefix() == null ||
- this.parentNode.getPrefix().equals(""));
-
- if (!parentHasSameDefaultNS) {
- AttrImpl attr = new AttrImpl(this.ownerNode, "xmlns",
- this.namespace.getNamespaceURI(), this.factory);
- attr.setOMNamespace(new NamespaceImpl(
- OMConstants.XMLNS_NS_URI,
- OMConstants.XMLNS_NS_PREFIX));
- attributeMap.addItem(attr);
+ OMNamespace ns = (OMNamespace)this.namespaces.get(prefix);
+
+ if ("".equals(prefix)) {
+ AttrImpl attr = new AttrImpl(this.ownerNode, "xmlns", ns.getNamespaceURI(), this.factory);
+ attr.setOMNamespace(new NamespaceImpl(OMConstants.XMLNS_NS_URI, null));
+ attributeMap.addItem(attr);
+ } else {
+ AttrImpl attr = new AttrImpl(this.ownerNode, prefix, ns.getNamespaceURI(), this.factory);
+ attr.setOMNamespace(new NamespaceImpl(OMConstants.XMLNS_NS_URI, OMConstants.XMLNS_NS_PREFIX));
+ attributeMap.addItem(attr);
+ }
}
}
}
Modified: webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/test/java/org/apache/axiom/om/impl/dom/DOMTestUtil.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/test/java/org/apache/axiom/om/impl/dom/DOMTestUtil.java?rev=988681&r1=988680&r2=988681&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/test/java/org/apache/axiom/om/impl/dom/DOMTestUtil.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/test/java/org/apache/axiom/om/impl/dom/DOMTestUtil.java Tue Aug 24 19:29:19 2010
@@ -41,7 +41,9 @@ public class DOMTestUtil {
public static void execute(Test test) throws Exception {
try {
- test.execute(new DocumentBuilderFactoryImpl());
+ DocumentBuilderFactory dbf = new DocumentBuilderFactoryImpl();
+ dbf.setNamespaceAware(true);
+ test.execute(dbf);
} catch (Throwable ex) {
Assert.fail("Invalid test case; execution failed with standard DOM implementation: "
+ ex.getMessage());
Modified: webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/test/java/org/apache/axiom/om/impl/dom/ElementImplTest.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/test/java/org/apache/axiom/om/impl/dom/ElementImplTest.java?rev=988681&r1=988680&r2=988681&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/test/java/org/apache/axiom/om/impl/dom/ElementImplTest.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/test/java/org/apache/axiom/om/impl/dom/ElementImplTest.java Tue Aug 24 19:29:19 2010
@@ -33,6 +33,7 @@ import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.InputSource;
+import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@@ -266,8 +267,7 @@ public class ElementImplTest extends OME
});
}
- // TODO: This provides evidence for WSCOMMONS-557 (not yet fixed)
- public void _testAttributes() throws Exception {
+ public void testAttributes() throws Exception {
DOMTestUtil.execute(new DOMTestUtil.Test() {
public void execute(DocumentBuilderFactory dbf) throws Exception {
Document doc = dbf.newDocumentBuilder().parse(getTestResource("attributetest.xml"));
@@ -281,8 +281,8 @@ public class ElementImplTest extends OME
NamedNodeMap attributes = directionResponse.getAttributes();
Attr attr = (Attr)attributes.item(0);
- assertEquals(attr.getName(), "xmlns");
- assertEquals(attr.getValue(), "http://www.example.org/webservices/");
+ assertEquals("xmlns", attr.getName());
+ assertEquals("http://www.example.org/webservices/", attr.getValue());
Element directionResult = (Element)bodyElement.getElementsByTagName("GetDirectionsResult").item(0);
assertFalse(directionResult.hasAttributes());
@@ -292,8 +292,8 @@ public class ElementImplTest extends OME
attributes = drivingDirection.getAttributes();
attr = (Attr)attributes.item(0);
- assertEquals(attr.getName(), "xmlns");
- assertEquals(attr.getValue(), "");
+ assertEquals("xmlns", attr.getName());
+ assertEquals("", attr.getValue());
Element route = (Element)drivingDirection.getElementsByTagName("route").item(0);
@@ -301,17 +301,16 @@ public class ElementImplTest extends OME
attributes = route.getAttributes();
attr = (Attr)attributes.item(0);
- assertEquals(attr.getName(), "distanceToTravel");
- assertEquals(attr.getValue(), "500m");
+ assertEquals("distanceToTravel", attr.getName());
+ assertEquals("500m", attr.getValue());
attr = (Attr)attributes.item(1);
- assertEquals(attr.getName(), "finalStep");
- assertEquals(attr.getValue(), "false");
+ assertEquals("finalStep", attr.getName());
+ assertEquals("false", attr.getValue());
attr = (Attr)attributes.item(2);
- assertEquals(attr.getName(), "id");
- assertEquals(attr.getValue(), "0");
-
+ assertEquals("id", attr.getName());
+ assertEquals("0", attr.getValue());
}
});
}
@@ -323,6 +322,44 @@ public class ElementImplTest extends OME
"<root><child xmlns=\"\"/></root>")));
Element element = (Element)doc.getDocumentElement().getFirstChild();
assertTrue(element.hasAttributes());
+ NamedNodeMap attributes = element.getAttributes();
+ assertEquals(1, attributes.getLength());
+ Attr attr = (Attr)attributes.item(0);
+ assertEquals("xmlns", attr.getName());
+ assertNull(attr.getPrefix());
+ assertEquals("xmlns", attr.getLocalName());
+ assertEquals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, attr.getNamespaceURI());
+ assertEquals("", attr.getValue());
+ }
+ });
+ }
+
+ public void testAttributes3() throws Exception {
+ DOMTestUtil.execute(new DOMTestUtil.Test() {
+ public void execute(DocumentBuilderFactory dbf) throws Exception {
+ Document doc = dbf.newDocumentBuilder().parse(new InputSource(new StringReader(
+ "<root><child xmlns:p=\"urn:ns1\"/></root>")));
+ Element element = (Element)doc.getDocumentElement().getFirstChild();
+ assertTrue(element.hasAttributes());
+ NamedNodeMap attributes = element.getAttributes();
+ assertEquals(1, attributes.getLength());
+ Attr attr = (Attr)attributes.item(0);
+ assertEquals("xmlns:p", attr.getName());
+ assertEquals("xmlns", attr.getPrefix());
+ assertEquals("p", attr.getLocalName());
+ assertEquals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, attr.getNamespaceURI());
+ assertEquals("urn:ns1", attr.getValue());
+ }
+ });
+ }
+
+ public void testAttributes4() throws Exception {
+ DOMTestUtil.execute(new DOMTestUtil.Test() {
+ public void execute(DocumentBuilderFactory dbf) throws Exception {
+ Document doc = dbf.newDocumentBuilder().parse(new InputSource(new StringReader(
+ "<root><child/></root>")));
+ Element element = (Element)doc.getDocumentElement().getFirstChild();
+ assertFalse(element.hasAttributes());
}
});
}