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
30 package mir.generator.tal;
\r
32 import java.io.InputStream;
\r
33 import java.util.Iterator;
\r
34 import java.util.List;
\r
35 import java.util.Map;
\r
37 import mir.generator.tal.interfaces.TALExpressionParser;
\r
38 import mir.util.HTMLRoutines;
\r
39 import mir.util.StringRoutines;
\r
40 import mir.util.XMLReader;
\r
41 import mir.util.XMLReader.AbstractSectionHandler;
\r
43 public class TALTemplateParser {
\r
45 private static final String TAL_PREFIX = "tal";
\r
47 public static TALTemplate parseString(String aData, TALExpressionParser aParser) throws TALExc, TALFailure {
\r
49 TALHandler handler = new TALHandler(aParser);
\r
50 XMLReader reader = new XMLReader(false);
\r
52 reader.parseString(aData, handler);
\r
54 return new TALTemplate(aParser, handler.getNode());
\r
56 catch (Throwable t) {
\r
57 throw new TALFailure(t);
\r
61 public static TALTemplate parseInputStream(InputStream anInputStream, TALExpressionParser aParser) throws TALExc, TALFailure {
\r
63 TALHandler handler = new TALHandler(aParser);
\r
64 XMLReader reader = new XMLReader(false);
\r
66 reader.parseInputStream(anInputStream, handler);
\r
68 return new TALTemplate(aParser, handler.getNode());
\r
70 catch (Throwable t) {
\r
71 throw new TALFailure(t);
\r
75 private static final String CONDITION_ATTRIBUTE = "tal:condition";
\r
76 private static final String REPEAT_ATTRIBUTE = "tal:repeat";
\r
77 private static final String CONTENT_ATTRIBUTE = "tal:content";
\r
78 private static final String ERROR_ATTRIBUTE = "tal:on-error";
\r
79 private static final String REPLACE_ATTRIBUTE = "tal:replace";
\r
80 private static final String DEFINITION_ATTRIBUTE = "tal:define";
\r
81 private static final String ONERROR_ATTRIBUTE = "tal:onerror";
\r
82 private static final String OMITTAG_ATTRIBUTE = "tal:omit-tag";
\r
83 private static final String ATTRIBUTE_ATTRIBUTE = "tal:attributes";
\r
86 protected static class TALHandler extends XMLReader.AbstractSectionHandler {
\r
87 private TALTemplate.CompositeTemplateNode compositeNode;
\r
88 private StringBuffer data;
\r
89 private TALExpressionParser parser;
\r
90 private TALTemplate.SmartTemplateNode smartNode;
\r
91 private boolean smartTag;
\r
92 private String currentTag;
\r
94 public TALHandler(TALExpressionParser aParser) {
\r
96 data = new StringBuffer();
\r
97 compositeNode = new TALTemplate.CompositeTemplateNode();
\r
100 private void flushData() {
\r
101 if (data.length()!=0) {
\r
102 compositeNode.appendSubNode(new TALTemplate.PlainTextTemplateNode(data.toString()));
\r
103 data.delete(0, data.length());
\r
107 public XMLReader.SectionHandler startElement(XMLReader.XMLName aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
\r
110 currentTag = aTag.getLocalName();
\r
111 if (aTag.getPrefix().length()>0)
\r
112 currentTag = aTag.getPrefix() + ":" + currentTag;
\r
114 smartTag = (aTag.getPrefix().equals(TAL_PREFIX));
\r
116 Iterator i = anAttributes.keySet().iterator();
\r
118 while (!smartTag && i.hasNext()) {
\r
119 XMLReader.XMLName name = (XMLReader.XMLName) i.next();
\r
120 smartTag = smartTag || (name.getPrefix().equals(TAL_PREFIX));
\r
124 appendCode("<"+currentTag);
\r
125 i = anAttributes.entrySet().iterator();
\r
127 while (i.hasNext()) {
\r
128 Map.Entry entry = (Map.Entry) i.next();
\r
130 appendCode(" "+ (String) entry.getKey());
\r
132 appendText((String) entry.getValue());
\r
138 smartNode = new TALTemplate.SmartTemplateNode(currentTag);
\r
139 if (aTag.getPrefix().equals(TAL_PREFIX))
\r
140 smartNode.setOmitTag(parser.preparseTRUE());
\r
142 i = anAttributes.entrySet().iterator();
\r
143 while (i.hasNext()) {
\r
144 Map.Entry entry = (Map.Entry) i.next();
\r
145 XMLReader.XMLName name = (XMLReader.XMLName) entry.getKey();
\r
147 if (!name.getPrefix().equals(TAL_PREFIX)) {
\r
148 smartNode.addFixedAttribute(name.getLocalName(), (String) entry.getValue());
\r
151 if (name.getLocalName().equalsIgnoreCase(DEFINITION_ATTRIBUTE)) {
\r
152 List definitions = StringRoutines.splitStringWithEscape((String) entry.getValue(), ';', '\\');
\r
154 Iterator j = definitions.iterator(); {
\r
155 List parts = StringRoutines.separateString((String) i.next(), " ");
\r
157 if (parts.size()==2) {
\r
158 smartNode.addDefinition(parser.preparseReferenceExpression((String) parts.get(0)), parser.preparseExpression((String) parts.get(1)));
\r
162 else if (name.getLocalName().equalsIgnoreCase(CONDITION_ATTRIBUTE)) {
\r
163 smartNode.setCondition(parser.preparseBooleanExpression((String) entry.getValue()));
\r
165 else if (name.getLocalName().equalsIgnoreCase(CONTENT_ATTRIBUTE)) {
\r
166 smartNode.setContent(parser.preparseStringExpression((String) entry.getValue()));
\r
168 else if (name.getLocalName().equalsIgnoreCase(ERROR_ATTRIBUTE)) {
\r
169 smartNode.setError(parser.preparseStringExpression((String) entry.getValue()));
\r
171 else if (name.getLocalName().equalsIgnoreCase(OMITTAG_ATTRIBUTE)) {
\r
172 if (((String) entry.getValue()).trim().length()==0)
\r
173 smartNode.setOmitTag(parser.preparseTRUE());
\r
175 smartNode.setOmitTag(parser.preparseBooleanExpression((String) entry.getValue()));
\r
177 else if (name.getLocalName().equalsIgnoreCase(REPLACE_ATTRIBUTE)) {
\r
178 smartNode.setOmitTag(parser.preparseTRUE());
\r
179 smartNode.setContent(parser.preparseStringExpression((String) entry.getValue()));
\r
181 else if (name.getLocalName().equalsIgnoreCase(REPEAT_ATTRIBUTE)) {
\r
182 List parts = StringRoutines.separateString((String) entry.getValue(), " ");
\r
184 if (parts.size()==2) {
\r
185 smartNode.setRepeat(parser.preparseReferenceExpression((String) parts.get(0)), parser.preparseExpression((String) parts.get(1)));
\r
188 else if (name.getLocalName().equalsIgnoreCase(ATTRIBUTE_ATTRIBUTE)) {
\r
189 List attributes = StringRoutines.splitStringWithEscape((String) entry.getValue(), ';', '\\');
\r
191 Iterator j = attributes.iterator(); {
\r
192 List parts = StringRoutines.separateString((String) i.next(), " ");
\r
194 if (parts.size()==2) {
\r
195 smartNode.addModifiedAttribute((String) parts.get(0), parser.preparseExpression((String) parts.get(1)));
\r
205 return new TALHandler(parser);
\r
208 public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
\r
210 appendSubNode(((TALHandler) aHandler).getNode());
\r
211 appendCode("</"+currentTag+">");
\r
214 smartNode.setBody(((TALHandler) aHandler).getNode());
\r
215 appendSubNode(smartNode);
\r
220 protected void appendSubNode(TALTemplate.TemplateNode aNode) {
\r
221 compositeNode.appendSubNode(aNode);
\r
224 protected void appendCode(String aCode) {
\r
225 data.append(aCode);
\r
228 protected void appendText(String aText) {
\r
229 data.append(HTMLRoutines.encodeHTML(aText));
\r
232 public void finishSection() throws XMLReader.XMLReaderExc {
\r
236 public TALTemplate.TemplateNode getNode() {
\r
237 return compositeNode;
\r
240 public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
\r
241 appendText(aCharacters);
\r