    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.LessSource;
    016import org.apache.commons.io.IOUtils;
    017import org.apache.tapestry5.ioc.Resource;
    018import org.apache.tapestry5.ioc.internal.util.InternalUtils;
    019import org.apache.tapestry5.services.assets.ResourceDependencies;
    020
    021import java.io.FileNotFoundException;
    022import java.io.IOException;
    023import java.io.InputStreamReader;
    024import java.io.Reader;
    025
    026public class ResourceLessSource extends LessSource
    027{
    028    private final Resource resource;
    029
    030    private final ResourceDependencies dependencies;
    031
    032    ResourceLessSource(Resource resource, ResourceDependencies dependencies)
    033    {
    034        this.resource = resource;
    035        this.dependencies = dependencies;
    036    }
    037
    038    @Override
    039    public LessSource relativeSource(String filename) throws FileNotFound, CannotReadFile, StringSourceException
    040    {
    041        Resource relative = resource.forFile(filename);
    042
    043        if (!relative.exists())
    044        {
    045            throw new FileNotFound();
    046        }
    047
    048        dependencies.addDependency(relative);
    049
    050        return new ResourceLessSource(relative, dependencies);
    051    }
    052
    053    @Override
    054    public String getContent() throws FileNotFound, CannotReadFile
    055    {
    056        // Adapted from Less's URLSource
    057        Reader input = null;
    058        try
    059        {
    060            input = new InputStreamReader(resource.openStream());
    061            String content = IOUtils.toString(input).replace("\r\n", "\n");
    062
    063            return content;
    064        } catch (FileNotFoundException ex)
    065        {
    066            throw new FileNotFound();
    067        } catch (IOException ex)
    068        {
    069            throw new CannotReadFile();
    070        } finally
    071        {
    072            InternalUtils.close(input);
    073        }
    074    }
    075
    076    @Override
    077    public byte[] getBytes() throws FileNotFound, CannotReadFile
    078    {
    079        Reader input = null;
    080        try
    081        {
    082            input = new InputStreamReader(resource.openStream());
    083
    084            return IOUtils.toByteArray(input);
    085        } catch (FileNotFoundException ex)
    086        {
    087            throw new FileNotFound();
    088        } catch (IOException ex)
    089        {
    090            throw new CannotReadFile();
    091        } finally
    092        {
    093            InternalUtils.close(input);
    094        }
    095
    096    }
    097}