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

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

Introduction

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

Prototype

void beginJavaDocComment();

Source Link

Document

Begin emitting a JavaDoc comment.

Usage

From source file:com.sencha.gxt.core.rebind.XTemplatesGenerator.java

License:sencha.com license

/**
 * Handles a given template chunk container by creating a method in the
 * safeHtmlTemplates impl/*from  ww w. j a  v  a  2 s .  c  o m*/
 * 
 * @param sw the current sourcewriter
 * @param wrapper the chunk container to act recursively on
 * @param safeHtml creator to add new SafeHtml calls to
 * @param scopeContext current scope to make method calls to
 * @param invokables
 * @throws UnableToCompleteException
 */
private void buildSafeHtmlTemplates(String safeHtmlVar, SourceWriter sw, ContainerTemplateChunk wrapper,
        SafeHtmlTemplatesCreator safeHtml, Context scopeContext, MethodCollector invokables)
        throws UnableToCompleteException {

    // debugging section to see what is about to be printed
    sw.beginJavaDocComment();
    sw.print(wrapper.toString());
    sw.endJavaDocComment();

    // make a new interface method for this content
    StringBuilder sb = new StringBuilder();
    List<String> paramTypes = new ArrayList<String>();
    List<String> params = new ArrayList<String>();

    // write out children to local vars or to the template
    int argCount = 0;
    for (TemplateChunk chunk : wrapper.children) {
        if (chunk instanceof ContentChunk) {
            ContentChunk contentChunk = (ContentChunk) chunk;
            // build up the template
            if (contentChunk.type == ContentType.LITERAL) {
                sb.append(contentChunk.content);
            } else if (contentChunk.type == ContentType.CODE) {
                sb.append("{").append(argCount++).append("}");
                paramTypes.add("java.lang.String");
                StringBuffer expr = new StringBuffer("\"\" + (");

                // parse out the quoted string literals first
                Matcher str = Pattern.compile("\"[^\"]+\"").matcher(contentChunk.content);
                TreeLogger code = logger.branch(Type.DEBUG,
                        "Parsing code segment: \"" + contentChunk.content + "\"");
                int lastMatchEnd = 0;
                while (str.find()) {
                    int begin = str.start(), end = str.end();
                    String escapedString = str.group();
                    String unmatched = contentChunk.content.substring(lastMatchEnd, begin);

                    appendCodeBlockOperatorOrIdentifier(scopeContext, expr, code, unmatched);

                    expr.append(escapedString);
                    lastMatchEnd = end;
                }

                //finish rest of non-string-lit expression
                appendCodeBlockOperatorOrIdentifier(scopeContext, expr, code,
                        contentChunk.content.substring(lastMatchEnd));

                params.add(expr.append(")").toString());
                code.log(Type.DEBUG, "Final compiled expression: " + expr);
            } else if (contentChunk.type == ContentType.REFERENCE) {
                sb.append("{").append(argCount++).append("}");

                JType argType = scopeContext.getType(contentChunk.content);
                if (argType == null) {
                    logger.log(Type.ERROR, "Reference could not be found: '" + contentChunk.content
                            + "'. Please fix the expression in your template.");
                    throw new UnableToCompleteException();
                }
                paramTypes.add(argType.getParameterizedQualifiedSourceName());
                params.add(scopeContext.deref(contentChunk.content));

            } else {
                assert false : "Content type not supported + " + contentChunk.type;
            }

        } else if (chunk instanceof ControlChunk) {
            ControlChunk controlChunk = (ControlChunk) chunk;
            // build logic, get scoped name
            boolean hasIf = controlChunk.controls.containsKey("if");
            boolean hasFor = controlChunk.controls.containsKey("for");

            if (!hasIf && !hasFor) {
                logger.log(Type.ERROR, "<tpl> tag did not define a 'for' or 'if' attribute!");
                throw new UnableToCompleteException();
            }

            // declare a sub-template, and stash content in there, interleaving it
            // into the current template
            String subTemplate = scopeContext.declareLocalVariable("subTemplate");
            String templateInBlock = scopeContext.declareLocalVariable("innerTemplate");
            sb.append("{").append(argCount++).append("}");
            paramTypes.add("com.google.gwt.safehtml.shared.SafeHtml");
            params.add(subTemplate);
            sw.println("SafeHtml %1$s;", subTemplate);
            sw.println("SafeHtmlBuilder %1$s_builder = new SafeHtmlBuilder();", subTemplate);

            // find the context that should be passed to the child template
            final Context childScope;

            // if we have both for and if, if needs to wrap the for
            if (hasIf) {
                ConditionParser p = new ConditionParser(logger);
                List<Token> tokens = p.parse(controlChunk.controls.get("if"));
                StringBuilder condition = new StringBuilder();
                for (Token t : tokens) {
                    switch (t.type) {
                    case ExpressionLiteral:
                        condition.append(t.contents);
                        break;
                    case MethodInvocation:
                        Matcher invoke = Pattern.compile("([a-zA-Z0-9\\._]+)\\:([a-zA-Z0-9_]+)\\(([^\\)]*)\\)")
                                .matcher(t.contents);
                        invoke.matches();
                        String deref = scopeContext.deref(invoke.group(1));
                        String methodName = invoke.group(2);
                        String args = "";
                        for (String a : invoke.group(3).split(",")) {
                            String possible = scopeContext.deref(a);
                            args += possible == null ? a : possible;
                        }

                        condition.append(invokables.getMethodInvocation(methodName, deref, args));
                        break;
                    case Reference:
                        condition.append("(").append(scopeContext.deref(t.contents)).append(")");
                        break;
                    default:
                        logger.log(Type.ERROR, "Unexpected token type: " + t.type);
                        throw new UnableToCompleteException();
                    }
                }
                sw.println("if (%1$s) {", condition.toString());
                sw.indent();
            }
            // if there is a for, print it out, and change scope
            if (hasFor) {
                String loopRef = controlChunk.controls.get("for");

                JType collectionType = scopeContext.getType(loopRef);
                if (collectionType == null) {
                    logger.log(Type.ERROR, "Reference in 'for' attribute could not be found: '" + loopRef
                            + "'. Please fix the expression in your template.");
                    throw new UnableToCompleteException();
                }
                final JType localType;// type accessed within the loop
                final String localAccessor;// expr to access looped instance, where
                                           // %1$s is the loop obj, and %2$s is the
                                           // int index
                if (collectionType.isArray() != null) {
                    localType = collectionType.isArray().getComponentType();
                    localAccessor = "%1$s[%2$s]";
                } else {// List subtype
                    localType = ModelUtils.findParameterizationOf(listInterface,
                            collectionType.isClassOrInterface())[0];
                    localAccessor = "%1$s.get(%2$s)";
                }

                String loopVar = scopeContext.declareLocalVariable("i");
                // make sure the collection isnt null
                sw.println("if (%1$s != null) {", scopeContext.deref(loopRef));
                sw.indent();
                sw.println("for (int %1$s = 0; %1$s < %2$s; %1$s++) {", loopVar,
                        scopeContext.derefCount(loopRef));
                String itemExpr = String.format(localAccessor, scopeContext.deref(loopRef), loopVar);
                childScope = new Context(scopeContext, itemExpr, localType);
                childScope.setCountVar(loopVar);
                sw.indent();
            } else {
                // if no for, use the same scope as the outer content
                childScope = scopeContext;
            }
            // generate a subtemplate, insert that
            sw.println("SafeHtml %1$s;", templateInBlock);
            buildSafeHtmlTemplates(templateInBlock, sw, controlChunk, safeHtml, childScope, invokables);
            sw.println("%1$s_builder.append(%2$s);", subTemplate, templateInBlock);

            // close up the blocks
            if (hasFor) {
                sw.outdent();
                sw.println("}");
                sw.outdent();
                sw.println("}");
            }
            if (hasIf) {
                sw.outdent();
                sw.println("}");
            }

            sw.println("%1$s = %1$s_builder.toSafeHtml();", subTemplate);

        } else {
            assert false : "Unsupported chunk type: " + chunk.getClass();
        }
    }

    String methodName = safeHtml.addTemplate(sb.toString(), paramTypes);
    sw.beginJavaDocComment();
    sw.println("safehtml content:");
    sw.indent();
    sw.println(sb.toString());
    sw.outdent();
    sw.println("params:");
    sw.indent();
    sw.print(args(params));
    sw.outdent();
    sw.endJavaDocComment();
    sw.println("%4$s = %1$s.%2$s(%3$s);", safeHtml.getInstanceExpression(), methodName, args(params),
            safeHtmlVar);
}

From source file:net.sf.gilead.proxy.gwt.AbstractGwtProxyGenerator.java

License:Apache License

/**
 * Generates an additional attribute// w w  w.ja v  a 2 s  . c om
 * 
 * @param sourceWriter
 * @param attribute
 */
protected void generateAttribute(SourceWriter sourceWriter, Attribute attribute) {
    // Javadoc comment if needed
    //
    if (StringUtils.isEmpty(attribute.getJavadoc()) == false) {
        sourceWriter.beginJavaDocComment();
        sourceWriter.println(attribute.getJavadoc());
        sourceWriter.endJavaDocComment();
    }

    // Add attribute
    //
    sourceWriter.println(attribute.toJava5String());
    //
}

From source file:net.sf.gilead.proxy.gwt.AbstractGwtProxyGenerator.java

License:Apache License

/**
 * Generates an additional attribute// ww w  . j a v  a2 s  .c om
 * 
 * @param sourceWriter
 * @param attribute
 */
protected void generateMethod(SourceWriter sourceWriter, Method method) {
    // Javadoc comment if needed
    //
    if (StringUtils.isEmpty(method.getJavadoc()) == false) {
        sourceWriter.beginJavaDocComment();
        sourceWriter.println(method.getJavadoc());
        sourceWriter.endJavaDocComment();
    }

    // Add Signature and code
    //
    sourceWriter.println(method.computeJava5Signature());
    sourceWriter.println(method.getCode());
}