use of org.jsoup.nodes.Element in project JsoupXpath by zhegexiaohuozi.
the class JXDocument method selN.
public List<JXNode> selN(String xpath) throws XpathSyntaxErrorException {
List<JXNode> finalRes = new LinkedList<>();
try {
CharStream input = CharStreams.fromString(xpath);
XpathLexer lexer = new XpathLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
XpathParser parser = new XpathParser(tokens);
parser.setErrorHandler(new DoFailOnErrorHandler());
ParseTree tree = parser.main();
XpathProcessor processor = new XpathProcessor(elements);
XValue calRes = processor.visit(tree);
if (calRes.isElements()) {
for (Element el : calRes.asElements()) {
finalRes.add(JXNode.e(el));
}
} else if (calRes.isList()) {
for (String str : calRes.asList()) {
finalRes.add(JXNode.t(str));
}
}
} catch (Exception e) {
String msg = "Please check the syntax of your xpath expr, ";
throw new XpathSyntaxErrorException(msg + ExceptionUtils.getRootCauseMessage(e), e);
}
return finalRes;
}
use of org.jsoup.nodes.Element in project JsoupXpath by zhegexiaohuozi.
the class CommonUtil method precedingSibling.
public static Elements precedingSibling(Element el) {
Elements rs = new Elements();
Element tmp = el.previousElementSibling();
while (tmp != null) {
rs.add(tmp);
tmp = tmp.previousElementSibling();
}
if (rs.size() > 0) {
return rs;
}
return null;
}
use of org.jsoup.nodes.Element in project JsoupXpath by zhegexiaohuozi.
the class Text method call.
/**
* 函数具体逻辑
*
* @param scope 上下文
* @return 计算好的节点
*/
@Override
public XValue call(Scope scope) {
Elements context = scope.context();
List<String> res = new LinkedList<>();
if (context != null && context.size() > 0) {
if (scope.isRecursion()) {
NodeTest allTextFun = Scanner.findNodeTestByName("allText");
return allTextFun.call(scope);
} else {
for (Element e : context) {
if ("script".equals(e.nodeName())) {
res.add(e.data());
} else {
res.add(e.ownText());
}
}
}
}
return XValue.create(res);
}
use of org.jsoup.nodes.Element in project JsoupXpath by zhegexiaohuozi.
the class ParentSelector method apply.
@Override
public XValue apply(Elements context) {
Set<Element> total = new HashSet<>();
Elements parents = new Elements();
for (Element el : context) {
total.add(el.parent());
}
parents.addAll(total);
return XValue.create(parents);
}
use of org.jsoup.nodes.Element in project JsoupXpath by zhegexiaohuozi.
the class PrecedingSelector method apply.
/**
* @param context
* @return res
*/
@Override
public XValue apply(Elements context) {
Elements preceding = new Elements();
Set<Element> total = new HashSet<>();
for (Element el : context) {
Elements p = el.parents();
for (Element pe : p) {
Elements ps = CommonUtil.precedingSibling(pe);
if (ps == null) {
continue;
}
total.addAll(ps);
}
Elements ps = CommonUtil.precedingSibling(el);
if (ps == null) {
continue;
}
total.addAll(ps);
}
preceding.addAll(total);
return XValue.create(preceding);
}
Aggregations