diff --git a/src/fr/devinsy/xidyn/presenters/DomPresenter.java b/src/fr/devinsy/xidyn/presenters/DomPresenter.java index aa04419..d521204 100644 --- a/src/fr/devinsy/xidyn/presenters/DomPresenter.java +++ b/src/fr/devinsy/xidyn/presenters/DomPresenter.java @@ -12,11 +12,12 @@ import fr.devinsy.xidyn.data.TagDataManager; /** * */ -public class DomPresenter extends DomPresenterCore implements Presenter +public class DomPresenter implements Presenter { - static final public char INDEX_SEPARATOR = '_'; static private Logger logger = LoggerFactory.getLogger(DomPresenter.class); - protected Document doc; + private Document doc; + private boolean isOutdated; + private StringBuffer defaultHtmlTarget; /** * @@ -24,6 +25,7 @@ public class DomPresenter extends DomPresenterCore implements Presenter public DomPresenter() { this.doc = null; + this.isOutdated = false; } /** @@ -34,22 +36,39 @@ public class DomPresenter extends DomPresenterCore implements Presenter setSource(doc); } + /** + * {@inheritDoc} + */ @Override public StringBuffer dynamize() throws Exception { - // TODO Auto-generated method stub - return null; + StringBuffer result; + + if ((this.isOutdated) || (this.defaultHtmlTarget == null)) + { + this.defaultHtmlTarget = dynamize(null); + this.isOutdated = false; + } + + result = this.defaultHtmlTarget; + + // + return result; } /** - * + * {@inheritDoc} */ @Override - public StringBuffer dynamize(final TagDataListById datas) throws Exception + public StringBuffer dynamize(final TagDataManager data) throws Exception { StringBuffer result; - if (this.doc == null) + if (data == null) + { + result = dynamize(new TagDataManager()); + } + else if (this.doc == null) { String errorMessage = "source not defined"; logger.error(errorMessage); @@ -59,24 +78,16 @@ public class DomPresenter extends DomPresenterCore implements Presenter else { // Build the web page. - StringWriter htmlCode = new StringWriter(); - DomPresenterCore.dynamize(htmlCode, this.doc, datas); - result = new StringBuffer(htmlCode.toString()); + StringWriter writer = new StringWriter(20000); + DomPresenterCore.dynamize(writer, this.doc, data); + result = writer.getBuffer(); + this.isOutdated = false; } // return (result); } - /** - * - */ - @Override - public StringBuffer dynamize(final TagDataManager datas) throws Exception - { - return (dynamize(datas.getIdsDataById())); - } - /** * */ @@ -91,7 +102,7 @@ public class DomPresenter extends DomPresenterCore implements Presenter } /** - * + * {@inheritDoc} */ @Override public Object getSource() @@ -105,14 +116,14 @@ public class DomPresenter extends DomPresenterCore implements Presenter } /** - * + * {@inheritDoc} */ @Override public boolean isOutdated() throws Exception { boolean result; - result = false; + result = this.isOutdated; // return result; @@ -124,6 +135,7 @@ public class DomPresenter extends DomPresenterCore implements Presenter public void setDOM(final Document doc) { this.doc = doc; + this.isOutdated = true; } /** @@ -132,6 +144,51 @@ public class DomPresenter extends DomPresenterCore implements Presenter public void setSource(final Document doc) { this.doc = doc; - DomPresenterCore.addMetaTag(this.doc, "generator", "XIDYN 1.0.0"); + DomPresenterCore.addMetaTag(this.doc, "generator", "XIDYN"); + this.isOutdated = true; } + + /** + * + * @throws Exception + */ + public void update() throws Exception + { + + } + + /** + * + * @param doc + * @param data + * @return + * @throws Exception + */ + public static StringBuffer dynamize(final Document doc, final TagDataListById data) throws Exception + { + StringBuffer result; + + result = DomPresenterCore.dynamize(doc, data); + + // + return result; + } + + /** + * + * @param doc + * @param data + * @return + * @throws Exception + */ + public static StringBuffer dynamize(final Document doc, final TagDataManager data) throws Exception + { + StringBuffer result; + + result = DomPresenterCore.dynamize(doc, data); + + // + return result; + } + } diff --git a/src/fr/devinsy/xidyn/presenters/DomPresenterCore.java b/src/fr/devinsy/xidyn/presenters/DomPresenterCore.java index 3d3f8c0..49b74fd 100644 --- a/src/fr/devinsy/xidyn/presenters/DomPresenterCore.java +++ b/src/fr/devinsy/xidyn/presenters/DomPresenterCore.java @@ -40,8 +40,9 @@ public class DomPresenterCore { static final public char INDEX_SEPARATOR = '_'; static private Logger logger = LoggerFactory.getLogger(DomPresenterCore.class); + /** - * + * This method adds a tag to a DOM object. */ static public void addMetaTag(final Document doc, final String name, final String content) { @@ -63,7 +64,7 @@ public class DomPresenterCore } /** - * + * This method build a DOM object from a source. */ static public Document buildDom(final InputStream source) throws Exception { @@ -110,7 +111,7 @@ public class DomPresenterCore } else { - DomPresenterCore.addMetaTag(result, "generator", "XID 0.0"); + DomPresenterCore.addMetaTag(result, "generator", "XIDYN"); } } catch (ParserConfigurationException exception) @@ -140,14 +141,14 @@ public class DomPresenterCore } /** - * Xidyn a file with data. + * Dynamize a doc with data. */ - static public StringBuffer dynamize(final Document doc, final TagDataListById datas) throws Exception + static public StringBuffer dynamize(final Document doc, final TagDataListById data) throws Exception { StringBuffer result; StringWriter htmlCode = new StringWriter(); - DomPresenterCore.dynamize(htmlCode, doc, datas); + DomPresenterCore.dynamize(htmlCode, doc, data); result = htmlCode.getBuffer(); // @@ -155,13 +156,34 @@ public class DomPresenterCore } /** - * Dynamize a file with data. + * Dynamize a doc with data. + */ + static public StringBuffer dynamize(final Document doc, final TagDataManager data) throws Exception + { + StringBuffer result; + + result = dynamize(doc, data.getIdsDataById()); + + // + return (result); + } + + /** + * Dynamize a doc with data. */ static public void dynamize(final Writer result, final Document doc, final TagDataListById data) throws Exception { process(result, doc, data); } + /** + * Dynamize a doc with data. + */ + static public void dynamize(final Writer result, final Document doc, final TagDataManager data) throws Exception + { + dynamize(result, doc, data.getIdsDataById()); + } + /** * Define in Presenter cause needs this possibility. */ @@ -390,9 +412,9 @@ public class DomPresenterCore /** * */ - private static void process(final Writer result, final Node node, final TagDataListById datas) throws Exception + private static void process(final Writer result, final Node node, final TagDataListById data) throws Exception { - DomPresenterCore.process(result, node, datas, ""); + process(result, node, data, ""); } /** diff --git a/src/fr/devinsy/xidyn/presenters/FilePresenter.java b/src/fr/devinsy/xidyn/presenters/FilePresenter.java index 36d8b12..9af0307 100644 --- a/src/fr/devinsy/xidyn/presenters/FilePresenter.java +++ b/src/fr/devinsy/xidyn/presenters/FilePresenter.java @@ -8,18 +8,19 @@ import java.net.URI; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.w3c.dom.Document; -import fr.devinsy.xidyn.data.TagDataListById; import fr.devinsy.xidyn.data.TagDataManager; import fr.devinsy.xidyn.utils.XidynUtils; /** * */ -public class FilePresenter extends DomPresenter +public class FilePresenter implements Presenter { static Logger logger = LoggerFactory.getLogger(FilePresenter.class); + private Document doc; private String sourceFilePathname; private long sourceFileTime; private String doctype; @@ -49,10 +50,26 @@ public class FilePresenter extends DomPresenter } /** - * No need to be synchronized. + * {@inheritDoc} */ @Override - public StringBuffer dynamize(final TagDataListById datas) throws Exception + public StringBuffer dynamize() throws Exception + { + StringBuffer result; + + result = dynamize((TagDataManager) null); + + // + return result; + } + + /** + * No need to be synchronized. + * + * {@inheritDoc} + */ + @Override + public StringBuffer dynamize(final TagDataManager data) throws Exception { StringBuffer result; @@ -79,12 +96,12 @@ public class FilePresenter extends DomPresenter else if ((this.doc == null) || (this.sourceFileTime != source.lastModified())) { this.sourceFileTime = source.lastModified(); - this.doc = DomPresenter.fileToDom(this.sourceFilePathname); - this.doctype = getDoctype(this.sourceFilePathname); + this.doc = DomPresenterCore.fileToDom(this.sourceFilePathname); if (this.doc != null) { - DomPresenter.addMetaTag(doc, "generator", "XID 0.0"); + DomPresenterCore.addMetaTag(doc, "generator", "XID 0.0"); } + this.doctype = getDoctype(this.sourceFilePathname); } // Build the web page. @@ -92,7 +109,16 @@ public class FilePresenter extends DomPresenter htmlCode.write(doctype); htmlCode.write('\n'); - DomPresenter.dynamize(htmlCode, doc, datas); + TagDataManager targetData; + if (data == null) + { + targetData = new TagDataManager(); + } + else + { + targetData = data; + } + DomPresenterCore.dynamize(htmlCode, doc, targetData); result = htmlCode.getBuffer(); // @@ -100,16 +126,7 @@ public class FilePresenter extends DomPresenter } /** - * - */ - @Override - public StringBuffer dynamize(final TagDataManager datas) throws Exception - { - return (dynamize(datas.getIdsDataById())); - } - - /** - * + * {@inheritDoc} */ @Override public String getSource() @@ -123,7 +140,7 @@ public class FilePresenter extends DomPresenter } /** - * + * {@inheritDoc} */ @Override public boolean isOutdated() diff --git a/src/fr/devinsy/xidyn/presenters/Presenter.java b/src/fr/devinsy/xidyn/presenters/Presenter.java index 2423f28..37fb5c9 100644 --- a/src/fr/devinsy/xidyn/presenters/Presenter.java +++ b/src/fr/devinsy/xidyn/presenters/Presenter.java @@ -1,13 +1,11 @@ package fr.devinsy.xidyn.presenters; -import fr.devinsy.xidyn.data.TagDataListById; import fr.devinsy.xidyn.data.TagDataManager; /** * */ -public interface Presenter -{ +public interface Presenter { /** * @@ -15,7 +13,7 @@ public interface Presenter * @return * @throws Exception */ - public abstract StringBuffer dynamize() throws Exception; + public StringBuffer dynamize() throws Exception; /** * @@ -23,25 +21,17 @@ public interface Presenter * @return * @throws Exception */ - public abstract StringBuffer dynamize(final TagDataListById datas) throws Exception; - - /** - * - * @param datas - * @return - * @throws Exception - */ - public abstract StringBuffer dynamize(final TagDataManager datas) throws Exception; + public StringBuffer dynamize(final TagDataManager datas) throws Exception; /** * * @return */ - public abstract Object getSource(); + public Object getSource(); /** * * @return */ - public abstract boolean isOutdated() throws Exception; + public boolean isOutdated() throws Exception; } diff --git a/src/fr/devinsy/xidyn/presenters/StringPresenter.java b/src/fr/devinsy/xidyn/presenters/StringPresenter.java index d730637..bb4b66c 100644 --- a/src/fr/devinsy/xidyn/presenters/StringPresenter.java +++ b/src/fr/devinsy/xidyn/presenters/StringPresenter.java @@ -5,28 +5,27 @@ import java.io.StringWriter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.w3c.dom.Document; -import fr.devinsy.xidyn.data.TagDataListById; import fr.devinsy.xidyn.data.TagDataManager; import fr.devinsy.xidyn.utils.XidynUtils; /** * */ -public class StringPresenter extends DomPresenter +public class StringPresenter implements Presenter { static private Logger logger = LoggerFactory.getLogger(StringPresenter.class); - private String html; + private String source; + private Document doc; /** * */ public StringPresenter() { - super(); - this.doc = null; - this.html = null; + setSource(""); } /** @@ -34,68 +33,99 @@ public class StringPresenter extends DomPresenter */ public StringPresenter(final String html) { - this.doc = null; - this.html = html; + setSource(html); } /** - * + * {@inheritDoc} */ @Override - public StringBuffer dynamize(final TagDataListById datas) throws Exception + public StringBuffer dynamize() throws Exception { StringBuffer result; + result = new StringBuffer(this.source); + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public StringBuffer dynamize(final TagDataManager datas) throws Exception + { + StringBuffer result; + + // if (this.doc == null) { // Build doc from this.html. - String htmlSource; - if ((this.html.startsWith("")) || (this.html.startsWith(""))) + String sourceHtml; + if (source == null) { - htmlSource = html; + String errorMessage = "source not defined"; + logger.error(errorMessage); + result = null; + throw new Exception(errorMessage); + } + else if ((this.source.startsWith("")) || (this.source.startsWith(""))) + { + sourceHtml = this.source; } else { - StringBuffer buffer = new StringBuffer(html.length() + 100); + StringBuffer buffer = new StringBuffer(source.length() + 100); buffer.append("\n"); - buffer.append(html); + buffer.append(source); buffer.append(""); - htmlSource = buffer.toString(); + sourceHtml = buffer.toString(); } // StringBufferInputStream is deprecated so we use another solution. // (see // http://www.developpez.net/forums/archive/index.php/t-14101.html). - doc = DomPresenter.buildDom(new ByteArrayInputStream(htmlSource.getBytes())); + this.doc = DomPresenterCore.buildDom(new ByteArrayInputStream(sourceHtml.getBytes())); } - StringWriter htmlCode = new StringWriter(XidynUtils.estimatedTargetLength(this.html.length())); - if ((this.html.startsWith("'))); - } - DomPresenter.dynamize(htmlCode, doc, datas); - StringBuffer htmlTarget = htmlCode.getBuffer(); - - if (htmlTarget == null) - { - result = null; - } - else if ((this.html.startsWith("")) || (this.html.startsWith(""))) - { - result = htmlTarget; + result = dynamize(); } else { - String bodyContent = XidynUtils.extractBodyContent(htmlTarget); + // + StringWriter htmlCode = new StringWriter(XidynUtils.estimatedTargetLength(this.source.length())); + if ((this.source.startsWith("'))); + } + DomPresenterCore.dynamize(htmlCode, doc, datas); + StringBuffer htmlTarget = htmlCode.getBuffer(); - if (bodyContent == null) + // + if (htmlTarget == null) { result = null; } + else if ((this.source.startsWith("")) || (this.source.startsWith(""))) + { + result = htmlTarget; + } else { - result = new StringBuffer(bodyContent); + String bodyContent = XidynUtils.extractBodyContent(htmlTarget); + + if (bodyContent == null) + { + result = null; + } + else + { + result = new StringBuffer(bodyContent); + } } } @@ -104,41 +134,53 @@ public class StringPresenter extends DomPresenter } /** - * - */ - @Override - public StringBuffer dynamize(final TagDataManager datas) throws Exception - { - return (dynamize(datas.getIdsDataById())); - } - - /** - * + * {@inheritDoc} */ @Override public String getSource() { String result; - result = this.html; + result = this.source; // return (result); } + /** + * {@inheritDoc} + */ + @Override + public boolean isOutdated() throws Exception + { + boolean result; + + if (this.doc == null) + { + result = true; + } + else + { + result = false; + } + + // + return result; + } + /** * */ public void setSource(final String html) { - this.html = html; + this.source = html; this.doc = null; } /** - * Xid a string with html in. + * Dynamize a string with HTML in. */ - static public StringBuffer dynamize(final String html, final TagDataListById datas) throws Exception + static public StringBuffer dynamize(final String html, final TagDataManager datas) throws Exception { StringBuffer result; @@ -149,12 +191,4 @@ public class StringPresenter extends DomPresenter // return (result); } - - /** - * - */ - static public StringBuffer dynamize(final String html, final TagDataManager datas) throws Exception - { - return (dynamize(html, datas.getIdsDataById())); - } } diff --git a/src/fr/devinsy/xidyn/presenters/URLPresenter.java b/src/fr/devinsy/xidyn/presenters/URLPresenter.java index bdcd8ed..7943c4c 100644 --- a/src/fr/devinsy/xidyn/presenters/URLPresenter.java +++ b/src/fr/devinsy/xidyn/presenters/URLPresenter.java @@ -3,25 +3,25 @@ package fr.devinsy.xidyn.presenters; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; -import java.io.InputStreamReader; import java.io.StringWriter; import java.net.MalformedURLException; import java.net.URL; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.w3c.dom.Document; -import fr.devinsy.xidyn.data.TagDataListById; import fr.devinsy.xidyn.data.TagDataManager; import fr.devinsy.xidyn.utils.XidynUtils; /** * */ -public class URLPresenter extends DomPresenter +public class URLPresenter implements Presenter { static private Logger logger = LoggerFactory.getLogger(URLPresenter.class); + private Document doc; private String sourcePathname; private URL sourceURL; private long sourceFileTime; @@ -59,7 +59,7 @@ public class URLPresenter extends DomPresenter { StringBuffer result; - result = dynamize((TagDataListById) null); + result = dynamize((TagDataManager) null); // return result; @@ -69,7 +69,7 @@ public class URLPresenter extends DomPresenter * No need to be synchronized. */ @Override - public StringBuffer dynamize(final TagDataListById datas) throws Exception + public StringBuffer dynamize(final TagDataManager datas) throws Exception { StringBuffer result; @@ -84,14 +84,7 @@ public class URLPresenter extends DomPresenter } 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(); + result = dynamize(new TagDataManager()); } else { @@ -99,12 +92,12 @@ public class URLPresenter extends DomPresenter if ((this.doc == null) || (this.sourceFileTime != lastModified)) { this.sourceFileTime = lastModified; - this.doc = DomPresenter.buildDom(this.sourceURL.openStream()); - this.doctype = ""; // TODO Made generic. + this.doc = DomPresenterCore.buildDom(this.sourceURL.openStream()); if (this.doc != null) { - DomPresenter.addMetaTag(doc, "generator", "XID 0.0"); + DomPresenterCore.addMetaTag(doc, "generator", "XID 0.0"); } + this.doctype = ""; // TODO Made generic. } // Build the web page. @@ -112,7 +105,7 @@ public class URLPresenter extends DomPresenter htmlCode.write(doctype); htmlCode.write('\n'); - DomPresenter.dynamize(htmlCode, doc, datas); + DomPresenterCore.dynamize(htmlCode, doc, datas); result = htmlCode.getBuffer(); } @@ -120,15 +113,6 @@ public class URLPresenter extends DomPresenter return (result); } - /** - * {@inheritDoc} - */ - @Override - public StringBuffer dynamize(final TagDataManager datas) throws Exception - { - return (dynamize(datas.getIdsDataById())); - } - /** * {@inheritDoc} */ @@ -200,7 +184,6 @@ public class URLPresenter extends DomPresenter /** * * @param source - * @throws MalformedURLException */ public void setSource(final URL source) {