com.google.caja.reporting.MessageContext

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

1. ParseTreeNodeTest#testFormatTree()

Project: caja
Source File: ParseTreeNodeTest.java
View license
public final void testFormatTree() throws Exception {
    StringBuilder sb = new StringBuilder();
    MessageContext mc = new MessageContext();
    root.formatTree(mc, 0, sb);
    assertEquals("LabeledStmtWrapper : $0\n" + "  Block\n" + "    LabeledStmtWrapper : $1\n" + "      Block\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 2\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 3\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 4\n" + "    LabeledStmtWrapper : $5\n" + "      Block\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 6\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 7\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 8", sb.toString());
}

2. ParseTreeNodeTest#testFormatTreePostRemove()

Project: caja
Source File: ParseTreeNodeTest.java
View license
public final void testFormatTreePostRemove() throws Exception {
    doRemove(5);
    StringBuilder sb = new StringBuilder();
    MessageContext mc = new MessageContext();
    root.formatTree(mc, 0, sb);
    assertEquals("LabeledStmtWrapper : $0\n" + "  Block\n" + "    LabeledStmtWrapper : $1\n" + "      Block\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 2\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 3\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 4", sb.toString());
}

3. ParseTreeNodeTest#testFormatTreePostRemove2()

Project: caja
Source File: ParseTreeNodeTest.java
View license
public final void testFormatTreePostRemove2() throws Exception {
    doRemove(1);
    StringBuilder sb = new StringBuilder();
    MessageContext mc = new MessageContext();
    root.formatTree(mc, 0, sb);
    assertEquals("LabeledStmtWrapper : $0\n" + "  Block\n" + "    LabeledStmtWrapper : $5\n" + "      Block\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 6\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 7\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 8", sb.toString());
}

4. MatchExperiments#parse()

Project: caja
Source File: MatchExperiments.java
View license
public static ParseTreeNode parse(String src) throws Exception {
    MessageContext mc = new MessageContext();
    MessageQueue mq = TestUtil.createTestMessageQueue(mc);
    InputSource is = new InputSource(new URI("file:///no/input/source"));
    CharProducer cp = CharProducer.Factory.create(new StringReader(src), is);
    JsLexer lexer = new JsLexer(cp);
    JsTokenQueue tq = new JsTokenQueue(lexer, is, JsTokenQueue.NO_COMMENT);
    Parser p = new Parser(tq, mq);
    Statement stmt = p.parse();
    p.getTokenQueue().expectEmpty();
    return stmt;
}

5. FilePosition#formatShort()

Project: caja
Source File: FilePosition.java
View license
public final void formatShort(Appendable out) throws IOException {
    MessageContext mc = new MessageContext();
    InputSource is = source();
    mc.addInputSource(is);
    out.append(mc.abbreviate(is)).append(":").append(String.valueOf(this.startLineNo()));
}

6. Linter#build()

Project: caja
Source File: Linter.java
View license
public boolean build(List<File> inputs, List<File> dependencies, Map<String, Object> options, File output) throws IOException {
    Set<?> ignores = (Set<?>) options.get("toIgnore");
    if (ignores == null) {
        ignores = Collections.emptySet();
    }
    MessageContext mc = new MessageContext();
    Map<InputSource, CharSequence> contentMap = Maps.newLinkedHashMap();
    MessageQueue mq = new SimpleMessageQueue();
    List<LintJob> lintJobs = parseInputs(inputs, contentMap, mc, mq);
    lint(lintJobs, env, mq);
    if (!ignores.isEmpty()) {
        for (Iterator<Message> it = mq.getMessages().iterator(); it.hasNext(); ) {
            if (ignores.contains(it.next().getMessageType().name())) {
                it.remove();
            }
        }
    }
    if (output.getName().endsWith(".stamp") || output.getName().endsWith(".tstamp")) {
        if (ErrorReporter.reportErrors(contentMap, mc, mq, System.out).compareTo(MessageLevel.WARNING) < 0) {
            // Touch the time-stamp file to make it clear that the inputs were
            // successfully linted.
            (new FileOutputStream(output)).close();
            return true;
        } else {
            return false;
        }
    } else {
        Writer reportOut = new OutputStreamWriter(new FileOutputStream(output), Charsets.UTF_8);
        boolean result;
        try {
            result = ErrorReporter.reportErrors(contentMap, mc, mq, reportOut).compareTo(MessageLevel.WARNING) < 0;
        } finally {
            try {
                reportOut.close();
            } catch (IOException ex) {
                result = false;
            }
        }
        return result;
    }
}

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. 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;
}

9. ProxyServlet#serializeMessageQueue()

Project: caja
Source File: ProxyServlet.java
View license
// TODO(jasvir): The service like the gwt version should accumulate
// input sources and use html snippet producer to produce messages
private static String serializeMessageQueue(MessageQueue mq) {
    StringBuilder sb = new StringBuilder();
    MessageContext mc = new MessageContext();
    for (Message m : mq.getMessages()) {
        sb.append(m.getMessageLevel().name()).append(": ");
        Escaping.escapeXml(m.format(mc), false, sb);
        sb.append("\n");
    }
    return sb.toString();
}

10. ParseTreeNodeTest#testFormatTreePostReplace()

Project: caja
Source File: ParseTreeNodeTest.java
View license
public final void testFormatTreePostReplace() throws Exception {
    doReplace();
    StringBuilder sb = new StringBuilder();
    MessageContext mc = new MessageContext();
    root.formatTree(mc, 0, sb);
    assertEquals("LabeledStmtWrapper : $0\n" + "  Block\n" + "    LabeledStmtWrapper : $1\n" + "      Block\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 2\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 3\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 4\n" + "    LabeledStmtWrapper : $9\n" + "      Block\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 10\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 11\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 12", sb.toString());
}

11. ParseTreeNodeTest#testFormatTreePostInsert()

Project: caja
Source File: ParseTreeNodeTest.java
View license
public final void testFormatTreePostInsert() throws Exception {
    doInsert(5);
    StringBuilder sb = new StringBuilder();
    MessageContext mc = new MessageContext();
    root.formatTree(mc, 0, sb);
    assertEquals("LabeledStmtWrapper : $0\n" + "  Block\n" + "    LabeledStmtWrapper : $1\n" + "      Block\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 2\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 3\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 4\n" + "    LabeledStmtWrapper : $9\n" + "      Block\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 10\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 11\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 12\n" + "    LabeledStmtWrapper : $5\n" + "      Block\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 6\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 7\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 8", sb.toString());
}

12. ParseTreeNodeTest#testFormatTreePostInsert2()

Project: caja
Source File: ParseTreeNodeTest.java
View license
public final void testFormatTreePostInsert2() throws Exception {
    doInsert(1);
    StringBuilder sb = new StringBuilder();
    MessageContext mc = new MessageContext();
    root.formatTree(mc, 0, sb);
    assertEquals("LabeledStmtWrapper : $0\n" + "  Block\n" + "    LabeledStmtWrapper : $9\n" + "      Block\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 10\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 11\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 12\n" + "    LabeledStmtWrapper : $1\n" + "      Block\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 2\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 3\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 4\n" + "    LabeledStmtWrapper : $5\n" + "      Block\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 6\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 7\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 8", sb.toString());
}

13. ParseTreeNodeTest#testFormatTreePostInsert3()

Project: caja
Source File: ParseTreeNodeTest.java
View license
public final void testFormatTreePostInsert3() throws Exception {
    doInsert(-1);
    StringBuilder sb = new StringBuilder();
    MessageContext mc = new MessageContext();
    root.formatTree(mc, 0, sb);
    assertEquals("LabeledStmtWrapper : $0\n" + "  Block\n" + "    LabeledStmtWrapper : $1\n" + "      Block\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 2\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 3\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 4\n" + "    LabeledStmtWrapper : $5\n" + "      Block\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 6\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 7\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 8\n" + "    LabeledStmtWrapper : $9\n" + "      Block\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 10\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 11\n" + "        ExpressionStmt\n" + "          IntegerLiteral : 12", sb.toString());
}

14. MatchExperiments#format()

Project: caja
Source File: MatchExperiments.java
View license
private static String format(ParseTreeNode n) throws Exception {
    MessageContext mc = new MessageContext();
    StringBuilder output = new StringBuilder();
    n.format(mc, output);
    return output.toString();
}

15. RhinoTestBed#runJsUnittestFromHtml()

Project: caja
Source File: RhinoTestBed.java
View license
/**
   * Given an HTML file that references javascript sources, load all
   * the scripts, set up the DOM using env.js, and start JSUnit.
   *
   * <p>This lets us write test html files that can be run both
   * in a browser, and automatically via ANT.
   *
   * <p>NOTE: This method interprets the input HTML in an idiosyncratic way to
   * facilitate conveniently bundling test code into one file. It runs each
   * {@code <script>} block as plain JavaScript.
   *
   * @param html an HTML DOM tree to run in Rhino.
   */
public static void runJsUnittestFromHtml(DocumentFragment html) throws IOException, ParseException {
    // Used to get HTML to env.js
    TestUtil.enableContentUrls();
    List<Executor.Input> inputs = Lists.newArrayList();
    // Stub out the Browser
    inputs.add(new Executor.Input(RhinoTestBed.class, "../plugin/console-stubs.js"));
    inputs.add(new Executor.Input(RhinoTestBed.class, "/js/envjs/env.js"));
    int injectHtmlIndex = inputs.size();
    List<Pair<String, InputSource>> scriptContent = new ArrayList<Pair<String, InputSource>>();
    MessageContext mc = new MessageContext();
    List<Element> scripts = new ArrayList<Element>();
    for (Node root : Nodes.childrenOf(html)) {
        if (root.getNodeType() == 1) {
            for (Element script : Nodes.nodeListIterable(((Element) root).getElementsByTagNameNS(HTML_NS, "script"), Element.class)) {
                scripts.add(script);
            }
        }
    }
    for (Element script : scripts) {
        Attr src = script.getAttributeNodeNS(HTML_NS, "src");
        CharProducer scriptBody;
        if (src != null) {
            String resourcePath = src.getNodeValue();
            InputSource resource;
            if (resourcePath.startsWith("/")) {
                try {
                    resource = new InputSource(RhinoTestBed.class.getResource(resourcePath).toURI());
                } catch (URISyntaxException ex) {
                    throw new SomethingWidgyHappenedError("java.net.URL is not a valid java.net.URI", ex);
                }
            } else {
                InputSource baseUri = Nodes.getFilePositionFor(html).source();
                resource = new InputSource(baseUri.getUri().resolve(resourcePath));
            }
            scriptBody = loadResource(resource);
        } else {
            scriptBody = textContentOf(script);
        }
        String scriptText;
        InputSource isrc = scriptBody.getSourceBreaks(0).source();
        // Add blank lines at the front so that Rhino stack traces have correct
        // line numbers.
        scriptText = prefixWithBlankLines(scriptBody.toString(0, scriptBody.getLimit()), Nodes.getFilePositionFor(script).startLineNo() - 1);
        scriptContent.add(Pair.pair(scriptText, isrc));
        mc.addInputSource(isrc);
        script.getParentNode().removeChild(script);
    }
    for (Pair<String, InputSource> script : scriptContent) {
        inputs.add(new Executor.Input(script.a, mc.abbreviate(script.b)));
    }
    // Set up the DOM.  env.js requires that location be set to a URI before it
    // creates a DOM.  Since it fetches HTML via java.net.URL and passes it off
    // to the org.w3c parser, we use a content: URL which is handled by handlers
    // registered in TestUtil so that we can provide html without having a file
    // handy.
    String domJs = "window.location = " + StringLiteral.toQuotedValue(TestUtil.makeContentUrl(Nodes.render(html))) + ";";
    String htmlSource = Nodes.getFilePositionFor(html).source().toString();
    inputs.add(injectHtmlIndex, new Executor.Input(domJs, htmlSource));
    inputs.add(new Executor.Input("(function () {\n" + "   var onload = document.body.getAttribute('onload');\n" + "   onload && eval(onload);\n" + "   return document.title;\n" + " })();", htmlSource));
    // Execute tests
    String title = (String) runJs(inputs.toArray(new Executor.Input[inputs.size()]));
    // Test for success
    if (title == null || !title.contains("all tests passed")) {
        throw new junit.framework.AssertionFailedError("Rhino tests did not pass; title = " + title);
    }
}