2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\r
6 * Mir is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 2 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Mir is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with Mir; if not, write to the Free Software
\r
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 * In addition, as a special exception, The Mir-coders gives permission to link
\r
21 * the code of this program with any library licensed under the Apache Software License,
\r
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
\r
23 * (or with modified versions of the above that use the same license as the above),
\r
24 * and distribute linked combinations including the two. You must obey the
\r
25 * GNU General Public License in all respects for all of the code used other than
\r
26 * the above mentioned libraries. If you modify this file, you may extend this
\r
27 * exception to your version of the file, but you are not obligated to do so.
\r
28 * If you do not wish to do so, delete this exception statement from your version.
\r
31 package mir.generator.tal.template;
\r
34 import java.util.Map;
\r
35 import java.util.List;
\r
36 import java.util.HashMap;
\r
37 import java.util.Vector;
\r
38 import java.util.Iterator;
\r
40 import mir.generator.tal.interfaces.TALExpressionParser;
\r
41 import mir.generator.tal.interfaces.TALLogger;
\r
42 import mir.util.HTMLRoutines;
\r
43 import mir.util.StringRoutines;
\r
44 import mir.util.xml.XMLName;
\r
45 import mir.util.xml.XMLParserExc;
\r
46 import mir.util.xml.XMLReaderTool;
\r
48 public class TALBasicTemplateNodeLibrary implements TALTemplateNodeLibrary {
\r
49 private String prefix;
\r
52 private boolean isOurTag(XMLName aName) {
\r
53 return prefix.equals(aName.getPrefix()) || uri.equals(aName.getNamespaceURI());
\r
56 public TALBasicTemplateNodeLibrary(String aPrefix, String aUri) {
\r
61 private static final String CONDITION_ATTRIBUTE = "condition";
\r
62 private static final String REPEAT_ATTRIBUTE = "repeat";
\r
63 private static final String CONTENT_ATTRIBUTE = "content";
\r
64 private static final String ERROR_ATTRIBUTE = "on-error";
\r
65 private static final String REPLACE_ATTRIBUTE = "replace";
\r
66 private static final String DEFINITION_ATTRIBUTE = "define";
\r
67 private static final String OMITTAG_ATTRIBUTE = "omit-tag";
\r
68 private static final String ATTRIBUTE_ATTRIBUTE = "attributes";
\r
70 public TemplateNode constructTemplateNode(TALExpressionParser aParser, XMLName aTag, Map anAttributes, TemplateNode aChildTemplateNode) throws XMLParserExc {
\r
71 TALBasicTemplateNode result = new TALBasicTemplateNode(XMLReaderTool.normalizeXMLName(aTag));
\r
72 result.setBody(aChildTemplateNode);
\r
75 result.setOmitTag(aParser.preparseTRUE());
\r
77 Iterator i = anAttributes.entrySet().iterator();
\r
78 while (i.hasNext()) {
\r
79 Map.Entry entry = (Map.Entry) i.next();
\r
80 XMLName name = (XMLName) entry.getKey();
\r
82 if (!isOurTag(name)) {
\r
83 result.addFixedAttribute(XMLReaderTool.normalizeXMLName(name), (String) entry.getValue());
\r
86 if (name.getLocalName().equalsIgnoreCase(DEFINITION_ATTRIBUTE)) {
\r
87 List definitions = StringRoutines.splitStringWithEscape((String) entry.getValue(), ';', '\\');
\r
89 Iterator j = definitions.iterator();
\r
92 List parts = StringRoutines.separateString((String) j.next(), " ");
\r
94 if (parts.size()==2) {
\r
95 result.addDefinition(aParser.preparseReferenceExpression((String) parts.get(0)), aParser.preparseExpression((String) parts.get(1)));
\r
99 else if (name.getLocalName().equalsIgnoreCase(CONDITION_ATTRIBUTE)) {
\r
100 result.setCondition(aParser.preparseBooleanExpression((String) entry.getValue()));
\r
102 else if (name.getLocalName().equalsIgnoreCase(CONTENT_ATTRIBUTE)) {
\r
103 result.setContent(aParser.preparseStringExpression((String) entry.getValue()));
\r
105 else if (name.getLocalName().equalsIgnoreCase(ERROR_ATTRIBUTE)) {
\r
106 result.setError(aParser.preparseStringExpression((String) entry.getValue()));
\r
108 else if (name.getLocalName().equalsIgnoreCase(OMITTAG_ATTRIBUTE)) {
\r
109 if (((String) entry.getValue()).trim().length()==0)
\r
110 result.setOmitTag(aParser.preparseTRUE());
\r
112 result.setOmitTag(aParser.preparseBooleanExpression((String) entry.getValue()));
\r
114 else if (name.getLocalName().equalsIgnoreCase(REPLACE_ATTRIBUTE)) {
\r
115 result.setOmitTag(aParser.preparseTRUE());
\r
116 result.setContent(aParser.preparseStringExpression((String) entry.getValue()));
\r
118 else if (name.getLocalName().equalsIgnoreCase(REPEAT_ATTRIBUTE)) {
\r
119 List parts = StringRoutines.separateString((String) entry.getValue(), " ");
\r
121 if (parts.size()==2) {
\r
122 result.setRepeat(aParser.preparseReferenceExpression((String) parts.get(0)), aParser.preparseExpression((String) parts.get(1)));
\r
125 else if (name.getLocalName().equalsIgnoreCase(ATTRIBUTE_ATTRIBUTE)) {
\r
126 List attributes = StringRoutines.splitStringWithEscape((String) entry.getValue(), ';', '\\');
\r
128 Iterator j = attributes.iterator();
\r
129 while (j.hasNext()) {
\r
130 String value = (String) j.next();
\r
131 List parts = StringRoutines.separateString(value, " ");
\r
133 if (parts.size()==2) {
\r
134 result.addModifiedAttribute((String) parts.get(0), aParser.preparseExpression((String) parts.get(1)));
\r
137 throw new XMLParserExc(ATTRIBUTE_ATTRIBUTE + " tag should have exactly 2 parts ("+value+")");
\r
147 public static class TALBasicTemplateNode implements TemplateNode {
\r
148 private String tag;
\r
149 private Map fixedAttributes;
\r
150 private Map attributeModifiers;
\r
152 private List definitions;
\r
153 private Object condition;
\r
155 private Object repeatVariable;
\r
156 private Object repeatExpression;
\r
157 private Object contentExpression;
\r
158 private Object omitTagExpression;
\r
159 private Object errorExpression;
\r
161 private TemplateNode body;
\r
163 public TALBasicTemplateNode(String aTag) {
\r
166 fixedAttributes = new HashMap();
\r
167 attributeModifiers = new HashMap();
\r
169 definitions = new Vector();
\r
172 repeatVariable = null;
\r
173 repeatExpression = null;
\r
174 contentExpression = null;
\r
175 omitTagExpression = null;
\r
180 public void setBody(TemplateNode aBody) {
\r
184 public void addFixedAttribute(String aKey, String aValue) {
\r
185 fixedAttributes.put(aKey, aValue);
\r
188 public void addModifiedAttribute(String aKey, Object aValue) {
\r
189 attributeModifiers.put(aKey, aValue);
\r
192 public void addDefinition(Object aKey, Object aValue) {
\r
193 definitions.add(new Definition(aKey, aValue));
\r
196 public void setCondition(Object aCondition) {
\r
197 condition = aCondition;
\r
200 public void setRepeat(Object aRepeatVariable, Object aRepeatExpression) {
\r
201 repeatVariable = aRepeatVariable;
\r
202 repeatExpression = aRepeatExpression;
\r
205 public void setContent(Object aContentExpression) {
\r
206 contentExpression = aContentExpression;
\r
209 public void setOmitTag(Object anOmitTagExpression) {
\r
210 omitTagExpression = anOmitTagExpression;
\r
213 public void setError(Object anErrorExpression) {
\r
214 errorExpression = anErrorExpression;
\r
217 public static class Definition {
\r
218 private Object variable;
\r
219 private Object expression;
\r
221 public Definition(Object aVariable, Object anExpression) {
\r
222 variable = aVariable;
\r
223 expression = anExpression;
\r
226 public Object getVariable() {
\r
230 public Object getExpression() {
\r
235 public void process(TALExpressionParser aParser, Object aContext, StringBuffer aDestination, TALLogger aLogger) throws TemplateProcessingException {
\r
238 i = definitions.iterator();
\r
239 while (i.hasNext()) {
\r
240 Definition d = (Definition) i.next();
\r
241 aParser.processAssignment(aContext, d.getVariable(), d.getExpression());
\r
244 if (condition == null || aParser.evaluateBooleanExpression(aContext, condition)) {
\r
245 if (repeatExpression != null) {
\r
246 i = aParser.evaluateListExpression(aContext, repeatExpression);
\r
248 while (i.hasNext()) {
\r
249 aParser.processDirectAssignment(aContext, repeatVariable, i.next());
\r
250 innerProcess(aParser, aContext, aDestination, aLogger);
\r
254 innerProcess(aParser, aContext, aDestination, aLogger);
\r
259 private void innerProcess(TALExpressionParser aParser, Object aContext, StringBuffer aDestination, TALLogger aLogger)
\r
260 throws TemplateProcessingException
\r
262 boolean omitTag = false;
\r
263 if (omitTagExpression != null)
\r
264 omitTag = aParser.evaluateBooleanExpression(aContext, omitTagExpression);
\r
267 Map generatedAttributes = new HashMap(fixedAttributes);
\r
269 Iterator i = attributeModifiers.entrySet().iterator();
\r
270 while (i.hasNext()) {
\r
271 Map.Entry entry = (Map.Entry) i.next();
\r
273 generatedAttributes.put(entry.getKey(), aParser.evaluateStringExpression(aContext, entry.getValue()));
\r
276 aDestination.append("<");
\r
277 aDestination.append(tag);
\r
279 i = generatedAttributes.entrySet().iterator();
\r
280 while (i.hasNext()) {
\r
281 Map.Entry entry = (Map.Entry) i.next();
\r
282 aDestination.append(" ");
\r
283 aDestination.append(entry.getKey());
\r
284 aDestination.append("=");
\r
285 aDestination.append("\"");
\r
286 aDestination.append(HTMLRoutines.encodeHTML( (String) entry.getValue()));
\r
287 aDestination.append("\"");
\r
289 aDestination.append(">");
\r
292 StringBuffer destination = aDestination;
\r
293 if (errorExpression != null) {
\r
294 destination = new StringBuffer();
\r
297 if (contentExpression != null) {
\r
298 destination.append(aParser.evaluateStringExpression(aContext, contentExpression));
\r
302 body.process(aParser, aContext, destination, aLogger);
\r
305 catch (Throwable t) {
\r
306 if (!(t instanceof TemplateProcessingException))
\r
307 aLogger.error("TALTemplate.TALBasicTemplateNode.processs", "exception occurred: " + t.toString());
\r
308 if (errorExpression != null) {
\r
310 destination.delete(0, destination.length());
\r
312 aParser.processPseudoAssignment(aContext, "exception", t);
\r
313 destination.append(aParser.evaluateStringExpression(aContext, errorExpression));
\r
315 catch (Throwable s) {
\r
319 throw new TemplateProcessingException(t);
\r
323 if (errorExpression != null) {
\r
324 aDestination.append(destination);
\r
328 aDestination.append("</");
\r
329 aDestination.append(tag);
\r
330 aDestination.append(">");
\r