001    package 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    
024    import org.apache.turbine.services.TurbineServices;
025    
026    import org.apache.turbine.util.RunData;
027    
028    /**
029     * This is a simple static accessor to common TemplateService tasks such as
030     * getting a Screen that is associated with a screen template.
031     *
032     * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
033     * @version $Id: TurbineTemplate.java 615328 2008-01-25 20:25:05Z tv $
034     */
035    public abstract class TurbineTemplate
036    {
037        /**
038         * Utility method for accessing the service
039         * implementation
040         *
041         * @return a TemplateService implementation instance
042         */
043        public static TemplateService getService()
044        {
045            return (TemplateService) TurbineServices
046                .getInstance().getService(TemplateService.SERVICE_NAME);
047        }
048    
049        /**
050         * Returns true if the Template Service has caching activated
051         *
052         * @return true if Caching is active.
053         */
054        public static final boolean isCaching()
055        {
056            return getService().isCaching();
057        }
058    
059        /**
060         * Get the default extension given in the properties file.
061         *
062         * @return A String with the extension.
063         */
064        public static final String getDefaultExtension()
065        {
066            return getService().getDefaultExtension();
067        }
068    
069        /**
070         * Return Extension for a supplied template
071         *
072         * @param template The template name
073         *
074         * @return extension The extension for the supplied template
075         */
076        public static final String getExtension(String template)
077        {
078            return getService().getExtension(template);
079        }
080    
081        /**
082         * Returns the Default Template Name with the Default Extension.
083         * If the extension is unset, return only the template name
084         *
085         * @return The default template Name
086         */
087        public static final String getDefaultTemplate()
088        {
089            return getService().getDefaultTemplate();
090        }
091    
092        /**
093         * Get the default page module name of the template engine
094         * service corresponding to the default template name extension.
095         *
096         * @return The default page module name.
097         */
098        public static final String getDefaultPage()
099        {
100            return getService().getDefaultPage();
101        }
102    
103        /**
104         * Get the Screen template given in the properties file.
105         *
106         * @return A String which is the value of the TemplateService
107         * default.screen property.
108         */
109        public static final String getDefaultScreen()
110        {
111            return getService().getDefaultScreen();
112        }
113    
114        /**
115         * Get the default layout module name of the template engine
116         * service corresponding to the default template name extension.
117         *
118         * @return The default layout module name.
119         */
120        public static final String getDefaultLayout()
121        {
122            return getService().getDefaultLayout();
123        }
124    
125        /**
126         * Get the default Navigation given in the properties file.
127         *
128         * @return A String which is the value of the TemplateService
129         * default.navigation property.
130         */
131        public static final String getDefaultNavigation()
132        {
133            return getService().getDefaultNavigation();
134        }
135    
136        /**
137         * Get the default layout template given in the properties file.
138         *
139         * @return A String which is the value of the TemplateService
140         * default.layout.template property.
141         */
142        public static final String getDefaultLayoutTemplate()
143        {
144            return getService().getDefaultLayoutTemplate();
145        }
146    
147        /**
148         * Get the default page module name of the template engine
149         * service corresponding to the template name extension of
150         * the named template.
151         *
152         * @param template The template name.
153         * @return The default page module name.
154         */
155        public static final String getDefaultPageName(String template)
156        {
157            return getService().getDefaultPageName(template);
158        }
159    
160        /**
161         * Get the default screen module name of the template engine
162         * service corresponding to the template name extension of
163         * the named template.
164         *
165         * @param template The template name.
166         * @return The default screen module name.
167         */
168        public static final String getDefaultScreenName(String template)
169        {
170            return getService().getDefaultScreenName(template);
171        }
172    
173        /**
174         * Get the default layout module name of the template engine
175         * service corresponding to the template name extension of
176         * the named template.
177         *
178         * @param template The template name.
179         * @return The default layout module name.
180         */
181        public static final String getDefaultLayoutName(String template)
182        {
183            return getService().getDefaultLayoutName(template);
184        }
185    
186        /**
187         * Get the default navigation module name of the template engine
188         * service corresponding to the template name extension of
189         * the named template.
190         *
191         * @param template The template name.
192         * @return The default navigation module name.
193         */
194        public static final String getDefaultNavigationName(String template)
195        {
196            return getService().getDefaultNavigationName(template);
197        }
198    
199        /**
200         * Get the default layout template name of the template engine
201         * service corresponding to the template name extension of
202         * the named template.
203         *
204         * @param template The template name.
205         * @return The default layout template name.
206         */
207        public static final String getDefaultLayoutTemplateName(String template)
208        {
209            return getService().getDefaultLayoutTemplateName(template);
210        }
211    
212        /**
213         * Find the default page module name for the given request.
214         *
215         * @param data The encapsulation of the request to retrieve the
216         *             default page for.
217         * @return The default page module name.
218         */
219        public static final String getDefaultPageName(RunData data)
220        {
221            return getService().getDefaultPageName(data);
222        }
223    
224        /**
225         * Find the default layout module name for the given request.
226         *
227         * @param data The encapsulation of the request to retrieve the
228         *             default layout for.
229         * @return The default layout module name.
230         */
231        public static final String getDefaultLayoutName(RunData data)
232        {
233            return getService().getDefaultLayoutName(data);
234        }
235    
236        /**
237         * Locate and return the name of a Screen module.
238         *
239         * @param name A String with the name of the template.
240         * @return A String with the name of the screen.
241         * @exception Exception, a generic exception.
242         */
243        public static final String getScreenName(String name)
244            throws Exception
245        {
246            return getService().getScreenName(name);
247        }
248    
249        /**
250         * Locate and return the name of the layout module to be used
251         * with the named layout template.
252         *
253         * @param template The layout template name.
254         * @return The found layout module name.
255         * @exception Exception, a generic exception.
256         */
257        public static final String getLayoutName(String template)
258            throws Exception
259        {
260            return getService().getLayoutName(template);
261        }
262    
263        /**
264         * Locate and return the name of the navigation module to be used
265         * with the named navigation template.
266         *
267         * @param template The navigation template name.
268         * @return The found navigation module name.
269         * @exception Exception, a generic exception.
270         */
271        public static final String getNavigationName(String template)
272            throws Exception
273        {
274            return getService().getNavigationName(template);
275        }
276    
277        /**
278         * Locate and return the name of a screen template.
279         *
280         * @param key A String which is the key to the template.
281         * @return A String with the screen template path.
282         * @exception Exception, a generic exception.
283         */
284        public static final String getScreenTemplateName(String key)
285            throws Exception
286        {
287            return getService().getScreenTemplateName(key);
288        }
289    
290        /**
291         * Locate and return the name of a layout template.
292         *
293         * @param name A String with the name of the template.
294         * @return A String with the layout template path.
295         * @exception Exception, a generic exception.
296         */
297        public static final String getLayoutTemplateName(String name)
298            throws Exception
299        {
300            return getService().getLayoutTemplateName(name);
301        }
302    
303        /**
304         * Locate and return the name of a navigation template.
305         *
306         * @param key A String which is the key to the template.
307         * @return A String with the navigation template path.
308         * @exception Exception, a generic exception.
309         */
310        public static final String getNavigationTemplateName(String key)
311            throws Exception
312        {
313            return getService().getNavigationTemplateName(key);
314        }
315    
316        /**
317         * Translates the supplied template paths into their Turbine-canonical
318         * equivalent (probably absolute paths).
319         *
320         * @param templatePaths An array of template paths.
321         * @return An array of translated template paths.
322         * @deprecated Each template engine service should know how to translate
323         *             a request onto a file.
324         */
325        public static final String[] translateTemplatePaths(String[] templatePaths)
326        {
327            return getService().translateTemplatePaths(templatePaths);
328        }
329    
330        /**
331         * Delegates to the appropriate {@link
332         * org.apache.turbine.services.template.TemplateEngineService} to
333         * check the existance of the specified template.
334         *
335         * @param template The template to check for the existance of.
336         * @param templatePaths The paths to check for the template.
337         * @deprecated Use templateExists from the various Templating Engines
338         */
339        public static final boolean templateExists(String template, String[] templatePaths)
340        {
341            return getService().templateExists(template, templatePaths);
342        }
343    
344        /**
345         * Registers the provided template engine for use by the
346         * <code>TemplateService</code>.
347         *
348         * @param service The <code>TemplateEngineService</code> to register.
349         */
350        public static final void registerTemplateEngineService(TemplateEngineService service)
351        {
352            getService().registerTemplateEngineService(service);
353        }
354    
355        /**
356         * The {@link org.apache.turbine.services.template.TemplateEngineService}
357         * associated with the specified template's file extension.
358         *
359         * @param template The template name.
360         * @return The template engine service.
361         */
362        public static final TemplateEngineService getTemplateEngineService(String template)
363        {
364            return getService().getTemplateEngineService(template);
365        }
366    }