Added features:
[mir.git] / source / mir / generator / tal / TALTemplateParser.java
1 /*
2  * Copyright (C) 2001, 2002 The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
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.
10  *
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.
15  *
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
19  *
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.
29  */
30 package mir.generator.tal;
31
32 import java.io.InputStream;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Map;
36
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;
42
43 public class TALTemplateParser {
44   private int bal;
45   private static final String TAL_PREFIX = "tal";
46
47   public static TALTemplate parseString(String aData, TALExpressionParser aParser) throws TALExc, TALFailure {
48     try {
49       TALHandler handler = new TALHandler(aParser);
50       XMLReader reader = new XMLReader(false);
51
52       reader.parseString(true, aData, handler);
53
54       return new TALTemplate(aParser, handler.getNode());
55     }
56     catch (Throwable t) {
57       throw new TALFailure(t);
58     }
59   }
60
61   public static TALTemplate parseInputStream(InputStream anInputStream, TALExpressionParser aParser) throws TALExc, TALFailure {
62     try {
63       TALHandler handler = new TALHandler(aParser);
64       XMLReader reader = new XMLReader(false);
65
66       reader.parseInputStream(true, anInputStream, handler);
67
68       return new TALTemplate(aParser, handler.getNode());
69     }
70     catch (Throwable t) {
71       throw new TALFailure(t);
72     }
73   }
74
75   private static String normalizeXMLName(XMLReader.XMLName aName) {
76     String result = aName.getLocalName();
77     if (aName.getPrefix().length() > 0)
78       result = aName.getPrefix() + ":" + result;
79
80     return result;
81   }
82
83
84
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";
94
95
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;
103
104     public TALHandler(TALExpressionParser aParser) {
105       parser = aParser;
106       data = new StringBuffer();
107       compositeNode = new TALTemplate.CompositeTemplateNode();
108     }
109
110     private void flushData() {
111       if (data.length()!=0) {
112         compositeNode.appendSubNode(new TALTemplate.PlainTextTemplateNode(data.toString()));
113         data.delete(0, data.length());
114       }
115     }
116
117     public XMLReader.SectionHandler startElement(XMLReader.XMLName aTag, Map anAttributes) throws XMLReader.XMLReaderExc {
118       smartTag = false;
119
120       currentTag = normalizeXMLName(aTag);
121
122       smartTag = (aTag.getPrefix().equals(TAL_PREFIX));
123
124       Iterator i = anAttributes.keySet().iterator();
125
126       while (!smartTag && i.hasNext()) {
127         String name = (String) i.next();
128         smartTag = smartTag || (name.startsWith(TAL_PREFIX+":"));
129       }
130
131       if (!smartTag) {
132         appendCode("<"+currentTag);
133         i = anAttributes.entrySet().iterator();
134
135         while (i.hasNext()) {
136           Map.Entry entry = (Map.Entry) i.next();
137
138           appendCode(" "+ entry.getKey());
139           appendCode("=\"");
140           appendText((String) entry.getValue());
141           appendCode("\"");
142         }
143         appendCode(">");
144       }
145       else {
146         smartNode = new TALTemplate.SmartTemplateNode(currentTag);
147         if (aTag.getPrefix().equals(TAL_PREFIX))
148           smartNode.setOmitTag(parser.preparseTRUE());
149
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, ":");
155           String prefix = "";
156           String name = "";
157           if (attributeParts.size()==2) {
158             name = (String) attributeParts.get(1);
159             prefix = (String) attributeParts.get(0);
160           }
161           else {
162             name = (String) attributeParts.get(0);
163           }
164
165           if (!prefix.equals(TAL_PREFIX)) {
166             smartNode.addFixedAttribute(fullAttributeName, (String) entry.getValue());
167           }
168           else {
169             if (name.equalsIgnoreCase(DEFINITION_ATTRIBUTE)) {
170               List definitions = StringRoutines.splitStringWithEscape((String) entry.getValue(), ';', '\\');
171
172               Iterator j = definitions.iterator(); {
173                 List parts = StringRoutines.separateString((String) i.next(), " ");
174
175                 if (parts.size()==2) {
176                   smartNode.addDefinition(parser.preparseReferenceExpression((String) parts.get(0)), parser.preparseExpression((String) parts.get(1)));
177                 }
178               }
179             }
180             else if (name.equalsIgnoreCase(CONDITION_ATTRIBUTE)) {
181               smartNode.setCondition(parser.preparseBooleanExpression((String) entry.getValue()));
182             }
183             else if (name.equalsIgnoreCase(CONTENT_ATTRIBUTE)) {
184               smartNode.setContent(parser.preparseStringExpression((String) entry.getValue()));
185             }
186             else if (name.equalsIgnoreCase(ERROR_ATTRIBUTE)) {
187               smartNode.setError(parser.preparseStringExpression((String) entry.getValue()));
188             }
189             else if (name.equalsIgnoreCase(OMITTAG_ATTRIBUTE)) {
190               if (((String) entry.getValue()).trim().length()==0)
191                 smartNode.setOmitTag(parser.preparseTRUE());
192               else
193                 smartNode.setOmitTag(parser.preparseBooleanExpression((String) entry.getValue()));
194             }
195             else if (name.equalsIgnoreCase(REPLACE_ATTRIBUTE)) {
196               smartNode.setOmitTag(parser.preparseTRUE());
197               smartNode.setContent(parser.preparseStringExpression((String) entry.getValue()));
198             }
199             else if (name.equalsIgnoreCase(REPEAT_ATTRIBUTE)) {
200               List parts = StringRoutines.separateString((String) entry.getValue(), " ");
201
202               if (parts.size()==2) {
203                 smartNode.setRepeat(parser.preparseReferenceExpression((String) parts.get(0)), parser.preparseExpression((String) parts.get(1)));
204               }
205             }
206             else if (name.equalsIgnoreCase(ATTRIBUTE_ATTRIBUTE)) {
207               List attributes = StringRoutines.splitStringWithEscape((String) entry.getValue(), ';', '\\');
208
209               Iterator j = attributes.iterator(); {
210                 List parts = StringRoutines.separateString((String) i.next(), " ");
211
212                 if (parts.size()==2) {
213                   smartNode.addModifiedAttribute((String) parts.get(0), parser.preparseExpression((String) parts.get(1)));
214                 }
215               }
216             }
217           }
218         }
219       }
220
221       flushData();
222
223       return new TALHandler(parser);
224     };
225
226     public void endElement(XMLReader.SectionHandler aHandler) throws XMLReader.XMLReaderExc {
227       if (!smartTag) {
228         appendSubNode(((TALHandler) aHandler).getNode());
229         appendCode("</"+currentTag+">");
230       }
231       else {
232         smartNode.setBody(((TALHandler) aHandler).getNode());
233         appendSubNode(smartNode);
234         smartNode=null;
235       }
236     };
237
238     protected void appendSubNode(TALTemplate.TemplateNode aNode) {
239       compositeNode.appendSubNode(aNode);
240     }
241
242     protected void appendCode(String aCode) {
243       data.append(aCode);
244     }
245
246     protected void appendText(String aText) {
247       data.append(HTMLRoutines.encodeHTML(aText));
248     }
249
250     public void finishSection() throws XMLReader.XMLReaderExc {
251       flushData();
252     }
253
254     public TALTemplate.TemplateNode getNode() {
255       return compositeNode;
256     }
257
258     public void characters(String aCharacters) throws XMLReader.XMLReaderExc {
259       appendText(aCharacters);
260     }
261   }
262 }