Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make compressors more usable from Java context #257

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/com/yahoo/platform/yui/compressor/CssCompressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
39 changes: 32 additions & 7 deletions src/com/yahoo/platform/yui/compressor/JavaScriptCompressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
Expand All @@ -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();
Expand Down