Chris Holland added a comment -
22/Feb/08 5:38 PM To get both going you'll have to be super-careful about rhino.jar vs custom_rhino.jar. I've run into issues when migrating my optimized static resource servlet from using shrinksafe to yui compressor, simply because i had both vanilla rhino which yahoo builds on-top of, and shrinksafe's custom rhino jars in my class path.
I'd highly recommend going with YUI though, as from what i've seen it compresses at least as well as shrinksafe, and is far cleaner/easier to integrate.
assuming scriptSource is a String representation of your JS source code .... here's a basic integration i did with YUI compressor:
StringReader resourceReader = new StringReader(scriptSource);
JavaScriptCompressor jsCompressor =
new JavaScriptCompressor(
resourceReader,
new ErrorReporter() {
public void warning(String message, String sourceName,
int line, String lineSource, int lineOffset) {
if (line < 0) {
log.warn(message);
} else {
log.error("\n" + line + ':' + lineOffset + ':' + message);
}
}
public void error(String message, String sourceName,
int line, String lineSource, int lineOffset) {
if (line < 0) {
log.error(message);
} else {
log.error(line + ':' + lineOffset + ':' + message);
}
}//
public EvaluatorException runtimeError(String message, String sourceName,
int line, String lineSource, int lineOffset) {
error(message, sourceName, line, lineSource, lineOffset);
return new EvaluatorException(message);
}
}//error reporter
);//JavaScriptCompressor constructor call;
resourceReader.close();
StringWriter writer = new StringWriter();
jsCompressor.compress(writer, 20000, false, false, false, false);
//up to you here ... :)
//minifiedTextResource = writer.toString().getBytes("UTF-8");
writer.close();