001: package org.apache.turbine.util.template;
002:
003: /*
004: * Copyright 2001-2005 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License")
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import org.apache.turbine.services.template.TurbineTemplate;
023: import org.apache.turbine.util.RunData;
024: import org.apache.turbine.util.uri.URIConstants;
025:
026: /**
027: * This is a wrapper for Template specific information. It's part of
028: * the RunData object and can extract the information it needs to do
029: * the job directly from the data.getParameters().
030: *
031: * @author <a href="mailto:mbryson@mindspring.com">Dave Bryson</a>
032: * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
033: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
034: * @version $Id: TemplateInfo.java 278824 2005-09-05 20:01:15Z henning $
035: */
036: public class TemplateInfo {
037:
038: /* Constants for tempStorage hash map. */
039: public static final String NAVIGATION_TEMPLATE = "00navigation_template00";
040: public static final String LAYOUT_TEMPLATE = "00layout_template00";
041: public static final String SERVICE_NAME = "template_service";
042:
043: /* Handle to the RunData object. */
044: private RunData data = null;
045:
046: /* Place to store information about templates. */
047: private Map tempStorage = null;
048:
049: /**
050: * Constructor
051: *
052: * @param RunData A Turbine Rundata object.
053: */
054: public TemplateInfo(RunData data) {
055: this .data = data;
056: tempStorage = new HashMap(10);
057: }
058:
059: /**
060: * Get the value of navigationTemplate.
061: *
062: * @return A String with the value of navigationTemplate.
063: */
064: public String getNavigationTemplate() {
065: return getString(TemplateInfo.NAVIGATION_TEMPLATE);
066: }
067:
068: /**
069: * Set the value of navigationTemplate.
070: *
071: * @param v Value to assign to navigationTemplate.
072: */
073: public void setNavigationTemplate(String v) {
074: setTemp(TemplateInfo.NAVIGATION_TEMPLATE, v);
075: }
076:
077: /**
078: * Get the value of screen for the RunData parameters. This
079: * information comes from PathInfo or a QueryString.
080: *
081: * @return A String with the value of screen.
082: */
083: public String getScreenTemplate() {
084: return data.getParameters().getString(
085: URIConstants.CGI_TEMPLATE_PARAM, null);
086: }
087:
088: /**
089: * Set the value of screen. This is really just a method to hide
090: * using the RunData Parameter.
091: *
092: * @param v Value to assign to screen.
093: */
094: public void setScreenTemplate(String v) {
095: data.getParameters().setString(URIConstants.CGI_TEMPLATE_PARAM,
096: v);
097:
098: // We have changed the screen template so
099: // we should now update the layout template
100: // as well. We will use the template service
101: // to help us out.
102: try {
103: setLayoutTemplate(TurbineTemplate.getLayoutTemplateName(v));
104: } catch (Exception e) {
105: /*
106: * do nothing.
107: */
108: }
109: }
110:
111: /**
112: * Get the value of layout.
113: *
114: * @return A String with the value of layout.
115: */
116: public String getLayoutTemplate() {
117: String value = getString(TemplateInfo.LAYOUT_TEMPLATE);
118: return value;
119: }
120:
121: /**
122: * Set the value of layout.
123: *
124: * @param v Value to assign to layout.
125: */
126: public void setLayoutTemplate(String v) {
127: setTemp(TemplateInfo.LAYOUT_TEMPLATE, v);
128: }
129:
130: /**
131: * Get the value of Template context. This will be cast to the
132: * proper Context by its Service.
133: *
134: * @param name The name of the template context.
135: * @return An Object with the Value of context.
136: */
137: public Object getTemplateContext(String name) {
138: return getTemp(name);
139: }
140:
141: /**
142: * Set the value of context.
143: *
144: * @param name The name of the template context.
145: * @param v Value to assign to context.
146: */
147: public void setTemplateContext(String name, Object v) {
148: setTemp(name, v);
149: }
150:
151: /**
152: * Get the value of service.
153: *
154: * @return A String with the value of service.
155: */
156: public String getService() {
157: return getString(TemplateInfo.SERVICE_NAME);
158: }
159:
160: /**
161: * Set the value of service.
162: *
163: * @param v Value to assign to service.
164: */
165: public void setService(String v) {
166: setTemp(TemplateInfo.SERVICE_NAME, v);
167: }
168:
169: /**
170: * Get an object from temporary storage.
171: *
172: * @param name A String with the name of the object.
173: * @return An Object.
174: */
175: public Object getTemp(String name) {
176: return tempStorage.get(name);
177: }
178:
179: /**
180: * Get an object from temporary storage, or a default value.
181: *
182: * @param name A String with the name of the object.
183: * @param def An Object, the default value.
184: * @return An Object.
185: */
186: public Object getTemp(String name, Object def) {
187: try {
188: Object val = tempStorage.get(name);
189: return (val != null) ? val : def;
190: } catch (Exception e) {
191: return def;
192: }
193: }
194:
195: /**
196: * Put an object into temporary storage.
197: *
198: * @param name A String with the name of the object.
199: * @param value An Object, the value.
200: */
201: public void setTemp(String name, Object value) {
202: tempStorage.put(name, value);
203: }
204:
205: /**
206: * Return a String[] from the temp hash map.
207: *
208: * @param name A String with the name of the object.
209: * @return A String[].
210: */
211: public String[] getStringArray(String name) {
212: String[] value = null;
213: Object object = getTemp(name, null);
214: if (object != null) {
215: value = (String[]) object;
216: }
217: return value;
218: }
219:
220: /**
221: * Return a String from the temp hash map.
222: *
223: * @param name A String with the name of the object.
224: * @return A String.
225: */
226: public String getString(String name) {
227: String value = null;
228: Object object = getTemp(name, null);
229: if (object != null) {
230: value = (String) object;
231: }
232: return value;
233: }
234:
235: /**
236: * Remove an object from the temporary storage.
237: *
238: * @param name A String with the name of the object.
239: * @return The object that was removed or <code>null</code>
240: * if the name was not a key.
241: */
242: public Object removeTemp(String name) {
243: return tempStorage.remove(name);
244: }
245:
246: /*
247: * Returns all the available names in the temporary storage.
248: *
249: * @return A object array with the keys.
250: */
251: public Object[] getTempKeys() {
252: return tempStorage.keySet().toArray();
253: }
254: }
|