Example usage for java.util Deque pop

List of usage examples for java.util Deque pop

Introduction

In this page you can find the example usage for java.util Deque pop.

Prototype

E pop();

Source Link

Document

Pops an element from the stack represented by this deque.

Usage

From source file:com.fizzed.rocker.compiler.JavaGenerator.java

private void createSourceTemplate(TemplateModel model, Writer w) throws GeneratorException, IOException {
    if (model.getOptions().getPostProcessing() != null) {
        // allow post-processors to transform the model
        try {/* w w w  .jav  a 2s  . c o m*/
            model = postProcess(model);
        } catch (PostProcessorException ppe) {
            throw new GeneratorException("Error during post-processing of model.", ppe);
        }
    }

    // Used to register any withstatements we encounter, so we can generate all dynamic consumers at the end.
    final WithStatementConsumerGenerator withStatementConsumerGenerator = new WithStatementConsumerGenerator();

    // simple increment to help create unique var names
    int varCounter = -1;

    if (model.getPackageName() != null && !model.getPackageName().equals("")) {
        w.append("package ").append(model.getPackageName()).append(";").append(CRLF);
    }

    // imports regardless of template
    w.append(CRLF);
    w.append("import ").append(java.io.IOException.class.getName()).append(";").append(CRLF);
    w.append("import ").append(com.fizzed.rocker.ForIterator.class.getName()).append(";").append(CRLF);
    w.append("import ").append(com.fizzed.rocker.RenderingException.class.getName()).append(";").append(CRLF);
    w.append("import ").append(com.fizzed.rocker.RockerContent.class.getName()).append(";").append(CRLF);
    w.append("import ").append(com.fizzed.rocker.RockerOutput.class.getName()).append(";").append(CRLF);
    w.append("import ").append(com.fizzed.rocker.runtime.DefaultRockerTemplate.class.getName()).append(";")
            .append(CRLF);
    w.append("import ").append(com.fizzed.rocker.runtime.PlainTextUnloadedClassLoader.class.getName())
            .append(";").append(CRLF);

    // template imports
    if (model.getImports().size() > 0) {
        for (JavaImport i : model.getImports()) {
            w.append("// import ").append(sourceRef(i)).append(CRLF);
            w.append("import ").append(i.getStatement()).append(";").append(CRLF);
        }
    }

    w.append(CRLF);
    w.append("/*").append(CRLF);
    w.append(" * Auto generated code to render template ").append(model.getPackageName().replace('.', '/'))
            .append("/").append(model.getTemplateName()).append(CRLF);
    w.append(" * Do not edit this file. Changes will eventually be overwritten by Rocker parser!").append(CRLF);
    w.append(" */").append(CRLF);

    int indent = 0;

    // MODEL CLASS

    // class definition
    tab(w, indent).append("public class ").append(model.getName()).append(" extends ")
            .append(model.getOptions().getExtendsModelClass()).append(" {").append(CRLF);

    indent++;

    w.append(CRLF);

    // static info about this template
    tab(w, indent).append("static public final ").append(ContentType.class.getCanonicalName())
            .append(" CONTENT_TYPE = ").append(ContentType.class.getCanonicalName()).append(".")
            .append(model.getContentType().toString()).append(";").append(CRLF);
    tab(w, indent).append("static public final String TEMPLATE_NAME = \"").append(model.getTemplateName())
            .append("\";").append(CRLF);
    tab(w, indent).append("static public final String TEMPLATE_PACKAGE_NAME = \"")
            .append(model.getPackageName()).append("\";").append(CRLF);
    tab(w, indent).append("static public final String HEADER_HASH = \"").append(model.createHeaderHash() + "")
            .append("\";").append(CRLF);

    // Don't include MODIFIED_AT header when optimized compiler is used since this implicitly disables hot reloading anyhow
    if (!model.getOptions().getOptimize()) {
        tab(w, indent).append("static public final long MODIFIED_AT = ").append(model.getModifiedAt() + "")
                .append("L;").append(CRLF);
    }

    tab(w, indent).append("static public final String[] ARGUMENT_NAMES = {");
    StringBuilder argNameList = new StringBuilder();
    for (Argument arg : model.getArgumentsWithoutRockerBody()) {
        if (argNameList.length() > 0) {
            argNameList.append(",");
        }
        argNameList.append(" \"").append(arg.getExternalName()).append("\"");
    }
    w.append(argNameList).append(" };").append(CRLF);

    // model arguments as members of model class
    appendArgumentMembers(model, w, "private", false, indent);

    // model setters & getters with builder-style pattern
    // special case for the RockerBody argument which sorta "hides" its getter/setter
    if (model.getArguments().size() > 0) {
        for (Argument arg : model.getArguments()) {
            // setter
            w.append(CRLF);
            tab(w, indent).append("public ").append(model.getName()).append(" ").append(arg.getExternalName())
                    .append("(" + arg.getExternalType()).append(" ").append(arg.getName()).append(") {")
                    .append(CRLF);
            tab(w, indent + 1).append("this.").append(arg.getName()).append(" = ").append(arg.getName())
                    .append(";").append(CRLF);
            tab(w, indent + 1).append("return this;").append(CRLF);
            tab(w, indent).append("}").append(CRLF);

            // getter
            w.append(CRLF);
            tab(w, indent).append("public ").append(arg.getExternalType()).append(" ")
                    .append(arg.getExternalName()).append("() {").append(CRLF);
            tab(w, indent + 1).append("return this.").append(arg.getName()).append(";").append(CRLF);
            tab(w, indent).append("}").append(CRLF);
        }
    }

    w.append(CRLF);

    //
    // model "template" static builder
    //
    tab(w, indent).append("static public ").append(model.getName()).append(" template(");

    if (model.getArguments().size() > 0) {
        int i = 0;
        // RockerBody is NOT included (it is passed via a closure block in other templates)
        // so we only care about the other arguments
        for (Argument arg : model.getArgumentsWithoutRockerBody()) {
            if (i != 0) {
                w.append(", ");
            }
            w.append(arg.getType()).append(" ").append(arg.getName());
            i++;
        }
    }
    w.append(") {").append(CRLF);

    tab(w, indent + 1).append("return new ").append(model.getName()).append("()");
    if (model.getArguments().size() > 0) {
        int i = 0;
        for (Argument arg : model.getArgumentsWithoutRockerBody()) {
            w.append(CRLF);
            tab(w, indent + 2).append(".").append(arg.getName()).append("(").append(arg.getName()).append(")");
            i++;
        }
    }
    w.append(";").append(CRLF);
    tab(w, indent).append("}").append(CRLF);

    //
    // render of model
    //
    w.append(CRLF);

    tab(w, indent).append("@Override").append(CRLF);
    tab(w, indent).append("protected DefaultRockerTemplate buildTemplate() throws RenderingException {")
            .append(CRLF);

    if (model.getOptions().getOptimize()) {
        // model "template" static builder (not reloading support, fastest performance)
        tab(w, indent + 1).append("// optimized for performance (via rocker.optimize flag; no auto reloading)")
                .append(CRLF);
        tab(w, indent + 1).append("return new Template(this);").append(CRLF);
        //tab(w, indent+1).append("return template.__render(context);").append(CRLF);
    } else {
        tab(w, indent + 1).append(
                "// optimized for convenience (runtime auto reloading enabled if rocker.reloading=true)")
                .append(CRLF);
        // use bootstrap to create underlying template
        tab(w, indent + 1).append("return ").append(RockerRuntime.class.getCanonicalName())
                .append(".getInstance().getBootstrap().template(this.getClass(), this);").append(CRLF);
        //tab(w, indent+1).append("return template.__render(context);").append(CRLF);
    }

    tab(w, indent).append("}").append(CRLF);

    //
    // TEMPLATE CLASS
    //

    w.append(CRLF);

    // class definition
    tab(w, indent).append("static public class Template extends ").append(model.getOptions().getExtendsClass());

    w.append(" {").append(CRLF);

    indent++;

    // plain text -> map of chunks of text (Java only supports 2-byte length of string constant)
    LinkedHashMap<String, LinkedHashMap<String, String>> plainTextMap = model
            .createPlainTextMap(PLAIN_TEXT_CHUNK_LENGTH);

    if (!plainTextMap.isEmpty()) {

        w.append(CRLF);

        for (String plainText : plainTextMap.keySet()) {

            // include static text as comments in source (limit to 500)
            tab(w, indent).append("// ")
                    .append(StringUtils.abbreviate(RockerUtil.ESCAPE_JAVA.translate(plainText), 500))
                    .append(CRLF);
            for (Map.Entry<String, String> chunk : plainTextMap.get(plainText).entrySet()) {

                if (this.plainTextStrategy == PlainTextStrategy.STATIC_STRINGS) {
                    tab(w, indent).append("static private final String ").append(chunk.getKey()).append(" = \"")
                            .append(StringEscapeUtils.escapeJava(chunk.getValue())).append("\";").append(CRLF);
                } else if (this.plainTextStrategy == PlainTextStrategy.STATIC_BYTE_ARRAYS_VIA_UNLOADED_CLASS) {
                    tab(w, indent).append("static private final byte[] ").append(chunk.getKey()).append(";")
                            .append(CRLF);
                }

            }
        }

        // generate the static initializer
        if (this.plainTextStrategy == PlainTextStrategy.STATIC_BYTE_ARRAYS_VIA_UNLOADED_CLASS) {

            w.append(CRLF);

            tab(w, indent).append("static {").append(CRLF);

            String loaderClassName = unqualifiedClassName(PlainTextUnloadedClassLoader.class);
            tab(w, indent + 1).append(loaderClassName).append(" loader = ").append(loaderClassName)
                    .append(".tryLoad(").append(model.getName()).append(".class.getClassLoader(), ")
                    .append(model.getName()).append(".class.getName()").append(" + \"$PlainText\", \"")
                    .append(model.getOptions().getTargetCharset()).append("\");").append(CRLF);

            for (String plainText : plainTextMap.keySet()) {

                for (Map.Entry<String, String> chunk : plainTextMap.get(plainText).entrySet()) {

                    if (this.plainTextStrategy == PlainTextStrategy.STATIC_BYTE_ARRAYS_VIA_UNLOADED_CLASS) {
                        tab(w, indent + 1).append(chunk.getKey()).append(" = loader.tryGet(\"")
                                .append(chunk.getKey()).append("\");").append(CRLF);
                    }

                }

            }

            tab(w, indent).append("}").append(CRLF);
        }

    }

    // arguments as members of template class
    appendArgumentMembers(model, w, "protected", true, indent);

    w.append(CRLF);

    // constructor
    tab(w, indent).append("public Template(").append(model.getName()).append(" model) {").append(CRLF);

    tab(w, indent + 1).append("super(model);").append(CRLF);
    tab(w, indent + 1).append("__internal.setCharset(\"").append(model.getOptions().getTargetCharset())
            .append("\");").append(CRLF);
    tab(w, indent + 1).append("__internal.setContentType(CONTENT_TYPE);").append(CRLF);
    tab(w, indent + 1).append("__internal.setTemplateName(TEMPLATE_NAME);").append(CRLF);
    tab(w, indent + 1).append("__internal.setTemplatePackageName(TEMPLATE_PACKAGE_NAME);").append(CRLF);

    // each model argument passed along as well
    for (Argument arg : model.getArguments()) {
        tab(w, indent + 1).append("this.").append(arg.getName()).append(" = model.")
                .append(arg.getExternalName()).append("();").append(CRLF);
    }

    tab(w, indent).append("}").append(CRLF);

    w.append(CRLF);

    tab(w, indent).append("@Override").append(CRLF);
    tab(w, indent).append("protected void __doRender() throws IOException, RenderingException {").append(CRLF);

    // build rendering code
    int depth = 1;
    Deque<String> blockEnd = new ArrayDeque<>();

    for (TemplateUnit unit : model.getUnits()) {
        if (unit instanceof Comment) {
            continue;
        }

        // something like
        // IfBeginBlock
        // __internal.aboutToExecutePosInSourceTemplate(5, 10);
        appendCommentAndSourcePositionUpdate(w, depth + indent, unit);

        if (unit instanceof PlainText) {
            PlainText plain = (PlainText) unit;

            LinkedHashMap<String, String> chunks = plainTextMap.get(plain.getText());

            for (String chunkName : chunks.keySet()) {

                tab(w, depth + indent).append("__internal.writeValue(").append(chunkName).append(");")
                        .append(CRLF);

            }

        } else if (unit instanceof ValueExpression) {
            ValueExpression value = (ValueExpression) unit;
            tab(w, depth + indent).append("__internal.renderValue(").append(value.getExpression()).append(", ")
                    .append("" + value.isNullSafe()).append(");").append(CRLF);
        } else if (unit instanceof NullTernaryExpression) {
            NullTernaryExpression nullTernary = (NullTernaryExpression) unit;
            tab(w, depth + indent).append("{").append(CRLF);
            tab(w, depth + indent + 1).append("final Object __v = ").append(nullTernary.getLeftExpression())
                    .append(";").append(CRLF);
            tab(w, depth + indent + 1).append("if (__v != null) { __internal.renderValue(__v, false); }")
                    .append(CRLF);
            if (nullTernary.getRightExpression() != null) {
                tab(w, depth + indent + 1).append("else {__internal.renderValue(")
                        .append(nullTernary.getRightExpression()).append(", true); }").append(CRLF);
            }
            tab(w, depth + indent).append("}").append(CRLF);
        } else if (unit instanceof ValueClosureBegin) {

            ValueClosureBegin closure = (ValueClosureBegin) unit;
            tab(w, depth + indent).append("__internal.renderValue(").append(closure.getExpression())
                    .append(".__body(");

            // Java 1.8+ use lambda
            if (isJava8Plus(model)) {
                w.append("() -> {").append(CRLF);

                depth++;

                blockEnd.push("}), false);");
            }
            // Java 1.7- uses anonymous inner class
            else {
                w.append("new ").append(unqualifiedClassName(RockerContent.class)).append("() {").append(CRLF);

                depth++;

                blockEnd.push("}), false);");

                tab(w, depth + indent).append("@Override").append(CRLF);

                tab(w, depth + indent).append("public void render() throws IOException, RenderingException {")
                        .append(CRLF);

                depth++;

                blockEnd.push("}");
            }
        } else if (unit instanceof ValueClosureEnd) {
            // Java 1.8+ use lambda
            if (isJava8Plus(model)) {
                depth--;

                tab(w, depth + indent).append(blockEnd.pop()).append(" // value closure end ")
                        .append(sourceRef(unit)).append(CRLF);
            }
            // Java 1.7- uses anonymous inner class
            else {
                depth--;

                tab(w, depth + indent).append(blockEnd.pop()).append(CRLF);

                depth--;

                tab(w, depth + indent).append(blockEnd.pop()).append(" // value closure end ")
                        .append(sourceRef(unit)).append(CRLF);
            }
        } else if (unit instanceof ContentClosureBegin) {

            ContentClosureBegin closure = (ContentClosureBegin) unit;
            tab(w, depth + indent).append("RockerContent ").append(closure.getIdentifier()).append(" = ");

            // Java 1.8+ use lambda
            if (isJava8Plus(model)) {
                w.append("() -> {").append(CRLF);

                depth++;

                blockEnd.push("};");
            }
            // Java 1.7- uses anonymous inner class
            else {
                w.append("new ").append(unqualifiedClassName(com.fizzed.rocker.RockerContent.class))
                        .append("() {").append(CRLF);

                depth++;

                blockEnd.push("};");

                tab(w, depth + indent).append("@Override").append(CRLF);

                tab(w, depth + indent).append("public void render() throws IOException, RenderingException {")
                        .append(CRLF);

                depth++;

                blockEnd.push("}");
            }
        } else if (unit instanceof ContentClosureEnd) {
            // Java 1.8+ use lambda
            if (isJava8Plus(model)) {
                depth--;

                tab(w, depth + indent).append(blockEnd.pop()).append(" // content closure end ")
                        .append(sourceRef(unit)).append(CRLF);
            }
            // Java 1.7- uses anonymous inner class
            else {
                depth--;

                tab(w, depth + indent).append(blockEnd.pop()).append(CRLF);

                depth--;

                tab(w, depth + indent).append(blockEnd.pop()).append(" // content closure end ")
                        .append(sourceRef(unit)).append(CRLF);
            }
        } else if (unit instanceof IfBlockBegin) {
            IfBlockBegin block = (IfBlockBegin) unit;

            tab(w, depth + indent).append("if ").append(block.getExpression()).append(" {").append(CRLF);

            blockEnd.push("}");
            depth++;
        } else if (unit instanceof IfBlockElseIf) {
            final IfBlockElseIf block = (IfBlockElseIf) unit;

            depth--;

            // This keeps else-if nicely formatted in generated code.
            tab(w, depth + indent).append("} else if ").append(block.getExpression()).append(" {").append(CRLF);

            depth++;
        } else if (unit instanceof IfBlockElse) {
            depth--;

            tab(w, depth + indent).append("} else {").append(" // else ").append(sourceRef(unit)).append(CRLF);

            depth++;
        } else if (unit instanceof IfBlockEnd) {
            depth--;

            tab(w, depth + indent).append(blockEnd.pop()).append(" // if end ").append(sourceRef(unit))
                    .append(CRLF);

        } else if (unit instanceof WithBlockBegin) {
            WithBlockBegin block = (WithBlockBegin) unit;
            WithStatement stmt = block.getStatement();

            String statementConsumerName = withStatementConsumerGenerator.register(stmt);

            final List<WithStatement.VariableWithExpression> variables = stmt.getVariables();

            if (isJava8Plus(model)) {
                tab(w, depth + indent)
                        .append(variables.size() == 1 ? qualifiedClassName(WithBlock.class)
                                : WithStatementConsumerGenerator.WITH_BLOCKS_GENERATED_CLASS_NAME)
                        .append(".with(");
                // All expressions
                for (int i = 0; i < variables.size(); i++) {
                    final WithStatement.VariableWithExpression var = variables.get(i);
                    if (i > 0) {
                        w.append(", ");
                    }
                    w.append(var.getValueExpression());
                }
                w.append(", ").append(stmt.isNullSafe() + "").append(", (");
                for (int i = 0; i < variables.size(); i++) {
                    final WithStatement.VariableWithExpression var = variables.get(i);
                    if (i > 0) {
                        w.append(", ");
                    }
                    w.append(var.getVariable().getName());
                }
                w.append(") -> {").append(CRLF);

                depth++;

                blockEnd.push("});");

            } else {
                tab(w, depth + indent)
                        // Note for standard 1 variable with block we use the predefined consumers.
                        // Otherwise we fallback to the generated ones.
                        .append(variables.size() == 1 ? qualifiedClassName(WithBlock.class)
                                : WithStatementConsumerGenerator.WITH_BLOCKS_GENERATED_CLASS_NAME)
                        .append(".with(");

                // All expressions
                for (int i = 0; i < variables.size(); i++) {
                    final WithStatement.VariableWithExpression var = variables.get(i);
                    if (i > 0) {
                        w.append(", ");
                    }
                    w.append(var.getValueExpression());
                }
                w.append(", ").append(stmt.isNullSafe() + "").append(", (new ").append(statementConsumerName)
                        .append('<');

                // Types for the .with(..)
                for (int i = 0; i < variables.size(); i++) {
                    final JavaVariable variable = variables.get(i).getVariable();
                    if (i > 0) {
                        w.append(", ");
                    }
                    w.append(variable.getType());
                }
                w.append(">() {").append(CRLF);
                tab(w, depth + indent + 1).append("@Override public void accept(");
                for (int i = 0; i < variables.size(); i++) {
                    final JavaVariable variable = variables.get(i).getVariable();
                    if (i > 0) {
                        w.append(", ");
                    }
                    w.append("final ").append(variable.toString());
                }
                w.append(") throws IOException {").append(CRLF);

                depth++;

                blockEnd.push("}}));");
            }
        } else if (unit instanceof WithBlockElse) {
            depth--;

            if (isJava8Plus(model)) {
                tab(w, depth + indent).append("}, () -> {").append(CRLF);
            } else {
                tab(w, depth + indent).append("}}), (new ")
                        .append(qualifiedClassName(WithBlock.Consumer0.class)).append("() { ")
                        .append("@Override public void accept() throws IOException {").append(CRLF);
            }
            depth++;
        } else if (unit instanceof WithBlockEnd) {
            depth--;

            tab(w, depth + indent).append(blockEnd.pop()).append(" // with end ").append(sourceRef(unit))
                    .append(CRLF);
        } else if (unit instanceof ForBlockBegin) {
            ForBlockBegin block = (ForBlockBegin) unit;
            ForStatement stmt = block.getStatement();

            // break support via try and catch mechanism (works across lambdas!)
            tab(w, depth + indent).append("try {").append(CRLF);

            depth++;

            if (stmt.getForm() == ForStatement.Form.GENERAL) {
                // print out raw statement including parentheses
                tab(w, depth + indent).append("for ").append(block.getExpression()).append(" {").append(CRLF);

                blockEnd.push("}");
            } else if (stmt.getForm() == ForStatement.Form.ENHANCED) {
                // Java 1.8+ (use lambdas)
                if (stmt.hasAnyUntypedArguments() && isJava8Plus(model)) {

                    // build list of lambda vars
                    String localVars = "";
                    for (JavaVariable arg : stmt.getArguments()) {
                        if (localVars.length() != 0) {
                            localVars += ",";
                        }
                        localVars += arg.getName();
                    }

                    tab(w, depth + indent).append(Java8Iterator.class.getName()).append(".forEach(")
                            .append(stmt.getValueExpression()).append(", (").append(localVars).append(") -> {")
                            .append(CRLF);

                    blockEnd.push("});");
                } else {

                    // is the first argument a "ForIterator" ?
                    boolean forIterator = isForIteratorType(stmt.getArguments().get(0).getType());
                    int collectionCount = (forIterator ? 2 : 1);
                    int mapCount = (forIterator ? 3 : 2);

                    // type and value we are going to iterate thru
                    String iterateeType = null;
                    String valueExpression = null;
                    if (stmt.getArguments().size() == collectionCount) {
                        iterateeType = stmt.getArguments().get(collectionCount - 1).getTypeAsNonPrimitiveType();
                        valueExpression = stmt.getValueExpression();
                    } else if (stmt.getArguments().size() == mapCount) {
                        iterateeType = "java.util.Map.Entry<"
                                + stmt.getArguments().get(mapCount - 2).getTypeAsNonPrimitiveType() + ","
                                + stmt.getArguments().get(mapCount - 1).getTypeAsNonPrimitiveType() + ">";
                        valueExpression = stmt.getValueExpression() + ".entrySet()";
                    }

                    // create unique variable name for iterator
                    String forIteratorVarName = "__forIterator" + (++varCounter);

                    // ForIterator for collection and make it final to assure nested anonymous
                    // blocks can access it as well.
                    tab(w, depth + indent).append("final ")
                            .append(com.fizzed.rocker.runtime.CollectionForIterator.class.getName()).append("<")
                            .append(iterateeType).append(">").append(" ").append(forIteratorVarName)
                            .append(" = new ")
                            .append(com.fizzed.rocker.runtime.CollectionForIterator.class.getName()).append("<")
                            .append(iterateeType).append(">").append("(").append(valueExpression).append(");")
                            .append(CRLF);

                    // for loop same regardless of map vs. collection
                    tab(w, depth + indent).append("while (").append(forIteratorVarName).append(".hasNext()) {")
                            .append(CRLF);

                    // if forIterator request assign to local var and make it final to assure nested anonymous
                    // blocks can access it as well.
                    if (forIterator) {
                        tab(w, depth + indent + 1).append("final ")
                                .append(com.fizzed.rocker.ForIterator.class.getName()).append(" ")
                                .append(stmt.getArguments().get(0).getName()).append(" = ")
                                .append(forIteratorVarName).append(";").append(CRLF);
                    }

                    if (stmt.getArguments().size() == collectionCount) {
                        // assign item to local var and make it final to assure nested anonymous
                        // blocks can access it as well.
                        tab(w, depth + indent + 1).append("final ")
                                .append(stmt.getArguments().get(collectionCount - 1).toString()).append(" = ")
                                .append(forIteratorVarName).append(".next();").append(CRLF);
                    } else if (stmt.getArguments().size() == mapCount) {
                        // create unique variable name for iterator
                        String entryVarName = "__entry" + (++varCounter);

                        // assign map entry to local var
                        tab(w, depth + indent + 1).append("final ").append(iterateeType).append(" ")
                                .append(entryVarName).append(" = ").append(forIteratorVarName)
                                .append(".next();").append(CRLF);

                        // assign entry to local values  make it final to assure nested anonymous
                        // blocks can access it as well.
                        tab(w, depth + indent + 1).append("final ")
                                .append(stmt.getArguments().get(mapCount - 2).toString()).append(" = ")
                                .append(entryVarName).append(".getKey();").append(CRLF);

                        tab(w, depth + indent + 1).append("final ")
                                .append(stmt.getArguments().get(mapCount - 1).toString()).append(" = ")
                                .append(entryVarName).append(".getValue();").append(CRLF);
                    } else {
                        throw new GeneratorException("Unsupported number of arguments for for loop");
                    }

                    blockEnd.push("}");
                }
            }

            depth++;

            // continue support via try and catch mechanism (works across lambdas!)
            tab(w, depth + indent).append("try {").append(CRLF);

            depth++;

        } else if (unit instanceof ForBlockEnd) {
            depth--;

            // continue support via try and catch mechanism (works across lambdas!)
            tab(w, depth + indent).append("} catch (").append(ContinueException.class.getCanonicalName())
                    .append(" e) {").append(CRLF);

            tab(w, depth + indent + 1).append("// support for continuing for loops").append(CRLF);

            tab(w, depth + indent).append("}").append(CRLF);

            depth--;

            tab(w, depth + indent).append(blockEnd.pop()).append(" // for end ").append(sourceRef(unit))
                    .append(CRLF);

            depth--;

            // break support via try and catch mechanism (works across lambdas!)
            tab(w, depth + indent).append("} catch (").append(BreakException.class.getCanonicalName())
                    .append(" e) {").append(CRLF);

            tab(w, depth + indent + 1).append("// support for breaking for loops").append(CRLF);

            tab(w, depth + indent).append("}").append(CRLF);
        } else if (unit instanceof BreakStatement) {
            tab(w, depth + indent).append("__internal.throwBreakException();").append(CRLF);
        } else if (unit instanceof ContinueStatement) {
            tab(w, depth + indent).append("__internal.throwContinueException();").append(CRLF);
        }
        //log.info(" src (@ {}): [{}]", unit.getSourceRef(), unit.getSourceRef().getConsoleFriendlyText());
    }

    // end of render()
    tab(w, indent).append("}").append(CRLF);

    indent--;

    // end of template class
    tab(w, indent).append("}").append(CRLF);

    // Generate class with all gathered consumer interfaces for all withblocks
    withStatementConsumerGenerator.generate(this, w);

    if (this.plainTextStrategy == PlainTextStrategy.STATIC_BYTE_ARRAYS_VIA_UNLOADED_CLASS
            && !plainTextMap.isEmpty()) {

        w.append(CRLF);

        tab(w, indent).append("private static class PlainText {").append(CRLF);
        w.append(CRLF);

        for (String plainText : plainTextMap.keySet()) {

            for (Map.Entry<String, String> chunk : plainTextMap.get(plainText).entrySet()) {

                tab(w, indent + 1).append("static private final String ").append(chunk.getKey()).append(" = \"")
                        .append(StringEscapeUtils.escapeJava(chunk.getValue())).append("\";").append(CRLF);

            }

        }

        w.append(CRLF);
        tab(w, indent).append("}").append(CRLF);
    }

    w.append(CRLF);
    w.append("}").append(CRLF);
}

From source file:nl.knaw.huc.di.tag.tagml.importer.TAGMLListener.java

private TAGMarkup removeFromMarkupStack(String extendedTag, Deque<TAGMarkup> markupStack) {
    if (markupStack == null || markupStack.isEmpty()) {
        return null;
    }// ww  w  . j a v  a  2  s  .  c  om
    final TAGMarkup expected = markupStack.peek();
    if (extendedTag.equals(expected.getExtendedTag())) {
        markupStack.pop();
        currentTextVariationState().removeOpenMarkup(expected);
        return expected;
    }
    return null;
}

From source file:org.alfresco.repo.content.transform.TransformerDebug.java

private void pop(Call callType, boolean suppressFinish) {
    Deque<Frame> ourStack = ThreadInfo.getStack();
    if (!ourStack.isEmpty()) {
        Frame frame = ourStack.peek();

        if ((frame.callType == callType)
                || (frame.callType == Call.AVAILABLE_AND_TRANSFORM && callType == Call.AVAILABLE)) {
            int size = ourStack.size();
            String ms = ms(System.currentTimeMillis() - frame.start);

            logInfo(frame, size, ms);/* ww w. ja v  a2  s .com*/

            boolean firstLevel = size == 1;
            if (!suppressFinish && (firstLevel || logger.isTraceEnabled())) {
                log(FINISHED_IN + ms + (frame.callType == Call.AVAILABLE ? " Transformer NOT called" : "")
                        + (firstLevel ? "\n" : ""), firstLevel);
            }

            setDebugOutput(frame.origDebugOutput);
            ourStack.pop();
        }
    }
}

From source file:org.apache.asterix.app.translator.QueryTranslator.java

private static ARecordType createEnforcedType(ARecordType initialType, List<Index> indexes)
        throws AlgebricksException {
    ARecordType enforcedType = initialType;
    for (Index index : indexes) {
        if (!index.isSecondaryIndex() || !index.isEnforcingKeyFileds()) {
            continue;
        }//from   w  ww.  j  a va 2 s. c  o m
        if (index.hasMetaFields()) {
            throw new AlgebricksException("Indexing an open field is only supported on the record part");
        }
        for (int i = 0; i < index.getKeyFieldNames().size(); i++) {
            Deque<Pair<ARecordType, String>> nestedTypeStack = new ArrayDeque<>();
            List<String> splits = index.getKeyFieldNames().get(i);
            ARecordType nestedFieldType = enforcedType;
            boolean openRecords = false;
            String bridgeName = nestedFieldType.getTypeName();
            int j;
            // Build the stack for the enforced type
            for (j = 1; j < splits.size(); j++) {
                nestedTypeStack.push(new Pair<ARecordType, String>(nestedFieldType, splits.get(j - 1)));
                bridgeName = nestedFieldType.getTypeName();
                nestedFieldType = (ARecordType) enforcedType.getSubFieldType(splits.subList(0, j));
                if (nestedFieldType == null) {
                    openRecords = true;
                    break;
                }
            }
            if (openRecords) {
                // create the smallest record
                enforcedType = new ARecordType(splits.get(splits.size() - 2),
                        new String[] { splits.get(splits.size() - 1) },
                        new IAType[] { AUnionType.createUnknownableType(index.getKeyFieldTypes().get(i)) },
                        true);
                // create the open part of the nested field
                for (int k = splits.size() - 3; k > (j - 2); k--) {
                    enforcedType = new ARecordType(splits.get(k), new String[] { splits.get(k + 1) },
                            new IAType[] { AUnionType.createUnknownableType(enforcedType) }, true);
                }
                // Bridge the gap
                Pair<ARecordType, String> gapPair = nestedTypeStack.pop();
                ARecordType parent = gapPair.first;

                IAType[] parentFieldTypes = ArrayUtils.addAll(parent.getFieldTypes().clone(),
                        new IAType[] { AUnionType.createUnknownableType(enforcedType) });
                enforcedType = new ARecordType(bridgeName,
                        ArrayUtils.addAll(parent.getFieldNames(), enforcedType.getTypeName()), parentFieldTypes,
                        true);
            } else {
                //Schema is closed all the way to the field
                //enforced fields are either null or strongly typed
                LinkedHashMap<String, IAType> recordNameTypesMap = createRecordNameTypeMap(nestedFieldType);
                // if a an enforced field already exists and the type is correct
                IAType enforcedFieldType = recordNameTypesMap.get(splits.get(splits.size() - 1));
                if (enforcedFieldType != null && enforcedFieldType.getTypeTag() == ATypeTag.UNION
                        && ((AUnionType) enforcedFieldType).isUnknownableType()) {
                    enforcedFieldType = ((AUnionType) enforcedFieldType).getActualType();
                }
                if (enforcedFieldType != null && !ATypeHierarchy.canPromote(enforcedFieldType.getTypeTag(),
                        index.getKeyFieldTypes().get(i).getTypeTag())) {
                    throw new AlgebricksException("Cannot enforce field " + index.getKeyFieldNames().get(i)
                            + " to have type " + index.getKeyFieldTypes().get(i));
                }
                if (enforcedFieldType == null) {
                    recordNameTypesMap.put(splits.get(splits.size() - 1),
                            AUnionType.createUnknownableType(index.getKeyFieldTypes().get(i)));
                }
                enforcedType = new ARecordType(nestedFieldType.getTypeName(),
                        recordNameTypesMap.keySet().toArray(new String[recordNameTypesMap.size()]),
                        recordNameTypesMap.values().toArray(new IAType[recordNameTypesMap.size()]),
                        nestedFieldType.isOpen());
            }

            // Create the enforced type for the nested fields in the schema, from the ground up
            if (!nestedTypeStack.isEmpty()) {
                while (!nestedTypeStack.isEmpty()) {
                    Pair<ARecordType, String> nestedTypePair = nestedTypeStack.pop();
                    ARecordType nestedRecType = nestedTypePair.first;
                    IAType[] nestedRecTypeFieldTypes = nestedRecType.getFieldTypes().clone();
                    nestedRecTypeFieldTypes[nestedRecType.getFieldIndex(nestedTypePair.second)] = enforcedType;
                    enforcedType = new ARecordType(nestedRecType.getTypeName() + "_enforced",
                            nestedRecType.getFieldNames(), nestedRecTypeFieldTypes, nestedRecType.isOpen());
                }
            }
        }
    }
    return enforcedType;
}

From source file:org.apache.asterix.metadata.utils.TypeUtil.java

/**
 * Merges typed index fields with specified recordType, allowing indexed fields to be optional.
 * I.e. the type { "personId":int32, "name": string, "address" : { "street": string } } with typed indexes
 * on age:int32, address.state:string will be merged into type { "personId":int32, "name": string,
 * "age": int32? "address" : { "street": string, "state": string? } } Used by open indexes to enforce
 * the type of an indexed record/*from  ww w.j a va2 s  .  c om*/
 */
public static Pair<ARecordType, ARecordType> createEnforcedType(ARecordType recordType, ARecordType metaType,
        List<Index> indexes) throws AlgebricksException {
    ARecordType enforcedRecordType = recordType;
    ARecordType enforcedMetaType = metaType;
    for (Index index : indexes) {
        if (!index.isSecondaryIndex() || !index.isEnforcingKeyFileds()) {
            continue;
        }
        if (index.hasMetaFields()) {
            throw new AlgebricksException("Indexing an open field is only supported on the record part");
        }
        for (int i = 0; i < index.getKeyFieldNames().size(); i++) {
            Deque<Pair<ARecordType, String>> nestedTypeStack = new ArrayDeque<>();
            List<String> splits = index.getKeyFieldNames().get(i);
            ARecordType nestedFieldType = enforcedRecordType;
            boolean openRecords = false;
            String bridgeName = nestedFieldType.getTypeName();
            int j;
            // Build the stack for the enforced type
            for (j = 1; j < splits.size(); j++) {
                nestedTypeStack.push(new Pair<>(nestedFieldType, splits.get(j - 1)));
                bridgeName = nestedFieldType.getTypeName();
                nestedFieldType = (ARecordType) enforcedRecordType.getSubFieldType(splits.subList(0, j));
                if (nestedFieldType == null) {
                    openRecords = true;
                    break;
                }
            }
            if (openRecords) {
                // create the smallest record
                enforcedRecordType = new ARecordType(splits.get(splits.size() - 2),
                        new String[] { splits.get(splits.size() - 1) },
                        new IAType[] { AUnionType.createUnknownableType(index.getKeyFieldTypes().get(i)) },
                        true);
                // create the open part of the nested field
                for (int k = splits.size() - 3; k > (j - 2); k--) {
                    enforcedRecordType = new ARecordType(splits.get(k), new String[] { splits.get(k + 1) },
                            new IAType[] { AUnionType.createUnknownableType(enforcedRecordType) }, true);
                }
                // Bridge the gap
                Pair<ARecordType, String> gapPair = nestedTypeStack.pop();
                ARecordType parent = gapPair.first;

                IAType[] parentFieldTypes = ArrayUtils.addAll(parent.getFieldTypes().clone(),
                        new IAType[] { AUnionType.createUnknownableType(enforcedRecordType) });
                enforcedRecordType = new ARecordType(bridgeName,
                        ArrayUtils.addAll(parent.getFieldNames(), enforcedRecordType.getTypeName()),
                        parentFieldTypes, true);
            } else {
                //Schema is closed all the way to the field
                //enforced fields are either null or strongly typed
                Map<String, IAType> recordNameTypesMap = TypeUtil.createRecordNameTypeMap(nestedFieldType);
                // if a an enforced field already exists and the type is correct
                IAType enforcedFieldType = recordNameTypesMap.get(splits.get(splits.size() - 1));
                if (enforcedFieldType != null && enforcedFieldType.getTypeTag() == ATypeTag.UNION
                        && ((AUnionType) enforcedFieldType).isUnknownableType()) {
                    enforcedFieldType = ((AUnionType) enforcedFieldType).getActualType();
                }
                if (enforcedFieldType != null && !ATypeHierarchy.canPromote(enforcedFieldType.getTypeTag(),
                        index.getKeyFieldTypes().get(i).getTypeTag())) {
                    throw new AlgebricksException("Cannot enforce field " + index.getKeyFieldNames().get(i)
                            + " to have type " + index.getKeyFieldTypes().get(i));
                }
                if (enforcedFieldType == null) {
                    recordNameTypesMap.put(splits.get(splits.size() - 1),
                            AUnionType.createUnknownableType(index.getKeyFieldTypes().get(i)));
                }
                enforcedRecordType = new ARecordType(nestedFieldType.getTypeName(),
                        recordNameTypesMap.keySet().toArray(new String[recordNameTypesMap.size()]),
                        recordNameTypesMap.values().toArray(new IAType[recordNameTypesMap.size()]),
                        nestedFieldType.isOpen());
            }

            // Create the enforced type for the nested fields in the schema, from the ground up
            if (!nestedTypeStack.isEmpty()) {
                while (!nestedTypeStack.isEmpty()) {
                    Pair<ARecordType, String> nestedTypePair = nestedTypeStack.pop();
                    ARecordType nestedRecType = nestedTypePair.first;
                    IAType[] nestedRecTypeFieldTypes = nestedRecType.getFieldTypes().clone();
                    nestedRecTypeFieldTypes[nestedRecType
                            .getFieldIndex(nestedTypePair.second)] = enforcedRecordType;
                    enforcedRecordType = new ARecordType(nestedRecType.getTypeName() + "_enforced",
                            nestedRecType.getFieldNames(), nestedRecTypeFieldTypes, nestedRecType.isOpen());
                }
            }
        }
    }
    return new Pair<>(enforcedRecordType, enforcedMetaType);
}

From source file:org.apache.asterix.om.typecomputer.impl.RecordRemoveFieldsTypeComputer.java

private IAType buildOutputType(Deque<String> fieldPathStack, ARecordType inputRecordType,
        Set<String> fieldNameSet, List<List<String>> pathList) throws AlgebricksException {
    List<String> resultFieldNames = new ArrayList<>();
    List<IAType> resultFieldTypes = new ArrayList<>();

    String[] fieldNames = inputRecordType.getFieldNames();
    IAType[] fieldTypes = inputRecordType.getFieldTypes();

    for (int i = 0; i < fieldNames.length; i++) {
        if (!fieldNameSet.contains(fieldNames[i])) { // The main field is to be kept
            addField(inputRecordType, fieldNames[i], resultFieldNames, resultFieldTypes);
        } else if (!pathList.isEmpty()) { // Further check needed for nested fields
            if (fieldTypes[i].getTypeTag() == ATypeTag.RECORD) {
                ARecordType subRecord = (ARecordType) fieldTypes[i];

                fieldPathStack.push(fieldNames[i]);
                subRecord = deepCheckAndCopy(fieldPathStack, subRecord, pathList, inputRecordType.isOpen());
                fieldPathStack.pop();
                if (subRecord != null) {
                    resultFieldNames.add(fieldNames[i]);
                    resultFieldTypes.add(subRecord);
                }//  w  w  w  .  j  a  va2s  .co  m
            }
        }
    }

    int n = resultFieldNames.size();
    String resultTypeName = "result-record(" + inputRecordType.getTypeName() + ")";

    return new ARecordType(resultTypeName, resultFieldNames.toArray(new String[n]),
            resultFieldTypes.toArray(new IAType[n]), true); // Make the output type open always

}

From source file:org.apache.asterix.om.typecomputer.impl.RecordRemoveFieldsTypeComputer.java

private ARecordType deepCheckAndCopy(Deque<String> fieldPath, ARecordType srcRecType,
        List<List<String>> pathList, boolean isOpen) throws AlgebricksException {
    // Make sure the current path is valid before going further
    if (isRemovePath(fieldPath, pathList)) {
        return null;
    }/*from www  . ja  v  a2s .com*/

    String srcFieldNames[] = srcRecType.getFieldNames();
    IAType srcFieldTypes[] = srcRecType.getFieldTypes();

    List<IAType> destFieldTypes = new ArrayList<>();
    List<String> destFieldNames = new ArrayList<>();

    for (int i = 0; i < srcFieldNames.length; i++) {
        fieldPath.push(srcFieldNames[i]);
        if (!isRemovePath(fieldPath, pathList)) {
            if (srcFieldTypes[i].getTypeTag() == ATypeTag.RECORD) {
                ARecordType subRecord = (ARecordType) srcFieldTypes[i];
                subRecord = deepCheckAndCopy(fieldPath, subRecord, pathList, isOpen);
                if (subRecord != null) {
                    destFieldNames.add(srcFieldNames[i]);
                    destFieldTypes.add(subRecord);
                }
            } else {
                destFieldNames.add(srcFieldNames[i]);
                destFieldTypes.add(srcFieldTypes[i]);
            }
        }
        fieldPath.pop();
    }

    int n = destFieldNames.size();
    if (n == 0) {
        return null;
    }
    return new ARecordType(srcRecType.getTypeName(), destFieldNames.toArray(new String[n]),
            destFieldTypes.toArray(new IAType[n]), isOpen);
}

From source file:org.apache.hadoop.hive.ql.parse.ASTNode.java

/**
 * For every node in this subtree, make sure it's start/stop token's
 * are set.  Walk depth first, visit bottom up.  Only updates nodes
 * with at least one token index < 0.
 *
 * In contrast to the method in the parent class, this method is
 * iterative./*w w w .jav a2s  . c o m*/
 */
@Override
public void setUnknownTokenBoundaries() {
    Deque<ASTNode> stack1 = new ArrayDeque<ASTNode>();
    Deque<ASTNode> stack2 = new ArrayDeque<ASTNode>();
    stack1.push(this);

    while (!stack1.isEmpty()) {
        ASTNode next = stack1.pop();
        stack2.push(next);

        if (next.children != null) {
            for (int i = next.children.size() - 1; i >= 0; i--) {
                stack1.push((ASTNode) next.children.get(i));
            }
        }
    }

    while (!stack2.isEmpty()) {
        ASTNode next = stack2.pop();

        if (next.children == null) {
            if (next.startIndex < 0 || next.stopIndex < 0) {
                next.startIndex = next.stopIndex = next.token.getTokenIndex();
            }
        } else if (next.startIndex >= 0 && next.stopIndex >= 0) {
            continue;
        } else if (next.children.size() > 0) {
            ASTNode firstChild = (ASTNode) next.children.get(0);
            ASTNode lastChild = (ASTNode) next.children.get(next.children.size() - 1);
            next.startIndex = firstChild.getTokenStartIndex();
            next.stopIndex = lastChild.getTokenStopIndex();
        }
    }
}

From source file:org.apache.hadoop.hive.ql.parse.ASTNode.java

private StringBuilder dump(StringBuilder sb) {
    Deque<ASTNode> stack = new ArrayDeque<ASTNode>();
    stack.push(this);
    int tabLength = 0;

    while (!stack.isEmpty()) {
        ASTNode next = stack.peek();/*from  w  w w .jav  a 2 s  .  co m*/

        if (!next.visited) {
            sb.append(StringUtils.repeat(" ", tabLength * 3));
            sb.append(next.toString());
            sb.append("\n");

            if (next.children != null) {
                for (int i = next.children.size() - 1; i >= 0; i--) {
                    stack.push((ASTNode) next.children.get(i));
                }
            }

            tabLength++;
            next.visited = true;
        } else {
            tabLength--;
            next.visited = false;
            stack.pop();
        }
    }

    return sb;
}

From source file:org.apache.hadoop.hive.ql.parse.ASTNode.java

private String toStringTree(ASTNode rootNode) {
    Deque<ASTNode> stack = new ArrayDeque<ASTNode>();
    stack.push(this);

    while (!stack.isEmpty()) {
        ASTNode next = stack.peek();//  w w  w. j a  v a 2 s  .co  m
        if (!next.visited) {
            if (next.parent != null && next.parent.getChildCount() > 1 && next != next.parent.getChild(0)) {
                rootNode.addtoMemoizedString(" ");
            }

            next.rootNode = rootNode;
            next.startIndx = rootNode.getMemoizedStringLen();

            // Leaf
            if (next.children == null || next.children.size() == 0) {
                String str = next.toString();
                rootNode.addtoMemoizedString(
                        next.getType() != HiveParser.StringLiteral ? str.toLowerCase() : str);
                next.endIndx = rootNode.getMemoizedStringLen();
                stack.pop();
                continue;
            }

            if (!next.isNil()) {
                rootNode.addtoMemoizedString("(");
                String str = next.toString();
                rootNode.addtoMemoizedString(
                        (next.getType() == HiveParser.StringLiteral || null == str) ? str : str.toLowerCase());
                rootNode.addtoMemoizedString(" ");
            }

            if (next.children != null) {
                for (int i = next.children.size() - 1; i >= 0; i--) {
                    stack.push((ASTNode) next.children.get(i));
                }
            }

            next.visited = true;
        } else {
            if (!next.isNil()) {
                rootNode.addtoMemoizedString(")");
            }
            next.endIndx = rootNode.getMemoizedStringLen();
            next.visited = false;
            stack.pop();
        }

    }

    return rootNode.getMemoizedSubString(startIndx, endIndx);
}