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