45 lines
1.3 KiB
Java
45 lines
1.3 KiB
Java
package com.github.dtschust.zork.parser.dom;
|
|
|
|
import com.github.dtschust.zork.parser.DOMUtils;
|
|
import org.w3c.dom.Element;
|
|
import org.w3c.dom.Node;
|
|
import org.w3c.dom.NodeList;
|
|
|
|
import java.util.AbstractList;
|
|
import java.util.List;
|
|
import java.util.RandomAccess;
|
|
import java.util.stream.Collectors;
|
|
|
|
public class Elements extends AbstractList<Element> implements RandomAccess {
|
|
private final NodeList list;
|
|
|
|
public static Elements byTagName(final Element parent, final String name) {
|
|
return new Elements(parent.getElementsByTagName(name));
|
|
}
|
|
|
|
public static List<String> innerTextByTagName(final Element parent, final String name) {
|
|
return Elements.byTagName(parent, name).stream()
|
|
.map(DOMUtils::getInnerText)
|
|
.collect(Collectors.toList());
|
|
}
|
|
|
|
private Elements(final NodeList l) {
|
|
list = l;
|
|
}
|
|
|
|
@Override
|
|
public Element get(int index) {
|
|
final Node e = list.item(index);
|
|
if (!(e instanceof Element)) {
|
|
// the contract of getElementsByTagName states that it returns a list of Element objects
|
|
throw new IllegalStateException("unreachable");
|
|
}
|
|
return (Element) e;
|
|
}
|
|
|
|
@Override
|
|
public int size() {
|
|
return list.getLength();
|
|
}
|
|
}
|