From 46f4e47510acf7839884d7220e115e796cee74aa Mon Sep 17 00:00:00 2001 From: Evgeny Gryaznov Date: Fri, 25 Jan 2008 14:39:09 +0000 Subject: [PATCH] initial idea to generate site from templates git-svn-id: https://webim.svn.sourceforge.net/svnroot/webim/trunk@44 c66351dc-e62f-0410-b875-e3a5c0b9693f --- .../sf/webim/converter/ConverterHelper.java | 26 +++++++++++++++++++ .../net/sf/webim/converter/JspConverter.java | 16 +++++++++--- .../net.sf.webim.converter/templates/conv.ltp | 20 ++++++++++++++ .../net.sf.webim.converter/templates/php.ltp | 8 ++++++ .../net.sf.webim.converter/templates/x.ltp | 9 +++++++ .../net.sf.webim.converter/test/index.xml | 8 ++++++ 6 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 src/converter/net.sf.webim.converter/src/net/sf/webim/converter/ConverterHelper.java create mode 100644 src/converter/net.sf.webim.converter/templates/conv.ltp create mode 100644 src/converter/net.sf.webim.converter/templates/php.ltp create mode 100644 src/converter/net.sf.webim.converter/templates/x.ltp create mode 100644 src/converter/net.sf.webim.converter/test/index.xml diff --git a/src/converter/net.sf.webim.converter/src/net/sf/webim/converter/ConverterHelper.java b/src/converter/net.sf.webim.converter/src/net/sf/webim/converter/ConverterHelper.java new file mode 100644 index 00000000..9d20f7aa --- /dev/null +++ b/src/converter/net.sf.webim.converter/src/net/sf/webim/converter/ConverterHelper.java @@ -0,0 +1,26 @@ +package net.sf.webim.converter; + +import net.sf.lapg.templates.api.impl.DefaultStaticMethods; +import net.sf.lapg.templates.model.xml.XmlData; +import net.sf.lapg.templates.model.xml.XmlNode; + + +public class ConverterHelper extends DefaultStaticMethods { + + + public boolean isNode(XmlNode element) { + return true; + } + + public boolean isNode(XmlData element) { + return false; + } + + public boolean containsColon(String s) { + return s.indexOf(":") >= 0; + } + + public String replaceColon(String s) { + return s.replace(':', '.'); + } +} diff --git a/src/converter/net.sf.webim.converter/src/net/sf/webim/converter/JspConverter.java b/src/converter/net.sf.webim.converter/src/net/sf/webim/converter/JspConverter.java index f38b5050..8bfeecd9 100644 --- a/src/converter/net.sf.webim.converter/src/net/sf/webim/converter/JspConverter.java +++ b/src/converter/net.sf.webim.converter/src/net/sf/webim/converter/JspConverter.java @@ -1,22 +1,32 @@ package net.sf.webim.converter; +import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; +import net.sf.lapg.templates.api.EvaluationContext; +import net.sf.lapg.templates.api.impl.FolderTemplateLoader; +import net.sf.lapg.templates.api.impl.TemplateEnvironment; import net.sf.lapg.templates.model.xml.XmlModel; +import net.sf.lapg.templates.model.xml.XmlNavigationFactory; import net.sf.lapg.templates.model.xml.XmlNode; public class JspConverter { public static void main(String[] args) { - String toProcess = getFileContents("../test.xml"); + String toProcess = getFileContents("test/index.xml"); XmlNode root = XmlModel.load(toProcess); - System.out.println(">>>\n" + root.toString() + "<<<"); - + ConverterHelper helper = new ConverterHelper(); + TemplateEnvironment env = new TemplateEnvironment( + new XmlNavigationFactory(), + new FolderTemplateLoader(new File("templates"))); + EvaluationContext context = new EvaluationContext(root.getChildren()); + context.setVariable("helper", helper); + System.out.println(env.executeTemplate("conv.convertList", context, null)); } private static String getFileContents(String file) { diff --git a/src/converter/net.sf.webim.converter/templates/conv.ltp b/src/converter/net.sf.webim.converter/templates/conv.ltp new file mode 100644 index 00000000..27a26dfa --- /dev/null +++ b/src/converter/net.sf.webim.converter/templates/conv.ltp @@ -0,0 +1,20 @@ + +${template convert-} +${if helper.isNode(this)-} +${if helper.containsColon(tagName)-} +${call(`helper.replaceColon(tagName)`) this-} +${else-} +${this.getNodeDeclaration()-} +${call(convertList) children-} +${end-} +${else-} +${this-} +${end-} +${end} + + +${template convertList-} +${foreach child in this-} +${call(convert) child-} +${end-} +${end} \ No newline at end of file diff --git a/src/converter/net.sf.webim.converter/templates/php.ltp b/src/converter/net.sf.webim.converter/templates/php.ltp new file mode 100644 index 00000000..e21523d0 --- /dev/null +++ b/src/converter/net.sf.webim.converter/templates/php.ltp @@ -0,0 +1,8 @@ + +${template hidden-} +${if attrs.value-} + +${else-} + +${end-} +${end} diff --git a/src/converter/net.sf.webim.converter/templates/x.ltp b/src/converter/net.sf.webim.converter/templates/x.ltp new file mode 100644 index 00000000..6f6edfd0 --- /dev/null +++ b/src/converter/net.sf.webim.converter/templates/x.ltp @@ -0,0 +1,9 @@ + +${template html-} + + +${call(conv.convertList)children-} + +${end} + + diff --git a/src/converter/net.sf.webim.converter/test/index.xml b/src/converter/net.sf.webim.converter/test/index.xml new file mode 100644 index 00000000..c6ea5897 --- /dev/null +++ b/src/converter/net.sf.webim.converter/test/index.xml @@ -0,0 +1,8 @@ + + +A + + +B + + \ No newline at end of file