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.docbook;
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 DocbookEnhancementSource implements Source
031{
032    private String _content;
033    private String _location;
034    private long _timestamp;
035    
036    /**
037     * Build a source
038     * @param location The source location
039     * @param content The source content
040     * @param timestamp the application startup time.
041     */
042    public DocbookEnhancementSource(String location, String content, long timestamp)
043    {
044        _content = content;
045        _location = location;
046        _timestamp = timestamp;
047    }
048    
049    @Override
050    public boolean exists()
051    {
052        return true;
053    }
054
055    @Override
056    public long getContentLength()
057    {
058        return _content.length();
059    }
060
061    @Override
062    public InputStream getInputStream() throws IOException, SourceNotFoundException
063    {
064        byte[] contentAsBytes = _content.getBytes("UTF-8");
065        InputStream is = new ByteArrayInputStream(contentAsBytes);
066        return is;
067    }
068
069    @Override
070    public long getLastModified()
071    {
072        return _timestamp;
073    }
074
075    @Override
076    public String getMimeType()
077    {
078        return "text/xml";
079    }
080
081    @Override
082    public String getScheme()
083    {
084        return _location.substring(0, _location.indexOf(":"));
085    }
086
087    @Override
088    public String getURI()
089    {
090        return _location;
091    }
092
093    @Override
094    public SourceValidity getValidity()
095    {
096        return new NOPValidity();
097    }
098
099    @Override
100    public void refresh()
101    {
102        // empty
103    }
104}