Freemarkerv1JarCacheRetriever.java

00001 /*
00002  * FreeMarker: a tool that allows Java programs to generate HTML
00003  * output using templates.
00004  * Copyright (C) 1998-2004 Benjamin Geer
00005  * Email: beroul@users.sourceforge.net
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Library General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Library General Public
00018  * License along with this library; if not, write to the
00019  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020  * Boston, MA  02111-1307, USA.
00021  */
00022 
00023 package org.openmobileis.embedded.webserver.templates;
00024 
00025 import java.io.File;
00026 import java.io.IOException;
00027 import java.io.InputStream;
00028 import java.util.LinkedList;
00029 import java.util.List;
00030 
00031 import freemarker.template.Compileable;
00032 import freemarker.template.TemplateException;
00033 import freemarker.template.TextEncoding;
00034 import freemarker.template.cache.CacheRetriever;
00035 import freemarker.template.cache.Cacheable;
00036 import freemarker.template.cache.RegistryAccepter;
00037 import freemarker.template.cache.TemplateRegistry;
00038 
00045 public class Freemarkerv1JarCacheRetriever implements CacheRetriever,
00046         TextEncoding, RegistryAccepter {
00047 
00049     protected String directoryRoot;
00051     protected String filenameSuffix;
00053     protected String encoding;
00055     protected TemplateRegistry registry;
00056     
00057         private ClassLoader loader;
00058 
00059 
00061     public Freemarkerv1JarCacheRetriever(ClassLoader loader) {
00062         super();
00063                 this.loader = loader;
00064    }
00065 
00074     public Freemarkerv1JarCacheRetriever(String path, ClassLoader loader) {
00075         this(loader);
00076       setConnection(path);
00077     }
00078 
00086     public boolean connectionOk() throws TemplateException {
00087         if( directoryRoot == null ) {
00088             throw new TemplateException( "Root directory is not defined" );
00089         }
00090         return true;
00091     }
00092 
00100     public void setConnection(String path) {
00101         if(path == null) {
00102             throw new IllegalArgumentException("Root cache path cannot be null");
00103         }
00104         directoryRoot = path;
00105     }
00106 
00111     public String getConnection() {
00112         if( directoryRoot == null ) {
00113             return null;
00114         }
00115         return directoryRoot.toString();
00116     }
00117 
00124     public void setPath(File dir) {
00125         if(dir == null) {
00126             throw new IllegalArgumentException("Root cache directory cannot be null");
00127         }
00128         this.directoryRoot = dir.getPath();
00129     }
00130 
00136     public File getPath() {
00137         return new File(directoryRoot);
00138     }
00139 
00147     public void setFilenameSuffix(String filenameSuffix) {
00148         this.filenameSuffix = filenameSuffix;
00149     }
00150 
00158     public String getFilenameSuffix() {
00159         return filenameSuffix;
00160     }
00161 
00172     public boolean exists(String location) {
00173 
00174         try {
00175                 InputStream in = this.getClass().getClassLoader().getResourceAsStream(nameToFile(location));
00176           return(in != null);
00177         } catch( Throwable e ) {
00178             return false;
00179         }
00180     }
00181 
00188     public List getPreloadData() throws TemplateException {
00189         List visitedFiles = new LinkedList();
00190 
00191         try {
00192             readDirectory(directoryRoot, "", visitedFiles);
00193         } catch (IOException e) {
00194             throw new TemplateException( "Could not get preload data", e );
00195         }
00196         return visitedFiles;
00197     }
00198 
00208     protected void readDirectory(String dir, String relativeDirPath,
00209             List visitedFiles) throws IOException {
00210     }
00211 
00222     public long lastModified(String location) throws TemplateException {
00223          return 0;
00224     }
00225 
00233     protected boolean isSuffixValid( String name ) throws TemplateException {
00234         if (!(filenameSuffix == null || name.endsWith(filenameSuffix))) {
00235             throw new TemplateException("The requested name, \"" + name +
00236                     "\", does not have the filename suffix \"" +
00237                     filenameSuffix + '"');
00238         }
00239         return true;
00240     }
00241 
00247     protected String nameToFile(final String name) throws TemplateException {
00248         return directoryRoot+name;
00249     }
00250 
00259     public Cacheable loadData(String location, String type)
00260             throws TemplateException {
00261         String    file = nameToFile(location);
00262 
00263         try {
00264           InputStream inputStream = this.loader.getResourceAsStream(file);
00265           Compileable template = (Compileable)registry.getTemplate( type );
00266 
00267             if (encoding == null) {
00268                 template.compileFromStream(inputStream);
00269             } else {
00270                 template.compileFromStream(inputStream, encoding);
00271             }
00272             inputStream.close();
00273             return (Cacheable)template;
00274         } catch(java.io.IOException e) {
00275             throw new TemplateException( "Could not load data", e );
00276         } catch(NullPointerException e) {
00277             throw new TemplateException( "Could not load data", e );
00278         }
00279     }
00280 
00287     public void setEncoding(String encoding) {
00288         this.encoding = encoding;
00289     }
00290 
00297     public String getEncoding() {
00298         return encoding;
00299     }
00300 
00305      public void setTemplateRegistry( TemplateRegistry cRegistry ) {
00306          registry = cRegistry;
00307      }
00308 
00312      public TemplateRegistry getTemplateRegistry() {
00313          return registry;
00314      }
00315 
00323     public boolean equals( Object o ) {
00324         if( this == o )
00325             return true;
00326 
00327         if( !( o instanceof Freemarkerv1JarCacheRetriever ))
00328             return false;
00329 
00330         final Freemarkerv1JarCacheRetriever fileRetriever = ( Freemarkerv1JarCacheRetriever ) o;
00331 
00332         if( directoryRoot == null ?
00333                 fileRetriever.directoryRoot != null :
00334                 !directoryRoot.equals( fileRetriever.directoryRoot ))
00335             return false;
00336         if( encoding == null ?
00337                 fileRetriever.encoding != null :
00338                 !encoding.equals( fileRetriever.encoding ))
00339             return false;
00340         if( filenameSuffix == null ?
00341                 fileRetriever.filenameSuffix != null :
00342                 !filenameSuffix.equals( fileRetriever.filenameSuffix ))
00343             return false;
00344         if( registry == null ?
00345                 fileRetriever.registry != null :
00346                 !registry.equals( fileRetriever.registry ))
00347             return false;
00348 
00349         return true;
00350     }
00351 
00356     public int hashCode() {
00357         int result = 13;
00358         result = 29 * result + ( directoryRoot != null ? directoryRoot.hashCode() : 0 );
00359         result = 29 * result + ( filenameSuffix != null ? filenameSuffix.hashCode() : 0 );
00360         result = 29 * result + ( encoding != null ? encoding.hashCode() : 0 );
00361         result = 29 * result + ( registry != null ? registry.hashCode() : 0 );
00362         return result;
00363     }
00364 
00370     public String toString() {
00371         StringBuffer buffer = new StringBuffer();
00372 
00373         if( directoryRoot != null ) {
00374             buffer.append( "Root path: " );
00375             buffer.append( directoryRoot );
00376         }
00377         if( filenameSuffix != null ) {
00378             buffer.append( ", filename suffix: " );
00379             buffer.append( filenameSuffix );
00380         }
00381         if( encoding != null ) {
00382             buffer.append( ", encoding: " );
00383             buffer.append( encoding );
00384         }
00385         if( registry != null ) {
00386             buffer.append( ", registry: " );
00387             buffer.append( registry );
00388         }
00389         return buffer.toString();
00390     }
00391 }

Generated on Mon Jan 11 21:19:14 2010 for OpenMobileIS by  doxygen 1.5.4