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.template;
29 import mir.generator.tal.interfaces.TALExpressionParser;
30 import mir.generator.tal.interfaces.TALLogger;
31 import mir.util.HTMLRoutines;
32 import mir.util.StringRoutines;
33 import mir.util.xml.XMLName;
34 import mir.util.xml.XMLParserExc;
35 import mir.util.xml.XMLReaderTool;
37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.HashMap;
40 import java.util.Iterator;
41 import java.util.List;
43 import java.util.TreeMap;
45 public class CoreTemplateNodeLibrary implements TemplateNodeLibrary {
46 private String prefix;
49 private boolean isOurTag(XMLName aName) {
50 return prefix.equals(aName.getPrefix()) || uri.equals(aName.getNamespaceURI());
53 public CoreTemplateNodeLibrary(String aPrefix, String aUri) {
58 private static final String CONDITION_ATTRIBUTE = "condition";
59 private static final String REPEAT_ATTRIBUTE = "repeat";
60 private static final String CONTENT_ATTRIBUTE = "content";
61 private static final String ERROR_ATTRIBUTE = "on-error";
62 private static final String REPLACE_ATTRIBUTE = "replace";
63 private static final String DEFINITION_ATTRIBUTE = "define";
64 private static final String OMITTAG_ATTRIBUTE = "omit-tag";
65 private static final String ATTRIBUTE_ATTRIBUTE = "attributes";
67 public TemplateNode constructTemplateNode(TALExpressionParser aParser, XMLName aTag, Map anAttributes,
68 TemplateNode aChildTemplateNode, Map aTemplateContext) throws XMLParserExc {
69 TALBasicTemplateNode result = new TALBasicTemplateNode(XMLReaderTool.normalizeXMLName(aTag));
70 result.setBody(aChildTemplateNode);
73 result.setOmitTag(aParser.preparseTRUE());
75 Iterator i = anAttributes.entrySet().iterator();
77 Map.Entry entry = (Map.Entry) i.next();
78 XMLName name = (XMLName) entry.getKey();
80 if (!isOurTag(name)) {
81 result.addFixedAttribute(XMLReaderTool.normalizeXMLName(name), (String) entry.getValue());
84 if (name.getLocalName().equalsIgnoreCase(DEFINITION_ATTRIBUTE)) {
85 List definitions = StringRoutines.splitStringWithEscape((String) entry.getValue(), ';', '\\');
87 Iterator j = definitions.iterator();
90 List parts = StringRoutines.separateString((String) j.next(), " ");
92 if (parts.size()==2) {
93 result.addDefinition(aParser.preparseReferenceExpression((String) parts.get(0)), aParser.preparseExpression((String) parts.get(1)));
97 else if (name.getLocalName().equalsIgnoreCase(CONDITION_ATTRIBUTE)) {
98 result.setCondition(aParser.preparseBooleanExpression((String) entry.getValue()));
100 else if (name.getLocalName().equalsIgnoreCase(CONTENT_ATTRIBUTE)) {
101 result.setContent(aParser.preparseStringExpression((String) entry.getValue()));
103 else if (name.getLocalName().equalsIgnoreCase(ERROR_ATTRIBUTE)) {
104 result.setError(aParser.preparseStringExpression((String) entry.getValue()));
106 else if (name.getLocalName().equalsIgnoreCase(OMITTAG_ATTRIBUTE)) {
107 if (((String) entry.getValue()).trim().length()==0)
108 result.setOmitTag(aParser.preparseTRUE());
110 result.setOmitTag(aParser.preparseBooleanExpression((String) entry.getValue()));
112 else if (name.getLocalName().equalsIgnoreCase(REPLACE_ATTRIBUTE)) {
113 result.setOmitTag(aParser.preparseTRUE());
114 result.setContent(aParser.preparseStringExpression((String) entry.getValue()));
116 else if (name.getLocalName().equalsIgnoreCase(REPEAT_ATTRIBUTE)) {
117 List parts = StringRoutines.separateString((String) entry.getValue(), " ");
119 if (parts.size()==2) {
120 result.setRepeat(aParser.preparseReferenceExpression((String) parts.get(0)), aParser.preparseExpression((String) parts.get(1)));
123 else if (name.getLocalName().equalsIgnoreCase(ATTRIBUTE_ATTRIBUTE)) {
124 List attributes = StringRoutines.splitStringWithEscape((String) entry.getValue(), ';', '\\');
126 Iterator j = attributes.iterator();
127 while (j.hasNext()) {
128 String value = (String) j.next();
129 List parts = StringRoutines.separateString(value.trim(), " ");
131 if (parts.size()==2) {
132 result.addModifiedAttribute((String) parts.get(0), aParser.preparseExpression((String) parts.get(1)));
135 throw new XMLParserExc(ATTRIBUTE_ATTRIBUTE + " tag should have exactly 2 parts ("+value+")");
145 public static class TALBasicTemplateNode implements TemplateNode {
147 private Map fixedAttributes;
148 private Map attributeModifiers;
150 private List definitions;
151 private Object condition;
153 private Object repeatVariable;
154 private Object repeatExpression;
155 private Object contentExpression;
156 private Object omitTagExpression;
157 private Object errorExpression;
159 private TemplateNode body;
161 public TALBasicTemplateNode(String aTag) {
164 fixedAttributes = new HashMap();
165 attributeModifiers = new HashMap();
167 definitions = new ArrayList();
170 repeatVariable = null;
171 repeatExpression = null;
172 contentExpression = null;
173 omitTagExpression = null;
178 public void setBody(TemplateNode aBody) {
182 public void addFixedAttribute(String aKey, String aValue) {
183 fixedAttributes.put(aKey, aValue);
186 public void addModifiedAttribute(String aKey, Object aValue) {
187 attributeModifiers.put(aKey, aValue);
190 public void addDefinition(Object aKey, Object aValue) {
191 definitions.add(new Definition(aKey, aValue));
194 public void setCondition(Object aCondition) {
195 condition = aCondition;
198 public void setRepeat(Object aRepeatVariable, Object aRepeatExpression) {
199 repeatVariable = aRepeatVariable;
200 repeatExpression = aRepeatExpression;
203 public void setContent(Object aContentExpression) {
204 contentExpression = aContentExpression;
207 public void setOmitTag(Object anOmitTagExpression) {
208 omitTagExpression = anOmitTagExpression;
211 public void setError(Object anErrorExpression) {
212 errorExpression = anErrorExpression;
215 public static class Definition {
216 private Object variable;
217 private Object expression;
219 public Definition(Object aVariable, Object anExpression) {
220 variable = aVariable;
221 expression = anExpression;
224 public Object getVariable() {
228 public Object getExpression() {
233 public void process(TALExpressionParser aParser, Object aContext,
234 StringBuffer aDestination, TALLogger aLogger, Map aTemplateContext,
235 TemplateLibrary aLibrary) throws TemplateProcessingException {
236 if (errorExpression != null) {
237 StringBuffer destination = new StringBuffer();
240 outerProcess(aParser, aContext, destination, aLogger, aTemplateContext, aLibrary);
242 catch (Throwable t) {
244 // destination.delete(0, destination.length());
245 aParser.processPseudoAssignment(aContext, "exception", t);
246 destination.insert(0, aParser.evaluateStringExpression(aContext, errorExpression));
247 destination.append(" >>>ERROR POSITION<<< ");
249 catch (Throwable s) {
250 throw new TemplateProcessingException(s);
254 aDestination.append(destination);
258 outerProcess(aParser, aContext, aDestination, aLogger, aTemplateContext, aLibrary);
262 public String getPlainText() {
263 return body.getPlainText();
266 public void outerProcess(TALExpressionParser aParser, Object aContext,
267 StringBuffer aDestination, TALLogger aLogger, Map aTemplateContext,
268 TemplateLibrary aLibrary) throws TemplateProcessingException {
270 Object oldAttributes = aParser.evaluatePseudoVariable(aContext, "tagattributes");
271 aParser.processPseudoAssignment(aContext, "tagattributes", Collections.unmodifiableMap(fixedAttributes));
273 Object oldContent = aParser.evaluatePseudoVariable(aContext, "tagcontent");
274 aParser.processPseudoAssignment(aContext, "tagcontent", body.getPlainText());
279 i = definitions.iterator();
280 while (i.hasNext()) {
281 Definition d = (Definition) i.next();
282 aParser.processAssignment(aContext, d.getVariable(), d.getExpression());
285 if (condition == null || aParser.evaluateBooleanExpression(aContext, condition)) {
286 if (repeatExpression != null) {
287 i = aParser.evaluateListExpression(aContext, repeatExpression);
289 while (i.hasNext()) {
290 aParser.processDirectAssignment(aContext, repeatVariable, i.next());
291 innerProcess(aParser, aContext, aDestination, aLogger, aTemplateContext, aLibrary);
295 innerProcess(aParser, aContext, aDestination, aLogger, aTemplateContext, aLibrary);
301 aParser.processPseudoAssignment(aContext, "tagattributes", oldAttributes);
302 aParser.processPseudoAssignment(aContext, "tagcontent", oldContent);
304 catch (Throwable t) {
309 private void innerProcess(TALExpressionParser aParser, Object aContext,
310 StringBuffer aDestination, TALLogger aLogger, Map aTemplateContext, TemplateLibrary aLibrary)
311 throws TemplateProcessingException {
313 boolean omitTag = false;
314 StringBuffer content = aDestination;
316 if (omitTagExpression != null)
317 omitTag = aParser.evaluateBooleanExpression(aContext, omitTagExpression);
320 content = new StringBuffer();
321 Map generatedAttributes = new TreeMap(fixedAttributes);
323 Iterator i = attributeModifiers.entrySet().iterator();
324 while (i.hasNext()) {
325 Map.Entry entry = (Map.Entry) i.next();
327 generatedAttributes.put(entry.getKey(), aParser.evaluateStringExpression(aContext, entry.getValue()));
330 aDestination.append("<");
331 aDestination.append(tag);
333 i = generatedAttributes.entrySet().iterator();
334 while (i.hasNext()) {
335 Map.Entry entry = (Map.Entry) i.next();
336 aDestination.append(" ");
337 aDestination.append(entry.getKey());
338 aDestination.append("=");
339 aDestination.append("\"");
340 aDestination.append(HTMLRoutines.encodeHTML( (String) entry.getValue()));
341 aDestination.append("\"");
346 if (contentExpression != null) {
347 content.append(aParser.evaluateStringExpression(aContext, contentExpression));
351 body.process(aParser, aContext, content, aLogger, aTemplateContext, aLibrary);
355 if (content.length()==0) {
356 aDestination.append(" />");
359 aDestination.append(">");
360 aDestination.append(content);
361 aDestination.append("</");
362 aDestination.append(tag);
363 aDestination.append(">");
367 catch (Throwable t) {
369 aDestination.append(content);
372 if (t instanceof TemplateProcessingException) {
373 throw (TemplateProcessingException) t;
375 else if (t instanceof RuntimeException) {
376 throw (RuntimeException) t;
378 else throw new TemplateProcessingException(t.toString());