You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SelectorGeneratorCssToXPath.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright 2011, The gwtquery team.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.gwt.query.rebind;
  17. import com.google.gwt.core.ext.TreeLogger;
  18. import com.google.gwt.core.ext.UnableToCompleteException;
  19. import com.google.gwt.core.ext.typeinfo.JMethod;
  20. import com.google.gwt.query.client.Selector;
  21. import com.google.gwt.query.client.impl.SelectorEngineCssToXPath;
  22. import com.google.gwt.query.client.impl.SelectorEngineCssToXPath.ReplaceCallback;
  23. import com.google.gwt.query.client.impl.SelectorEngineCssToXPath.Replacer;
  24. import com.google.gwt.user.rebind.SourceWriter;
  25. import java.util.ArrayList;
  26. import java.util.regex.MatchResult;
  27. import java.util.regex.Matcher;
  28. import java.util.regex.Pattern;
  29. import javax.xml.xpath.XPath;
  30. import javax.xml.xpath.XPathExpressionException;
  31. import javax.xml.xpath.XPathFactory;
  32. /**
  33. * Compile time selector generator which translates selector into XPath at
  34. * compile time. It Uses the SelectorEngineCssToXpath to produce the xpath
  35. * selectors
  36. */
  37. public class SelectorGeneratorCssToXPath extends SelectorGeneratorBase {
  38. /**
  39. * The replacer implementation for the JVM.
  40. */
  41. public static final Replacer replacer = new Replacer() {
  42. public String replaceAll(String s, String r, Object o) {
  43. Pattern p = Pattern.compile(r);
  44. if (o instanceof ReplaceCallback) {
  45. final Matcher matcher = p.matcher(s);
  46. ReplaceCallback callback = (ReplaceCallback) o;
  47. while (matcher.find()) {
  48. final MatchResult matchResult = matcher.toMatchResult();
  49. ArrayList<String> argss = new ArrayList<>();
  50. for (int i = 0; i < matchResult.groupCount() + 1; i++) {
  51. argss.add(matchResult.group(i));
  52. }
  53. final String replacement = callback.foundMatch(argss);
  54. s = s.substring(0, matchResult.start()) + replacement
  55. + s.substring(matchResult.end());
  56. matcher.reset(s);
  57. }
  58. return s;
  59. } else {
  60. return p.matcher(s).replaceAll(o.toString());
  61. }
  62. }
  63. };
  64. private SelectorEngineCssToXPath engine = new SelectorEngineCssToXPath(
  65. replacer);
  66. protected String css2Xpath(String s) {
  67. return engine.css2Xpath(s);
  68. }
  69. private XPathFactory factory = XPathFactory.newInstance();
  70. private XPath xpath = factory.newXPath();
  71. protected void generateMethodBody(SourceWriter sw, JMethod method,
  72. TreeLogger treeLogger, boolean hasContext)
  73. throws UnableToCompleteException {
  74. String selector = method.getAnnotation(Selector.class).value();
  75. String xselector = css2Xpath(selector);
  76. // Validate the generated xpath selector.
  77. try {
  78. validateXpath(xselector);
  79. } catch (XPathExpressionException e1) {
  80. System.err.println("Invalid XPath generated selector, please revise it: " + xselector);
  81. if (!selector.equals(xselector)) {
  82. System.err
  83. .println("If your css2 selector syntax is correct, open an issue in the gwtquery project. cssselector:"
  84. + selector + " xpath: " + xselector);
  85. }
  86. throw new UnableToCompleteException();
  87. }
  88. sw.println("return "
  89. + wrap(method, "xpathEvaluate(\"" + xselector + "\", root)") + ";");
  90. }
  91. public void validateXpath(String xselector) throws XPathExpressionException {
  92. xpath.compile(xselector);
  93. }
  94. protected String getImplSuffix() {
  95. return "CssToXPath" + super.getImplSuffix();
  96. }
  97. }