Source Code Cross Referenced for TurbineTemplateService.java in  » Web-Framework » TURBINE » org » apache » turbine » services » template » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » TURBINE » org.apache.turbine.services.template 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.apache.turbine.services.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.io.File;
023:
024:        import java.util.Collections;
025:        import java.util.HashMap;
026:        import java.util.Map;
027:
028:        import org.apache.commons.configuration.Configuration;
029:
030:        import org.apache.commons.lang.StringUtils;
031:
032:        import org.apache.commons.logging.Log;
033:        import org.apache.commons.logging.LogFactory;
034:
035:        import org.apache.turbine.Turbine;
036:        import org.apache.turbine.TurbineConstants;
037:        import org.apache.turbine.modules.LayoutLoader;
038:        import org.apache.turbine.modules.Loader;
039:        import org.apache.turbine.modules.NavigationLoader;
040:        import org.apache.turbine.modules.PageLoader;
041:        import org.apache.turbine.modules.ScreenLoader;
042:        import org.apache.turbine.services.InitializationException;
043:        import org.apache.turbine.services.TurbineBaseService;
044:        import org.apache.turbine.services.factory.TurbineFactory;
045:        import org.apache.turbine.services.servlet.TurbineServlet;
046:        import org.apache.turbine.services.template.mapper.BaseTemplateMapper;
047:        import org.apache.turbine.services.template.mapper.ClassMapper;
048:        import org.apache.turbine.services.template.mapper.DirectMapper;
049:        import org.apache.turbine.services.template.mapper.DirectTemplateMapper;
050:        import org.apache.turbine.services.template.mapper.LayoutTemplateMapper;
051:        import org.apache.turbine.services.template.mapper.Mapper;
052:        import org.apache.turbine.services.template.mapper.ScreenTemplateMapper;
053:        import org.apache.turbine.util.RunData;
054:        import org.apache.turbine.util.TurbineException;
055:        import org.apache.turbine.util.uri.URIConstants;
056:
057:        /**
058:         * This service provides a method for mapping templates to their
059:         * appropriate Screens or Navigations.  It also allows templates to
060:         * define a layout/navigations/screen modularization within the
061:         * template structure.  It also performs caching if turned on in the
062:         * properties file.
063:         *
064:         * This service is not bound to a specific templating engine but we
065:         * will use the Velocity templating engine for the examples. It is
066:         * available by using the VelocityService.
067:         *
068:         * This assumes the following properties in the Turbine configuration:
069:         *
070:         * <pre>
071:         * # Register the VelocityService for the "vm" extension.
072:         * services.VelocityService.template.extension=vm
073:         *
074:         * # Default Java class for rendering a Page in this service
075:         * # (must be found on the class path (org.apache.turbine.modules.page.VelocityPage))
076:         * services.VelocityService.default.page = VelocityPage
077:         *
078:         * # Default Java class for rendering a Screen in this service
079:         * # (must be found on the class path (org.apache.turbine.modules.screen.VelocityScreen))
080:         * services.VelocityService.default.screen=VelocityScreen
081:         *
082:         * # Default Java class for rendering a Layout in this service
083:         * # (must be found on the class path (org.apache.turbine.modules.layout.VelocityOnlyLayout))
084:         * services.VelocityService.default.layout = VelocityOnlyLayout
085:         *
086:         * # Default Java class for rendering a Navigation in this service
087:         * # (must be found on the class path (org.apache.turbine.modules.navigation.VelocityNavigation))
088:         * services.VelocityService.default.navigation=VelocityNavigation
089:         *
090:         * # Default Template Name to be used as Layout. If nothing else is
091:         * # found, return this as the default name for a layout
092:         * services.VelocityService.default.layout.template = Default.vm
093:         * </pre>
094:         * If you want to render a template, a search path is used to find
095:         * a Java class which might provide information for the context of
096:         * this template.
097:         *
098:         * If you request e.g. the template screen
099:         *
100:         * about,directions,Driving.vm
101:         *
102:         * then the following class names are searched (on the module search
103:         * path):
104:         *
105:         * 1. about.directions.Driving     &lt;- direct matching the template to the class name
106:         * 2. about.directions.Default     &lt;- matching the package, class name is Default
107:         * 3. about.Default                &lt;- stepping up in the package hierarchy, looking for Default
108:         * 4. Default                      &lt;- Class called "Default" without package
109:         * 5. VelocityScreen               &lt;- The class configured by the Service (VelocityService) to
110:         *
111:         * And if you have the following module packages configured:
112:         *
113:         * module.packages = org.apache.turbine.modules, com.mycorp.modules
114:         *
115:         * then the class loader will look for
116:         *
117:         * org.apache.turbine.modules.screens.about.directions.Driving
118:         * com.mycorp.modules.screens.about.directions.Driving
119:         * org.apache.turbine.modules.screens.about.directions.Default
120:         * com.mycorp.modules.screens.about.directions.Default
121:         * org.apache.turbine.modules.screens.about.Default
122:         * com.mycorp.modules.screens.about.Default
123:         * org.apache.turbine.modules.screens.Default
124:         * com.mycorp.modules.screens.Default
125:         * org.apache.turbine.modules.screens.VelocityScreen
126:         * com.mycorp.modules.screens.VelocityScreen
127:         *
128:         * Most of the times, you don't have any backing Java class for a
129:         * template screen, so the first match will be
130:         * org.apache.turbine.modules.screens.VelocityScreen
131:         * which then renders your screen.
132:         *
133:         * Please note, that your Screen Template (Driving.vm) must exist!
134:         * If it does not exist, the Template Service will report an error.
135:         *
136:         * Once the screen is found, the template service will look for
137:         * the Layout and Navigation templates of your Screen. Here, the
138:         * template service looks for matching template names!
139:         *
140:         * Consider our example:  about,directions,Driving.vm (Screen Name)
141:         *
142:         * Now the template service will look for the following Navigation
143:         * and Layout templates:
144:         *
145:         * 1. about,directions,Driving.vm      &lt;- exact match
146:         * 2. about,directions,Default.vm      &lt;- package match, Default name
147:         * 3. about,Default.vm                 &lt;- stepping up in the hierarchy
148:         * 4. Default.vm                       &lt;- The name configured as default.layout.template
149:         *                                        in the Velocity service.
150:         *
151:         * And now Hennings' two golden rules for using templates:
152:         *
153:         * Many examples and docs from older Turbine code show template pathes
154:         * with a slashes. Repeat after me: "TEMPLATE NAMES NEVER CONTAIN SLASHES!"
155:         *
156:         * Many examples and docs from older Turbine code show templates that start
157:         * with "/". This is not only a violation of the rule above but actively breaks
158:         * things like loading templates from a jar with the velocity jar loader. Repeat
159:         * after me: "TEMPLATE NAMES ARE NOT PATHES. THEY'RE NOT ABSOLUTE AND HAVE NO
160:         * LEADING /".
161:         *
162:         * If you now wonder how a template name is mapped to a file name: This is
163:         * scope of the templating engine. Velocity e.g. has this wonderful option to
164:         * load templates from jar archives. There is no single file but you tell
165:         * velocity "get about,directions,Driving.vm" and it returns the rendered
166:         * template. This is not the job of the Templating Service but of the Template
167:         * rendering services like VelocityService.
168:         *
169:         * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
170:         * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
171:         * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
172:         * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
173:         * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
174:         * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
175:         * @version $Id: TurbineTemplateService.java 534527 2007-05-02 16:10:59Z tv $
176:         */
177:        public class TurbineTemplateService extends TurbineBaseService
178:                implements  TemplateService {
179:            /** Logging */
180:            private static Log log = LogFactory
181:                    .getLog(TurbineTemplateService.class);
182:
183:            /** Represents Page Objects */
184:            public static final int PAGE_KEY = 0;
185:
186:            /** Represents Page Objects */
187:            public static final String PAGE_NAME = "page";
188:
189:            /** Represents Screen Objects */
190:            public static final int SCREEN_KEY = 1;
191:
192:            /** Represents Screen Objects */
193:            public static final String SCREEN_NAME = "screen";
194:
195:            /** Represents Layout Objects */
196:            public static final int LAYOUT_KEY = 2;
197:
198:            /** Represents Layout Objects */
199:            public static final String LAYOUT_NAME = "layout";
200:
201:            /** Represents Navigation Objects */
202:            public static final int NAVIGATION_KEY = 3;
203:
204:            /** Represents Navigation Objects */
205:            public static final String NAVIGATION_NAME = "navigation";
206:
207:            /** Represents Layout Template Objects */
208:            public static final int LAYOUT_TEMPLATE_KEY = 4;
209:
210:            /** Represents Layout Template Objects */
211:            public static final String LAYOUT_TEMPLATE_NAME = "layout.template";
212:
213:            /** Represents Screen Template Objects */
214:            public static final int SCREEN_TEMPLATE_KEY = 5;
215:
216:            /** Represents Screen Template Objects */
217:            public static final String SCREEN_TEMPLATE_NAME = "screen.template";
218:
219:            /** Represents Navigation Template Objects */
220:            public static final int NAVIGATION_TEMPLATE_KEY = 6;
221:
222:            /** Represents Navigation Template Objects */
223:            public static final String NAVIGATION_TEMPLATE_NAME = "navigation.template";
224:
225:            /** Number of different Template Types that we know of */
226:            public static final int TEMPLATE_TYPES = 7;
227:
228:            /** Here we register the mapper objects for our various object types */
229:            private Mapper[] mapperRegistry = null;
230:
231:            /**
232:             * The default file extension used as a registry key when a
233:             * template's file extension cannot be determined.
234:             *
235:             * @deprecated. Use TemplateService.DEFAULT_EXTENSION_VALUE.
236:             */
237:            protected static final String NO_FILE_EXT = TemplateService.DEFAULT_EXTENSION_VALUE;
238:
239:            /** Flag set if cache is to be used. */
240:            private boolean useCache = false;
241:
242:            /** Default extension for templates. */
243:            private String defaultExtension;
244:
245:            /** Default template without the default extension. */
246:            private String defaultTemplate;
247:
248:            /**
249:             * The mappings of template file extensions to {@link
250:             * org.apache.turbine.services.template.TemplateEngineService}
251:             * implementations. Implementing template engines can locate
252:             * templates within the capability of any resource loaders they
253:             * may possess, and other template engines are stuck with file
254:             * based template hierarchy only.
255:             */
256:            private Map templateEngineRegistry = null;
257:
258:            /**
259:             * C'tor
260:             */
261:            public TurbineTemplateService() {
262:            }
263:
264:            /**
265:             * Called the first time the Service is used.
266:             *
267:             * @exception InitializationException Something went wrong when
268:             *                                     setting up the Template Service.
269:             */
270:            public void init() throws InitializationException {
271:                // Get the configuration for the template service.
272:                Configuration config = getConfiguration();
273:
274:                // Get the default extension to use if nothing else is applicable.
275:                defaultExtension = config.getString(
276:                        TemplateService.DEFAULT_EXTENSION_KEY,
277:                        TemplateService.DEFAULT_EXTENSION_VALUE);
278:
279:                defaultTemplate = config.getString(
280:                        TemplateService.DEFAULT_TEMPLATE_KEY,
281:                        TemplateService.DEFAULT_TEMPLATE_VALUE);
282:
283:                // Check to see if we are going to be caching modules.
284:                // Aaargh, who moved this _out_ of the TemplateService package?
285:                useCache = Turbine.getConfiguration().getBoolean(
286:                        TurbineConstants.MODULE_CACHE_KEY,
287:                        TurbineConstants.MODULE_CACHE_DEFAULT);
288:
289:                log.debug("Default Extension: " + defaultExtension);
290:                log.debug("Default Template:  " + defaultTemplate);
291:                log.debug("Use Caching:       " + useCache);
292:
293:                templateEngineRegistry = Collections
294:                        .synchronizedMap(new HashMap());
295:
296:                initMapper(config);
297:                setInit(true);
298:            }
299:
300:            /**
301:             * Returns true if the Template Service has caching activated
302:             *
303:             * @return true if Caching is active.
304:             */
305:            public boolean isCaching() {
306:                return useCache;
307:            }
308:
309:            /**
310:             * Get the default template name extension specified
311:             * in the template service properties. If no extension
312:             * is defined, return the empty string.
313:             *
314:             * @return The default extension.
315:             */
316:            public String getDefaultExtension() {
317:                return StringUtils.isNotEmpty(defaultExtension) ? defaultExtension
318:                        : "";
319:            }
320:
321:            /**
322:             * Return Extension for a supplied template
323:             *
324:             * @param template The template name
325:             *
326:             * @return extension The extension for the supplied template
327:             */
328:            public String getExtension(String template) {
329:                if (StringUtils.isEmpty(template)) {
330:                    return getDefaultExtension();
331:                }
332:
333:                int dotIndex = template.indexOf(EXTENSION_SEPARATOR);
334:
335:                return (dotIndex < 0) ? getDefaultExtension() : template
336:                        .substring(dotIndex + 1);
337:            }
338:
339:            /**
340:             * Returns the Default Template Name with the Default Extension.
341:             * If the extension is unset, return only the template name
342:             *
343:             * @return The default template Name
344:             */
345:            public String getDefaultTemplate() {
346:                StringBuffer sb = new StringBuffer();
347:                sb.append(defaultTemplate);
348:                if (StringUtils.isNotEmpty(defaultExtension)) {
349:                    sb.append(EXTENSION_SEPARATOR);
350:                    sb.append(getDefaultExtension());
351:                }
352:                return sb.toString();
353:            }
354:
355:            /**
356:             * Get the default page module name of the template engine
357:             * service corresponding to the default template name extension.
358:             *
359:             * @return The default page module name.
360:             */
361:            public String getDefaultPage() {
362:                return getDefaultPageName(getDefaultTemplate());
363:            }
364:
365:            /**
366:             * Get the default screen module name of the template engine
367:             * service corresponding to the default template name extension.
368:             *
369:             * @return The default screen module name.
370:             */
371:            public String getDefaultScreen() {
372:                return getDefaultScreenName(getDefaultTemplate());
373:            }
374:
375:            /**
376:             * Get the default layout module name of the template engine
377:             * service corresponding to the default template name extension.
378:             *
379:             * @return The default layout module name.
380:             */
381:            public String getDefaultLayout() {
382:                return getDefaultLayoutName(getDefaultTemplate());
383:            }
384:
385:            /**
386:             * Get the default navigation module name of the template engine
387:             * service corresponding to the default template name extension.
388:             *
389:             * @return The default navigation module name.
390:             */
391:            public String getDefaultNavigation() {
392:                return getDefaultNavigationName(getDefaultTemplate());
393:            }
394:
395:            /**
396:             * Get the default layout template name of the template engine
397:             * service corresponding to the default template name extension.
398:             *
399:             * @return The default layout template name.
400:             */
401:            public String getDefaultLayoutTemplate() {
402:                return getDefaultLayoutTemplateName(getDefaultTemplate());
403:            }
404:
405:            /**
406:             * Get the default page module name of the template engine
407:             * service corresponding to the template name extension of
408:             * the named template.
409:             *
410:             * @param template The template name.
411:             * @return The default page module name.
412:             */
413:            public String getDefaultPageName(String template) {
414:                return ((Mapper) mapperRegistry[PAGE_KEY])
415:                        .getDefaultName(template);
416:            }
417:
418:            /**
419:             * Get the default screen module name of the template engine
420:             * service corresponding to the template name extension of
421:             * the named template.
422:             *
423:             * @param template The template name.
424:             * @return The default screen module name.
425:             */
426:            public String getDefaultScreenName(String template) {
427:                return ((Mapper) mapperRegistry[SCREEN_KEY])
428:                        .getDefaultName(template);
429:            }
430:
431:            /**
432:             * Get the default layout module name of the template engine
433:             * service corresponding to the template name extension of
434:             * the named template.
435:             *
436:             * @param template The template name.
437:             * @return The default layout module name.
438:             */
439:            public String getDefaultLayoutName(String template) {
440:                return ((Mapper) mapperRegistry[LAYOUT_KEY])
441:                        .getDefaultName(template);
442:            }
443:
444:            /**
445:             * Get the default navigation module name of the template engine
446:             * service corresponding to the template name extension of
447:             * the named template.
448:             *
449:             * @param template The template name.
450:             * @return The default navigation module name.
451:             */
452:            public String getDefaultNavigationName(String template) {
453:                return ((Mapper) mapperRegistry[NAVIGATION_KEY])
454:                        .getDefaultName(template);
455:            }
456:
457:            /**
458:             * Get the default layout template name of the template engine
459:             * service corresponding to the template name extension of
460:             * the named template.
461:             *
462:             * @param template The template name.
463:             * @return The default layout template name.
464:             */
465:            public String getDefaultLayoutTemplateName(String template) {
466:                return ((Mapper) mapperRegistry[LAYOUT_TEMPLATE_KEY])
467:                        .getDefaultName(template);
468:            }
469:
470:            /**
471:             * Find the default page module name for the given request.
472:             *
473:             * @param data The encapsulation of the request to retrieve the
474:             *             default page for.
475:             * @return The default page module name.
476:             */
477:            public String getDefaultPageName(RunData data) {
478:                String template = data.getParameters().get(
479:                        URIConstants.CGI_TEMPLATE_PARAM);
480:                return (template != null) ? getDefaultPageName(template)
481:                        : getDefaultPage();
482:            }
483:
484:            /**
485:             * Find the default layout module name for the given request.
486:             *
487:             * @param data The encapsulation of the request to retrieve the
488:             *             default layout for.
489:             * @return The default layout module name.
490:             */
491:            public String getDefaultLayoutName(RunData data) {
492:                String template = data.getParameters().get(
493:                        URIConstants.CGI_TEMPLATE_PARAM);
494:                return (template != null) ? getDefaultLayoutName(template)
495:                        : getDefaultLayout();
496:            }
497:
498:            /**
499:             * Locate and return the name of the screen module to be used
500:             * with the named screen template.
501:             *
502:             * @param template The screen template name.
503:             * @return The found screen module name.
504:             * @exception Exception, a generic exception.
505:             */
506:            public String getScreenName(String template) throws Exception {
507:                return ((Mapper) mapperRegistry[SCREEN_KEY])
508:                        .getMappedName(template);
509:            }
510:
511:            /**
512:             * Locate and return the name of the layout module to be used
513:             * with the named layout template.
514:             *
515:             * @param template The layout template name.
516:             * @return The found layout module name.
517:             * @exception Exception, a generic exception.
518:             */
519:            public String getLayoutName(String template) throws Exception {
520:                return ((Mapper) mapperRegistry[LAYOUT_KEY])
521:                        .getMappedName(template);
522:            }
523:
524:            /**
525:             * Locate and return the name of the navigation module to be used
526:             * with the named navigation template.
527:             *
528:             * @param template The navigation template name.
529:             * @return The found navigation module name.
530:             * @exception Exception, a generic exception.
531:             */
532:            public String getNavigationName(String template) throws Exception {
533:                return ((Mapper) mapperRegistry[NAVIGATION_KEY])
534:                        .getMappedName(template);
535:            }
536:
537:            /**
538:             * Locate and return the name of the screen template corresponding
539:             * to the given template name parameter. This might return null if
540:             * the screen is not found!
541:             *
542:             * @param template The template name parameter.
543:             * @return The found screen template name.
544:             * @exception Exception, a generic exception.
545:             */
546:            public String getScreenTemplateName(String template)
547:                    throws Exception {
548:                return ((Mapper) mapperRegistry[SCREEN_TEMPLATE_KEY])
549:                        .getMappedName(template);
550:            }
551:
552:            /**
553:             * Locate and return the name of the layout template corresponding
554:             * to the given screen template name parameter.
555:             *
556:             * @param template The template name parameter.
557:             * @return The found screen template name.
558:             * @exception Exception, a generic exception.
559:             */
560:            public String getLayoutTemplateName(String template)
561:                    throws Exception {
562:                return ((Mapper) mapperRegistry[LAYOUT_TEMPLATE_KEY])
563:                        .getMappedName(template);
564:            }
565:
566:            /**
567:             * Locate and return the name of the navigation template corresponding
568:             * to the given template name parameter. This might return null if
569:             * the navigation is not found!
570:             *
571:             * @param template The template name parameter.
572:             * @return The found navigation template name.
573:             * @exception Exception, a generic exception.
574:             */
575:            public String getNavigationTemplateName(String template)
576:                    throws Exception {
577:                return ((Mapper) mapperRegistry[NAVIGATION_TEMPLATE_KEY])
578:                        .getMappedName(template);
579:            }
580:
581:            /**
582:             * Translates the supplied template paths into their Turbine-canonical
583:             * equivalent (probably absolute paths). This is used if the templating
584:             * engine (e.g. JSP) does not provide any means to load a page but
585:             * the page path is passed to the servlet container.
586:             *
587:             * @param templatePaths An array of template paths.
588:             * @return An array of translated template paths.
589:             * @deprecated Each template engine service should know how to translate
590:             *             a request onto a file.
591:             */
592:            public String[] translateTemplatePaths(String[] templatePaths) {
593:                for (int i = 0; i < templatePaths.length; i++) {
594:                    templatePaths[i] = TurbineServlet
595:                            .getRealPath(templatePaths[i]);
596:                }
597:                return templatePaths;
598:            }
599:
600:            /**
601:             * Delegates to the appropriate {@link
602:             * org.apache.turbine.services.template.TemplateEngineService} to
603:             * check the existance of the specified template.
604:             *
605:             * @param template The template to check for the existance of.
606:             * @param templatePaths The paths to check for the template.
607:             * @deprecated Use templateExists from the various Templating Engines
608:             */
609:            public boolean templateExists(String template,
610:                    String[] templatePaths) {
611:                for (int i = 0; i < templatePaths.length; i++) {
612:                    if (new File(templatePaths[i], template).exists()) {
613:                        return true;
614:                    }
615:                }
616:                return false;
617:            }
618:
619:            /**
620:             * Registers the provided template engine for use by the
621:             * <code>TemplateService</code>.
622:             *
623:             * @param service The <code>TemplateEngineService</code> to register.
624:             */
625:            public synchronized void registerTemplateEngineService(
626:                    TemplateEngineService service) {
627:                String[] exts = service.getAssociatedFileExtensions();
628:
629:                for (int i = 0; i < exts.length; i++) {
630:                    templateEngineRegistry.put(exts[i], service);
631:                }
632:            }
633:
634:            /**
635:             * The {@link org.apache.turbine.services.template.TemplateEngineService}
636:             * associated with the specified template's file extension.
637:             *
638:             * @param template The template name.
639:             * @return The template engine service.
640:             */
641:            public TemplateEngineService getTemplateEngineService(
642:                    String template) {
643:                return (TemplateEngineService) templateEngineRegistry
644:                        .get(getExtension(template));
645:            }
646:
647:            /**
648:             * Register a template Mapper to the service. This Mapper
649:             * performs the template mapping and searching for a specific
650:             * object type which is managed by the TemplateService.
651:             *
652:             * @param templateKey  One of the _KEY constants for the Template object types.
653:             * @param mapper  An object which implements the Mapper interface.
654:             */
655:            private void registerMapper(int templateKey, Mapper mapper) {
656:                mapper.init();
657:                mapperRegistry[templateKey] = mapper;
658:            }
659:
660:            /**
661:             * Load and configure the Template mappers for
662:             * the Template Service.
663:             *
664:             * @param conf The current configuration object.
665:             * @throws InitializationException A problem occured trying to set up the mappers.
666:             */
667:            private void initMapper(Configuration conf)
668:                    throws InitializationException {
669:                // Create a registry with the number of Template Types managed by this service.
670:                // We could use a List object here and extend the number of managed objects
671:                // dynamically. However, by using an Object Array, we get much more performance
672:                // out of the Template Service.
673:                mapperRegistry = new Mapper[TEMPLATE_TYPES];
674:
675:                String[] mapperNames = new String[] { PAGE_NAME, SCREEN_NAME,
676:                        LAYOUT_NAME, NAVIGATION_NAME, LAYOUT_TEMPLATE_NAME,
677:                        SCREEN_TEMPLATE_NAME, NAVIGATION_TEMPLATE_NAME };
678:
679:                String[] mapperClasses = new String[] {
680:                        DirectMapper.class.getName(),
681:                        ClassMapper.class.getName(),
682:                        ClassMapper.class.getName(),
683:                        ClassMapper.class.getName(),
684:                        LayoutTemplateMapper.class.getName(),
685:                        ScreenTemplateMapper.class.getName(),
686:                        DirectTemplateMapper.class.getName() };
687:
688:                int[] mapperCacheSize = new int[] {
689:                        0,
690:                        conf.getInt(TurbineConstants.SCREEN_CACHE_SIZE_KEY,
691:                                TurbineConstants.SCREEN_CACHE_SIZE_DEFAULT),
692:                        conf.getInt(TurbineConstants.LAYOUT_CACHE_SIZE_KEY,
693:                                TurbineConstants.LAYOUT_CACHE_SIZE_DEFAULT),
694:                        conf.getInt(TurbineConstants.NAVIGATION_CACHE_SIZE_KEY,
695:                                TurbineConstants.NAVIGATION_CACHE_SIZE_DEFAULT),
696:                        conf.getInt(TurbineConstants.LAYOUT_CACHE_SIZE_KEY,
697:                                TurbineConstants.LAYOUT_CACHE_SIZE_DEFAULT),
698:                        conf.getInt(TurbineConstants.SCREEN_CACHE_SIZE_KEY,
699:                                TurbineConstants.SCREEN_CACHE_SIZE_DEFAULT),
700:                        conf.getInt(TurbineConstants.NAVIGATION_CACHE_SIZE_KEY,
701:                                TurbineConstants.NAVIGATION_CACHE_SIZE_DEFAULT) };
702:
703:                String[] mapperDefaultProperty = new String[] {
704:                        TemplateEngineService.DEFAULT_PAGE,
705:                        TemplateEngineService.DEFAULT_SCREEN,
706:                        TemplateEngineService.DEFAULT_LAYOUT,
707:                        TemplateEngineService.DEFAULT_NAVIGATION,
708:                        TemplateEngineService.DEFAULT_LAYOUT_TEMPLATE,
709:                        TemplateEngineService.DEFAULT_SCREEN_TEMPLATE,
710:                        TemplateEngineService.DEFAULT_NAVIGATION_TEMPLATE };
711:
712:                char[] mapperSeparator = new char[] { '.', '.', '.', '.', '/',
713:                        '/', '/' };
714:
715:                Loader[] mapperLoader = new Loader[] {
716:                        PageLoader.getInstance(), ScreenLoader.getInstance(),
717:                        LayoutLoader.getInstance(),
718:                        NavigationLoader.getInstance(), null, null, null };
719:
720:                String[] mapperPrefix = new String[] { null, null, null, null,
721:                        TurbineConstants.LAYOUT_PREFIX,
722:                        TurbineConstants.SCREEN_PREFIX,
723:                        TurbineConstants.NAVIGATION_PREFIX };
724:
725:                for (int i = 0; i < TEMPLATE_TYPES; i++) {
726:                    StringBuffer mapperProperty = new StringBuffer();
727:                    mapperProperty.append("mapper.");
728:                    mapperProperty.append(mapperNames[i]);
729:                    mapperProperty.append(".class");
730:
731:                    String mapperClass = conf.getString(mapperProperty
732:                            .toString(), mapperClasses[i]);
733:
734:                    log.info("Using " + mapperClass + " to map "
735:                            + mapperNames[i] + " elements");
736:
737:                    Mapper tm = null;
738:
739:                    try {
740:                        tm = (Mapper) TurbineFactory.getInstance(mapperClass);
741:                    } catch (TurbineException te) {
742:                        throw new InitializationException("", te);
743:                    }
744:
745:                    tm.setUseCache(useCache);
746:                    tm.setCacheSize(mapperCacheSize[i]);
747:                    tm.setDefaultProperty(mapperDefaultProperty[i]);
748:                    tm.setSeparator(mapperSeparator[i]);
749:
750:                    if ((mapperLoader[i] != null)
751:                            && (tm instanceof  ClassMapper)) {
752:                        ((ClassMapper) tm).setLoader(mapperLoader[i]);
753:                    }
754:
755:                    if ((mapperPrefix[i] != null)
756:                            && (tm instanceof  BaseTemplateMapper)) {
757:                        ((BaseTemplateMapper) tm).setPrefix(mapperPrefix[i]);
758:                    }
759:
760:                    registerMapper(i, tm);
761:                }
762:            }
763:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.