001/*
002 *  Copyright 2010 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.cms.transformation.html;
017
018import java.io.ByteArrayInputStream;
019import java.io.IOException;
020import java.io.InputStream;
021
022import org.apache.excalibur.source.Source;
023import org.apache.excalibur.source.SourceNotFoundException;
024import org.apache.excalibur.source.SourceValidity;
025import org.apache.excalibur.source.impl.validity.NOPValidity;
026
027/**
028 * Source based upon a string
029 */
030public class HTMLEnhancementSource implements Source
031{
032    private String _content;
033    private String _location;
034    
035    /**
036     * Build a source
037     * @param location The source location
038     * @param content The source content
039     */
040    public HTMLEnhancementSource(String location, String content)
041    {
042        _content = content;
043        _location = location;
044    }
045    
046    @Override
047    public boolean exists()
048    {
049        return true;
050    }
051
052    @Override
053    public long getContentLength()
054    {
055        return _content.length();
056    }
057
058    @Override
059    public InputStream getInputStream() throws IOException, SourceNotFoundException
060    {
061        byte[] contentAsBytes = _content.getBytes("UTF-8");
062        InputStream is = new ByteArrayInputStream(contentAsBytes);
063        return is;
064    }
065
066    @Override
067    public long getLastModified()
068    {
069        return -1;
070    }
071
072    @Override
073    public String getMimeType()
074    {
075        return "text/xml";
076    }
077
078    @Override
079    public String getScheme()
080    {
081        return _location.substring(0, _location.indexOf(":"));
082    }
083
084    @Override
085    public String getURI()
086    {
087        return _location;
088    }
089
090    @Override
091    public SourceValidity getValidity()
092    {
093        return new NOPValidity();
094    }
095
096    @Override
097    public void refresh()
098    {
099        // empty
100    }
101
102}