2 * Copyright (C) 2005 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with any library licensed under the Apache Software License.
22 * You must obey the GNU General Public License in all respects for all of the code used
23 * other than the above mentioned libraries. If you modify this file, you may extend this
24 * exception to your version of the file, but you are not obligated to do so.
25 * If you do not wish to do so, delete this exception statement from your version.
27 package mir.generator.tal;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.util.List;
35 import mir.generator.tal.interfaces.TALExpressionParser;
36 import mir.generator.tal.template.CoreTemplateNodeLibrary;
37 import mir.generator.tal.template.MacroTemplateNodeLibrary;
38 import mir.generator.tal.template.Template;
39 import mir.generator.tal.template.TemplateLibrary;
40 import mir.generator.tal.template.TemplateNode;
41 import mir.generator.tal.template.TemplateNodeLibrary;
42 import mir.generator.tal.template.TemplateProcessingException;
43 import mir.util.StringRoutines;
45 public class TALTemplateEngine implements TemplateLibrary {
46 private TALExpressionParser expressionParser;
47 private File basePath;
48 private CachingFileLoader loader;
50 private SimpleTemplateNodeLibraryRegistry registry;
52 public TALTemplateEngine(TALExpressionParser anExpressionParser, File aBasePath) {
53 expressionParser = anExpressionParser;
55 loader = new CachingFileLoader(100, new TemplateFactory());
57 registry = new SimpleTemplateNodeLibraryRegistry();
59 registerTemplateNodeLibrary("tal", "http://xml.zope.org/namespaces/tal",
60 new CoreTemplateNodeLibrary("tal", "http://xml.zope.org/namespaces/tal"));
61 registerTemplateNodeLibrary("metal", "http://xml.zope.org/namespaces/metal",
62 new MacroTemplateNodeLibrary("metal", "http://xml.zope.org/namespaces/metal"));
65 public void registerTemplateNodeLibrary(String aPrefix, String aUrl, TemplateNodeLibrary aLibrary) {
66 registry.registerTemplateNodeLibrary(aPrefix, aUrl, aLibrary);
69 public Template loadTemplate(String aName) throws IOException, TemplateProcessingException {
70 List parts = StringRoutines.splitString(aName, "#");
72 if (parts.size()!=1 && parts.size()!=2) {
73 throw new TemplateProcessingException("Invalid template name: " + aName);
76 File location = new File(basePath, (String) parts.get(0));
78 Template baseTemplate = (Template) loader.retrieveFile(location.getCanonicalPath());
80 if (baseTemplate!=null && parts.size()>1) {
81 Map definitions = (Map) baseTemplate.getContext().get(MacroTemplateNodeLibrary.MACRO_DEFINITIONS_KEY);
82 if (definitions==null || !definitions.containsKey(parts.get(1))) {
83 throw new TemplateProcessingException("No macro '"+(String) parts.get(1)+"' found in template '" + (String) parts.get(0) + "'");
86 return new Template(expressionParser, (TemplateNode) definitions.get(parts.get(1)), baseTemplate.getContext());
91 public Template lookupTemplate(String aName) throws TemplateProcessingException {
93 return loadTemplate(aName);
95 catch (IOException e) {
96 throw new TemplateProcessingException("Can't load template " + aName + ": " + e.toString(), e);
100 private class TemplateFactory implements CachingFileLoader.CachedFileObjectFactory {
101 public Object constructObject(InputStream aStream) {
103 TALTemplateParser parser = new TALTemplateParser(registry);
105 return parser.parse(aStream, expressionParser);
107 catch (Throwable t) {
108 throw new TALFailure(t);