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(true, 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(true, 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 String normalizeXMLName(XMLReader.XMLName aName) {
\r
76 String result = aName.getLocalName();
\r
77 if (aName.getPrefix().length() > 0)
\r
78 result = aName.getPrefix() + ":" + result;
\r
85 private static final String CONDITION_ATTRIBUTE = "condition";
\r
86 private static final String REPEAT_ATTRIBUTE = "repeat";
\r
87 private static final String CONTENT_ATTRIBUTE = "content";
\r
88 private static final String ERROR_ATTRIBUTE = "on-error";
\r
89 private static final String REPLACE_ATTRIBUTE = "replace";
\r
90 private static final String DEFINITION_ATTRIBUTE = "define";
\r
91 private static final String ONERROR_ATTRIBUTE = "onerror";
\r
92 private static final String OMITTAG_ATTRIBUTE = "omit-tag";
\r
93 private static final String ATTRIBUTE_ATTRIBUTE = "attributes";
\r
96 protected static class TALHandler extends XMLReader.AbstractSectionHandler {
\r
97 private TALTemplate.CompositeTemplateNode compositeNode;
\r
98 private StringBuffer data;
\r
99 private TALExpressionParser parser;
\r
100 private TALTemplate.SmartTemplateNode smartNode;
\r
101 private boolean smartTag;
\r
102 private String currentTag;
\r
104 public TALHandler(TALExpressionParser aParser) {
\r
106 data = new StringBuffer();
\r
107 compositeNode = new TALTemplate.CompositeTemplateNode();
\r
110 private void flushData() {
\r
111 if (data.length()!=0) {
\r
112 compositeNode.appendSubNode(new TALTemplate.PlainTextTemplateNode(data.toString()));
\r
113 data.delete(0, data.length());
\r
117 public XMLReader.SectionHandler startElement(XMLReader.XMLName aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
\r
120 currentTag = normalizeXMLName(aTag);
\r
122 smartTag = (aTag.getPrefix().equals(TAL_PREFIX));
\r
124 Iterator i = anAttributes.keySet().iterator();
\r
126 while (!smartTag && i.hasNext()) {
\r
127 String name = (String) i.next();
\r
128 smartTag = smartTag || (name.startsWith(TAL_PREFIX+":"));
\r
132 appendCode("<"+currentTag);
\r
133 i = anAttributes.entrySet().iterator();
\r
135 while (i.hasNext()) {
\r
136 Map.Entry entry = (Map.Entry) i.next();
\r
138 appendCode(" "+ entry.getKey());
\r
140 appendText((String) entry.getValue());
\r
146 smartNode = new TALTemplate.SmartTemplateNode(currentTag);
\r
147 if (aTag.getPrefix().equals(TAL_PREFIX))
\r
148 smartNode.setOmitTag(parser.preparseTRUE());
\r
150 i = anAttributes.entrySet().iterator();
\r
151 while (i.hasNext()) {
\r
152 Map.Entry entry = (Map.Entry) i.next();
\r
153 String fullAttributeName = (String) entry.getKey();
\r
154 List attributeParts = StringRoutines.separateString(fullAttributeName, ":");
\r
155 String prefix = "";
\r
157 if (attributeParts.size()==2) {
\r
158 name = (String) attributeParts.get(1);
\r
159 prefix = (String) attributeParts.get(0);
\r
162 name = (String) attributeParts.get(0);
\r
165 if (!prefix.equals(TAL_PREFIX)) {
\r
166 smartNode.addFixedAttribute(fullAttributeName, (String) entry.getValue());
\r
169 if (name.equalsIgnoreCase(DEFINITION_ATTRIBUTE)) {
\r
170 List definitions = StringRoutines.splitStringWithEscape((String) entry.getValue(), ';', '\\');
\r
172 Iterator j = definitions.iterator(); {
\r
173 List parts = StringRoutines.separateString((String) i.next(), " ");
\r
175 if (parts.size()==2) {
\r
176 smartNode.addDefinition(parser.preparseReferenceExpression((String) parts.get(0)), parser.preparseExpression((String) parts.get(1)));
\r
180 else if (name.equalsIgnoreCase(CONDITION_ATTRIBUTE)) {
\r
181 smartNode.setCondition(parser.preparseBooleanExpression((String) entry.getValue()));
\r
183 else if (name.equalsIgnoreCase(CONTENT_ATTRIBUTE)) {
\r
184 smartNode.setContent(parser.preparseStringExpression((String) entry.getValue()));
\r
186 else if (name.equalsIgnoreCase(ERROR_ATTRIBUTE)) {
\r
187 smartNode.setError(parser.preparseStringExpression((String) entry.getValue()));
\r
189 else if (name.equalsIgnoreCase(OMITTAG_ATTRIBUTE)) {
\r
190 if (((String) entry.getValue()).trim().length()==0)
\r
191 smartNode.setOmitTag(parser.preparseTRUE());
\r
193 smartNode.setOmitTag(parser.preparseBooleanExpression((String) entry.getValue()));
\r
195 else if (name.equalsIgnoreCase(REPLACE_ATTRIBUTE)) {
\r
196 smartNode.setOmitTag(parser.preparseTRUE());
\r
197 smartNode.setContent(parser.preparseStringExpression((String) entry.getValue()));
\r
199 else if (name.equalsIgnoreCase(REPEAT_ATTRIBUTE)) {
\r
200 List parts = StringRoutines.separateString((String) entry.getValue(), " ");
\r
202 if (parts.size()==2) {
\r
203 smartNode.setRepeat(parser.preparseReferenceExpression((String) parts.get(0)), parser.preparseExpression((String) parts.get(1)));
\r
206 else if (name.equalsIgnoreCase(ATTRIBUTE_ATTRIBUTE)) {
\r
207 List attributes = StringRoutines.splitStringWithEscape((String) entry.getValue(), ';', '\\');
\r
209 Iterator j = attributes.iterator(); {
\r
210 List parts = StringRoutines.separateString((String) i.next(), " ");
\r
212 if (parts.size()==2) {
\r
213 smartNode.addModifiedAttribute((String) parts.get(0), parser.preparseExpression((String) parts.get(1)));
\r
223 return new TALHandler(parser);
\r
226 public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
\r
228 appendSubNode(((TALHandler) aHandler).getNode());
\r
229 appendCode("</"+currentTag+">");
\r
232 smartNode.setBody(((TALHandler) aHandler).getNode());
\r
233 appendSubNode(smartNode);
\r
238 protected void appendSubNode(TALTemplate.TemplateNode aNode) {
\r
239 compositeNode.appendSubNode(aNode);
\r
242 protected void appendCode(String aCode) {
\r
243 data.append(aCode);
\r
246 protected void appendText(String aText) {
\r
247 data.append(HTMLRoutines.encodeHTML(aText));
\r
250 public void finishSection() throws XMLReader.XMLReaderExc {
\r
254 public TALTemplate.TemplateNode getNode() {
\r
255 return compositeNode;
\r
258 public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
\r
259 appendText(aCharacters);
\r