KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > teamkonzept > lib > TemplateUtils


1 package com.teamkonzept.lib;
2
3 import java.io.*;
4 import java.util.Hashtable JavaDoc;
5 import com.teamkonzept.lib.*;
6 import com.teamkonzept.lib.templates.*;
7 import de.webman.template.jsp.*;
8 import de.webman.template.xslt.*;
9 import com.teamkonzept.web.TKHTMLTemplate;
10
11 /**
12  * Utility Methoden fuer Templates
13  *
14  * @author $Author: uli $
15  * @version $Revision: 1.11 $
16  */

17 public class TemplateUtils
18     implements TemplateTypes
19 {
20
21     /**
22      * The cache for JSP and XSLT templates
23      */

24     private static Hashtable JavaDoc cache = new Hashtable JavaDoc();
25
26     /**
27      * The top level generation directory.
28      */

29     private static final String JavaDoc TOP_LEVEL_GENDIR = "html_templates";
30
31     /**
32      * The second level generation directory.
33      */

34     private static final String JavaDoc SECOND_LEVEL_GENDIR = "templates";
35
36     /**
37      * The template source directory.
38      */

39     private static final String JavaDoc WEBMAN_TEMPLATE_DIR = "templates";
40
41     /**
42      * Returns the template source directory.
43      *
44      * @return the template source directory.
45      */

46     public static String JavaDoc getWebmanTemplateDirectory ()
47     {
48         return WEBMAN_TEMPLATE_DIR;
49     }
50
51     /**
52      * Returns the generation directory.
53      *
54      * @return the generation directory.
55      */

56     public static String JavaDoc getGenerationDirectory ()
57     {
58         return TOP_LEVEL_GENDIR + File.separator + SECOND_LEVEL_GENDIR + File.separator;
59     }
60
61     /**
62      * Returns the top level generation directory.
63      *
64      * @return the top level generation directory.
65      */

66     public static String JavaDoc getTopLevelGenDir ()
67     {
68         return TOP_LEVEL_GENDIR;
69     }
70
71     /**
72      * Returns the second level generation directory.
73      *
74      * @return the second level generation directory.
75      */

76     public static String JavaDoc getSecondLevelGenDir ()
77     {
78         return SECOND_LEVEL_GENDIR;
79     }
80
81     /**
82      * Return the specified template object.
83      *
84      * @param name the template source file name.
85      * @param root the root directory.
86      * @param editor the template editing mode.
87      * @return the specified template object.
88      * @throws TKTemplateSyntaxException if the specified template source is errornous.
89      * @throws FileNotFoundException if the specified template source does not exist.
90      */

91     public static TemplateBasic getTemplate (String JavaDoc name,
92                                              String JavaDoc root,
93                                              boolean editor)
94         throws TKTemplateSyntaxException, FileNotFoundException
95     {
96         if (name.toUpperCase().endsWith(JSP_TEMPLATE) || name.toUpperCase().endsWith(XSLT_TEMPLATE))
97         {
98             // falsche RootDir ?
99
int index = root.indexOf(TOP_LEVEL_GENDIR);
100             if ( index != -1)
101             {
102                 root = root.substring(0, index);
103             }
104             index = root.indexOf("file:");
105             if ( index != -1)
106             {
107                 root = root.substring(5, root.length());
108             }
109             String JavaDoc key = name + new Boolean JavaDoc(editor).toString();
110             TemplateBasic t = (TemplateBasic)cache.get(key);
111             if (t != null)
112             {
113                 return t;
114             }
115             if (name.toUpperCase().endsWith(JSP_TEMPLATE) )
116             {
117                 t = new JSPTemplate(root, name, editor);
118                 cache.put(key, t);
119                 return t;
120             }
121             else
122             {
123                 t = new XSLTTemplate(root, name, editor);
124                 cache.put(key, t);
125                 return t;
126             }
127         }
128
129         if (name.toUpperCase().endsWith(TK_TEMPLATE))
130         {
131             return new TKHTMLTemplate(root + name);
132         }
133
134         return null;
135     }
136
137 }
138
Popular Tags