36 lines
1.0 KiB
Java
36 lines
1.0 KiB
Java
package org.openautonomousconnection.htmlparser.dom;
|
|
|
|
import java.util.ArrayDeque;
|
|
import java.util.Deque;
|
|
|
|
public final class Document {
|
|
private final Element root;
|
|
|
|
public Document(Element root) {
|
|
this.root = root;
|
|
}
|
|
|
|
public Element getRoot() { return root; }
|
|
|
|
public Element createElement(String tag) { return new Element(tag); }
|
|
public TextNode createTextNode(String text) { return new TextNode(text); }
|
|
|
|
public Element getElementById(String id) {
|
|
if (id == null) return null;
|
|
Deque<DomNode> stack = new ArrayDeque<>();
|
|
stack.push(root);
|
|
while (!stack.isEmpty()) {
|
|
DomNode n = stack.pop();
|
|
if (n instanceof Element e) {
|
|
if (id.equals(e.id())) return e;
|
|
}
|
|
for (int i = n.getChildren().size() - 1; i >= 0; i--) stack.push(n.getChildren().get(i));
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public Element querySelector(String selector) {
|
|
return DomQuery.querySelector(root, selector);
|
|
}
|
|
}
|