*/
public class Compressor {
public static String compile(List<FragmentDescriptor> scripts, IRequestProxy request,
CompressorSettings settings) throws JSCompileException {
Compiler compiler = new Compiler();
CompilerOptions options = new CompilerOptions();
options.markAsCompiled = true;
if (settings.getLocale() != null)
options.locale = settings.getLocale();
options.prettyPrint = settings.isFormatPrettyPrint();
options.printInputDelimiter = settings.isFormatPrintInputDelimiter();
if (settings.getOptimization() != null) {
if (settings.getOptimization().equalsIgnoreCase(CompressorSettings.ADVANCED_OPTIMIZATIONS_VALUE))
CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
else if (settings.getOptimization().equalsIgnoreCase(CompressorSettings.WHITESPACE_ONLY_VALUE))
CompilationLevel.WHITESPACE_ONLY.setOptionsForCompilationLevel(options);
else CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
}
List<JSSourceFile> sources = new ArrayList<JSSourceFile>();
for (FragmentDescriptor sd : scripts) {
JSSourceFile input;
if (sd instanceof ExternalFragment)
input = JSSourceFile.fromFile(request.getRealPath(((ExternalFragment) sd).getFilePath()));
else
input = JSSourceFile.fromCode("fragment" + Integer.toString(scripts.indexOf(sd)),
((InternalFragment) sd).getText());
sources.add(input);
}
Result res = compiler.compile(new JSSourceFile[]{}, sources.toArray(new JSSourceFile[]{}), options);
if (!res.success)
throw new JSCompileException();
String licenses = "";
try {
licenses = getLicenses(scripts, settings, request);
} catch (IOException e) {
throw new JSCompileException(e);
}
// The compiler is responsible for generating the compiled code; it is not
// accessible via the Result.
if (settings.getOutputWrapper() == null)
return licenses + compiler.toSource();
else
return licenses + settings.getOutputWrapper().replace(settings.getOutputWrapperMarker(),
compiler.toSource());
}