|
|
@@ -25,7 +25,6 @@ import org.w3c.dom.Element;
|
|
|
import org.w3c.dom.Node;
|
|
|
import org.w3c.dom.NodeList;
|
|
|
import se.fishtank.css.selectors.Selectors;
|
|
|
-import se.fishtank.css.selectors.dom.W3CNode;
|
|
|
import se.fishtank.css.selectors.matching.SelectorMatcher;
|
|
|
import se.fishtank.css.selectors.selector.Selector;
|
|
|
|
|
|
@@ -40,41 +39,49 @@ public final class W3Selectors {
|
|
|
/* utility class */
|
|
|
}
|
|
|
|
|
|
- public static XMLSelector compile(String text) {
|
|
|
- if(text.startsWith("? ")) {
|
|
|
- return compileXP(text.substring(2));
|
|
|
+ public static XMLSelector compile(String selector) {
|
|
|
+ if(isxpath(selector)) {
|
|
|
+ return xpath(selector.substring(2));
|
|
|
}
|
|
|
- if(isTag(text)) {
|
|
|
- return new Name(text);
|
|
|
+ if(isname(selector)) {
|
|
|
+ return new Name(selector);
|
|
|
}
|
|
|
- return compileCSS(text);
|
|
|
+ return css(selector);
|
|
|
}
|
|
|
|
|
|
- private static boolean isTag(String name) {
|
|
|
- if(name.trim().equals("*")) {
|
|
|
+ public static XMLSelector css(String selector) {
|
|
|
+ return new CSS(selector);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static XMLSelector xpath(String selector) {
|
|
|
+ return new XPath(selector);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static XMLSelector text(String selector) {
|
|
|
+ return new Text(selector);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean isxpath(String selector) {
|
|
|
+ return selector.startsWith("? ");
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean isname(String selector) {
|
|
|
+ if(selector.trim().equals("*")) {
|
|
|
return true;
|
|
|
}
|
|
|
- for(int i=0,n=name.length(); i<n; i++) {
|
|
|
- if( !isTag(name.charAt(i))) {
|
|
|
+ for(int i=0,n=selector.length(); i<n; i++) {
|
|
|
+ if( !isname(selector.charAt(i))) {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
- private static boolean isTag(char cn) {
|
|
|
+ private static boolean isname(char cn) {
|
|
|
return (cn=='-') || (cn>='a' && cn<='z') || (cn>='A' && cn<='Z');
|
|
|
}
|
|
|
|
|
|
- public static XMLSelector compileCSS(String selector) {
|
|
|
- return new CSS(selector);
|
|
|
- }
|
|
|
-
|
|
|
- public static XMLSelector compileXP(String selector) {
|
|
|
- return new XPath(selector);
|
|
|
- }
|
|
|
-
|
|
|
- public static final class Text implements XMLSelector {
|
|
|
+ static final class Text implements XMLSelector {
|
|
|
|
|
|
private final Predicate<String> predicate;
|
|
|
|
|
|
@@ -100,7 +107,7 @@ public final class W3Selectors {
|
|
|
|
|
|
}
|
|
|
|
|
|
- public static final class Name implements XMLSelector {
|
|
|
+ static final class Name implements XMLSelector {
|
|
|
|
|
|
private final String name;
|
|
|
|
|
|
@@ -133,9 +140,9 @@ public final class W3Selectors {
|
|
|
|
|
|
}
|
|
|
|
|
|
- private static final class CSS implements XMLSelector {
|
|
|
+ static final class CSS implements XMLSelector {
|
|
|
|
|
|
- private static final SelectorMatcher<W3CNode> MATCHER = new SelectorMatcher<>();
|
|
|
+ private static final SelectorMatcher<W3Element> MATCHER = new SelectorMatcher<>();
|
|
|
|
|
|
private final List<Selector> scanner;
|
|
|
|
|
|
@@ -145,18 +152,17 @@ public final class W3Selectors {
|
|
|
|
|
|
@Override
|
|
|
public XMLElements apply(XMLContext c) {
|
|
|
- W3CNode node = new W3CNode(c.node().node());
|
|
|
- return new CElements(new Selectors<>(node).querySelectorAll(scanner));
|
|
|
+ return new CElements(new Selectors<>((W3Element)c.node()).querySelectorAll(scanner));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public boolean test(XMLContext c) {
|
|
|
- return MATCHER.matchesSelectors(scanner, new W3CNode(c.node().node()));
|
|
|
+ return MATCHER.matchesSelectors(scanner, (W3Element)c.node());
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
- private static final class XPath implements XMLSelector {
|
|
|
+ static final class XPath implements XMLSelector {
|
|
|
|
|
|
private final XPathExpression xpe;
|
|
|
|
|
|
@@ -189,8 +195,6 @@ public final class W3Selectors {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
|
|
|
}
|