001// Copyright 2013 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007// http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.internal.webresources;
016
017import com.google.javascript.jscomp.*;
018import com.google.javascript.jscomp.Compiler;
019import org.apache.commons.io.IOUtils;
020import org.apache.tapestry5.ioc.OperationTracker;
021import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
022import org.apache.tapestry5.ioc.internal.util.InternalUtils;
023import org.apache.tapestry5.services.assets.AssetChecksumGenerator;
024import org.apache.tapestry5.services.assets.StreamableResource;
025import org.slf4j.Logger;
026
027import java.io.IOException;
028import java.io.InputStream;
029import java.util.Collections;
030import java.util.List;
031import java.util.logging.Level;
032
033/**
034 * A wrapper around the Google Closure {@link Compiler} used to minimize
035 * a JavaScript resource.
036 */
037public class GoogleClosureMinimizer extends AbstractMinimizer
038{
039    private final List<SourceFile> EXTERNS = Collections.emptyList();
040
041    static
042    {
043        Compiler.setLoggingLevel(Level.SEVERE);
044    }
045
046    public GoogleClosureMinimizer(Logger logger, OperationTracker tracker, AssetChecksumGenerator checksumGenerator)
047    {
048        super(logger, tracker, checksumGenerator, "text/javascript");
049    }
050
051    @Override
052    protected InputStream doMinimize(StreamableResource resource) throws IOException
053    {
054        // Don't bother to pool the Compiler
055
056        CompilerOptions options = new CompilerOptions();
057        options.setCodingConvention(new ClosureCodingConvention());
058        options.setOutputCharset("utf-8");
059        options.setWarningLevel(DiagnosticGroups.CHECK_VARIABLES, CheckLevel.WARNING);
060
061        Compiler compiler = new Compiler();
062
063        compiler.disableThreads();
064
065        SourceFile input = SourceFile.fromInputStream(resource.toString(), resource.openStream());
066
067        List<SourceFile> inputs = Collections.singletonList(input);
068
069        Result result = compiler.compile(EXTERNS, inputs, options);
070
071        if (result.success)
072        {
073            return IOUtils.toInputStream(compiler.toSource());
074        }
075
076        throw new RuntimeException(String.format("Compilation failed: %s.",
077                InternalUtils.join(CollectionFactory.newList(result.errors), ";")));
078    }
079}