com.google.caja.reporting.RenderContext

Here are the examples of the java api class com.google.caja.reporting.RenderContext taken from open source projects.

1. EnvironmentData#normJs()

Project: caja
Source File: EnvironmentData.java
View license
static String normJs(String js, MessageQueue mq) throws ParseException {
    JsLexer lexer = new JsLexer(CharProducer.Factory.fromString(js, FilePosition.UNKNOWN));
    JsTokenQueue tq = new JsTokenQueue(lexer, InputSource.UNKNOWN);
    Expression e = new Parser(tq, mq).parseExpression(true);
    tq.expectEmpty();
    StringBuilder sb = new StringBuilder(js.length() + 16);
    RenderContext rc = new RenderContext(new JsMinimalPrinter(sb));
    e.render(rc);
    rc.getOut().noMoreTokens();
    return sb.toString();
}

2. HtmlDefinitions#generateSourceText()

Project: caja
Source File: HtmlDefinitions.java
View license
private static void generateSourceText(HtmlSchema schema, Writer out) throws IOException {
    String currentDate = "" + new Date();
    if (currentDate.indexOf("\n") >= 0) {
        throw new SomethingWidgyHappenedError("Date should not contain newline");
    }
    out.write("// Copyright Google Inc.\n");
    out.write("// Licensed under the Apache Licence Version 2.0\n");
    out.write("// Autogenerated at " + currentDate + "\n");
    out.write("// @overrides window\n");
    out.write("// @provides html4\n");
    Block node = generateJavascriptDefinitions(schema);
    RenderContext rc = new RenderContext(node.makeRenderer(out, null)).withPropertyNameQuotingMode(PropertyNameQuotingMode.PRESERVE_QUOTES);
    renderFlattenedBlocks(node, rc);
    rc.getOut().noMoreTokens();
    out.write("\n");
    out.write("// export for Closure Compiler\n");
    out.write("if (typeof window !== 'undefined') {\n");
    out.write("  window['html4'] = html4;\n");
    out.write("}\n");
}

3. RenderedExpression#render()

Project: caja
Source File: RenderedExpression.java
View license
private static String render(ParseTreeNode n, boolean minify) {
    StringBuilder buf = new StringBuilder();
    TokenConsumer tc;
    if (minify) {
        tc = new JsMinimalPrinter(new Concatenator(buf));
    } else {
        tc = new JsPrettyPrinter(new Concatenator(buf));
    }
    RenderContext rc = new RenderContext(tc);
    n.render(rc);
    tc.noMoreTokens();
    return buf.toString();
}

4. NodesTest#testRenderWithMaskedOutputNamespace1()

Project: caja
Source File: NodesTest.java
View license
public final void testRenderWithMaskedOutputNamespace1() throws Exception {
    DocumentFragment fragment = xmlFragment(fromString("<svg:foo><xml:bar/></svg:foo>"));
    Namespaces ns = new Namespaces(Namespaces.HTML_DEFAULT, "svg", Namespaces.XML_NAMESPACE_URI);
    StringBuilder sb = new StringBuilder();
    RenderContext rc = new RenderContext(new Concatenator(sb)).withMarkupRenderMode(MarkupRenderMode.XML);
    Nodes.render(fragment, ns, rc);
    rc.getOut().noMoreTokens();
    assertEquals("" + "<_ns2:foo xmlns:_ns2=\"http://www.w3.org/2000/svg\">" + "<svg:bar></svg:bar></_ns2:foo>", sb.toString());
}

5. NodesTest#testHtmlNamesNormalized()

Project: caja
Source File: NodesTest.java
View license
public final void testHtmlNamesNormalized() throws Exception {
    Document doc = DomParser.makeDocument(null, null);
    Element el = doc.createElementNS(Namespaces.HTML_NAMESPACE_URI, "SPAN");
    el.setAttributeNS(Namespaces.HTML_NAMESPACE_URI, "TITLE", "Howdy");
    assertEquals("<span title=\"Howdy\"></span>", Nodes.render(el));
    Namespaces ns = new Namespaces(Namespaces.HTML_DEFAULT, "html", Namespaces.HTML_NAMESPACE_URI);
    StringBuilder sb = new StringBuilder();
    RenderContext rc = new RenderContext(new Concatenator(sb)).withMarkupRenderMode(MarkupRenderMode.HTML);
    Nodes.render(el, ns, rc);
    rc.getOut().noMoreTokens();
    assertEquals("<html:span title=\"Howdy\"></html:span>", sb.toString());
}

6. ParserTest#runRenderTest()

Project: caja
Source File: ParserTest.java
View license
private void runRenderTest(String testFile, String goldenFile) throws Exception {
    Statement parseTree = js(fromResource(testFile));
    checkFilePositionInvariants(parseTree);
    StringBuilder sb = new StringBuilder();
    TokenConsumer tc = new JsPrettyPrinter(sb);
    RenderContext rc = new RenderContext(tc);
    parseTree.render(rc);
    tc.noMoreTokens();
    sb.append('\n');
    String golden = TestUtil.readResource(getClass(), goldenFile);
    String actual = sb.toString();
    assertEquals(actual, golden, actual);
}

7. JsOptimizer#main()

Project: caja
Source File: JsOptimizer.java
View license
public static void main(String... args) throws IOException {
    MessageQueue mq = new SimpleMessageQueue();
    MessageContext mc = new MessageContext();
    JsOptimizer opt = new JsOptimizer(mq);
    opt.setRename(true);
    opt.setEnvJson(new ObjectConstructor(FilePosition.UNKNOWN));
    try {
        for (int i = 0, n = args.length; i < n; ++i) {
            String arg = args[i];
            if ("--norename".equals(arg)) {
                opt.setRename(false);
            } else if (arg.startsWith("--envjson=")) {
                String jsonfile = arg.substring(arg.indexOf('=') + 1);
                CharProducer json = CharProducer.Factory.fromFile(new File(jsonfile), "UTF-8");
                opt.setEnvJson((ObjectConstructor) jsExpr(json, mq));
            } else {
                if ("--".equals(arg)) {
                    ++i;
                }
                for (; i < n; ++i) {
                    CharProducer cp = CharProducer.Factory.fromFile(new File(args[i]), "UTF-8");
                    mc.addInputSource(cp.getCurrentPosition().source());
                    opt.addInput(js(cp, mq));
                }
            }
        }
    } catch (ParseException ex) {
        ex.toMessageQueue(mq);
    }
    Statement out = opt.optimize();
    for (Message msg : mq.getMessages()) {
        msg.format(mc, System.err);
        System.err.println();
    }
    JsMinimalPrinter printer = new JsMinimalPrinter(new Concatenator(System.out, null));
    RenderContext rc = new RenderContext(printer).withPropertyNameQuotingMode(PropertyNameQuotingMode.NO_QUOTES);
    if (out instanceof Block) {
        ((Block) out).renderBody(rc);
    } else {
        out.render(rc);
    }
    printer.noMoreTokens();
}

8. CssPropertySignature#toString()

Project: caja
Source File: CssPropertySignature.java
View license
@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    RenderContext rc = new RenderContext(makeRenderer(sb, null));
    render(rc);
    rc.getOut().noMoreTokens();
    return sb.toString();
}

9. Nodes#render()

Project: caja
Source File: Nodes.java
View license
private static String render(Node node, MarkupRenderMode renderMode, boolean renderUnsafe) {
    StringBuilder sb = new StringBuilder();
    RenderContext rc = new RenderContext(new Concatenator(sb, null)).withMarkupRenderMode(renderMode);
    render(node, rc, renderUnsafe);
    rc.getOut().noMoreTokens();
    return sb.toString();
}

10. Minify#minify()

Project: caja
Source File: Minify.java
View license
public static boolean minify(Iterable<Pair<InputSource, File>> inputs, Writer out, PrintWriter err) throws IOException {
    MessageContext mc = new MessageContext();
    for (Pair<InputSource, File> input : inputs) {
        mc.addInputSource(input.a);
    }
    final MessageQueue errs = new EchoingMessageQueue(err, mc, false);
    RenderContext rc = new RenderContext(new JsMinimalPrinter(new Concatenator(out, new Callback<IOException>() {

        public void handle(IOException ex) {
            errs.addMessage(MessageType.IO_ERROR, MessagePart.Factory.valueOf(ex.getMessage()));
        }
    })));
    for (Pair<InputSource, File> input : inputs) {
        CharProducer cp = CharProducer.Factory.fromFile(input.b, Charsets.UTF_8.name());
        JsLexer lexer = new JsLexer(cp);
        JsTokenQueue tq = new JsTokenQueue(lexer, input.a);
        Parser p = new Parser(tq, errs);
        try {
            while (!tq.isEmpty()) {
                Block b = p.parse();
                for (Statement topLevelStmt : b.children()) {
                    topLevelStmt.render(rc);
                    if (!topLevelStmt.isTerminal()) {
                        rc.getOut().consume(";");
                    }
                }
            }
        } catch (ParseException ex) {
            ex.toMessageQueue(errs);
        }
    }
    rc.getOut().noMoreTokens();
    out.flush();
    MessageLevel maxMessageLevel = MessageLevel.values()[0];
    for (Message msg : errs.getMessages()) {
        if (msg.getMessageLevel().compareTo(maxMessageLevel) >= 0) {
            maxMessageLevel = msg.getMessageLevel();
        }
    }
    return maxMessageLevel.compareTo(MessageLevel.ERROR) < 0;
}

11. JsOptimizerTest#renderProgram()

Project: caja
Source File: JsOptimizerTest.java
View license
private static String renderProgram(Statement s) {
    StringBuilder out = new StringBuilder();
    RenderContext rc = new RenderContext(new JsMinimalPrinter(out));
    if (s instanceof Block) {
        ((Block) s).renderBody(rc);
    } else {
        s.render(rc);
    }
    rc.getOut().noMoreTokens();
    return out.toString();
}

12. StatementSimplifierTest#renderBody()

Project: caja
Source File: StatementSimplifierTest.java
View license
private static String renderBody(Block bl) {
    StringBuilder sb = new StringBuilder();
    RenderContext rc = new RenderContext(bl.makeRenderer(sb, null));
    bl.renderBody(rc);
    rc.getOut().noMoreTokens();
    return sb.toString();
}

13. StatementSimplifierTest#renderStmt()

Project: caja
Source File: StatementSimplifierTest.java
View license
private static String renderStmt(Statement s) {
    StringBuilder sb = new StringBuilder();
    RenderContext rc = new RenderContext(s.makeRenderer(sb, null));
    s.renderBlock(rc, true);
    rc.getOut().noMoreTokens();
    return sb.toString();
}

14. ParserTest#assertRender()

Project: caja
Source File: ParserTest.java
View license
private void assertRender(String code, String expectedRendering) throws Exception {
    StringBuilder sb = new StringBuilder();
    TokenConsumer tc = new JsPrettyPrinter(sb);
    RenderContext rc = new RenderContext(tc);
    js(fromString(code)).children().get(0).render(rc);
    tc.noMoreTokens();
    assertEquals(code, expectedRendering, sb.toString());
}

15. ParserTest#assertMinified()

Project: caja
Source File: ParserTest.java
View license
private void assertMinified(String code, String expectedRendering) throws Exception {
    StringBuilder sb = new StringBuilder();
    TokenConsumer tc = new JsMinimalPrinter(sb);
    RenderContext rc = new RenderContext(tc);
    js(fromString(code)).children().get(0).render(rc);
    tc.noMoreTokens();
    assertEquals(code, expectedRendering, sb.toString());
}