001// Licensed under the Apache License, Version 2.0 (the "License");
002// you may not use this file except in compliance with the License.
003// You may obtain a copy of the License at
004//
005// http://www.apache.org/licenses/LICENSE-2.0
006//
007// Unless required by applicable law or agreed to in writing, software
008// distributed under the License is distributed on an "AS IS" BASIS,
009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010// See the License for the specific language governing permissions and
011// limitations under the License.
012
013package org.apache.tapestry5.internal.webresources;
014
015import com.github.sommeri.less4j.Less4jException;
016import com.github.sommeri.less4j.LessCompiler;
017import com.github.sommeri.less4j.LessSource;
018import com.github.sommeri.less4j.core.DefaultLessCompiler;
019import org.apache.tapestry5.ContentType;
020import org.apache.tapestry5.internal.services.assets.BytestreamCache;
021import org.apache.tapestry5.ioc.Resource;
022import org.apache.tapestry5.services.assets.ResourceDependencies;
023import org.apache.tapestry5.services.assets.ResourceTransformer;
024
025import java.io.IOException;
026import java.io.InputStream;
027import java.io.UnsupportedEncodingException;
028
029/**
030 * Direct wrapper around the LessCompiler, so that Less source files may use {@code @import}, which isn't
031 * supported by the normal WRO4J processor.
032 */
033public class LessResourceTransformer implements ResourceTransformer
034{
035    private static final ContentType CSS = new ContentType("text/css");
036
037    private final LessCompiler compiler = new DefaultLessCompiler();
038
039    @Override
040    public ContentType getTransformedContentType()
041    {
042        return CSS;
043    }
044
045
046    @Override
047    public InputStream transform(Resource source, ResourceDependencies dependencies) throws IOException
048    {
049        BytestreamCache compiled = invokeLessCompiler(source, dependencies);
050
051        return compiled.openStream();
052    }
053
054    private BytestreamCache invokeLessCompiler(Resource source, ResourceDependencies dependencies) throws IOException
055    {
056        try
057        {
058            LessSource lessSource = new ResourceLessSource(source, dependencies);
059
060            LessCompiler.CompilationResult compilationResult = compile(compiler, lessSource);
061
062            // Currently, ignoring any warnings.
063
064            return new BytestreamCache(compilationResult.getCss().getBytes("utf-8"));
065
066        } catch (Less4jException ex)
067        {
068            throw new IOException(ex);
069        } catch (UnsupportedEncodingException ex)
070        {
071            throw new IOException(ex);
072        }
073    }
074
075    /**
076     * Invoked from {@link #transform(org.apache.tapestry5.ioc.Resource, org.apache.tapestry5.services.assets.ResourceDependencies)}
077     * to perform the actual work of compiling a {@link org.apache.tapestry5.ioc.Resource} which has been wrapped as a
078     * {@link com.github.sommeri.less4j.LessSource}.
079     */
080    protected LessCompiler.CompilationResult compile(LessCompiler compiler, LessSource lessSource) throws Less4jException
081    {
082        return compiler.compile(lessSource);
083    }
084}