From 1e38111d0522ca41d24f784af55baa3bf9fc3909 Mon Sep 17 00:00:00 2001 From: vigorouscoding Date: Mon, 22 Aug 2016 15:58:37 +0200 Subject: [PATCH 1/2] make `CssCompressor` more usable from a Java context - introduce a constructor which takes a String argument so we do not need a file - move actual CSS compression to method `getCompressedCss()` so we can obtain the minified CSS without needing to write it to a file --- .../platform/yui/compressor/CssCompressor.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/com/yahoo/platform/yui/compressor/CssCompressor.java b/src/com/yahoo/platform/yui/compressor/CssCompressor.java index 8a5cfe93..38e235bb 100644 --- a/src/com/yahoo/platform/yui/compressor/CssCompressor.java +++ b/src/com/yahoo/platform/yui/compressor/CssCompressor.java @@ -30,6 +30,10 @@ public CssCompressor(Reader in) throws IOException { } } + public CssCompressor(String in) { + srcsb.append(in); + } + /** * @param css - full css string * @param preservedToken - token to preserve @@ -103,8 +107,7 @@ protected String preserveToken(String css, String preservedToken, return sb.toString(); } - public void compress(Writer out, int linebreakpos) - throws IOException { + public String getCompressedCss(int linebreakpos) { Pattern p; Matcher m; @@ -505,6 +508,14 @@ public void compress(Writer out, int linebreakpos) // Trim the final string (for any leading or trailing white spaces) css = css.trim(); + return css; + } + + public void compress(Writer out, int linebreakpos) + throws IOException { + + String css = getCompressedCss(linebreakpos); + // Write the output... out.write(css); } From 31b9543faa6a89e0f8a7f073a7552ce6a7d8cab1 Mon Sep 17 00:00:00 2001 From: vigorouscoding Date: Mon, 22 Aug 2016 16:24:34 +0200 Subject: [PATCH 2/2] make `JavaScriptCompressor` more usable from a Java context - introduced a new constructr which takes a String argument so we do not need a file - move actuall JS compression to `getCompressedCode()` method so we can obtain the minified JS without needing to write to a file --- .../yui/compressor/JavaScriptCompressor.java | 39 +++++++++++++++---- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/src/com/yahoo/platform/yui/compressor/JavaScriptCompressor.java b/src/com/yahoo/platform/yui/compressor/JavaScriptCompressor.java index 03362036..caf8605f 100755 --- a/src/com/yahoo/platform/yui/compressor/JavaScriptCompressor.java +++ b/src/com/yahoo/platform/yui/compressor/JavaScriptCompressor.java @@ -313,8 +313,20 @@ private static ArrayList parse(Reader in, ErrorReporter reporter) env.setLanguageVersion(Context.VERSION_1_7); Parser parser = new Parser(env, reporter); parser.parse(in, null, 1); - String source = parser.getEncodedSource(); + return getTokensFromEncodedSource(parser.getEncodedSource()); + } + + private static ArrayList parse(String in, ErrorReporter reporter) + throws IOException, EvaluatorException { + + CompilerEnvirons env = new CompilerEnvirons(); + env.setLanguageVersion(Context.VERSION_1_7); + Parser parser = new Parser(env, reporter); + parser.parse(in, null, 1); + return getTokensFromEncodedSource(parser.getEncodedSource()); + } + private static ArrayList getTokensFromEncodedSource (String source) { int offset = 0; int length = (source != null) ? source.length() : 0; ArrayList tokens = new ArrayList(); @@ -539,6 +551,12 @@ public JavaScriptCompressor(Reader in, ErrorReporter reporter) this.logger = reporter; this.tokens = parse(in, reporter); } + public JavaScriptCompressor(String in, ErrorReporter reporter) + throws IOException, EvaluatorException { + + this.logger = reporter; + this.tokens = parse(in, reporter); + } public void compress(Writer out, int linebreak, boolean munge, boolean verbose, boolean preserveAllSemiColons, boolean disableOptimizations) throws IOException { @@ -549,6 +567,17 @@ public void compress(Writer out, Writer mungemap, int linebreak, boolean munge, boolean preserveAllSemiColons, boolean disableOptimizations, boolean preserveUnknownHints) throws IOException { + out.write(getCompressedCode(mungemap, linebreak, munge, verbose, preserveAllSemiColons, disableOptimizations, preserveUnknownHints)); + + if (mungemap != null) { + printMungeMapping(mungemap); + } + } + + public String getCompressedCode (Writer mungemap, int linebreak, boolean munge, boolean verbose, + boolean preserveAllSemiColons, boolean disableOptimizations, boolean preserveUnknownHints) + throws IOException { + this.munge = munge; this.verbose = verbose; this.preserveUnknownHints = preserveUnknownHints; @@ -565,12 +594,8 @@ public void compress(Writer out, Writer mungemap, int linebreak, boolean munge, mungeSymboltree(); StringBuffer sb = printSymbolTree(linebreak, preserveAllSemiColons); - out.write(sb.toString()); - - if (mungemap != null) { - printMungeMapping(mungemap); - } - } + return sb.toString(); + } private ScriptOrFnScope getCurrentScope() { return (ScriptOrFnScope) scopes.peek();