use of org.jsoup.nodes.Element in project JsoupXpath by zhegexiaohuozi.
the class XpathProcessor method visitStep.
@Override
public XValue visitStep(XpathParser.StepContext ctx) {
if (ctx.abbreviatedStep() != null && !ctx.abbreviatedStep().isEmpty()) {
return visit(ctx.abbreviatedStep());
}
boolean filterByAttr = false;
boolean isAxisOk = false;
if (ctx.axisSpecifier() != null && !ctx.axisSpecifier().isEmpty()) {
XValue axis = visit(ctx.axisSpecifier());
if (axis != null) {
isAxisOk = true;
if (axis.isElements()) {
updateCurrentContext(axis.asElements());
} else if (axis.isAttr()) {
filterByAttr = true;
}
}
}
if (ctx.nodeTest() != null && !ctx.nodeTest().isEmpty()) {
XValue nodeTest = visit(ctx.nodeTest());
if (filterByAttr) {
Elements context = currentScope().context();
String attrName = nodeTest.asString();
if (context.size() == 1) {
Element el = currentScope().singleEl();
return XValue.create(el.attr(attrName));
} else {
List<String> attrs = new LinkedList<>();
for (Element el : context) {
attrs.add(el.attr(attrName));
}
return XValue.create(attrs);
}
} else {
if (nodeTest.isExprStr()) {
String tagName = nodeTest.asString();
Elements current = currentScope().context();
if (currentScope().isRecursion()) {
updateCurrentContext(current.select(tagName));
} else {
Elements newContext = new Elements();
for (Element el : currentScope().context()) {
if (isAxisOk) {
if (el.nodeName().equals(tagName) || "*".equals(tagName)) {
newContext.add(el);
}
} else {
for (Element e : el.children()) {
if (e.nodeName().equals(tagName) || "*".equals(tagName)) {
newContext.add(e);
}
}
}
}
updateCurrentContext(newContext);
}
} else {
// nodeType 计算结果,直接返给上层
return nodeTest;
}
}
}
if (ctx.predicate() != null && ctx.predicate().size() > 0) {
for (XpathParser.PredicateContext predicate : ctx.predicate()) {
XValue predicateVal = visit(predicate);
updateCurrentContext(predicateVal.asElements());
}
}
return XValue.create(currentScope().context());
}
use of org.jsoup.nodes.Element in project JsoupXpath by zhegexiaohuozi.
the class AncestorOrSelfSelector method apply.
@Override
public XValue apply(Elements context) {
Set<Element> total = new HashSet<>();
Elements ancestor = new Elements();
for (Element el : context) {
total.addAll(el.parents());
// include self
total.add(el);
}
ancestor.addAll(total);
return XValue.create(ancestor);
}
use of org.jsoup.nodes.Element in project JsoupXpath by zhegexiaohuozi.
the class DescendantOrSelfSelector method apply.
@Override
public XValue apply(Elements context) {
Set<Element> total = new HashSet<>();
Elements descendant = new Elements();
for (Element el : context) {
Elements tmp = el.getAllElements();
total.addAll(tmp);
}
descendant.addAll(total);
return XValue.create(descendant);
}
use of org.jsoup.nodes.Element in project JsoupXpath by zhegexiaohuozi.
the class FollowingSelector method apply.
@Override
public XValue apply(Elements context) {
Elements following = new Elements();
Set<Element> total = new HashSet<>();
for (Element el : context) {
Elements p = el.parents();
for (Element pe : p) {
Elements fs = CommonUtil.followingSibling(pe);
if (fs == null) {
continue;
}
total.addAll(fs);
}
Elements fs = CommonUtil.followingSibling(el);
if (fs == null) {
continue;
}
total.addAll(fs);
}
following.addAll(total);
return XValue.create(following);
}
use of org.jsoup.nodes.Element in project JsoupXpath by zhegexiaohuozi.
the class FollowingSiblingOneSelector method apply.
/**
* @param context
* @return res
*/
@Override
public XValue apply(Elements context) {
Set<Element> total = new HashSet<>();
for (Element el : context) {
if (el.nextElementSibling() != null) {
total.add(el.nextElementSibling());
}
}
Elements newContext = new Elements();
newContext.addAll(total);
return XValue.create(newContext);
}
Aggregations