diff options
author | Manolo Carrasco <manolo@apache.org> | 2011-08-28 00:40:20 +0000 |
---|---|---|
committer | Manolo Carrasco <manolo@apache.org> | 2011-08-28 00:40:20 +0000 |
commit | e9305c4e70d3537b0e82997c612ad001cb3c4513 (patch) | |
tree | 9ffa7fb9a2f09d0e62bd17d2745669bafbf40b78 /gwtquery-core-2.0.1 | |
parent | 2ddf730da0221ab8048af059d4001efdbe0625e4 (diff) | |
download | gwtquery-e9305c4e70d3537b0e82997c612ad001cb3c4513.tar.gz gwtquery-e9305c4e70d3537b0e82997c612ad001cb3c4513.zip |
gwt 2.0.1 does not have shared RegExp
Diffstat (limited to 'gwtquery-core-2.0.1')
-rw-r--r-- | gwtquery-core-2.0.1/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorCssToXPath.java | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/gwtquery-core-2.0.1/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorCssToXPath.java b/gwtquery-core-2.0.1/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorCssToXPath.java new file mode 100644 index 00000000..04028ef0 --- /dev/null +++ b/gwtquery-core-2.0.1/src/main/java/com/google/gwt/query/rebind/SelectorGeneratorCssToXPath.java @@ -0,0 +1,112 @@ +/*
+ * Copyright 2011, The gwtquery team.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.query.rebind;
+
+import java.util.ArrayList;
+import java.util.regex.MatchResult;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+
+import com.google.gwt.core.ext.TreeLogger;
+import com.google.gwt.core.ext.UnableToCompleteException;
+import com.google.gwt.core.ext.typeinfo.JMethod;
+import com.google.gwt.query.client.Selector;
+import com.google.gwt.query.client.impl.SelectorEngineCssToXPath;
+import com.google.gwt.query.client.impl.SelectorEngineCssToXPath.ReplaceCallback;
+import com.google.gwt.query.client.impl.SelectorEngineCssToXPath.Replacer;
+import com.google.gwt.user.rebind.SourceWriter;
+
+/**
+ * Compile time selector generator which translates selector into XPath at
+ * compile time. It Uses the SelectorEngineCssToXpath to produce the xpath
+ * selectors
+ */
+public class SelectorGeneratorCssToXPath extends SelectorGeneratorBase {
+
+ /**
+ * The replacer implementation for the JVM.
+ */
+ public static final Replacer replacerJvm = new Replacer() {
+ public String replaceAll(String s, String r, Object o) {
+ Pattern p = Pattern.compile(r);
+ if (o instanceof ReplaceCallback) {
+ final Matcher matcher = p.matcher(s);
+ ReplaceCallback callback = (ReplaceCallback) o;
+ while (matcher.find()) {
+ final MatchResult matchResult = matcher.toMatchResult();
+ ArrayList<String> argss = new ArrayList<String>();
+ for (int i = 0; i < matchResult.groupCount() + 1; i++) {
+ argss.add(matchResult.group(i));
+ }
+ final String replacement = callback.foundMatch(argss);
+ s = s.substring(0, matchResult.start()) + replacement
+ + s.substring(matchResult.end());
+ matcher.reset(s);
+ }
+ return s;
+ } else {
+ return p.matcher(s).replaceAll(o.toString());
+ }
+ }
+ };
+
+ public static final Replacer replacer = replacerJvm;
+
+ private SelectorEngineCssToXPath engine = new SelectorEngineCssToXPath(
+ replacer);
+
+ protected String css2Xpath(String s) {
+ return engine.css2Xpath(s);
+ }
+
+ private XPathFactory factory = XPathFactory.newInstance();
+ private XPath xpath = factory.newXPath();
+
+ protected void generateMethodBody(SourceWriter sw, JMethod method,
+ TreeLogger treeLogger, boolean hasContext)
+ throws UnableToCompleteException {
+
+ String selector = method.getAnnotation(Selector.class).value();
+ String xselector = css2Xpath(selector);
+
+ // Validate the generated xpath selector.
+ try {
+ validateXpath(xselector);
+ } catch (XPathExpressionException e1) {
+ System.err.println("Invalid XPath generated selector, please revise it: " + xselector);
+ if (!selector.equals(xselector)) {
+ System.err.println("If your css2 selector syntax is correct, open an issue in the gwtquery project. cssselector:"
+ + selector + " xpath:" + xselector);
+ }
+ throw new UnableToCompleteException();
+ }
+
+ sw.println("return "
+ + wrap(method, "xpathEvaluate(\"" + xselector + "\", root)") + ";");
+ }
+
+ public void validateXpath(String xselector) throws XPathExpressionException {
+ xpath.compile(xselector);
+ }
+
+ protected String getImplSuffix() {
+ return "CssToXPath" + super.getImplSuffix();
+ }
+}
|