2 * Copyright (C) 2001, 2002 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 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23 * (or with modified versions of the above that use the same license as the above),
24 * and distribute linked combinations including the two. You must obey the
25 * GNU General Public License in all respects for all of the code used other than
26 * the above mentioned libraries. If you modify this file, you may extend this
27 * exception to your version of the file, but you are not obligated to do so.
28 * If you do not wish to do so, delete this exception statement from your version.
30 package mir.generator.tal;
32 import java.io.InputStream;
33 import java.util.Iterator;
34 import java.util.List;
37 import mir.generator.tal.interfaces.TALExpressionParser;
38 import mir.util.HTMLRoutines;
39 import mir.util.StringRoutines;
40 import mir.util.XMLReader;
41 import mir.util.XMLReader.AbstractSectionHandler;
43 public class TALTemplateParser {
45 private static final String TAL_PREFIX = "tal";
47 public static TALTemplate parseString(String aData, TALExpressionParser aParser) throws TALExc, TALFailure {
49 TALHandler handler = new TALHandler(aParser);
50 XMLReader reader = new XMLReader(false);
52 reader.parseString(true, aData, handler);
54 return new TALTemplate(aParser, handler.getNode());
57 throw new TALFailure(t);
61 public static TALTemplate parseInputStream(InputStream anInputStream, TALExpressionParser aParser) throws TALExc, TALFailure {
63 TALHandler handler = new TALHandler(aParser);
64 XMLReader reader = new XMLReader(false);
66 reader.parseInputStream(true, anInputStream, handler);
68 return new TALTemplate(aParser, handler.getNode());
71 throw new TALFailure(t);
75 private static String normalizeXMLName(XMLReader.XMLName aName) {
76 String result = aName.getLocalName();
77 if (aName.getPrefix().length() > 0)
78 result = aName.getPrefix() + ":" + result;
85 private static final String CONDITION_ATTRIBUTE = "condition";
86 private static final String REPEAT_ATTRIBUTE = "repeat";
87 private static final String CONTENT_ATTRIBUTE = "content";
88 private static final String ERROR_ATTRIBUTE = "on-error";
89 private static final String REPLACE_ATTRIBUTE = "replace";
90 private static final String DEFINITION_ATTRIBUTE = "define";
91 private static final String ONERROR_ATTRIBUTE = "onerror";
92 private static final String OMITTAG_ATTRIBUTE = "omit-tag";
93 private static final String ATTRIBUTE_ATTRIBUTE = "attributes";
96 protected static class TALHandler extends XMLReader.AbstractSectionHandler {
97 private TALTemplate.CompositeTemplateNode compositeNode;
98 private StringBuffer data;
99 private TALExpressionParser parser;
100 private TALTemplate.SmartTemplateNode smartNode;
101 private boolean smartTag;
102 private String currentTag;
104 public TALHandler(TALExpressionParser aParser) {
106 data = new StringBuffer();
107 compositeNode = new TALTemplate.CompositeTemplateNode();
110 private void flushData() {
111 if (data.length()!=0) {
112 compositeNode.appendSubNode(new TALTemplate.PlainTextTemplateNode(data.toString()));
113 data.delete(0, data.length());
117 public XMLReader.SectionHandler startElement(XMLReader.XMLName aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
120 currentTag = normalizeXMLName(aTag);
122 smartTag = (aTag.getPrefix().equals(TAL_PREFIX));
124 Iterator i = anAttributes.keySet().iterator();
126 while (!smartTag && i.hasNext()) {
127 String name = (String) i.next();
128 smartTag = smartTag || (name.startsWith(TAL_PREFIX+":"));
132 appendCode("<"+currentTag);
133 i = anAttributes.entrySet().iterator();
135 while (i.hasNext()) {
136 Map.Entry entry = (Map.Entry) i.next();
138 appendCode(" "+ entry.getKey());
140 appendText((String) entry.getValue());
146 smartNode = new TALTemplate.SmartTemplateNode(currentTag);
147 if (aTag.getPrefix().equals(TAL_PREFIX))
148 smartNode.setOmitTag(parser.preparseTRUE());
150 i = anAttributes.entrySet().iterator();
151 while (i.hasNext()) {
152 Map.Entry entry = (Map.Entry) i.next();
153 String fullAttributeName = (String) entry.getKey();
154 List attributeParts = StringRoutines.separateString(fullAttributeName, ":");
157 if (attributeParts.size()==2) {
158 name = (String) attributeParts.get(1);
159 prefix = (String) attributeParts.get(0);
162 name = (String) attributeParts.get(0);
165 if (!prefix.equals(TAL_PREFIX)) {
166 smartNode.addFixedAttribute(fullAttributeName, (String) entry.getValue());
169 if (name.equalsIgnoreCase(DEFINITION_ATTRIBUTE)) {
170 List definitions = StringRoutines.splitStringWithEscape((String) entry.getValue(), ';', '\\');
172 Iterator j = definitions.iterator(); {
173 List parts = StringRoutines.separateString((String) i.next(), " ");
175 if (parts.size()==2) {
176 smartNode.addDefinition(parser.preparseReferenceExpression((String) parts.get(0)), parser.preparseExpression((String) parts.get(1)));
180 else if (name.equalsIgnoreCase(CONDITION_ATTRIBUTE)) {
181 smartNode.setCondition(parser.preparseBooleanExpression((String) entry.getValue()));
183 else if (name.equalsIgnoreCase(CONTENT_ATTRIBUTE)) {
184 smartNode.setContent(parser.preparseStringExpression((String) entry.getValue()));
186 else if (name.equalsIgnoreCase(ERROR_ATTRIBUTE)) {
187 smartNode.setError(parser.preparseStringExpression((String) entry.getValue()));
189 else if (name.equalsIgnoreCase(OMITTAG_ATTRIBUTE)) {
190 if (((String) entry.getValue()).trim().length()==0)
191 smartNode.setOmitTag(parser.preparseTRUE());
193 smartNode.setOmitTag(parser.preparseBooleanExpression((String) entry.getValue()));
195 else if (name.equalsIgnoreCase(REPLACE_ATTRIBUTE)) {
196 smartNode.setOmitTag(parser.preparseTRUE());
197 smartNode.setContent(parser.preparseStringExpression((String) entry.getValue()));
199 else if (name.equalsIgnoreCase(REPEAT_ATTRIBUTE)) {
200 List parts = StringRoutines.separateString((String) entry.getValue(), " ");
202 if (parts.size()==2) {
203 smartNode.setRepeat(parser.preparseReferenceExpression((String) parts.get(0)), parser.preparseExpression((String) parts.get(1)));
206 else if (name.equalsIgnoreCase(ATTRIBUTE_ATTRIBUTE)) {
207 List attributes = StringRoutines.splitStringWithEscape((String) entry.getValue(), ';', '\\');
209 Iterator j = attributes.iterator(); {
210 List parts = StringRoutines.separateString((String) i.next(), " ");
212 if (parts.size()==2) {
213 smartNode.addModifiedAttribute((String) parts.get(0), parser.preparseExpression((String) parts.get(1)));
223 return new TALHandler(parser);
226 public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
228 appendSubNode(((TALHandler) aHandler).getNode());
229 appendCode("</"+currentTag+">");
232 smartNode.setBody(((TALHandler) aHandler).getNode());
233 appendSubNode(smartNode);
238 protected void appendSubNode(TALTemplate.TemplateNode aNode) {
239 compositeNode.appendSubNode(aNode);
242 protected void appendCode(String aCode) {
246 protected void appendText(String aText) {
247 data.append(HTMLRoutines.encodeHTML(aText));
250 public void finishSection() throws XMLReader.XMLReaderExc {
254 public TALTemplate.TemplateNode getNode() {
255 return compositeNode;
258 public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
259 appendText(aCharacters);