Skip to content

Commit

Permalink
[#183] Add Match.attrNames(): Set<String>
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaseder committed Feb 17, 2023
1 parent 3ab02e9 commit d77fec5
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 138 deletions.
19 changes: 19 additions & 0 deletions jOOX-java-8/src/main/java/org/joox/Impl.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@
import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPathVariableResolver;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

Expand Down Expand Up @@ -1197,6 +1199,23 @@ public final Impl prepend(Element... content) {
return this;
}

@Override
public final Set<String> attrNames() {
Set<String> result = new LinkedHashSet<>();

for (Element e : this) {
NamedNodeMap m = e.getAttributes();
int length = m.getLength();

for (int i = 0; i < length; i++) {
Attr a = (Attr) m.item(i);
result.add(a.getName());
}
}

return result;
}

@Override
public final String attr(String name) {
if (size() > 0)
Expand Down
6 changes: 6 additions & 0 deletions jOOX-java-8/src/main/java/org/joox/Match.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
Expand Down Expand Up @@ -1752,6 +1753,11 @@ public interface Match extends Iterable<Element> {
// Manipulation of attributes
// ---------------------------------------------------------------------

/**
* Get the set of available attribute names in the set of matched elements.
*/
Set<String> attrNames();

/**
* Get an attribute from the first element in the set of matched elements,
* or <code>null</code> if the first element does not have that attribute.
Expand Down
Loading

0 comments on commit d77fec5

Please sign in to comment.