From e77b5e70c41deecac9b91280207fed6afe03d373 Mon Sep 17 00:00:00 2001 From: administrateur Date: Mon, 12 Mar 2007 00:12:58 +0100 Subject: [PATCH] Add new class to use Presenter. --- src/xid/DomPresenter.java | 196 ++++++++++++++++++++++++++++++++++ src/xid/FilePresenter.java | 170 ++++++++++++++++++++++++++++++ src/xid/StringPresenter.java | 198 +++++++++++++++++++++++++++++++++++ 3 files changed, 564 insertions(+) create mode 100644 src/xid/DomPresenter.java create mode 100644 src/xid/FilePresenter.java create mode 100644 src/xid/StringPresenter.java diff --git a/src/xid/DomPresenter.java b/src/xid/DomPresenter.java new file mode 100644 index 0000000..bb2d242 --- /dev/null +++ b/src/xid/DomPresenter.java @@ -0,0 +1,196 @@ +package xid; + +import java.util.*; +import java.io.*; +import javax.xml.parsers.*; +import javax.xml.transform.stream.StreamSource; +import javax.xml.validation.Schema; +import org.xml.sax.*; +import org.w3c.dom.*; +/** + * + */ +public class DomPresenter extends Presenter +{ + static final public char INDEX_SEPARATOR = '_'; + + static protected org.apache.log4j.Logger log; + static + { + log = org.apache.log4j.Logger.getLogger (DomPresenter.class); + } + + static protected String staticRootPath; + static + { + staticRootPath = null; + } + + + protected String webappPath; + protected Document doc; + + /* + * + */ + public DomPresenter () + { + this.webappPath = Presenter.staticRootPath; + this.doc = null; + } + + + /* + * + */ + public DomPresenter (Document doc) + { + this.webappPath = Presenter.staticRootPath; + this.doc = doc; + DomPresenter.addMetaTag (this.doc, "generator", "XID 0.0"); + } + + + /* + * + */ + public DomPresenter (String webappPath, Document doc) + { + if ((webappPath == null) || (webappPath.equals (""))) + { + this.webappPath = Presenter.staticRootPath; + } + else + { + this.webappPath = webappPath; + } + + this.doc = doc; + DomPresenter.addMetaTag (this.doc, "generator", "XID 0.0"); + } + + + /* + * + */ + public String getWebappPath () + { + String result; + + result = this.webappPath; + + // + return (result); + } + + /* + * + */ + public void setWebappPath (String path) + { + this.webappPath = path; + } + + + /** + * + */ + public Document getDom () + { + Document result; + + result = this.doc; + + // + return (result); + } + + + /** + * + */ + public void setDom (Document doc) + { + this.doc = doc; + } + + + /* + * + */ + public void setSource (Document doc) + { + this.doc = doc; + + Presenter.addMetaTag (this.doc, "generator", "XID 0.0"); + } + + + /* + * + */ + public Object getSource () + { + Object result; + + result = this.doc; + + // + return (result); + } + + + /* + * + */ + public StringBuffer doXid (Data datas, StringBuffer errorOutput) + { + return (doXid (datas.getIdsDataById (), datas.getTagsDataById (), errorOutput)); + } + + /* + * + */ + public StringBuffer doXid (IdsDataById datas, TagsDataById tagsData, StringBuffer errorOutput) + { + StringBuffer result; + + if (this.doc == null) + { + String errorMessage = "source not defined"; + errorOutput.append (errorMessage); + log.error (errorMessage); + result = null; + } + else + { + // Build the web page. + result = Presenter.doXid (this.doc, datas, tagsData, this.webappPath, errorOutput); + } + + // + return (result); + } + + + /* + * Xid a file with data. + */ + static public StringBuffer doXid (Document doc, IdsDataById datas, String webappPath, StringBuffer errorOutput) + { + return (doXid (doc, datas, null, webappPath, errorOutput)); + } + + /* + * Xid a file with data. + */ + static public StringBuffer doXid (Document doc, IdsDataById datas, TagsDataById tagsData, String webappPath, StringBuffer errorOutput) + { + StringBuffer result; + + result = Presenter.process (doc, datas, tagsData, webappPath, errorOutput); + + // + return (result); + } +} diff --git a/src/xid/FilePresenter.java b/src/xid/FilePresenter.java new file mode 100644 index 0000000..2adbe82 --- /dev/null +++ b/src/xid/FilePresenter.java @@ -0,0 +1,170 @@ +package xid; + +import java.util.*; +import java.io.*; +import javax.xml.parsers.*; +import javax.xml.transform.stream.StreamSource; +import javax.xml.validation.Schema; +import org.xml.sax.*; +import org.w3c.dom.*; +/** + * + */ +public class FilePresenter extends DomPresenter +{ + static protected org.apache.log4j.Logger log; + static + { + log = org.apache.log4j.Logger.getLogger (FilePresenter.class); + } + + protected String sourceFileName; + protected long sourceFileTime; + + /* + * + */ + public FilePresenter () + { + this.webappPath = Presenter.staticRootPath; + this.sourceFileName = null; + this.sourceFileTime = 0; + this.doc = null; + } + + + /* + * + */ + public FilePresenter (String fileName) + { + this.webappPath = Presenter.staticRootPath; + this.sourceFileName = fileName; + this.sourceFileTime = 0; + this.doc = null; + } + + + /* + * + */ + public FilePresenter (String webappPath, String fileName) + { + if ((webappPath == null) || (webappPath.equals (""))) + { + this.webappPath = Presenter.staticRootPath; + } + else + { + this.webappPath = webappPath; + } + this.sourceFileName = fileName; + this.sourceFileTime = 0; + this.doc = null; + } + + + /* + * + */ + public void setSource (String fileName) + { + this.sourceFileName = fileName; + this.sourceFileTime = 0; + this.doc = null; + } + + + /* + * + */ + public String getSource () + { + String result; + + result = this.sourceFileName; + + // + return (result); + } + + + /* + * + */ + public StringBuffer doXid (Data datas, StringBuffer errorOutput) + { + return (doXid (datas.getIdsDataById (), datas.getTagsDataById (), errorOutput)); + } + + + /* + * + */ + public StringBuffer doXid (IdsDataById datas, TagsDataById tagsData, StringBuffer errorOutput) + { + StringBuffer result; + + String sourceFilePath = this.webappPath + File.separator + this.sourceFileName; + + // Get the good tree. + File source = new File (sourceFilePath); + + if (source == null) + { + String errorMessage = "source file not defined"; + errorOutput.append (errorMessage); + log.error (errorMessage); + result = null; + } + else if (!source.exists ()) + { + String errorMessage = "source file defined but not found (" + sourceFilePath + ")"; + errorOutput.append (errorMessage); + log.error (errorMessage); + result = null; + } + else if ((this.doc == null) || + (this.sourceFileTime != source.lastModified ())) + { + this.sourceFileTime = source.lastModified (); + this.doc = Presenter.fileToTree (sourceFilePath, errorOutput); + + if (this.doc != null) + { + Presenter.addMetaTag (doc, "generator", "XID 0.0"); + } + } + + // Build the web page. + result = Presenter.doXid (doc, datas, tagsData, this.webappPath, errorOutput); + + // + return (result); + } + + + /* + * Xid a file without data. + */ + static public StringBuffer doXid (String fileName, String webappPath, StringBuffer errorOutput) + { + StringBuffer result; + + Document doc = Presenter.fileToTree (fileName, errorOutput); + + if (doc == null) + { + result = null; + } + else + { + Presenter.addMetaTag (doc, "generator", "XID 0.0"); + + result = Presenter.doXid (doc, null, null, webappPath, errorOutput); + } + + // + return (result); + } +} diff --git a/src/xid/StringPresenter.java b/src/xid/StringPresenter.java new file mode 100644 index 0000000..c82fb4c --- /dev/null +++ b/src/xid/StringPresenter.java @@ -0,0 +1,198 @@ +package xid; + +import java.util.*; +import java.io.*; +import javax.xml.parsers.*; +import javax.xml.transform.stream.StreamSource; +import javax.xml.validation.Schema; +import org.xml.sax.*; +import org.w3c.dom.*; +/** + * + */ +public class StringPresenter extends DomPresenter +{ + static protected org.apache.log4j.Logger log; + static + { + log = org.apache.log4j.Logger.getLogger (StringPresenter.class); + } + + protected String html; + + /* + * + */ + public StringPresenter () + { + super (); + this.webappPath = Presenter.staticRootPath; + this.doc = null; + this.html = null; + } + + + /* + * + */ + public StringPresenter (String html) + { + this.webappPath = Presenter.staticRootPath; + this.doc = null; + this.html = html; + } + + + /* + * + */ + public StringPresenter (String webappPath, String html) + { + if ((webappPath == null) || (webappPath.equals (""))) + { + this.webappPath = Presenter.staticRootPath; + } + else + { + this.webappPath = webappPath; + } + this.doc = null; + this.html = html; + } + + + /* + * + */ + public void setSource (String html) + { + this.html = html; + this.doc = null; + } + + + /* + * + */ + public String getSource () + { + String result; + + result = this.html; + + // + return (result); + } + + + /* + * + */ + public StringBuffer doXid (Data datas, StringBuffer errorOutput) + { + return (doXid (datas.getIdsDataById (), datas.getTagsDataById (), errorOutput)); + } + + /* + * + */ + public StringBuffer doXid (IdsDataById datas, TagsDataById tagsData, StringBuffer errorOutput) + { + StringBuffer result; + + if (this.doc == null) + { + // Build doc from this.html. + String htmlSource; + if ((this.html.startsWith ("")) || + (this.html.startsWith (""))) + { + htmlSource = html; + } + else + { + htmlSource = "\n" + html + ""; + } + + // StringBufferInputStream is deprecated so we use another solution. + // (see http://www.developpez.net/forums/archive/index.php/t-14101.html). + doc = buildTree (new ByteArrayInputStream (htmlSource.getBytes ()), errorOutput); + } + + StringBuffer htmlTarget; + htmlTarget = Presenter.doXid (doc, datas, tagsData, this.webappPath, errorOutput); + + if (htmlTarget == null) + { + result = null; + } + else if ((this.html.startsWith ("")) || + (this.html.startsWith (""))) + { + result = htmlTarget; + } + else + { + String bodyContent = extractBodyContent (htmlTarget); + + if (bodyContent == null) + { + result = null; + } + else + { + result = new StringBuffer (bodyContent); + } + } + + // + return (result); + } + + + /* + * + */ + static public StringBuffer doXid (String html, Data datas, StringBuffer errorOutput) + { + return (doXid (html, datas.getIdsDataById (), datas.getTagsDataById (), "", errorOutput)); + } + + + /* + * + */ + static public StringBuffer doXid (String html, Data datas, String webappPath, StringBuffer errorOutput) + { + return (doXid (html, datas.getIdsDataById (), datas.getTagsDataById (), webappPath, errorOutput)); + } + + + /* + * Xid a string with html in. + */ + static public StringBuffer doXid (String html, IdsDataById datas, String webappPath, StringBuffer errorOutput) + { + return (doXid (html, datas, null, webappPath, errorOutput)); + } + + + /* + * Xid a string with html in. + */ + static public StringBuffer doXid (String html, IdsDataById datas, TagsDataById tagsData, String webappPath, StringBuffer errorOutput) + { + StringBuffer result; + + StringPresenter presenter = new StringPresenter (webappPath, html); + + result = presenter.doXid (datas, tagsData, errorOutput); + + // + return (result); + } +}