1 package org.apache.turbine.util.template; 2 3 4 /* 5 * Licensed to the Apache Software Foundation (ASF) under one 6 * or more contributor license agreements. See the NOTICE file 7 * distributed with this work for additional information 8 * regarding copyright ownership. The ASF licenses this file 9 * to you under the Apache License, Version 2.0 (the 10 * "License"); you may not use this file except in compliance 11 * with the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, 16 * software distributed under the License is distributed on an 17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 18 * KIND, either express or implied. See the License for the 19 * specific language governing permissions and limitations 20 * under the License. 21 */ 22 23 24 import java.util.HashMap; 25 import java.util.Map; 26 27 import org.apache.turbine.services.TurbineServices; 28 import org.apache.turbine.services.template.TemplateService; 29 import org.apache.turbine.util.RunData; 30 import org.apache.turbine.util.uri.URIConstants; 31 32 33 /** 34 * This is a wrapper for Template specific information. It's part of 35 * the RunData object and can extract the information it needs to do 36 * the job directly from the data.getParameters(). 37 * 38 * @author <a href="mailto:mbryson@mindspring.com">Dave Bryson</a> 39 * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a> 40 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> 41 * @version $Id$ 42 */ 43 public class TemplateInfo 44 { 45 46 /** Constants for tempStorage hash map. */ 47 public static final String NAVIGATION_TEMPLATE = "00navigation_template00"; 48 /** Constants for tempStorage hash map. */ 49 public static final String LAYOUT_TEMPLATE = "00layout_template00"; 50 /** Constants for tempStorage hash map. */ 51 public static final String SERVICE_NAME = "template_service"; 52 53 /* Handle to the RunData object. */ 54 private RunData data = null; 55 56 /* Place to store information about templates. */ 57 private Map<String, Object> tempStorage = null; 58 59 /** 60 * Constructor 61 * 62 * @param data A Turbine RunData object. 63 */ 64 public TemplateInfo(RunData data) 65 { 66 this.data = data; 67 tempStorage = new HashMap<>(10); 68 } 69 70 /** 71 * Get the value of navigationTemplate. 72 * 73 * @return A String with the value of navigationTemplate. 74 */ 75 public String getNavigationTemplate() 76 { 77 return getString(TemplateInfo.NAVIGATION_TEMPLATE); 78 } 79 80 /** 81 * Set the value of navigationTemplate. 82 * 83 * @param v Value to assign to navigationTemplate. 84 */ 85 public void setNavigationTemplate(String v) 86 { 87 setTemp(TemplateInfo.NAVIGATION_TEMPLATE, v); 88 } 89 90 /** 91 * Get the value of screen for the RunData parameters. This 92 * information comes from PathInfo or a QueryString. 93 * 94 * @return A String with the value of screen. 95 */ 96 public String getScreenTemplate() 97 { 98 return data.getParameters().getString(URIConstants.CGI_TEMPLATE_PARAM, null); 99 } 100 101 /** 102 * Set the value of screen. This is really just a method to hide 103 * using the RunData Parameter. 104 * 105 * @param v Value to assign to screen. 106 */ 107 public void setScreenTemplate(String v) 108 { 109 data.getParameters().setString(URIConstants.CGI_TEMPLATE_PARAM, v); 110 111 // We have changed the screen template so 112 // we should now update the layout template 113 // as well. We will use the template service 114 // to help us out. 115 try 116 { 117 TemplateService templateService = (TemplateService)TurbineServices.getInstance().getService(TemplateService.SERVICE_NAME); 118 setLayoutTemplate(templateService.getLayoutTemplateName(v)); 119 } 120 catch (Exception e) 121 { 122 /* 123 * do nothing. 124 */ 125 } 126 } 127 128 /** 129 * Get the value of layout. 130 * 131 * @return A String with the value of layout. 132 */ 133 public String getLayoutTemplate() 134 { 135 String value = getString(TemplateInfo.LAYOUT_TEMPLATE); 136 return value; 137 } 138 139 /** 140 * Set the value of layout. 141 * 142 * @param v Value to assign to layout. 143 */ 144 public void setLayoutTemplate(String v) 145 { 146 setTemp(TemplateInfo.LAYOUT_TEMPLATE, v); 147 } 148 149 /** 150 * Get the value of Template context. This will be cast to the 151 * proper Context by its Service. 152 * 153 * @param name The name of the template context. 154 * @return An Object with the Value of context. 155 */ 156 public Object getTemplateContext(String name) 157 { 158 return getTemp(name); 159 } 160 161 /** 162 * Set the value of context. 163 * 164 * @param name The name of the template context. 165 * @param v Value to assign to context. 166 */ 167 public void setTemplateContext(String name, Object v) 168 { 169 setTemp(name, v); 170 } 171 172 /** 173 * Get the value of service. 174 * 175 * @return A String with the value of service. 176 */ 177 public String getService() 178 { 179 return getString(TemplateInfo.SERVICE_NAME); 180 } 181 182 /** 183 * Set the value of service. 184 * 185 * @param v Value to assign to service. 186 */ 187 public void setService(String v) 188 { 189 setTemp(TemplateInfo.SERVICE_NAME, v); 190 } 191 192 /** 193 * Get an object from temporary storage. 194 * 195 * @param name A String with the name of the object. 196 * @return An Object. 197 */ 198 public Object getTemp(String name) 199 { 200 return tempStorage.get(name); 201 } 202 203 /** 204 * Get an object from temporary storage, or a default value. 205 * 206 * @param name A String with the name of the object. 207 * @param def An Object, the default value. 208 * @return An Object. 209 */ 210 public Object getTemp(String name, Object def) 211 { 212 try 213 { 214 Object val = tempStorage.get(name); 215 return (val != null) ? val : def; 216 } 217 catch (Exception e) 218 { 219 return def; 220 } 221 } 222 223 /** 224 * Put an object into temporary storage. 225 * 226 * @param name A String with the name of the object. 227 * @param value An Object, the value. 228 */ 229 public void setTemp(String name, Object value) 230 { 231 tempStorage.put(name, value); 232 } 233 234 /** 235 * Return a String[] from the temp hash map. 236 * 237 * @param name A String with the name of the object. 238 * @return A String[]. 239 */ 240 public String[] getStringArray(String name) 241 { 242 String[] value = null; 243 Object object = getTemp(name, null); 244 if (object != null) 245 { 246 value = (String[]) object; 247 } 248 return value; 249 } 250 251 /** 252 * Return a String from the temp hash map. 253 * 254 * @param name A String with the name of the object. 255 * @return A String. 256 */ 257 public String getString(String name) 258 { 259 String value = null; 260 Object object = getTemp(name, null); 261 if (object != null) 262 { 263 value = (String) object; 264 } 265 return value; 266 } 267 268 /** 269 * Remove an object from the temporary storage. 270 * 271 * @param name A String with the name of the object. 272 * @return The object that was removed or <code>null</code> 273 * if the name was not a key. 274 */ 275 public Object removeTemp(String name) 276 { 277 return tempStorage.remove(name); 278 } 279 280 /** 281 * Returns all the available names in the temporary storage. 282 * 283 * @return A object array with the keys. 284 */ 285 public Object[] getTempKeys() 286 { 287 return tempStorage.keySet().toArray(); 288 } 289 }