@Test public void testSimpleJs() throws Exception { final String js = "function myFunction()\n" + "{\n" + "var x=\"\",i;\n" + "for (i=0;i<5;i++)\n" + " {\n" + " x=x + \"The number is \" + i + \"<br>\";\n" + " }\n" + "document.getElementById(\"demo\").innerHTML=x;\n" + "}"; final StringReader reader = new StringReader(js); final StringWriter writer = new StringWriter(); googleClosureOutputProcessor.process(reader, writer); final String expected = "function myFunction(){var b=\"\",a;for(a=0;5>a;a++)b=b+\"The number is \"+a+\"\\x3cbr\\x3e\";document.getElementById(\"demo\").innerHTML=b};"; final String compiled = writer.getBuffer().toString(); assertEquals(expected, compiled); }
@Override public void process(final Reader reader, final Writer writer) throws IOException { final String originalJsSourceCode = IOUtils.toString(reader); try { Compiler.setLoggingLevel(Level.SEVERE); final Compiler compiler = new Compiler(); final CompilerOptions compilerOptions = newCompilerOptions(); compilationLevel.setOptionsForCompilationLevel(compilerOptions); compiler.disableThreads(); compiler.initOptions(compilerOptions); final SourceFile input = SourceFile.fromInputStream("dummy.js", new ByteArrayInputStream(originalJsSourceCode.getBytes(getEncoding()))); final Result result = compiler.compile(Lists.<SourceFile>newArrayList(), Lists.newArrayList(input), compilerOptions); logWarningsAndErrors(result); boolean origFallback = false; if (result.success) { final String minifiedJsSourceCode = compiler.toSource(); if (StringUtils.isEmpty(minifiedJsSourceCode)) { origFallback = true; } else { writer.write(minifiedJsSourceCode); } } else { origFallback = true; } if (origFallback) { writer.write(originalJsSourceCode); } } finally { reader.close(); writer.close(); } }
GoogleClosureOutputProcessor implements OutputProcessor { @Override public void process(final Reader reader, final Writer writer) throws IOException { final String originalJsSourceCode = IOUtils.toString(reader); try { Compiler.setLoggingLevel(Level.SEVERE); final Compiler compiler = new Compiler(); final CompilerOptions compilerOptions = newCompilerOptions(); compilationLevel.setOptionsForCompilationLevel(compilerOptions); compiler.disableThreads(); compiler.initOptions(compilerOptions); final SourceFile input = SourceFile.fromInputStream("dummy.js", new ByteArrayInputStream(originalJsSourceCode.getBytes(getEncoding()))); final Result result = compiler.compile(Lists.<SourceFile>newArrayList(), Lists.newArrayList(input), compilerOptions); logWarningsAndErrors(result); boolean origFallback = false; if (result.success) { final String minifiedJsSourceCode = compiler.toSource(); if (StringUtils.isEmpty(minifiedJsSourceCode)) { origFallback = true; } else { writer.write(minifiedJsSourceCode); } } else { origFallback = true; } if (origFallback) { writer.write(originalJsSourceCode); } } finally { reader.close(); writer.close(); } } }
GoogleClosureOutputProcessor implements OutputProcessor { @Override public void process(final Reader reader, final Writer writer) throws IOException { final String originalJsSourceCode = IOUtils.toString(reader); try { Compiler.setLoggingLevel(Level.SEVERE); final Compiler compiler = new Compiler(); final CompilerOptions compilerOptions = newCompilerOptions(); compilationLevel.setOptionsForCompilationLevel(compilerOptions); compiler.disableThreads(); compiler.initOptions(compilerOptions); final SourceFile input = SourceFile.fromInputStream("dummy.js", new ByteArrayInputStream(originalJsSourceCode.getBytes(getEncoding()))); final Result result = compiler.compile(Lists.<SourceFile>newArrayList(), Lists.newArrayList(input), compilerOptions); logWarningsAndErrors(result); boolean origFallback = false; if (result.success) { final String minifiedJsSourceCode = compiler.toSource(); if (StringUtils.isEmpty(minifiedJsSourceCode)) { origFallback = true; } else { writer.write(minifiedJsSourceCode); } } else { origFallback = true; } if (origFallback) { writer.write(originalJsSourceCode); } } finally { reader.close(); writer.close(); } }  }
GoogleClosureOutputProcessor implements OutputProcessor { @Override public void process(final Reader reader, final Writer writer) throws IOException { final String originalJsSourceCode = IOUtils.toString(reader); try { Compiler.setLoggingLevel(Level.SEVERE); final Compiler compiler = new Compiler(); final CompilerOptions compilerOptions = newCompilerOptions(); compilationLevel.setOptionsForCompilationLevel(compilerOptions); compiler.disableThreads(); compiler.initOptions(compilerOptions); final SourceFile input = SourceFile.fromInputStream("dummy.js", new ByteArrayInputStream(originalJsSourceCode.getBytes(getEncoding()))); final Result result = compiler.compile(Lists.<SourceFile>newArrayList(), Lists.newArrayList(input), compilerOptions); logWarningsAndErrors(result); boolean origFallback = false; if (result.success) { final String minifiedJsSourceCode = compiler.toSource(); if (StringUtils.isEmpty(minifiedJsSourceCode)) { origFallback = true; } else { writer.write(minifiedJsSourceCode); } } else { origFallback = true; } if (origFallback) { writer.write(originalJsSourceCode); } } finally { reader.close(); writer.close(); } }  @Override void process(final Reader reader, final Writer writer); String getEncoding(); void setEncoding(String encoding); void setCompilationLevel(String compilationLevel); CompilationLevel getCompilationLevel(); void setLogCompilerErrors(boolean logCompilerErrors); void setLogCompilerWarnings(boolean logCompilerWarnings); }
GoogleClosureOutputProcessor implements OutputProcessor { @Override public void process(final Reader reader, final Writer writer) throws IOException { final String originalJsSourceCode = IOUtils.toString(reader); try { Compiler.setLoggingLevel(Level.SEVERE); final Compiler compiler = new Compiler(); final CompilerOptions compilerOptions = newCompilerOptions(); compilationLevel.setOptionsForCompilationLevel(compilerOptions); compiler.disableThreads(); compiler.initOptions(compilerOptions); final SourceFile input = SourceFile.fromInputStream("dummy.js", new ByteArrayInputStream(originalJsSourceCode.getBytes(getEncoding()))); final Result result = compiler.compile(Lists.<SourceFile>newArrayList(), Lists.newArrayList(input), compilerOptions); logWarningsAndErrors(result); boolean origFallback = false; if (result.success) { final String minifiedJsSourceCode = compiler.toSource(); if (StringUtils.isEmpty(minifiedJsSourceCode)) { origFallback = true; } else { writer.write(minifiedJsSourceCode); } } else { origFallback = true; } if (origFallback) { writer.write(originalJsSourceCode); } } finally { reader.close(); writer.close(); } }  @Override void process(final Reader reader, final Writer writer); String getEncoding(); void setEncoding(String encoding); void setCompilationLevel(String compilationLevel); CompilationLevel getCompilationLevel(); void setLogCompilerErrors(boolean logCompilerErrors); void setLogCompilerWarnings(boolean logCompilerWarnings);  }