scripts/mir-setup/README: update with link to new doc on wiki
[mir.git] / source / mir / generator / tal / TALTemplateEngine.java
1 /*
2  * Copyright (C) 2005 The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
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.
10  *
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.
15  *
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
19  *
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.
26  */
27 package mir.generator.tal;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.util.List;
33 import java.util.Map;
34
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;
44
45 public class TALTemplateEngine implements TemplateLibrary {
46   private TALExpressionParser expressionParser;
47   private File basePath;
48   private CachingFileLoader loader;
49
50   private SimpleTemplateNodeLibraryRegistry registry;
51
52   public TALTemplateEngine(TALExpressionParser anExpressionParser, File aBasePath) {
53     expressionParser = anExpressionParser;
54     basePath = aBasePath;
55     loader = new CachingFileLoader(100, new TemplateFactory());
56
57     registry = new SimpleTemplateNodeLibraryRegistry();
58
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"));
63   }
64
65   public void registerTemplateNodeLibrary(String aPrefix, String aUrl, TemplateNodeLibrary aLibrary) {
66     registry.registerTemplateNodeLibrary(aPrefix, aUrl, aLibrary);
67   }
68
69   public Template loadTemplate(String aName) throws IOException, TemplateProcessingException {
70     List parts = StringRoutines.splitString(aName, "#");
71
72     if (parts.size()!=1 && parts.size()!=2) {
73       throw new TemplateProcessingException("Invalid template name: " + aName);
74     }
75
76     File location = new File(basePath, (String) parts.get(0));
77
78     Template baseTemplate = (Template) loader.retrieveFile(location.getCanonicalPath());
79
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) + "'");
84       }
85
86       return new Template(expressionParser, (TemplateNode) definitions.get(parts.get(1)), baseTemplate.getContext());
87     }
88                 return baseTemplate;
89   }
90
91   public Template lookupTemplate(String aName) throws TemplateProcessingException {
92     try {
93       return loadTemplate(aName);
94     }
95     catch (IOException e) {
96       throw new TemplateProcessingException("Can't load template " + aName + ": " + e.toString(), e);
97     }
98   }
99
100   private class TemplateFactory implements CachingFileLoader.CachedFileObjectFactory {
101     public Object constructObject(InputStream aStream) {
102       try {
103         TALTemplateParser parser = new TALTemplateParser(registry);
104
105         return parser.parse(aStream, expressionParser);
106       }
107       catch (Throwable t) {
108         throw new TALFailure(t);
109       }
110     }
111   }
112 }