KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > pipeline > DefaultResolver


1 package org.apache.turbine.pipeline;
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.util.ArrayList JavaDoc;
58 import java.util.List JavaDoc;
59 import java.util.Iterator JavaDoc;
60 import org.apache.turbine.Turbine;
61 import org.apache.turbine.TurbineConstants;
62 import org.apache.turbine.Resolver;
63 import org.apache.turbine.modules.Module;
64 import org.apache.commons.logging.Log;
65 import org.apache.commons.logging.LogFactory;
66
67 // Renderers and context builders
68
// We need to match up the target template with sibling
69
// templates and context builders for each of these templates.
70

71 // Given a target template
72

73 // 1. find all possible classes that could be used
74
// as context builders
75
// 2. find all possible sibling templates that could
76
// be used with the target template
77

78 // We are using the classic turbine method of using
79
// a screen template.
80

81 // Say we have a base template of:
82
//
83
// /base/science/chemistry/Titanium.vm
84
//
85
// 1. We want to find the context builder to this
86
// target template if there is one, there won't
87
// be if we're using pull.
88
//
89
// 2. Find the layout/nav templates to use and find
90
// their context builders.
91

92 /**
93  * Resolver that finds the appropriate context populator
94  * and sibling templates for a given target template.
95  *
96  * This is the 2.1 algorithm used to find a context populator
97  * (or module in 2.1 parlance), sibling templates and their
98  * context populators.
99  *
100  * This algorithm or strategy should be pluggable. For example
101  * using a different resolver would allow search for templates
102  * to occur in a localized manner possibly even for content
103  * type. Right now there is some code in Jetspeed called the
104  * profiler that David Taylor is working on and it would probably
105  * be good to turn that into a resolver so that the functionality
106  * is available in Turbine. There is also the Resources code in
107  * the commons which we can also leverage and that code can now
108  * deal with localization issues it appears. Would be good to
109  * take a look at all this code and pull the best out so that
110  * it can be placed in Turbine proper. The first task is to
111  * get a simple resolver working for the 'classic' pipeline.
112  * More flexibility in terms of configuration with XML can
113  * come in a while.
114  */

115
116 /*
117    NOTE: Currently the ModuleLoader handles a lot of the responsiblity
118    for finding, caching, and serving up modules. That functionaly
119    should be placed in the pluggable resolver. I'll leave it for now so
120    to make the changes more gradually.
121
122    subclasses can override findTemplate and findModule to
123    use a different algorithm, while using the caching mechanism
124    of the DefaultResolver
125
126    subclasses should override getTemplate and getModule if they
127    wish to use a different caching mechanism
128
129 */

130
131 /**
132  * This class is used to find templates and modules
133  *
134  * @author <a HREF="mailto:jvanzyl@apache.org">Jason van Zyl</a>
135  * @author <a HREF="mailto:mikeh@apache.org">Mike Haberman</a>
136  * @version $Id: DefaultResolver.java,v 1.16 2002/04/09 17:29:34 jtaylor Exp $
137  */

138 public class DefaultResolver
139     implements Resolver, TurbineConstants
140 {
141     private static final Log log = LogFactory.getLog(DefaultResolver.class);
142     
143     protected String JavaDoc defaultTemplate;
144
145     public void init()
146         throws Exception JavaDoc
147     {
148         defaultTemplate =
149             Turbine.getConfiguration().getString("template.default") +
150             "." +
151             Turbine.getConfiguration().getString("template.default.extension");
152
153         // insist that the default template starts with a '/'
154
int idx = defaultTemplate.indexOf('/');
155         if (idx == -1 || idx > 0)
156         {
157             defaultTemplate = "/" + defaultTemplate;
158         }
159
160         log.debug("DefaultResolver: default template " + defaultTemplate);
161     }
162
163     /**
164      * Get the qualified template name
165      * Used for resolving top level rendering process.
166      * For turbine classic edition,
167      * moduleType is one of (layouts, screens, navigations)
168      */

169     public String JavaDoc getTemplate(String JavaDoc moduleType, String JavaDoc targetTemplate)
170         throws Exception JavaDoc
171     {
172         return findTemplate(moduleType, targetTemplate);
173     }
174
175     /**
176      * Get an instance of a module
177      *
178      * For turbine classic edition, type is one of (actions, screens)
179      * @param type
180      * @param name
181      * @return
182      */

183     public Module getModule(String JavaDoc type, String JavaDoc name)
184         throws Exception JavaDoc
185     {
186         return findModule(type, name);
187     }
188
189     protected String JavaDoc findTemplate(String JavaDoc moduleType, String JavaDoc targetTemplate)
190         throws Exception JavaDoc
191     {
192         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
193         PipelineUtil.parseTemplatePath(targetTemplate, sb);
194         Iterator JavaDoc j = getPossibleTemplates(sb.toString());
195
196         while (j.hasNext())
197         {
198             String JavaDoc template = moduleType + "/" + (String JavaDoc) j.next();
199             if (Module.templateExists(template))
200             {
201                 return template;
202             }
203         }
204
205         // We should have a default template type for
206
// each corresponding module and a default extension.
207
return moduleType + defaultTemplate;
208     }
209     
210     /**
211      * Get the parsed module name for the specified template.
212      *
213      * @param template The template name.
214      * @return The parsed module name.
215      * @exception Exception a generic exception.
216      */

217     protected Iterator JavaDoc getPossibleTemplates(String JavaDoc template)
218         throws Exception JavaDoc
219     {
220         List JavaDoc packages = new ArrayList JavaDoc();
221
222         // Parse the template name and change it into a package.
223
StringBuffer JavaDoc pckage = new StringBuffer JavaDoc();
224         int i = PipelineUtil.parseTemplatePath(template,pckage);
225
226         if (pckage.charAt(0) == '/')
227         {
228             pckage.deleteCharAt(0);
229             i--;
230         }
231
232         String JavaDoc extension = Turbine.getConfiguration()
233             .getString("template.default.extension", "vm");
234
235         // Try first an exact match for a module having the same
236
// name as the input template, traverse then upper level
237
// packages to find a default module named Default.
238
int j = 9999;
239         String JavaDoc module;
240         while (j-- > 0)
241         {
242             module = pckage.toString();
243
244             packages.add(module);
245
246             pckage.setLength(i + 1);
247             if (i > 0)
248             {
249                 // We have still packages to traverse.
250
for (i = pckage.length() - 2; i >= 0; i--)
251                 {
252                     if (pckage.charAt(i) == '/')
253                     {
254                         break;
255                     }
256                 }
257             }
258             else if (j > 0)
259             {
260                 // Only the main level left.
261
j = 1;
262             }
263             pckage.append("Default").append(".").append(extension);
264         }
265
266         // Not found, return the default module name.
267
return packages.iterator();
268     }
269
270     protected Module findModule(String JavaDoc type, String JavaDoc name)
271         throws Exception JavaDoc
272     {
273         // just use the module loader's default algorithm
274
// perhaps that code should be put here
275
return Turbine.getModuleLoader().getModule(type,name);
276     }
277 }
278
Popular Tags