Java 에서 쓸 수 있는 Web Resource Compressor  관련 Framework 에 대해 간단히 소개해보고자 한다.


Web Resource Compressor  란 html, javascript, css 파일에 대한 Minifier, Beautifier 등의 기능을 수행하는 것을 말한다.


일종의 Web Resource 최적화 Framework 중 하나이다.



- Introduction


- 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() };
        }
    }

Posted by leechwin