001: package com.teamkonzept.lib;
002:
003: import java.io.*;
004: import java.util.Hashtable;
005: import com.teamkonzept.lib.*;
006: import com.teamkonzept.lib.templates.*;
007: import de.webman.template.jsp.*;
008: import de.webman.template.xslt.*;
009: import com.teamkonzept.web.TKHTMLTemplate;
010:
011: /**
012: * Utility Methoden fuer Templates
013: *
014: * @author $Author: uli $
015: * @version $Revision: 1.11 $
016: */
017: public class TemplateUtils implements TemplateTypes {
018:
019: /**
020: * The cache for JSP and XSLT templates
021: */
022: private static Hashtable cache = new Hashtable();
023:
024: /**
025: * The top level generation directory.
026: */
027: private static final String TOP_LEVEL_GENDIR = "html_templates";
028:
029: /**
030: * The second level generation directory.
031: */
032: private static final String SECOND_LEVEL_GENDIR = "templates";
033:
034: /**
035: * The template source directory.
036: */
037: private static final String WEBMAN_TEMPLATE_DIR = "templates";
038:
039: /**
040: * Returns the template source directory.
041: *
042: * @return the template source directory.
043: */
044: public static String getWebmanTemplateDirectory() {
045: return WEBMAN_TEMPLATE_DIR;
046: }
047:
048: /**
049: * Returns the generation directory.
050: *
051: * @return the generation directory.
052: */
053: public static String getGenerationDirectory() {
054: return TOP_LEVEL_GENDIR + File.separator + SECOND_LEVEL_GENDIR
055: + File.separator;
056: }
057:
058: /**
059: * Returns the top level generation directory.
060: *
061: * @return the top level generation directory.
062: */
063: public static String getTopLevelGenDir() {
064: return TOP_LEVEL_GENDIR;
065: }
066:
067: /**
068: * Returns the second level generation directory.
069: *
070: * @return the second level generation directory.
071: */
072: public static String getSecondLevelGenDir() {
073: return SECOND_LEVEL_GENDIR;
074: }
075:
076: /**
077: * Return the specified template object.
078: *
079: * @param name the template source file name.
080: * @param root the root directory.
081: * @param editor the template editing mode.
082: * @return the specified template object.
083: * @throws TKTemplateSyntaxException if the specified template source is errornous.
084: * @throws FileNotFoundException if the specified template source does not exist.
085: */
086: public static TemplateBasic getTemplate(String name, String root,
087: boolean editor) throws TKTemplateSyntaxException,
088: FileNotFoundException {
089: if (name.toUpperCase().endsWith(JSP_TEMPLATE)
090: || name.toUpperCase().endsWith(XSLT_TEMPLATE)) {
091: // falsche RootDir ?
092: int index = root.indexOf(TOP_LEVEL_GENDIR);
093: if (index != -1) {
094: root = root.substring(0, index);
095: }
096: index = root.indexOf("file:");
097: if (index != -1) {
098: root = root.substring(5, root.length());
099: }
100: String key = name + new Boolean(editor).toString();
101: TemplateBasic t = (TemplateBasic) cache.get(key);
102: if (t != null) {
103: return t;
104: }
105: if (name.toUpperCase().endsWith(JSP_TEMPLATE)) {
106: t = new JSPTemplate(root, name, editor);
107: cache.put(key, t);
108: return t;
109: } else {
110: t = new XSLTTemplate(root, name, editor);
111: cache.put(key, t);
112: return t;
113: }
114: }
115:
116: if (name.toUpperCase().endsWith(TK_TEMPLATE)) {
117: return new TKHTMLTemplate(root + name);
118: }
119:
120: return null;
121: }
122:
123: }
|