KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > format > template > TemplateLoader


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9
10 package org.jboss.portal.format.template;
11
12 import java.io.InputStream JavaDoc;
13 import java.io.InputStreamReader JavaDoc;
14 import java.io.Reader JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.dom4j.Document;
18 import org.jboss.portal.format.util.CLLoader;
19 import org.jboss.portal.format.util.Loader;
20 import org.jboss.portal.common.util.Tools;
21
22 /**
23  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
24  * @version $Revision: 1.2 $
25  */

26 public class TemplateLoader extends TemplateRepository
27 {
28
29    private Loader loader;
30 // private Map defaults = new HashMap();
31

32    public TemplateLoader(Loader loader)
33    {
34       setLoader(loader);
35    }
36
37    public Loader getLoader()
38    {
39       return loader;
40    }
41
42    public void setLoader(Loader loader)
43    {
44       if (loader == null)
45       {
46          throw new IllegalArgumentException JavaDoc("loader cannot be null");
47       }
48       this.loader = loader;
49    }
50
51 // public void load(String name)
52
// {
53
// InputStream in = loader.load(name + ".properties");
54
// if (in != null)
55
// {
56
// Properties props = new Properties();
57
// }
58
// else
59
// {
60
//
61
// }
62
// }
63

64 // /**
65
// * Creates a template instance.
66
// */
67
// public Template createTemplate(String name)
68
// {
69
// Properties props = (Properties)defaults.get(name);
70
// Context ctx = Context.NULL_CONTEXT;
71
// if (props != null)
72
// {
73
// ctx = new HashContext();
74
// ctx.putAll(props);
75
// }
76
// return createTemplate(name, ctx);
77
// }
78

79 // public void addTemplate(String name)
80
// {
81
// addTemplate(name, name);
82
// }
83
//
84
//
85

86    public void addTemplate(String JavaDoc name, String JavaDoc loadName) throws BuildException
87    {
88       Document doc = loadTemplate(loadName);
89       addTemplate(name, doc);
90    }
91
92    public void addTemplate(String JavaDoc name, String JavaDoc loadName, Set JavaDoc names) throws BuildException
93    {
94       Document doc = loadTemplate(loadName);
95       addTemplate(name, doc, names);
96    }
97
98 // public void addSubTemplate(String templateName, String loadName, String xpath, Set names)
99
// {
100
// Document template = loadTemplate(loadName);
101
// addTemplate(templateName, (Element)template.selectSingleNode(xpath), names);
102
// }
103

104 // public void addSubTemplate(String templateName, String loadName, String xpath)
105
// {
106
// InputStream pin = null;
107
// try
108
// {
109
// Properties props = new Properties();
110
// pin = loader.loadProperties(loadName);
111
// if (pin != null)
112
// {
113
// defaults.put(loadName, props);
114
// props.load(pin);
115
// }
116
// addSubTemplate(templateName, props.keySet());
117
// }
118
// catch(IOException e)
119
// {
120
// e.printStackTrace();
121
// }
122
// finally
123
// {
124
// Tools.safeClose(pin);
125
// }
126
// }
127

128    /**
129     * Simply loads a template and returns it.
130     */

131    public Document loadTemplate(String JavaDoc name) throws BuildException
132    {
133       InputStream JavaDoc tin = null;
134       try
135       {
136          tin = loader.load(name);
137          if (tin != null)
138          {
139             Reader JavaDoc reader = new InputStreamReader JavaDoc(tin);
140             TemplateParser parser = new TemplateParser(reader);
141             return parser.parse();
142          }
143          else
144          {
145             throw new BuildException("Template " + name + " not found");
146          }
147       }
148       catch(ParseException e)
149       {
150          throw new BuildException("Cannot parse the template source " + name, e);
151       }
152       finally
153       {
154          Tools.safeClose(tin);
155       }
156    }
157
158    public static TemplateLoader create(ClassLoader JavaDoc cl, String JavaDoc base)
159    {
160       return new TemplateLoader(new CLLoader(cl, base));
161    }
162 }
163
Popular Tags