Java 에서 쓸 수 있는 Web Resource Compressor 관련 Framework 에 대해 간단히 소개해보고자 한다.
Web Resource Compressor 란 html, javascript, css 파일에 대한 Minifier, Beautifier 등의 기능을 수행하는 것을 말한다.
일종의 Web Resource 최적화 Framework 중 하나이다.
- Introduction
- Minifier
- HTML / XML
- HTML Compressor and MinifierHTML
- Project Home: https://code.google.com/p/htmlcompressor/
- Library: htmlcompressor-1.5.3.jar (v 1.5.3)
- Release Date: 2012-03
- License: Apache License 2.0
- Javascript / CSS
- YUI Compressor
- Project Home: http://yui.github.io/yuicompressor/
- Library: yuicompressor-2.4.7.jar (v 2.4.7)
- Release Date: 2011-09-06
- License: BSD License
- Beautifier
- HTML / JavaScript / CSS
- js-beautify
- Project Home: http://jsbeautifier.org/
- Library: beautify.js, beautify-css.js, beautify-html.js (v 1.3.2)
- Release Date: 2013-04-29
- License: MIT License
- 참고
- js-beautify 는 javascript 라이브러리 이기때문에 JAVA 에서 사용하기 위해서는 Rhino 엔진을 사용한다.
- Rhino
- Project Home: https://developer.mozilla.org/en-US/docs/tag/Rhino
- Library: js.jar (Rhino 1.7R4)
- Release Date: 2012-06-18
- License: MPL 2.0 License
- How to use?
- HTML Minifier - HTML Compressor
public void compress(InputStream source, OutputStream target) throws IOException { HtmlCompressor compressor = new HtmlCompressor(); compressor.setCompressJavaScript(true); // compress inline css compressor.setCompressCss(true); // compress inline javascript String compressedHtml = compressor.compress( IOUtil.getString( source, true ) ); target.write( compressedHtml.getBytes() ); }
- XML Minifier - HTML Compressor
public void compress(InputStream source, OutputStream target) throws IOException { XmlCompressor compressor = new XmlCompressor(); String compressedXml = compressor.compress( IOUtil.getString( source, true ) ); target.write( compressedXml.getBytes() ); }
- JavaScript Minifier - YUI Compressor
public void compress( InputStream source, OutputStream target ) throws EvaluatorException, IOException { InputStreamReader reader = null; OutputStreamWriter writer = null; try { reader = new InputStreamReader(source); writer = new OutputStreamWriter(target); JavaScriptCompressor compressor = new JavaScriptCompressor( reader,new ErrorReporter() { @Override public void warning(String message, String sourceName, int line, String lineSource, int lineOffset) { if (line < 0) { logger.warn(message); } else { String logMessage = line + " : " + lineOffset + " : " + message; logger.warn(logMessage); } } @Override public void error(String message, String sourceName, int line, String lineSource, int lineOffset) { if (line < 0) { logger.error(message); } else { String logMessage = line + " : " + lineOffset + " : " + message; logger.error(logMessage); } } @Override public EvaluatorException runtimeError(String message, String sourceName, int line, String lineSource, int lineOffset) { error(message, sourceName, line, lineSource, lineOffset); return new EvaluatorException(message); } }); // (out, linebreak, munge, verbose, preserveAllSemiColons, disableOptimizations) compressor.compress( writer, -1, true, false, true, false); } catch (EvaluatorException e) { throw e; } catch (IOException e) { throw e; } finally { if ( reader != null ) { reader.close() }; if ( writer != null ) { writer.close() }; } }
- CSS Minifier - YUI Compressor
public void compress(InputStream source, OutputStream target) throws IOException { InputStreamReader reader = null; OutputStreamWriter writer = null; try { reader = new InputStreamReader(source); writer = new OutputStreamWriter(target); CssCompressor compressor = new CssCompressor(reader); compressor.compress(writer, -1); } catch (IOException e) { throw e; } finally { if ( reader != null ) { reader.close() }; if ( writer != null ) { writer.close() }; } }
'Java' 카테고리의 다른 글
[Java] Schema Validation 수행시 XSD 파일간의 파일관계가 복잡할경우 Resolver 이용하기 (0) | 2013.12.10 |
---|---|
UTF-8 인코딩시 BOM(Byte Order Mark) 문제 (2) | 2013.10.30 |
[Java] Invalid Thread Access Error with Java SWT (0) | 2013.10.02 |
[Java] Eclipse 에서 Specified VM install not found 에러가 발생하는 경우 (0) | 2013.04.22 |
[Java] Java Code Convention (0) | 2013.02.18 |