
leap.lang.jsoup.parser.TreeBuilder Maven / Gradle / Ivy
The newest version!
package leap.lang.jsoup.parser;
import java.util.ArrayList;
import java.util.List;
import leap.lang.jsoup.helper.DescendableLinkedList;
import leap.lang.jsoup.helper.Validate;
import leap.lang.jsoup.nodes.Document;
import leap.lang.jsoup.nodes.Element;
/**
* @author Jonathan Hedley
*/
abstract class TreeBuilder {
CharacterReader reader;
Tokeniser tokeniser;
protected Document doc; // current doc we are building into
protected DescendableLinkedList stack; // the stack of open elements
protected String baseUri; // current base uri, for creating new elements
protected Token currentToken; // currentToken is used only for error tracking.
protected ParseErrorList errors; // null when not tracking errors
protected void initialiseParse(String input, String baseUri, ParseErrorList errors) {
Validate.notNull(input, "String input must not be null");
Validate.notNull(baseUri, "BaseURI must not be null");
doc = new Document(baseUri);
reader = new CharacterReader(input);
this.errors = errors;
tokeniser = new Tokeniser(reader, errors);
stack = new DescendableLinkedList();
this.baseUri = baseUri;
}
Document parse(String input, String baseUri) {
return parse(input, baseUri, ParseErrorList.noTracking());
}
Document parse(String input, String baseUri, ParseErrorList errors) {
initialiseParse(input, baseUri, errors);
runParser();
return doc;
}
protected void runParser() {
while (true) {
Token token = tokeniser.read();
process(token);
if (token.type == Token.TokenType.EOF)
break;
}
}
protected abstract boolean process(Token token);
protected Element currentElement() {
return stack.getLast();
}
//added by evan
protected Element tryGetCurrentElement() {
return stack.isEmpty() ? null : stack.getLast();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy