001package org.apache.turbine.services.template; 002 003 004/* 005 * Licensed to the Apache Software Foundation (ASF) under one 006 * or more contributor license agreements. See the NOTICE file 007 * distributed with this work for additional information 008 * regarding copyright ownership. The ASF licenses this file 009 * to you under the Apache License, Version 2.0 (the 010 * "License"); you may not use this file except in compliance 011 * with the License. You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, 016 * software distributed under the License is distributed on an 017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 018 * KIND, either express or implied. See the License for the 019 * specific language governing permissions and limitations 020 * under the License. 021 */ 022 023 024import java.util.Hashtable; 025 026import org.apache.commons.configuration2.Configuration; 027import org.apache.turbine.services.TurbineBaseService; 028import org.apache.turbine.services.TurbineServices; 029 030/** 031 * The base implementation of Turbine {@link 032 * org.apache.turbine.services.template.TemplateEngineService}. 033 * 034 * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a> 035 * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a> 036 * @version $Id$ 037 */ 038public abstract class BaseTemplateEngineService 039 extends TurbineBaseService 040 implements TemplateEngineService 041{ 042 /** 043 * A Map containing the configuration for the template 044 * engine service. The configuration contains: 045 * 046 * 1) template extensions 047 * 2) default page 048 * 3) default screen 049 * 4) default layout 050 * 5) default navigation 051 * 6) default error screen 052 */ 053 private final Hashtable<String, Object> configuration = new Hashtable<>(); 054 055 /** 056 * @see org.apache.turbine.services.template.TemplateEngineService#registerConfiguration 057 */ 058 @Override 059 public void registerConfiguration(String defaultExt) 060 { 061 initConfiguration(defaultExt); 062 TemplateService templateService = (TemplateService)TurbineServices.getInstance().getService(TemplateService.SERVICE_NAME); 063 templateService.registerTemplateEngineService(this); 064 } 065 066 /** 067 * @see org.apache.turbine.services.template.TemplateEngineService#getTemplateEngineServiceConfiguration 068 */ 069 @Override 070 public Hashtable<String, Object> getTemplateEngineServiceConfiguration() 071 { 072 return configuration; 073 } 074 075 /** 076 * @see org.apache.turbine.services.template.TemplateEngineService#getAssociatedFileExtensions 077 */ 078 @Override 079 public String[] getAssociatedFileExtensions() 080 { 081 return (String[]) configuration.get(TEMPLATE_EXTENSIONS); 082 } 083 084 /** 085 * Initialize the Template Engine Service. 086 * 087 * Note engine file extension associations. First attempts to 088 * pull a list of custom extensions from the property file value 089 * keyed by <code>template.extension</code>. If none are defined, 090 * uses the value keyed by 091 * <code>template.default.extension</code>, defaulting to the 092 * emergency value supplied by <code>defaultExt</code>. 093 * 094 * @param defaultExt The default used when the default defined in the 095 * properties file is missing or misconfigured. 096 */ 097 protected void initConfiguration(String defaultExt) 098 { 099 Configuration config = getConfiguration(); 100 101 // 102 // Should modify the configuration class to take defaults 103 // here, should have to do this. 104 // 105 String[] fileExtensionAssociations = 106 config.getStringArray(TEMPLATE_EXTENSIONS); 107 108 if (fileExtensionAssociations == null || 109 fileExtensionAssociations.length == 0) 110 { 111 fileExtensionAssociations = new String[1]; 112 fileExtensionAssociations[0] = config.getString( 113 DEFAULT_TEMPLATE_EXTENSION, defaultExt); 114 } 115 116 configuration.put(TEMPLATE_EXTENSIONS, fileExtensionAssociations); 117 118 /* 119 * We need some better error checking here and should probably 120 * throw an exception here if these things aren't set 121 * up correctly. 122 */ 123 124 String[] copyParams = { 125 DEFAULT_PAGE, 126 DEFAULT_SCREEN, 127 DEFAULT_LAYOUT, 128 DEFAULT_NAVIGATION, 129 DEFAULT_ERROR_SCREEN, 130 DEFAULT_LAYOUT_TEMPLATE, 131 DEFAULT_SCREEN_TEMPLATE 132 }; 133 134 for (String copyParam : copyParams) 135 { 136 configuration.put(copyParam, config.getString(copyParam, "")); 137 } 138 } 139 140 /** 141 * @see org.apache.turbine.services.template.TemplateEngineService#templateExists 142 */ 143 @Override 144 public abstract boolean templateExists(String template); 145}