com.itextpdf.styledxmlparser.jsoup.select.StructuralEvaluator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of styled-xml-parser Show documentation
Show all versions of styled-xml-parser Show documentation
Styled XML parser is used by iText modules to parse HTML and XML
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2024 Apryse Group NV
Authors: Apryse Software.
This program is offered under a commercial and under the AGPL license.
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
AGPL licensing:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
package com.itextpdf.styledxmlparser.jsoup.select;
import com.itextpdf.commons.utils.MessageFormatUtil;
import com.itextpdf.styledxmlparser.jsoup.nodes.Element;
/**
* Base structural evaluator.
*/
abstract class StructuralEvaluator extends Evaluator {
Evaluator evaluator;
static class Root extends Evaluator {
public boolean matches(Element root, Element element) {
return root == element;
}
}
static class Has extends StructuralEvaluator {
public Has(Evaluator evaluator) {
this.evaluator = evaluator;
}
public boolean matches(Element root, Element element) {
for (Element e : element.getAllElements()) {
if (e != element && evaluator.matches(element, e))
return true;
}
return false;
}
@Override
public String toString() {
return MessageFormatUtil.format(":has({0})", evaluator);
}
}
static class Not extends StructuralEvaluator {
public Not(Evaluator evaluator) {
this.evaluator = evaluator;
}
public boolean matches(Element root, Element node) {
return !evaluator.matches(root, node);
}
@Override
public String toString() {
return MessageFormatUtil.format(":not({0})", evaluator);
}
}
static class Parent extends StructuralEvaluator {
public Parent(Evaluator evaluator) {
this.evaluator = evaluator;
}
public boolean matches(Element root, Element element) {
if (root == element)
return false;
Element parent = (Element) element.parent();
while (parent != null) {
if (evaluator.matches(root, parent))
return true;
if (parent == root)
break;
parent = (Element) parent.parent();
}
return false;
}
@Override
public String toString() {
return MessageFormatUtil.format("{0} ", evaluator);
}
}
static class ImmediateParent extends StructuralEvaluator {
public ImmediateParent(Evaluator evaluator) {
this.evaluator = evaluator;
}
public boolean matches(Element root, Element element) {
if (root == element)
return false;
Element parent = (Element) element.parent();
return parent != null && evaluator.matches(root, parent);
}
@Override
public String toString() {
return MessageFormatUtil.format("{0} > ", evaluator);
}
}
static class PreviousSibling extends StructuralEvaluator {
public PreviousSibling(Evaluator evaluator) {
this.evaluator = evaluator;
}
public boolean matches(Element root, Element element) {
if (root == element)
return false;
Element prev = element.previousElementSibling();
while (prev != null) {
if (evaluator.matches(root, prev))
return true;
prev = prev.previousElementSibling();
}
return false;
}
@Override
public String toString() {
return MessageFormatUtil.format("{0} ~ ", evaluator);
}
}
static class ImmediatePreviousSibling extends StructuralEvaluator {
public ImmediatePreviousSibling(Evaluator evaluator) {
this.evaluator = evaluator;
}
public boolean matches(Element root, Element element) {
if (root == element)
return false;
Element prev = element.previousElementSibling();
return prev != null && evaluator.matches(root, prev);
}
@Override
public String toString() {
return MessageFormatUtil.format("{0} + ", evaluator);
}
}
}