Package com.google.caja.reporting

Examples of com.google.caja.reporting.SimpleMessageQueue


    String publicId = "-//W3C//DTD XHTML 1.0 Transitional//EN";

    DocumentType documentType = domImpl.createDocumentType(
        qname, publicId, systemId);
    Document doc = domImpl.createDocument(null, null, documentType);
    mq = new SimpleMessageQueue();

    stack = new Html5ElementStack(doc, false, mq);
    stack.open(false);
  }
View Full Code Here


          node.getFilePosition().source(), requires, provides, overrides, node);
    }
  }

  private void runLinterTest(List<Linter.LintJob> inputs, String... messages) {
    MessageQueue mq = new SimpleMessageQueue();
    Linter.lint(inputs, new Linter.Environment(Sets.<String>newHashSet()), mq);
    List<String> actualMessageStrs = Lists.newArrayList();
    for (Message msg : mq.getMessages()) {
      actualMessageStrs.add(
          msg.getMessageLevel().name() + ": " + msg.format(mc));
    }

    List<String> goldenMessageStrs = Lists.newArrayList(messages);
View Full Code Here

    // where it will make things shorter.
    return (Statement) StatementSimplifier.optimize(block, mq);
  }

  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);
          opt.setEnvJson((ObjectConstructor) jsExpr(fromFile(jsonfile), mq));
        } else {
          if ("--".equals(arg)) { ++i; }
          for (;i < n; ++i) {
            CharProducer cp = fromFile(args[i]);
            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));
View Full Code Here

   * The default HTML4 whitelist.  See the JSON files in this directory for
   * the actual definitions.
   */
  public static HtmlSchema getDefault(MessageQueue mq) {
    if (defaultSchema == null) {
      SimpleMessageQueue cacheMq = new SimpleMessageQueue();
      URI elSrc = URI.create(
              "resource:///com/google/caja/lang/html/"
              + "html4-elements-extensions.json");
      URI attrSrc = URI.create(
              "resource:///com/google/caja/lang/html/"
              + "html4-attributes-extensions.json");
      try {
        defaultSchema = Pair.pair(
            new HtmlSchema(
                ConfigUtil.loadWhiteListFromJson(
                    elSrc, ConfigUtil.RESOURCE_RESOLVER, cacheMq),
                ConfigUtil.loadWhiteListFromJson(
                    attrSrc, ConfigUtil.RESOURCE_RESOLVER, cacheMq)),
            cacheMq.getMessages());
      // If the default schema is borked, there's not much we can do.
      } catch (IOException ex) {
        mq.getMessages().addAll(cacheMq.getMessages());
        throw new SomethingWidgyHappenedError("Default schema is borked", ex);
      } catch (ParseException ex) {
        cacheMq.getMessages().add(ex.getCajaMessage());
        mq.getMessages().addAll(cacheMq.getMessages());
        throw new SomethingWidgyHappenedError("Default schema is borked", ex);
      }
    }
    mq.getMessages().addAll(defaultSchema.b);
    return defaultSchema.a;
View Full Code Here

        if (Boolean.TYPE.equals(ptype)) {
          actuals[i] = Boolean.FALSE;  // No logging
        } else if (BuildInfo.class.isAssignableFrom(ptype)) {
          actuals[i] = BuildInfo.getInstance();
        } else if (MessageQueue.class.isAssignableFrom(ptype)) {
          actuals[i] = new SimpleMessageQueue();
        }
        ++i;
      }
      this.rewriter = ctor.newInstance(actuals);
    } catch (InstantiationException e) {
View Full Code Here

      for (File f : inputs) { canonFiles.add(f.getCanonicalFile()); }
    } catch (IOException ex) {
      logger.println(ex.toString());
      return false;
    }
    final MessageQueue mq = new SimpleMessageQueue();

    UriFetcher fetcher = new UriFetcher() {
        public FetchedData fetch(ExternalReference ref, String mimeType)
            throws UriFetchException {
          URI uri = ref.getUri();
          uri = ref.getReferencePosition().source().getUri().resolve(uri);
          InputSource is = new InputSource(uri);

          try {
            if (!canonFiles.contains(new File(uri).getCanonicalFile())) {
              throw new UriFetchException(ref, mimeType);
            }
          } catch (IllegalArgumentException ex) {
            throw new UriFetchException(ref, mimeType, ex);
          } catch (IOException ex) {
            throw new UriFetchException(ref, mimeType, ex);
          }

          try {
            String content = getSourceContent(is);
            if (content == null) {
              throw new UriFetchException(ref, mimeType);
            }
            return FetchedData.fromCharProducer(
                CharProducer.Factory.fromString(content, is),
                mimeType, Charsets.UTF_8.name());
          } catch (IOException ex) {
            throw new UriFetchException(ref, mimeType, ex);
          }
        }
      };

    UriPolicy policy;
    final UriPolicy prePolicy = new UriPolicy() {
      public String rewriteUri(
          ExternalReference u, UriEffect effect, LoaderType loader,
          Map<String, ?> hints) {
        // TODO(ihab.awad): Need to pass in the URI rewriter from the build
        // file somehow (as a Cajita program?). The below is a stub.
        return URI.create(
            "http://example.com/"
            + "?effect=" + effect + "&loader=" + loader
            + "&uri=" + UriUtil.encode("" + u.getUri()))
            .toString();
      }
    };
    final Set<?> lUrls = (Set<?>) options.get("canLink");
    if (!lUrls.isEmpty()) {
      policy = new UriPolicy() {
        public String rewriteUri(
            ExternalReference u, UriEffect effect,
            LoaderType loader, Map<String, ?> hints) {
          String uri = u.getUri().toString();
          if (lUrls.contains(uri)) { return uri; }
          return prePolicy.rewriteUri(u, effect, loader, hints);
        }
      };
    } else {
      policy = prePolicy;
    }

    MessageContext mc = new MessageContext();

    String language = (String) options.get("language");
    String rendererType = (String) options.get("renderer");

    if ("javascript".equals(language) && "concat".equals(rendererType)) {
      return concat(inputs, output, logger);
    }

    boolean passed = true;
    ParseTreeNode outputJs;
    Node outputHtml;
    if ("caja".equals(language)) {
      PluginMeta meta = new PluginMeta(fetcher, policy);
      meta.setPrecajoleMinify("minify".equals(rendererType));
      PluginCompiler compiler = new PluginCompiler(
          BuildInfo.getInstance(), meta, mq);
      compiler.setMessageContext(mc);
      if (Boolean.TRUE.equals(options.get("debug"))) {
        compiler.setGoals(compiler.getGoals()
            .without(PipelineMaker.ONE_CAJOLED_MODULE)
            .with(PipelineMaker.ONE_CAJOLED_MODULE_DEBUG));
      }
      if (Boolean.TRUE.equals(options.get("onlyJsEmitted"))) {
        compiler.setGoals(
            compiler.getGoals().without(PipelineMaker.HTML_SAFE_STATIC));
      }

      // Parse inputs
      for (File f : inputs) {
        try {
          URI fileUri = f.getCanonicalFile().toURI();
          ParseTreeNode parsedInput = new ParserContext(mq)
              .withInput(new InputSource(fileUri))
              .withConfig(meta)
              .build();
          if (parsedInput == null) {
            passed = false;
          } else {
            compiler.addInput(parsedInput, fileUri);
          }
        } catch (IOException ex) {
          logger.println("Failed to read " + f);
          passed = false;
        } catch (ParseException ex) {
          logger.println("Failed to parse " + f);
          ex.toMessageQueue(mq);
          passed = false;
        } catch (IllegalStateException e) {
          logger.println("Failed to configure parser " + e.getMessage());
          passed = false;
        }
      }

      // Cajole
      passed = passed && compiler.run();

      outputJs = passed ? compiler.getJavascript() : null;
      outputHtml = passed ? compiler.getStaticHtml() : null;
    } else if ("javascript".equals(language)) {
      PluginMeta meta = new PluginMeta(fetcher, policy);
      passed = true;
      JsOptimizer optimizer = new JsOptimizer(mq);
      for (File f : inputs) {
        try {
          if (f.getName().endsWith(".env.json")) {
            loadEnvJsonFile(f, optimizer, mq);
          } else {
            ParseTreeNode parsedInput = new ParserContext(mq)
            .withInput(new InputSource(f.getCanonicalFile().toURI()))
            .withConfig(meta)
            .build();
            if (parsedInput != null) {
              optimizer.addInput((Statement) parsedInput);
            }
          }
        } catch (IOException ex) {
          logger.println("Failed to read " + f);
          passed = false;
        } catch (ParseException ex) {
          logger.println("Failed to parse " + f);
          ex.toMessageQueue(mq);
          passed = false;
        } catch (IllegalStateException e) {
          logger.println("Failed to configure parser " + e.getMessage());
          passed = false;
        }
      }
      outputJs = optimizer.optimize();
      outputHtml = null;
    } else {
      throw new RuntimeException("Unrecognized language: " + language);
    }
    passed = passed && !hasErrors(mq);

    // From the ignore attribute to the <transform> element.
    Set<?> toIgnore = (Set<?>) options.get("toIgnore");
    if (toIgnore == null) { toIgnore = Collections.emptySet(); }

    // Log messages
    SnippetProducer snippetProducer = new SnippetProducer(originalSources, mc);
    for (Message msg : mq.getMessages()) {
      if (passed && MessageLevel.LOG.compareTo(msg.getMessageLevel()) >= 0) {
        continue;
      }
      String snippet = snippetProducer.getSnippet(msg);
      if (!"".equals(snippet)) { snippet = "\n" + snippet; }
View Full Code Here

  public boolean build(List<File> inputs, List<File> dependencies,
      Map<String, Object> options, File output) throws IOException {
    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();
        }
      }
    }
View Full Code Here

        mq.getMessages().addAll(p.b);
        return p.a;
      }
    }
    MessageQueue origMq = mq;
    SimpleMessageQueue cacheMq = new SimpleMessageQueue();
    this.mq = cacheMq;
    try {
      WhiteListSkeleton skel = loadSkeleton(
          expectJSONObject(JSONValue.parse(in), "whitelist"));
      if (in instanceof UriReader) {
        cache.put(
            ((UriReader) in).getUri(),
            Pair.pair(skel, cacheMq.getMessages()));
      }
      return skel;
    } finally {
      this.mq = origMq;
      origMq.getMessages().addAll(cacheMq.getMessages());
    }
  }
View Full Code Here

      return true;
    }
  }

  public static void main(String[] args) {
    HtmlSchema schema = HtmlSchema.getDefault(new SimpleMessageQueue());
    Block node = generateJavascriptDefinitions(schema);
    RenderContext rc = new RenderContext(node.makeRenderer(System.out, null));
    for (Statement s : node.children()) {
      s.render(rc);
      if (!s.isTerminal()) { rc.getOut().consume(";"); }
View Full Code Here

  private final Set<Name> propertiesAllowed;

  private static Pair<CssSchema, List<Message>> defaultSchema;
  public static CssSchema getDefaultCss21Schema(MessageQueue mq) {
    if (defaultSchema == null) {
      SimpleMessageQueue cacheMq = new SimpleMessageQueue();
      URI fnSrc = URI.create(
              "resource:///com/google/caja/lang/css/css-extensions-fns.json"),
          propSrc = URI.create(
              "resource:///com/google/caja/lang/css/css-extensions.json");
      WhiteList propDefs, fnDefs;
      try {
        propDefs = ConfigUtil.loadWhiteListFromJson(
            propSrc, ConfigUtil.RESOURCE_RESOLVER, cacheMq);
        fnDefs = ConfigUtil.loadWhiteListFromJson(
            fnSrc, ConfigUtil.RESOURCE_RESOLVER, cacheMq);
      // If the default schema is borked, there's not much we can do.
      } catch (IOException ex) {
        mq.getMessages().addAll(cacheMq.getMessages());
        throw new SomethingWidgyHappenedError("Default schema is borked", ex);
      } catch (ParseException ex) {
        ex.toMessageQueue(cacheMq);
        mq.getMessages().addAll(cacheMq.getMessages());
        throw new SomethingWidgyHappenedError("Default schema is borked", ex);
      }
      defaultSchema = Pair.pair(
          new CssSchema(propDefs, fnDefs), cacheMq.getMessages());
    }
    mq.getMessages().addAll(defaultSchema.b);
    return defaultSchema.a;
  }
View Full Code Here

TOP

Related Classes of com.google.caja.reporting.SimpleMessageQueue

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.