From 68006d388dca0d22ba86ec8c18bc8495950f59b3 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Mon, 1 Jul 2013 01:33:27 +0200 Subject: [PATCH] Add URLPresenter. Improve FilePresenter. Normalize source() as getSource(). --- src/fr/devinsy/xidyn/DomPresenter.java | 2 +- src/fr/devinsy/xidyn/FilePresenter.java | 68 ++++--- src/fr/devinsy/xidyn/FilePresenters.java | 4 +- src/fr/devinsy/xidyn/Presenter.java | 2 +- src/fr/devinsy/xidyn/StringPresenter.java | 2 +- src/fr/devinsy/xidyn/URLPresenter.java | 217 ++++++++++++++++++++++ 6 files changed, 269 insertions(+), 26 deletions(-) create mode 100644 src/fr/devinsy/xidyn/URLPresenter.java diff --git a/src/fr/devinsy/xidyn/DomPresenter.java b/src/fr/devinsy/xidyn/DomPresenter.java index 7a0f298..deea35a 100644 --- a/src/fr/devinsy/xidyn/DomPresenter.java +++ b/src/fr/devinsy/xidyn/DomPresenter.java @@ -99,7 +99,7 @@ public class DomPresenter extends Presenter /** * */ - public Object source() + public Object getSource() { Object result; diff --git a/src/fr/devinsy/xidyn/FilePresenter.java b/src/fr/devinsy/xidyn/FilePresenter.java index 1819cbd..6c78b42 100644 --- a/src/fr/devinsy/xidyn/FilePresenter.java +++ b/src/fr/devinsy/xidyn/FilePresenter.java @@ -4,6 +4,7 @@ import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.StringWriter; +import java.net.URI; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -24,10 +25,15 @@ public class FilePresenter extends DomPresenter */ public FilePresenter() { - this.sourceFilePathname = null; - this.sourceFileTime = 0; - this.doc = null; - this.doctype = ""; + setSource((String) null); + } + + /** + * + */ + public FilePresenter(final File source) + { + setSource(source); } /** @@ -35,10 +41,7 @@ public class FilePresenter extends DomPresenter */ public FilePresenter(final String filePathname) { - this.sourceFilePathname = filePathname; - this.sourceFileTime = 0; - this.doc = null; - this.doctype = ""; + setSource(filePathname); } /** @@ -52,7 +55,15 @@ public class FilePresenter extends DomPresenter logger.info("dynamize file [" + this.sourceFilePathname + "]"); // Get the good tree. - File source = new File(this.sourceFilePathname); + File source; + if (this.sourceFilePathname.matches(".+://.+")) + { + source = new File(new URI(this.sourceFilePathname)); + } + else + { + source = new File(this.sourceFilePathname); + } if (!source.exists()) { @@ -93,22 +104,11 @@ public class FilePresenter extends DomPresenter return (dynamize(datas.getIdsDataById())); } - /** - * - */ - public void setSource(final String filePathname) - { - this.sourceFilePathname = filePathname; - this.sourceFileTime = 0; - this.doc = null; - this.doctype = ""; - } - /** * */ @Override - public String source() + public String getSource() { String result; @@ -118,6 +118,32 @@ public class FilePresenter extends DomPresenter return (result); } + /** + * + */ + public void setSource(final File source) + { + if (source == null) + { + setSource((String) null); + } + else + { + setSource(source.getAbsolutePath()); + } + } + + /** + * + */ + public void setSource(final String filePathname) + { + this.sourceFilePathname = filePathname; + this.sourceFileTime = 0; + this.doc = null; + this.doctype = ""; + } + /** * Dynamize a file without data. */ diff --git a/src/fr/devinsy/xidyn/FilePresenters.java b/src/fr/devinsy/xidyn/FilePresenters.java index abf6a46..c3b0467 100644 --- a/src/fr/devinsy/xidyn/FilePresenters.java +++ b/src/fr/devinsy/xidyn/FilePresenters.java @@ -94,7 +94,7 @@ public class FilePresenters extends Vector { FilePresenter presenter = this.get(presenterIndex); - if (presenter.source().equals(source)) + if (presenter.getSource().equals(source)) { result = presenter; ended = true; @@ -138,7 +138,7 @@ public class FilePresenters extends Vector { FilePresenter presenter = this.get(presenterIndex); - if (presenter.source().equals(source)) + if (presenter.getSource().equals(source)) { result = presenterIndex; ended = true; diff --git a/src/fr/devinsy/xidyn/Presenter.java b/src/fr/devinsy/xidyn/Presenter.java index 07011a7..49fd195 100644 --- a/src/fr/devinsy/xidyn/Presenter.java +++ b/src/fr/devinsy/xidyn/Presenter.java @@ -462,7 +462,7 @@ public class Presenter // String publicId = dt.getPublicId(); String systemId = dt.getSystemId(); - if (systemId.equals(TRANSITIONAL_DTD)) + if ((systemId != null) && (systemId.equals(TRANSITIONAL_DTD))) { result.append(TRANSITIONAL_DOCTYPE); } diff --git a/src/fr/devinsy/xidyn/StringPresenter.java b/src/fr/devinsy/xidyn/StringPresenter.java index 6cfaf7a..9883930 100644 --- a/src/fr/devinsy/xidyn/StringPresenter.java +++ b/src/fr/devinsy/xidyn/StringPresenter.java @@ -117,7 +117,7 @@ public class StringPresenter extends DomPresenter * */ @Override - public String source() + public String getSource() { String result; diff --git a/src/fr/devinsy/xidyn/URLPresenter.java b/src/fr/devinsy/xidyn/URLPresenter.java new file mode 100644 index 0000000..59b45f7 --- /dev/null +++ b/src/fr/devinsy/xidyn/URLPresenter.java @@ -0,0 +1,217 @@ +package fr.devinsy.xidyn; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.InputStreamReader; +import java.io.StringWriter; +import java.net.MalformedURLException; +import java.net.URL; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + */ +public class URLPresenter extends DomPresenter { + static private Logger logger = LoggerFactory.getLogger(URLPresenter.class); + + private String sourcePathname; + private URL sourceURL; + private long sourceFileTime; + private String doctype; + + /** + * + */ + public URLPresenter() { + setSource((URL) null); + } + + /** + * @throws MalformedURLException + * + */ + public URLPresenter(final String source) { + setSource(source); + } + + /** + * + */ + public URLPresenter(final URL source) { + setSource(source); + } + + /** + * No need to be synchronized. + */ + public StringBuffer dynamize() throws Exception { + StringBuffer result; + + result = dynamize((TagDataListById) null); + + // + return result; + } + + /** + * No need to be synchronized. + */ + @Override + public StringBuffer dynamize(final TagDataListById datas) throws Exception { + StringBuffer result; + + logger.info("dynamize URL [" + this.sourcePathname + "]"); + + if (this.sourceURL == null) { + String errorMessage = "source file defined but not found (" + this.sourcePathname + ")"; + logger.error(errorMessage); + result = null; + throw new Exception(errorMessage); + } else if (datas == null) { + result = new StringBuffer(sourceURL.openConnection().getContentLength()); + BufferedReader in = new BufferedReader(new InputStreamReader(this.sourceURL.openStream())); + String line; + while ((line = in.readLine()) != null) { + result.append(line); + } + in.close(); + } else { + long lastModified = sourceURL.openConnection().getLastModified(); + if ((this.doc == null) || (this.sourceFileTime != lastModified)) { + this.sourceFileTime = lastModified; + this.doc = Presenter.buildTree(this.sourceURL.openStream()); + this.doctype = ""; // TODO Made generic. + if (this.doc != null) { + Presenter.addMetaTag(doc, "generator", "XID 0.0"); + } + } + + // Build the web page. + StringWriter htmlCode = new StringWriter(Presenter.estimatedTargetLength(sourceURL.openConnection().getContentLength())); + htmlCode.write(doctype); + htmlCode.write('\n'); + + Presenter.dynamize(htmlCode, doc, datas); + result = htmlCode.getBuffer(); + } + + // + return (result); + } + + /** + * + */ + @Override + public StringBuffer dynamize(final TagDataManager datas) throws Exception { + return (dynamize(datas.getIdsDataById())); + } + + /** + * + */ + @Override + public String getSource() { + String result; + + result = this.sourceURL.toString(); + + // + return (result); + } + + /** + * + */ + public void setSource(final String source) { + URL url; + if (source == null) { + url = null; + } else if (source.matches(".+://.+")) { + try { + url = new URL(source); + } catch (final MalformedURLException exception) { + logger.warn("UNKNOWN PROTOCOL [" + source + "]"); + url = null; + } + } else { + url = URLPresenter.class.getResource(source); + } + + setSource(url); + } + + /** + * + * @param source + * @throws MalformedURLException + */ + public void setSource(final URL source) { + if (source == null) { + this.sourcePathname = null; + } else { + this.sourcePathname = source.toString(); + } + this.sourceURL = source; + this.sourceFileTime = 0; + this.doc = null; + this.doctype = ""; + } + + /** + * Dynamize a file without data. + */ + static public StringBuffer dynamize(final String filePathname) throws Exception { + StringBuffer result; + + URLPresenter presenter = new URLPresenter(filePathname); + + result = presenter.dynamize((TagDataManager) null); + + // + return (result); + } + + /** + * Dynamize a file. + */ + static public StringBuffer dynamize(final String filePathname, final TagDataManager datas) throws Exception { + StringBuffer result; + + URLPresenter presenter = new URLPresenter(filePathname); + + result = presenter.dynamize(datas); + + // + return (result); + } + + /** + * + * @param filePathname + * @return + * @throws Exception + */ + static public String getDoctype(final String filePathname) throws Exception { + String result; + + // + BufferedReader in = new BufferedReader(new FileReader(filePathname)); + String doctype = in.readLine(); + in.close(); + + logger.info("doctype=[" + doctype + "]"); + + // + if ((doctype.startsWith("