X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmir%2Fgenerator%2Ftal%2FTALTemplateParser.java;h=9c06c61bde7e8523b16071fbaa1a7f4b1bd3d6d4;hb=HEAD;hp=d37f95f270337980cf1f4747ef1628a84b306bb0;hpb=ca01389abc85d4858723b1415531802288900bb4;p=mir.git diff --git a/source/mir/generator/tal/TALTemplateParser.java b/source/mir/generator/tal/TALTemplateParser.java index d37f95f2..9c06c61b 100755 --- a/source/mir/generator/tal/TALTemplateParser.java +++ b/source/mir/generator/tal/TALTemplateParser.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2001, 2002 The Mir-coders group + * Copyright (C) 2005 The Mir-coders group * * This file is part of Mir. * @@ -18,12 +18,9 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * In addition, as a special exception, The Mir-coders gives permission to link - * the code of this program with any library licensed under the Apache Software License, - * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library - * (or with modified versions of the above that use the same license as the above), - * and distribute linked combinations including the two. You must obey the - * GNU General Public License in all respects for all of the code used other than - * the above mentioned libraries. If you modify this file, you may extend this + * the code of this program with any library licensed under the Apache Software License. + * You must obey the GNU General Public License in all respects for all of the code used + * other than the above mentioned libraries. If you modify this file, you may extend this * exception to your version of the file, but you are not obligated to do so. * If you do not wish to do so, delete this exception statement from your version. */ @@ -37,15 +34,17 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.StringReader; +import java.util.HashMap; import java.util.Iterator; import java.util.Map; import mir.generator.tal.interfaces.TALExpressionParser; import mir.generator.tal.template.CompositeTemplateNode; import mir.generator.tal.template.PlainTextTemplateNode; -import mir.generator.tal.template.TALTemplate; -import mir.generator.tal.template.TALTemplateNodeLibrary; +import mir.generator.tal.template.Template; import mir.generator.tal.template.TemplateNode; +import mir.generator.tal.template.TemplateNodeLibrary; +import mir.generator.tal.template.TemplateNodeLibraryRegistry; import mir.util.HTMLRoutines; import mir.util.xml.SectionHandler; import mir.util.xml.XMLName; @@ -54,21 +53,18 @@ import mir.util.xml.XMLParserExc; import mir.util.xml.XMLParserFailure; import mir.util.xml.XMLReaderTool; -// TODO: add a templatelibrary repository and remove dependency of TALTemplateEngine - public class TALTemplateParser { - private TALTemplateEngine engine; + private TemplateNodeLibraryRegistry registry; - //private static final String TAL_PREFIX = "tal"; - public TALTemplateParser(TALTemplateEngine anEngine) { - engine = anEngine; + public TALTemplateParser(TemplateNodeLibraryRegistry aRegistry) { + registry = aRegistry; } - public TALTemplate parse(String aData, TALExpressionParser aParser) throws TALExc, TALFailure { + public Template parse(String aData, TALExpressionParser aParser) throws TALExc, TALFailure { return parse(new StringReader(aData), aParser); } - public TALTemplate parse(File aFile, TALExpressionParser aParser) throws TALExc, TALFailure { + public Template parse(File aFile, TALExpressionParser aParser) throws TALExc, TALFailure { try { return parse(new BufferedInputStream(new FileInputStream(aFile), 1024*128), aParser); } @@ -76,12 +72,13 @@ public class TALTemplateParser { throw new TALFailure(e); } } - public TALTemplate parse(InputStream anInputStream, TALExpressionParser aParser) throws TALExc, TALFailure { + public Template parse(InputStream anInputStream, TALExpressionParser aParser) throws TALExc, TALFailure { return parse(new InputStreamReader(anInputStream), aParser); } - public TALTemplate parse(Reader aReader, TALExpressionParser aParser) throws TALExc, TALFailure { - TALHandler handler = new TALHandler(aParser); + public Template parse(Reader aReader, TALExpressionParser aParser) throws TALExc, TALFailure { + Map templateContext = new HashMap(); + TALHandler handler = new TALHandler(aParser, templateContext); try { XMLParserEngine.getInstance().parse("html", aReader, handler); @@ -90,28 +87,33 @@ public class TALTemplateParser { throw new TALFailure(e); } - return new TALTemplate(aParser, handler.getNode()); + return new Template(aParser, handler.getNode(), templateContext); } protected class TALHandler implements SectionHandler { private CompositeTemplateNode compositeNode; private StringBuffer data; + private StringBuffer plainData; private TALExpressionParser parser; private String currentTag; - private TALTemplateNodeLibrary library; + private TemplateNodeLibrary library; private XMLName tag; private Map attributes; + private Map templateContext; - public TALHandler(TALExpressionParser aParser) { + public TALHandler(TALExpressionParser aParser, Map aTemplateContext) { parser = aParser; data = new StringBuffer(); + plainData = new StringBuffer(); compositeNode = new CompositeTemplateNode(); + templateContext = aTemplateContext; } private void flushData() { if (data.length()!=0) { - compositeNode.appendSubNode(new PlainTextTemplateNode(data.toString())); + compositeNode.appendSubNode(new PlainTextTemplateNode(data.toString(), plainData.toString())); data.delete(0, data.length()); + plainData.delete(0, plainData.length()); } } @@ -119,15 +121,15 @@ public class TALTemplateParser { appendCode(anExtraData); } - public TALTemplateNodeLibrary findLibrary(XMLName aName) { - TALTemplateNodeLibrary result = null; + public TemplateNodeLibrary findLibrary(XMLName aName) { + TemplateNodeLibrary result = null; if (aName.getNamespaceURI()!=null) { - result = engine.getLibraryForUrl(aName.getNamespaceURI()); + result = registry.findLibraryForUrl(aName.getNamespaceURI()); } if ((result == null) && (aName.getPrefix()!=null) && (aName.getPrefix().length()>0)) { - result = engine.getLibraryForPrefix(aName.getPrefix()); + result = registry.findLibraryForPrefix(aName.getPrefix()); } return result; @@ -155,27 +157,35 @@ public class TALTemplateParser { appendText((String) entry.getValue()); appendCode("\""); } - appendCode(">"); } else { tag = aTag; attributes = anAttributes; } - return new TALHandler(parser); - }; + return new TALHandler(parser, templateContext); + } public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc { if (library == null) { - appendSubNode(((TALHandler) aHandler).getNode()); - appendCode(""); + TemplateNode subNode = ((TALHandler) aHandler).getNode(); + if (subNode instanceof CompositeTemplateNode && + ((CompositeTemplateNode) subNode).isEmpty()) { + appendCode(" />"); + } + else { + appendCode(">"); + appendSubNode(subNode); + appendCode(""); + } } else { - appendSubNode(library.constructTemplateNode(parser, tag, attributes, ((TALHandler) aHandler).getNode())); + appendSubNode( + library.constructTemplateNode(parser, tag, attributes, ((TALHandler) aHandler).getNode(), templateContext)); tag = null; attributes = null; } - }; + } protected void appendSubNode(TemplateNode aNode) { flushData(); @@ -189,6 +199,7 @@ public class TALTemplateParser { protected void appendText(String aText) { data.append(HTMLRoutines.encodeHTML(aText)); + plainData.append(aText); } public void finishSection() throws XMLParserExc {