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