KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fulcrum > template > TurbineTemplateService


1 package org.apache.fulcrum.template;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache Turbine" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache Turbine", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import java.io.File JavaDoc;
58 import java.io.OutputStream JavaDoc;
59 import java.io.Writer JavaDoc;
60 import java.util.HashMap JavaDoc;
61
62 import org.apache.fulcrum.BaseService;
63 import org.apache.fulcrum.InitializationException;
64 import org.apache.fulcrum.ServiceException;
65
66 /**
67  * This service provides a method for mapping templates to their
68  * appropriate Screens or Navigations. It also allows templates to
69  * define a layout/navigations/screen modularization within the
70  * template structure. It also performs caching if turned on in the
71  * properties file.
72  *
73  * Since everything is keyed off the template variable,
74  * if data.getParameters().getString("template") returns
75  * /about_us/directions/driving.vm, the search for the
76  * Screen class is as follows (in order):
77  *
78  * 1. about_us.directions.Driving
79  * 2. about_us.directions.Default
80  * 3. about_us.Default
81  * 4. Default
82  * 5. VelocityScreen
83  *
84  * If the template variable does not exist, then VelocityScreen will be
85  * executed and templates/screens/index.vm will be executed.
86  * If index.vm is not found or if the template is invalid or Velocity
87  * execution throws an exception of any reason, then
88  * templates/screens/error.vm will be executed.
89  *
90  * For the Layouts and Navigations, the following paths will be
91  * searched in the layouts and navigations template
92  * subdirectories (in order):
93  *
94  * 1./about_us/directions/driving.vm
95  * 2./about_us/directions/default.vm
96  * 3./about_us/default.vm
97  * 4./default.vm
98  *
99  * @author <a HREF="mailto:jmcnally@collab.net">John D. McNally</a>
100  * @author <a HREF="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
101  * @author <a HREF="mailto:jvanzyl@apache.org">Jason van Zyl</a>
102  * @author <a HREF="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
103  * @author <a HREF="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
104  * @version $Id: TurbineTemplateService.java,v 1.1 2004/11/12 10:26:06 epugh Exp $
105  */

106 public class TurbineTemplateService
107     extends BaseService
108     implements TemplateService
109 {
110     /**
111      * The default file extension used as a registry key when a
112      * template's file extension cannot be determined.
113      */

114     protected static final String JavaDoc NO_FILE_EXT = "";
115
116     /**
117      * Default extension for templates.
118      */

119     private String JavaDoc defaultExtension;
120
121     /**
122      * The mappings of template file extensions to {@link
123      * org.apache.fulcrum.template.TemplateEngineService}
124      * implementations. Implementing template engines can locate
125      * templates within the capability of any resource loaders they
126      * may posses, and other template engines are stuck with file
127      * based template hierarchy only.
128      */

129     private HashMap JavaDoc templateEngineRegistry;
130
131     public TurbineTemplateService()
132     {
133     }
134
135     /**
136      * Called the first time the Service is used.
137      *
138      * @exception InitializationException.
139      */

140     public void init()
141         throws InitializationException
142     {
143         setInit(true);
144     }
145
146     /**
147      * Translates the supplied template paths into their Turbine-canonical
148      * equivalent (probably absolute paths).
149      *
150      * @param templatePaths An array of template paths.
151      * @return An array of translated template paths.
152      */

153     public String JavaDoc[] translateTemplatePaths(String JavaDoc[] templatePaths)
154     {
155         for (int i = 0; i < templatePaths.length; i++)
156         {
157             templatePaths[i] = getRealPath(templatePaths[i]);
158         }
159         return templatePaths;
160     }
161
162     /**
163      * Looks for the specified template file in each of the specified paths.
164      *
165      * @param template The template file to check for the existance of.
166      * @param templatePaths The paths to check for the template.
167      *
168      * @return true if a match is found in one of the supplied paths, or false.
169      */

170     public boolean templateExists(String JavaDoc template,
171                                   String JavaDoc[] templatePaths)
172     {
173         for (int i = 0; i < templatePaths.length; i++)
174         {
175             if (new File JavaDoc(templatePaths[i],template).exists())
176             {
177                 return true;
178             }
179         }
180         return false;
181     }
182
183     /**
184      * Determine if a template exists. Delegates to the appropriate {@link
185      * org.apache.fulcrum.template.TemplateEngineService} to check the
186      * existance of the specified template. If no template engine service is
187      * found for the template, false is returned.
188      *
189      * @param template The template file to check for the existence of.
190      *
191      * @return true if there is a template engine service registered for the
192      * given template, and it reports that the template exists,
193      * otherwise false.
194      */

195     public boolean templateExists(String JavaDoc template)
196     {
197         TemplateEngineService tes = getTemplateEngineService(template);
198
199         if (tes != null)
200         {
201             return tes.templateExists(template);
202         }
203         else
204         {
205             return false;
206         }
207     }
208
209     /**
210      * Registers the provided template engine for use by the
211      * <code>TemplateService</code>.
212      *
213      * @param service The <code>TemplateEngineService</code> to register.
214      */

215     public synchronized void registerTemplateEngineService(TemplateEngineService service)
216     {
217         // Clone the registry to write to non-sync'd
218
// Map implementations.
219
HashMap JavaDoc registry = templateEngineRegistry != null ?
220             (HashMap JavaDoc) templateEngineRegistry.clone() : new HashMap JavaDoc();
221
222         String JavaDoc[] exts = service.getAssociatedFileExtensions();
223
224         for (int i = 0; i < exts.length; i++)
225         {
226             registry.put(exts[i], service);
227         }
228         templateEngineRegistry = registry;
229     }
230
231     /**
232      * The {@link org.apache.fulcrum.template.TemplateEngineService}
233      * associated with the specified template's file extension.
234      *
235      * @param template The template name.
236      * @return The template engine service.
237      */

238     protected TemplateEngineService getTemplateEngineService(String JavaDoc template)
239     {
240         HashMap JavaDoc registry = templateEngineRegistry;
241         if (registry != null && template != null)
242         {
243             int dotIndex = template.lastIndexOf('.');
244             String JavaDoc ext = dotIndex == -1 ?
245                 defaultExtension : template.substring(dotIndex + 1);
246             return (TemplateEngineService) registry.get(ext);
247         }
248         else
249         {
250             return null;
251         }
252     }
253
254     public String JavaDoc handleRequest(TemplateContext context, String JavaDoc template)
255         throws ServiceException
256     {
257         TemplateEngineService tes = getTemplateEngineService(template);
258         return tes.handleRequest(context, template);
259     }
260
261     public void handleRequest(TemplateContext context, String JavaDoc template,
262                               OutputStream JavaDoc outputStream)
263         throws ServiceException
264     {
265         TemplateEngineService tes = getTemplateEngineService(template);
266         tes.handleRequest(context, template, outputStream);
267     }
268
269     public void handleRequest(TemplateContext context, String JavaDoc template,
270                               Writer JavaDoc writer)
271         throws ServiceException
272     {
273         TemplateEngineService tes = getTemplateEngineService(template);
274         tes.handleRequest(context, template, writer);
275     }
276
277     public TemplateContext getTemplateContext()
278     {
279         return new DefaultTemplateContext();
280     }
281 }
282
Popular Tags