FileTemplateRetrieverService.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or 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  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  Creation P.Delrieu
00026  * 
00027  */
00028 package org.openmobileis.embedded.webserver.templates;
00029 
00030 import java.io.File;
00031 
00032 import org.openmobileis.common.intl.IntlResourceManager;
00033 import org.openmobileis.common.util.PropertiesManager;
00034 
00035 import freemarker.template.FileTemplateCache;
00036 import freemarker.template.Template;
00037 
00042 public final class FileTemplateRetrieverService implements TemplateRetrieverService {
00043   private String rootTemplatePath;
00044   //use to retreive template from file system
00045   private FileTemplateCache fileTemplateCache = null;
00046 
00050         public FileTemplateRetrieverService() {
00051         rootTemplatePath = PropertiesManager.getManager().getProperty("org.openmobileis.services.templatesDir");
00052         fileTemplateCache = new FileTemplateCache(rootTemplatePath);
00053         fileTemplateCache.setFilenameSuffix("htm");
00054         }
00055 
00056         /* (non-Javadoc)
00057          * @see org.openmobileis.embedded.webserver.templates.TemplateRetrieverService#getModuleName()
00058          */
00059         public String getModuleName() {
00060                 return null;
00061         }
00062 
00063         /* (non-Javadoc)
00064          * @see org.openmobileis.embedded.webserver.templates.TemplateRetrieverService#getServiceTemplate(java.lang.String)
00065          */
00066         public Template getServiceTemplate(String relatifPath) {
00067     //use template cache to allow template include.
00068     IntlResourceManager resourceManager = IntlResourceManager.getManager();
00069     String newrelatifPath = resourceManager.getLocalizedFileName(relatifPath, false);
00070             //test if the file exist. If not use not localized version
00071             File file = new File(rootTemplatePath+newrelatifPath);
00072             if (!file.exists()) newrelatifPath = relatifPath;
00073     
00074    // LogManager.traceDebug(0, "TemaplateManager gettemplate newrelatifPath "+newrelatifPath);
00075    // LogManager.traceDebug(0, "TemaplateManager gettemplate relatifPath "+relatifPath);
00076    // LogManager.traceDebug(0, "TemaplateManager gettemplate rootTemplatePath "+rootTemplatePath);
00077     
00078     return (Template)fileTemplateCache.getItem(newrelatifPath);
00079         }
00080 
00081         /* (non-Javadoc)
00082          * @see org.openmobileis.embedded.webserver.templates.TemplateRetrieverService#isTemplateExist(java.lang.String)
00083          */
00084         public boolean isTemplateExist(String relatifPath) {
00085     IntlResourceManager resourceManager = IntlResourceManager.getManager();
00086     String newrelatifPath = resourceManager.getLocalizedFileName(relatifPath, false);
00087           //test if the file exist. If not use not localized version
00088           File file = new File(rootTemplatePath+File.separator+newrelatifPath);
00089           if (!file.exists()) newrelatifPath = relatifPath;
00090           file = new File(rootTemplatePath+File.separator+newrelatifPath);
00091           return file.exists();
00092         }
00093 
00094 }

Generated on Tue May 22 23:01:09 2007 for OpenMobileIS by  doxygen 1.5.1-p1