com.yahoo.platform.yui.compressor.JavaScriptCompressor Java Examples

The following examples show how to use com.yahoo.platform.yui.compressor.JavaScriptCompressor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Compressor.java    From GreasySpoon with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Javascript compression method, based on yuiCompressor
 * @param content uncompressed JavaScript
 * @return A compressed JavaScript 
 */
public static String cleanupJavaScript(String content) {
	int length = content.length();
	int firstlinefeed = content.indexOf("\n");
	if (firstlinefeed==-1 || (length>255 && firstlinefeed>255)){
		//script seems to be already minified=> skip
		return content;
	}
	Reader reader = new StringReader(content);
	Writer writer = new StringWriter();		
	try {
		JavaScriptCompressor jsc = new JavaScriptCompressor(reader,jsReporter);
		jsc.compress(writer, -1, true, false, false, false);
	    //options: (int linebreak, boolean munge, boolean verbose, boolean preserveAllSemiColons, boolean disableOptimizations)
	} catch (Exception e) {
		return content;
	}
	return writer.toString();				
}
 
Example #2
Source File: YUICompressor.java    From validator-web with Apache License 2.0 5 votes vote down vote up
@Override
public String compressJavaScript(String script) throws Exception {
    StringReader stringReader = new StringReader(script);
    JavaScriptCompressor javaScriptCompressor = new JavaScriptCompressor(stringReader, new YUIErrorReporter());
    StringWriter stringWriter = new StringWriter();
    javaScriptCompressor.compress(stringWriter, linebreak, munge, verbose, preserveAllSemiColons, disableOptimizations);
    String compressedScript = stringWriter.toString();
    return compressedScript;
}
 
Example #3
Source File: YuiJavaScriptCompressor.java    From htmlcompressor with Apache License 2.0 5 votes vote down vote up
@Override
public String compress(String source) {
	
	StringWriter result = new StringWriter();
	try {
		JavaScriptCompressor compressor = new JavaScriptCompressor(new StringReader(source), errorReporter);
		compressor.compress(result, lineBreak, !noMunge, false, preserveAllSemiColons, disableOptimizations);
	} catch (IOException e) {
		result.write(source);
		e.printStackTrace();
	}
	return result.toString();
	
}