001    package echopoint.template;
002    /* 
003     * This file is part of the Echo Point Project.  This project is a collection
004     * of Components that have extended the Echo Web Application Framework.
005     *
006     * Version: MPL 1.1/GPL 2.0/LGPL 2.1
007     *
008     * The contents of this file are subject to the Mozilla Public License Version
009     * 1.1 (the "License"); you may not use this file except in compliance with
010     * the License. You may obtain a copy of the License at
011     * http://www.mozilla.org/MPL/
012     *
013     * Software distributed under the License is distributed on an "AS IS" basis,
014     * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
015     * for the specific language governing rights and limitations under the
016     * License.
017     *
018     * Alternatively, the contents of this file may be used under the terms of
019     * either the GNU General Public License Version 2 or later (the "GPL"), or
020     * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
021     * in which case the provisions of the GPL or the LGPL are applicable instead
022     * of those above. If you wish to allow use of your version of this file only
023     * under the terms of either the GPL or the LGPL, and not to allow others to
024     * use your version of this file under the terms of the MPL, indicate your
025     * decision by deleting the provisions above and replace them with the notice
026     * and other provisions required by the GPL or the LGPL. If you do not delete
027     * the provisions above, a recipient may use your version of this file under
028     * the terms of any one of the MPL, the GPL or the LGPL.
029     */
030    import java.io.File;
031    import java.io.FileInputStream;
032    import java.io.IOException;
033    import java.io.InputStream;
034    
035    /**
036     * <code>FileTemplateDataSource</code> takes it template
037     * data from a File.
038     */
039    public class FileTemplateDataSource extends AbstractTemplateDataSource {
040    
041            private File file;
042            private String canonicalName; 
043    
044            /**
045             * Constructs a <code>FileTemplateDataSource</code> with no
046             * template file.
047             */
048            public FileTemplateDataSource() {
049                    this((File) null);
050            }
051            
052            /**
053             * Constructs a <code>FileTemplateDataSource</code> using the
054             * specified fileName as the template data source.
055             * 
056             * @param fileName - the file name to read
057             */
058            public FileTemplateDataSource(String fileName) {
059                    this(new File(fileName));
060            }
061            
062            /**
063             * Constructs a <code>FileTemplateDataSource</code> with the 
064             * specified File object as the template source
065             * 
066             * @param file - the File to use
067             */
068            public FileTemplateDataSource(File file) {
069                    this.file = file;
070                    try {
071                            canonicalName = "file:" + file.getCanonicalPath();
072                    } catch (Exception e) {
073                            System.out.println(e);
074                            /* should never happen */
075                    }
076                    
077                    SimpleTemplateCachingHints hints = (SimpleTemplateCachingHints) getCachingHints();
078                    hints.setLastModified(file.lastModified());
079            }
080            /**
081             * @see echopoint.template.TemplateDataSource#getCanonicalName()
082             */
083            public String getCanonicalName() {
084                    return canonicalName;
085            }
086    
087            /**
088             * @see echopoint.template.TemplateDataSource#getInputStream()
089             */
090            public InputStream getInputStream() throws IOException {
091                    return new FileInputStream(file);
092            }
093            
094            /**
095             * @return Returns the file.
096             */
097            public File getFile() {
098                    return file;
099            }
100            /**
101             * @param file The file to set.
102             */
103            public void setFile(File file) {
104                    this.file = file;
105            }
106    }