Permalink
Browse files

Fixes #73 - DOM getElementsByTagName() was returning 'self' as well.

This was broken in getElementsByTagNameNS() too.
This is resolved. Also the '*' concept has been added to getElementsByTagName().
This must have been 'over-thought' when I put it together. Not sure why the code was going out of the way to add in the 'self' element.
  • Loading branch information...
1 parent 6508466 commit 7d8ee2147ca4eb9fd7ee4abe4da44ac9bc674645 @rolfl rolfl committed Apr 19, 2012
Showing with 20 additions and 35 deletions.
  1. +20 −35 contrib/src/java/org/jdom2/contrib/dom/JDocument.java
View
55 contrib/src/java/org/jdom2/contrib/dom/JDocument.java
@@ -299,25 +299,18 @@ NodeList getElementsByTagName(final Parent xshadow, final String tagname) {
return EMPTYLIST;
}
final ArrayList<JElement> enodes = new ArrayList<JElement>();
- if (xshadow != null) {
- final Iterator<org.jdom2.Element> it =
- xshadow.getDescendants(Filters.element());
-
- org.jdom2.Element e = null;
- if (xshadow instanceof org.jdom2.Element) {
- e = (org.jdom2.Element)xshadow;
- } else {
- if (it.hasNext()) {
- e = it.next();
- }
- }
- while (e != null) {
- if ("*".equals(tagname) || tagname.equals(e.getQualifiedName())) {
- enodes.add(find(e));
- }
- e = it.hasNext() ? it.next() : null;
+ final boolean alltags = "*".equals(tagname);
+
+ final Iterator<org.jdom2.Element> it =
+ xshadow.getDescendants(Filters.element());
+
+ while (it.hasNext()) {
+ final org.jdom2.Element e = it.next();
+ if (alltags || tagname.equals(e.getQualifiedName())) {
+ enodes.add(find(e));
}
}
+
return new JNodeList(enodes);
}
@@ -333,26 +326,18 @@ NodeList getElementsByTagNameNS(final Parent xshadow, final String namespaceURI,
final boolean allname = "*".equals(localName);
final ArrayList<JElement> enodes = new ArrayList<JElement>();
- if (xshadow != null) {
- final Iterator<org.jdom2.Element> it =
- xshadow.getDescendants(Filters.element());
-
- org.jdom2.Element e = null;
- if (xshadow instanceof org.jdom2.Element) {
- e = (org.jdom2.Element)xshadow;
- } else {
- if (it.hasNext()) {
- e = it.next();
- }
- }
- while (e != null) {
- if ((allname || localName.equals(e.getName())) &&
- (alluri || namespaceURI.equals(e.getNamespaceURI()))) {
- enodes.add(find(e));
- }
- e = it.hasNext() ? it.next() : null;
+
+ final Iterator<org.jdom2.Element> it =
+ xshadow.getDescendants(Filters.element());
+
+ while (it.hasNext()) {
+ final org.jdom2.Element e = it.next();
+ if ((allname || localName.equals(e.getName())) &&
+ (alluri || namespaceURI.equals(e.getNamespaceURI()))) {
+ enodes.add(find(e));
}
}
+
return new JNodeList(enodes);
}

0 comments on commit 7d8ee21

Please sign in to comment.