Example usage for com.google.gwt.user.rebind SourceWriter println

List of usage examples for com.google.gwt.user.rebind SourceWriter println

Introduction

In this page you can find the example usage for com.google.gwt.user.rebind SourceWriter println.

Prototype

void println(String s);

Source Link

Usage

From source file:com.cgxlib.xq.rebind.SelectorGeneratorBase.java

License:Apache License

public void generateMethod(SourceWriter sw, JMethod method, TreeLogger logger)
        throws UnableToCompleteException {
    Selector selectorAnnotation = method.getAnnotation(Selector.class);
    if (selectorAnnotation == null) {
        return;//from  w ww  .j  a v a 2 s  .  co m
    }

    JParameter[] params = method.getParameters();

    String retType = method.getReturnType().getParameterizedQualifiedSourceName();
    sw.print("public final " + retType + " " + method.getName());
    boolean hasContext = false;
    if (params.length == 0) {
        sw.print("()");
    } else if (params.length == 1) {
        JClassType type = params[0].getType().isClassOrInterface();
        if (type != null && type.isAssignableTo(nodeType)) {
            sw.print("(Node root)");
            hasContext = true;
        }
    }
    sw.println(" {");
    sw.indent();
    Selector sel = method.getAnnotation(Selector.class);

    if (sel != null && sel.value().matches("^#\\w+$")) {
        // short circuit #foo
        sw.println("return " + wrap(method,
                "JsNodeArray.create(((Document)root).getElementById(\"" + sel.value().substring(1) + "\"))")
                + ";");
    } else if (sel != null && sel.value().matches("^\\w+$")) {
        // short circuit FOO
        sw.println("return "
                + wrap(method,
                        "JsNodeArray.create(((Element)root).getElementsByTagName(\"" + sel.value() + "\"))")
                + ";");
    } else if (sel != null && sel.value().matches("^\\.\\w+$") && hasGetElementsByClassName()) {
        // short circuit .foo for browsers with native getElementsByClassName
        sw.println("return " + wrap(method,
                "JsNodeArray.create(getElementsByClassName(\"" + sel.value().substring(1) + "\", root))")
                + ";");
    } else {
        generateMethodBody(sw, method, logger, hasContext);
    }
    sw.outdent();
    sw.println("}");
}

From source file:com.cgxlib.xq.rebind.SelectorGeneratorBase.java

License:Apache License

private void genGetAllMethod(SourceWriter sw, JMethod[] methods, TreeLogger treeLogger) {
    sw.println("public DeferredSelector[] getAllSelectors() {return ds;}");
    sw.println("private final DeferredSelector[] ds = new DeferredSelector[] {");
    sw.indent();/*from   www  .j  a v a 2s  .  c o  m*/
    for (JMethod m : methods) {
        Selector selectorAnnotation = m.getAnnotation(Selector.class);
        if (selectorAnnotation == null) {
            continue;
        }
        String selector = selectorAnnotation.value();

        sw.println("new DeferredSelector() {");
        sw.indent();
        sw.println("public String getSelector() { return \"" + selector + "\"; }");
        sw.println("public NodeList<Element> runSelector(Node ctx) { return "
                + (m.getName() + (m.getParameters().length == 0 ? "()" : "(ctx)"))
                + ("NodeList".equals(m.getReturnType().getSimpleSourceName()) ? "" : ".get()") + ";}");
        sw.outdent();
        sw.println("},");
    }
    sw.outdent();
    sw.println("};");
}

From source file:com.cgxlib.xq.rebind.SelectorGeneratorCssToXPath.java

License:Apache License

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 {//from w ww. jav  a2 s  . c om
        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)") + ";");
}

From source file:com.cgxlib.xq.rebind.SelectorGeneratorJS.java

License:Apache License

protected void generateMethodBody(SourceWriter sw, JMethod method, TreeLogger treeLogger, boolean hasContext)
        throws UnableToCompleteException {
    String selector = method.getAnnotation(Selector.class).value();
    sw.println("return " + wrap(method, "impl.select(\"" + selector + "\", root)") + ";");
}

From source file:com.cgxlib.xq.rebind.SelectorGeneratorJSOptimal.java

License:Apache License

protected void generateMethodBody(SourceWriter sw, JMethod method, TreeLogger treeLogger, boolean hasContext)
        throws UnableToCompleteException {

    String selector = method.getAnnotation(Selector.class).value();

    sw.println("return " + wrap(method, "impl.select(\"" + selector + "\", root)") + ";");
    //    sw.println("JSArray n = JSArray.create();");
    //    if(!hasContext) {
    //      sw.println("Node root = Document.get();");
    //    }//from ww  w.j ava 2  s . c  om
    //
    //    // add root node as context.
    //    // TODO: support any context
    //    sw.println("n.addNode(root);");
    //    String q = selector, lq = null;
    //    Matcher lmode = modeRe.matcher(q);
    //    Matcher mm = null;
    //    String mode = "";
    //    if (lmode.lookingAt() && notNull(lmode.group(1))) {
    //      mode = lmode.group(1).replaceAll(trimReStr, "").trim();
    //      q = q.replaceFirst("\\Q" + lmode.group(1) + "\\E", "");
    //    }
    //
    //    while (notNull(q) && !q.equals(lq)) {
    //      debug("Doing q=" + q);
    //
    //      lq = q;
    //      Matcher tm = tagTokenRe.matcher(q);
    //      if (tm.lookingAt()) {
    //        if ("#".equals(tm.group(1))) {
    //          sw.println("n = quickId(n, \"" + mode + "\", root, \"" + tm.group(2)
    //              + "\");");
    //        } else {
    //          String tagName = tm.group(2);
    //          tagName = "".equals(tagName) ? "*" : tagName;
    //     //     sw.println("if (n.size() == 0) { n=JSArray.create(); }");
    //          String func = "";
    //          if ("".equals(mode)) {
    //            func = "getDescendentNodes";
    //          } else if (">".equals(mode)) {
    //            func = "getChildNodes";
    //          } else if ("+".equals(mode)) {
    //            func = "getSiblingNodes";
    //          } else if ("~".equals(mode)) {
    //            func = "getGeneralSiblingNodes";
    //          } else {
    //            treeLogger.log(TreeLogger.ERROR, "Error parsing selector, combiner "
    //                + mode + " not recognized in " + selector, null);
    //            throw new UnableToCompleteException();
    //          }
    //          sw.println("n = " + func + "(n, \"" + tagName + "\");");
    //        }
    //        debug("replacing in q, the value " + tm.group(0));
    //        q = q.replaceFirst("\\Q" + tm.group(0) + "\\E", "");
    //      } else {
    //        String func = "";
    //        String tagName = "*";
    //        if ("".equals(mode)) {
    //          func = "getDescendentNodes";
    //        } else if (">".equals(mode)) {
    //          func = "getChildNodes";
    //        } else if ("+".equals(mode)) {
    //          func = "getSiblingNodes";
    //        } else if ("~".equals(mode)) {
    //          func = "getGeneralSiblingNodes";
    //        } else {
    //          treeLogger.log(TreeLogger.ERROR, "Error parsing selector, combiner "
    //              + mode + " not recognized in " + selector, null);
    //          throw new UnableToCompleteException();
    //        }
    //        sw.println("n = " + func + "(n, \"" + tagName + "\");");
    //      }
    //
    //      while (!(mm = modeRe.matcher(q)).lookingAt()) {
    //        debug("Looking at " + q);
    //        boolean matched = false;
    //        for (RuleMatcher rm : matchers) {
    //          Matcher rmm = rm.re.matcher(q);
    //          if (rmm.lookingAt()) {
    //            String res[] = new String[rmm.groupCount()];
    //            for (int i = 1; i <= rmm.groupCount(); i++) {
    //              res[i - 1] = rmm.group(i);
    //              debug("added param " + res[i - 1]);
    //            }
    //            Object[] r = res;
    //            // inline enum, perhaps type-tightening will allow inlined eval()
    //            // call
    //            if (rm.fnTemplate.indexOf("byPseudo") != -1) {
    //              sw.println("n = Pseudo."+res[0].toUpperCase().replace("-", "_") +
    //                  ".eval(n, \""+res[1]+"\");");
    //            } else {
    //              sw.println(MessageFormat.format(rm.fnTemplate, r));
    //            }
    //            q = q.replaceFirst("\\Q" + rmm.group(0) + "\\E", "");
    //            matched = true;
    //            break;
    //          }
    //        }
    //        if (!matched) {
    //          treeLogger
    //              .log(TreeLogger.ERROR, "Error parsing selector at " + q, null);
    //          throw new UnableToCompleteException();
    //        }
    //      }
    //
    //      if (notNull(mm.group(1))) {
    //        mode = mm.group(1).replaceAll(trimReStr, "");
    //        debug("replacing q=" + q + " this part: " + mm.group(1));
    //        q = q.replaceFirst("\\Q" + mm.group(1) + "\\E", "");
    //      }
    //    }
    //    sw.println("return "+wrap(method, "nodup(n)")+";");
}

From source file:com.cgxlib.xq.rebind.SelectorGeneratorNative.java

License:Apache License

@Override
protected void generateMethodBody(SourceWriter sw, JMethod method, TreeLogger treeLogger, boolean hasContext)
        throws UnableToCompleteException {

    String selector = method.getAnnotation(Selector.class).value();
    if (selector.matches("#[\\w\\-]+")) {
        sw.println("return " + wrap(method, "veryQuickId(\"" + selector.substring(1) + "\", root)") + ";");
    } else if (selector.equals("*") || selector.matches("[\\w\\-]+")) {
        sw.println("return " + wrap(method, "elementsByTagName(\"" + selector + "\", root)") + ";");
    } else if (selector.matches("\\.[\\w\\-]+")) {
        sw.println(/*  w  w w.  jav a  2 s.c  o m*/
                "return " + wrap(method, "elementsByClassName(\"" + selector.substring(1) + "\", root)") + ";");
    } else if (selector.contains("!=")) {
        sw.println(
                "return "
                        + wrap(method, "querySelectorAll(\""
                                + selector.replaceAll("(\\[\\w+)!(=[^\\]]+\\])", ":not($1$2)") + "\", root)")
                        + ";");
    } else if (selector.matches(SelectorEngineNative.NATIVE_EXCEPTIONS_REGEXP)) {
        super.generateMethodBody(sw, method, treeLogger, hasContext);
    } else {
        sw.println("return " + wrap(method, "querySelectorAll(\"" + selector + "\", root)") + ";");
    }
}

From source file:com.cgxlib.xq.rebind.SelectorGeneratorNativeIE8.java

License:Apache License

@Override
protected void generateMethodBody(SourceWriter sw, JMethod method, TreeLogger treeLogger, boolean hasContext)
        throws UnableToCompleteException {
    String selector = method.getAnnotation(Selector.class).value();
    if (selector.matches("#[\\w\\-]+")) {
        sw.println("return " + wrap(method, "veryQuickId(\"" + selector.substring(1) + "\", root)") + ";");
    } else if (selector.equals("*") || selector.matches("[\\w\\-]+")) {
        sw.println("return " + wrap(method, "elementsByTagName(\"" + selector + "\", root)") + ";");
    } else if (selector.matches("\\.[\\w\\-]+")) {
        sw.println(//from   w  w w .jav a  2  s  . c o m
                "return " + wrap(method, "elementsByClassName(\"" + selector.substring(1) + "\", root)") + ";");
    } else if (selector.matches(SelectorEngineNativeIE8.NATIVE_EXCEPTIONS_REGEXP)) {
        super.generateMethodBody(sw, method, treeLogger, hasContext);
    } else {
        sw.println("return " + wrap(method, "querySelectorAll(\"" + selector + "\", root)") + ";");
    }
}

From source file:com.cgxlib.xq.rebind.SelectorGeneratorNativeIE9.java

License:Apache License

@Override
protected void generateMethodBody(SourceWriter sw, JMethod method, TreeLogger treeLogger, boolean hasContext)
        throws UnableToCompleteException {
    String selector = method.getAnnotation(Selector.class).value();
    if (selector.matches("#[\\w\\-]+")) {
        sw.println("return " + wrap(method, "veryQuickId(\"" + selector.substring(1) + "\", root)") + ";");
    } else if (selector.equals("*") || selector.matches("[\\w\\-]+")) {
        sw.println("return " + wrap(method, "elementsByTagName(\"" + selector + "\", root)") + ";");
    } else if (selector.matches("\\.[\\w\\-]+")) {
        sw.println(//from  w w  w  .  j av  a  2 s . co  m
                "return " + wrap(method, "elementsByClassName(\"" + selector.substring(1) + "\", root)") + ";");
    } else if (selector.matches(SelectorEngineNative.NATIVE_EXCEPTIONS_REGEXP)) {
        super.generateMethodBody(sw, method, treeLogger, hasContext);
    } else {
        sw.println("return " + wrap(method, "querySelectorAll(\"" + selector + "\", root)") + ";");
    }
}

From source file:com.cgxlib.xq.rebind.SelectorGeneratorXPath.java

License:Apache License

protected void generateMethodBody(SourceWriter sw, JMethod method, TreeLogger treeLogger, boolean hasContext)
        throws UnableToCompleteException {

    String selector = method.getAnnotation(Selector.class).value();
    String[] cssRules = selector.replaceAll("\\s*(,)\\s*", "$1").split(",");
    String currentRule;/* w w  w .  j  av  a  2  s  .c o m*/
    boolean identical = false;
    String xPathExpression = ".";

    for (int i = 0; i < cssRules.length; i++) {
        currentRule = cssRules[i];

        if (i > 0) {
            identical = false;
            for (int x = 0, xl = i; x < xl; x++) {
                if (cssRules[i].equals(cssRules[x])) {
                    identical = true;
                    break;
                }
            }
            if (identical) {
                continue;
            }
        }

        ArrayList<String> cssSelectors = new ArrayList<String>();
        Matcher selm = selectorSplitRegExp.matcher(currentRule);
        while (selm.find()) {
            cssSelectors.add(selm.group(0));
        }

        Matcher cssSelector;
        for (int j = 0, jl = cssSelectors.size(); j < jl; j++) {
            cssSelector = cssSelectorRegExp.matcher(cssSelectors.get(j));
            if (cssSelector.matches()) {

                SplitRule splitRule = new SplitRule();
                splitRule.tag = prefix
                        + ((!notNull(cssSelector.group(1)) || "*".equals(cssSelector.group(3))) ? "*"
                                : cssSelector.group(1));
                splitRule.id = (!"*".equals(cssSelector.group(3))) ? cssSelector.group(2) : null;
                splitRule.allClasses = cssSelector.group(4);
                splitRule.allAttr = cssSelector.group(6);
                splitRule.allPseudos = cssSelector.group(10);
                splitRule.tagRelation = cssSelector.group(22);
                if (notNull(splitRule.tagRelation)) {
                    if (">".equals(splitRule.tagRelation)) {
                        xPathExpression += "/child::";
                    } else if ("+".equals(splitRule.tagRelation)) {
                        xPathExpression += "/following-sibling::*[1]/self::";
                    } else if ("~".equals(splitRule.tagRelation)) {
                        xPathExpression += "/following-sibling::";
                    }
                } else {
                    xPathExpression += (j > 0 && cssSelectors.get(j - 1).matches("(>|\\+|~)")) ? splitRule.tag
                            : ("/descendant::" + splitRule.tag);
                }

                if (notNull(splitRule.id)) {
                    xPathExpression += "[@id = '" + splitRule.id.replaceAll("^#", "") + "']";
                }
                if (notNull(splitRule.allClasses)) {
                    xPathExpression += splitRule.allClasses.replaceAll("\\.([a-zA-Z_0-9\u00C0 -\uFFFF\\-_]+)",
                            "[contains(concat(' ', @class, ' '), ' $1 ')]");
                }
                if (notNull(splitRule.allAttr)) {
                    xPathExpression += attrToXPath(splitRule.allAttr,
                            "(\\w+)(\\^|\\$|\\*|\\||~)?=?([a-zA-Z_0-9\u00C0-\uFFFF\\s\\-_\\.]+)?");
                }
                if (notNull(splitRule.allPseudos)) {
                    Pattern pseudoSplitRegExp = Pattern.compile(":(\\w[a-zA-Z_0-9\\-]*)(\\(([^\\)]+)\\))?");
                    Matcher m = Pattern.compile("(:\\w+[a-zA-Z_0-9\\-]*)(\\([^\\)]+\\))?")
                            .matcher(splitRule.allPseudos);
                    while (m.find()) {
                        String str = m.group(0);
                        Matcher pseudo = pseudoSplitRegExp.matcher(str == null ? "" : str);
                        if (pseudo.matches()) {
                            String pseudoClass = notNull(pseudo.group(1)) ? pseudo.group(1).toLowerCase()
                                    : null;
                            String pseudoValue = notNull(pseudo.group(3)) ? pseudo.group(3) : null;
                            String xpath = pseudoToXPath(splitRule.tag, pseudoClass, pseudoValue);
                            if (notNull(xpath)) {
                                xPathExpression += "[" + xpath + "]";
                            }
                        }
                    }
                }
            }
        }
    }

    sw.println(
            "return " + wrap(method, "SelectorEngine.xpathEvaluate(\"" + xPathExpression + "\", root)") + ";");
}

From source file:com.cgxlib.xq.rebind.XmlBuilderGenerator.java

License:Apache License

public void generateMethod(SourceWriter sw, JMethod method, TreeLogger logger)
        throws UnableToCompleteException {
    Name nameAnnotation = method.getAnnotation(Name.class);
    String name = nameAnnotation != null ? nameAnnotation.value()
            : method.getName().replaceFirst("^(get|set)", "");

    if (nameAnnotation == null) {
        name = name.substring(0, 1).toLowerCase() + name.substring(1);
    }/*  ww  w .jav  a2s.  co m*/

    String retType = method.getReturnType().getParameterizedQualifiedSourceName();

    sw.print("public final " + retType + " " + method.getName());
    JParameter[] params = method.getParameters();
    if (params.length == 0) {
        JArrayType arr = method.getReturnType().isArray();
        sw.println("() {");
        sw.indent();
        if (retType.matches("(java.lang.Boolean|boolean)")) {
            sw.println("return getBooleanBase(\"" + name + "\");");
        } else if (method.getReturnType().isPrimitive() != null) {
            sw.println("return (" + retType + ")getFloatBase(\"" + name + "\");");
        } else if (isTypeAssignableTo(method.getReturnType(), stringType)) {
            sw.println("return getStrBase(\"" + name + "\");");
        } else if (isTypeAssignableTo(method.getReturnType(), xmlBuilderType)) {
            String q = method.getReturnType().getQualifiedSourceName();
            sw.println("Element e = getElementBase(\"" + name + "\");");
            sw.println(
                    "return e == null ? null : (" + q + ")((" + q + ")GWT.create(" + q + ".class)).load(e);");
        } else if (retType.equals(Properties.class.getName())) {
            sw.println("return getPropertiesBase(\"" + name + "\");");
        } else if (arr != null) {
            String q = arr.getComponentType().getQualifiedSourceName();
            sw.println("ArrayList<" + q + "> l = new ArrayList<" + q + ">();");
            sw.println("for (Element e: getElementsBase(\"" + name + "\")) {");
            sw.println("  " + q + " c = GWT.create(" + q + ".class);");
            sw.println("  c.load(e);");
            sw.println("  l.add(c);");
            sw.println("}");
            sw.println("return l.toArray(new " + q + "[0]);");
        } else {
            sw.println("return null; // Unsupported return type: " + retType);
        }
        sw.outdent();
        sw.println("}");
    } else if (params.length == 1) {
        JType type = params[0].getType();
        JArrayType arr = type.isArray();
        String qname = type.getParameterizedQualifiedSourceName();
        sw.print("(" + qname + " a)");
        sw.println("{");
        sw.indent();
        if (arr != null) {
            sw.println("setArrayBase(\"" + name + "\", a);");
        } else {
            sw.println("setBase(\"" + name + "\", a);");
        }
        if (!"void".equals(retType)) {
            if (isTypeAssignableTo(method.getReturnType(), method.getEnclosingType())) {
                sw.println("return this;");
            } else {
                sw.println("return null;");
            }
        }
        sw.outdent();
        sw.println("}");
    }
}