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;
if ("caja".equals(language)) {
throw new IllegalArgumentException("language=caja no longer supported");
} 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();
} 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; }