X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmir%2Fgenerator%2Ftal%2FTALTemplateParser.java;h=9c06c61bde7e8523b16071fbaa1a7f4b1bd3d6d4;hb=HEAD;hp=f82ec6afe263a52d00fe94e3b673e560ad4fb032;hpb=444b653747131dbc1ed003f3efcf11c0481c8480;p=mir.git diff --git a/source/mir/generator/tal/TALTemplateParser.java b/source/mir/generator/tal/TALTemplateParser.java index f82ec6af..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,46 +18,53 @@ * 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. */ package mir.generator.tal; +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.StringReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.BufferedInputStream; -import java.io.FileNotFoundException; +import java.util.HashMap; import java.util.Iterator; -import java.util.List; 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.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.StringRoutines; -import mir.util.xml.XMLParserExc; -import mir.util.xml.XMLParserEngine; -import mir.util.xml.XMLParserFailure; import mir.util.xml.SectionHandler; import mir.util.xml.XMLName; +import mir.util.xml.XMLParserEngine; +import mir.util.xml.XMLParserExc; +import mir.util.xml.XMLParserFailure; +import mir.util.xml.XMLReaderTool; public class TALTemplateParser { - private static final String TAL_PREFIX = "tal"; + private TemplateNodeLibraryRegistry registry; + + public TALTemplateParser(TemplateNodeLibraryRegistry aRegistry) { + registry = aRegistry; + } - public static 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 static 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); } @@ -65,12 +72,13 @@ public class TALTemplateParser { throw new TALFailure(e); } } - public static 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 static 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); @@ -79,44 +87,33 @@ public class TALTemplateParser { throw new TALFailure(e); } - return new TALTemplate(aParser, handler.getNode()); + return new Template(aParser, handler.getNode(), templateContext); } - private static String normalizeXMLName(mir.util.xml.XMLName aName) { - String result = aName.getLocalName(); - if (aName.getPrefix().length() > 0) - result = aName.getPrefix() + ":" + result; - - return result; - } - - private static final String CONDITION_ATTRIBUTE = "condition"; - private static final String REPEAT_ATTRIBUTE = "repeat"; - private static final String CONTENT_ATTRIBUTE = "content"; - private static final String ERROR_ATTRIBUTE = "on-error"; - private static final String REPLACE_ATTRIBUTE = "replace"; - private static final String DEFINITION_ATTRIBUTE = "define"; - private static final String OMITTAG_ATTRIBUTE = "omit-tag"; - private static final String ATTRIBUTE_ATTRIBUTE = "attributes"; - - protected static class TALHandler implements SectionHandler { - private TALTemplate.CompositeTemplateNode compositeNode; + protected class TALHandler implements SectionHandler { + private CompositeTemplateNode compositeNode; private StringBuffer data; + private StringBuffer plainData; private TALExpressionParser parser; - private TALTemplate.SmartTemplateNode smartNode; - private boolean smartTag; private String currentTag; + 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(); - compositeNode = new TALTemplate.CompositeTemplateNode(); + plainData = new StringBuffer(); + compositeNode = new CompositeTemplateNode(); + templateContext = aTemplateContext; } private void flushData() { if (data.length()!=0) { - compositeNode.appendSubNode(new TALTemplate.PlainTextTemplateNode(data.toString())); + compositeNode.appendSubNode(new PlainTextTemplateNode(data.toString(), plainData.toString())); data.delete(0, data.length()); + plainData.delete(0, plainData.length()); } } @@ -124,123 +121,75 @@ public class TALTemplateParser { appendCode(anExtraData); } - public SectionHandler startElement(mir.util.xml.XMLName aTag, Map anAttributes) throws XMLParserExc { - smartTag = false; + public TemplateNodeLibrary findLibrary(XMLName aName) { + TemplateNodeLibrary result = null; - currentTag = normalizeXMLName(aTag); - smartTag = (aTag.getPrefix().equals(TAL_PREFIX)); + if (aName.getNamespaceURI()!=null) { + result = registry.findLibraryForUrl(aName.getNamespaceURI()); + } + + if ((result == null) && (aName.getPrefix()!=null) && (aName.getPrefix().length()>0)) { + result = registry.findLibraryForPrefix(aName.getPrefix()); + } + + return result; + } + + public SectionHandler startElement(mir.util.xml.XMLName aTag, Map anAttributes) throws XMLParserExc { + library = findLibrary(aTag); Iterator i = anAttributes.keySet().iterator(); - while (!smartTag && i.hasNext()) { - XMLName name = (XMLName) i.next(); - smartTag = smartTag || (name.getPrefix().equals(TAL_PREFIX)); + while (library==null && i.hasNext()) { + library=findLibrary((XMLName) i.next()); } - if (!smartTag) { + currentTag = XMLReaderTool.normalizeXMLName(aTag); + + if (library == null) { appendCode("<"+currentTag); i = anAttributes.entrySet().iterator(); while (i.hasNext()) { Map.Entry entry = (Map.Entry) i.next(); - appendCode(" "+ normalizeXMLName((XMLName) entry.getKey())); + appendCode(" "+ XMLReaderTool.normalizeXMLName((XMLName) entry.getKey())); appendCode("=\""); appendText((String) entry.getValue()); appendCode("\""); } - appendCode(">"); } else { - smartNode = new TALTemplate.SmartTemplateNode(currentTag); - if (aTag.getPrefix().equals(TAL_PREFIX)) - smartNode.setOmitTag(parser.preparseTRUE()); - - i = anAttributes.entrySet().iterator(); - while (i.hasNext()) { - Map.Entry entry = (Map.Entry) i.next(); - XMLName name = (XMLName) entry.getKey(); - - if (!name.getPrefix().equals(TAL_PREFIX)) { - smartNode.addFixedAttribute(normalizeXMLName(name), (String) entry.getValue()); - } - else { - if (name.getLocalName().equalsIgnoreCase(DEFINITION_ATTRIBUTE)) { - List definitions = StringRoutines.splitStringWithEscape((String) entry.getValue(), ';', '\\'); - - Iterator j = definitions.iterator(); - while (j.hasNext()) - { - List parts = StringRoutines.separateString((String) j.next(), " "); - - if (parts.size()==2) { - smartNode.addDefinition(parser.preparseReferenceExpression((String) parts.get(0)), parser.preparseExpression((String) parts.get(1))); - } - } - } - else if (name.getLocalName().equalsIgnoreCase(CONDITION_ATTRIBUTE)) { - smartNode.setCondition(parser.preparseBooleanExpression((String) entry.getValue())); - } - else if (name.getLocalName().equalsIgnoreCase(CONTENT_ATTRIBUTE)) { - smartNode.setContent(parser.preparseStringExpression((String) entry.getValue())); - } - else if (name.getLocalName().equalsIgnoreCase(ERROR_ATTRIBUTE)) { - smartNode.setError(parser.preparseStringExpression((String) entry.getValue())); - } - else if (name.getLocalName().equalsIgnoreCase(OMITTAG_ATTRIBUTE)) { - if (((String) entry.getValue()).trim().length()==0) - smartNode.setOmitTag(parser.preparseTRUE()); - else - smartNode.setOmitTag(parser.preparseBooleanExpression((String) entry.getValue())); - } - else if (name.getLocalName().equalsIgnoreCase(REPLACE_ATTRIBUTE)) { - smartNode.setOmitTag(parser.preparseTRUE()); - smartNode.setContent(parser.preparseStringExpression((String) entry.getValue())); - } - else if (name.getLocalName().equalsIgnoreCase(REPEAT_ATTRIBUTE)) { - List parts = StringRoutines.separateString((String) entry.getValue(), " "); - - if (parts.size()==2) { - smartNode.setRepeat(parser.preparseReferenceExpression((String) parts.get(0)), parser.preparseExpression((String) parts.get(1))); - } - } - else if (name.getLocalName().equalsIgnoreCase(ATTRIBUTE_ATTRIBUTE)) { - List attributes = StringRoutines.splitStringWithEscape((String) entry.getValue(), ';', '\\'); - - Iterator j = attributes.iterator(); - while (j.hasNext()) { - String value = (String) j.next(); - List parts = StringRoutines.separateString(value, " "); - - if (parts.size()==2) { - smartNode.addModifiedAttribute((String) parts.get(0), parser.preparseExpression((String) parts.get(1))); - } - else { - throw new XMLParserExc(ATTRIBUTE_ATTRIBUTE + " tag should have exactly 2 parts ("+value+")"); - } - } - } - } - } + tag = aTag; + attributes = anAttributes; } - flushData(); - - return new TALHandler(parser); - }; + return new TALHandler(parser, templateContext); + } public void endElement(mir.util.xml.SectionHandler aHandler) throws XMLParserExc { - if (!smartTag) { - appendSubNode(((TALHandler) aHandler).getNode()); - appendCode(""); + if (library == null) { + TemplateNode subNode = ((TALHandler) aHandler).getNode(); + if (subNode instanceof CompositeTemplateNode && + ((CompositeTemplateNode) subNode).isEmpty()) { + appendCode(" />"); + } + else { + appendCode(">"); + appendSubNode(subNode); + appendCode(""); + } } else { - smartNode.setBody(((TALHandler) aHandler).getNode()); - appendSubNode(smartNode); - smartNode=null; + appendSubNode( + library.constructTemplateNode(parser, tag, attributes, ((TALHandler) aHandler).getNode(), templateContext)); + tag = null; + attributes = null; } - }; + } + + protected void appendSubNode(TemplateNode aNode) { + flushData(); - protected void appendSubNode(TALTemplate.TemplateNode aNode) { compositeNode.appendSubNode(aNode); } @@ -250,13 +199,14 @@ public class TALTemplateParser { protected void appendText(String aText) { data.append(HTMLRoutines.encodeHTML(aText)); + plainData.append(aText); } public void finishSection() throws XMLParserExc { flushData(); } - public TALTemplate.TemplateNode getNode() { + public TemplateNode getNode() { return compositeNode; }