From: Manolo Carrasco Date: Sun, 28 Aug 2011 00:40:20 +0000 (+0000) Subject: gwt 2.0.1 does not have shared RegExp X-Git-Tag: release-1.3.2~229 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=e9305c4e70d3537b0e82997c612ad001cb3c4513;p=gwtquery.git gwt 2.0.1 does not have shared RegExp --- 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 argss = new ArrayList(); + 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(); + } +}