diff --git a/demo/XidynDemo.java b/demo/XidynDemo.java index b622b8f..422084c 100644 --- a/demo/XidynDemo.java +++ b/demo/XidynDemo.java @@ -6,9 +6,9 @@ import org.apache.log4j.Level; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import fr.devinsy.xidyn.SimpleTagData; -import fr.devinsy.xidyn.StringPresenter; -import fr.devinsy.xidyn.TagDataManager; +import fr.devinsy.xidyn.data.SimpleTagData; +import fr.devinsy.xidyn.data.TagDataManager; +import fr.devinsy.xidyn.presenters.StringPresenter; /** * diff --git a/src/fr/devinsy/xidyn/DomPresenter.java b/src/fr/devinsy/xidyn/DomPresenter.java deleted file mode 100644 index 12ad011..0000000 --- a/src/fr/devinsy/xidyn/DomPresenter.java +++ /dev/null @@ -1,126 +0,0 @@ -package fr.devinsy.xidyn; - -import java.io.StringWriter; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.w3c.dom.Document; - -/** - * - */ -public class DomPresenter extends Presenter -{ - static final public char INDEX_SEPARATOR = '_'; - - static private Logger logger = LoggerFactory.getLogger(DomPresenter.class); - protected Document doc; - - /** - * - */ - public DomPresenter() - { - this.doc = null; - } - - /** - * - */ - public DomPresenter(final Document doc) - { - setSource(doc); - } - - /** - * - */ - public Document getDOM() - { - Document result; - - result = this.doc; - - // - return (result); - } - - /** - * - */ - public StringBuffer dynamize(final TagDataListById datas) throws Exception - { - StringBuffer result; - - if (this.doc == null) - { - String errorMessage = "source not defined"; - logger.error(errorMessage); - result = null; - throw new Exception(errorMessage); - } - else - { - // Build the web page. - StringWriter htmlCode = new StringWriter(); - Presenter.dynamize(htmlCode, this.doc, datas); - result = new StringBuffer(htmlCode.toString()); - } - - // - return (result); - } - - /** - * - */ - public StringBuffer dynamize(final TagDataManager datas) throws Exception - { - return (dynamize(datas.getIdsDataById())); - } - - /** - * - */ - public void setDOM(final Document doc) - { - this.doc = doc; - } - - /* - * - */ - public void setSource(final Document doc) - { - this.doc = doc; - addMetaTag(this.doc, "generator", "XID 0.0"); - } - - /** - * - */ - public Object getSource() - { - Object result; - - result = this.doc; - - // - return (result); - } - - /** - * Xid a file with data. - */ - static public StringBuffer dynamize(final Document doc, final TagDataListById datas) throws Exception - { - StringBuffer result; - - StringWriter htmlCode = new StringWriter(); - Presenter.process(htmlCode, doc, datas); - result = htmlCode.getBuffer(); - - // - return (result); - } -} diff --git a/src/fr/devinsy/xidyn/SimpleTagData.java b/src/fr/devinsy/xidyn/data/SimpleTagData.java similarity index 92% rename from src/fr/devinsy/xidyn/SimpleTagData.java rename to src/fr/devinsy/xidyn/data/SimpleTagData.java index 6d549c2..00dc024 100644 --- a/src/fr/devinsy/xidyn/SimpleTagData.java +++ b/src/fr/devinsy/xidyn/data/SimpleTagData.java @@ -1,4 +1,4 @@ -package fr.devinsy.xidyn; +package fr.devinsy.xidyn.data; import java.io.Serializable; diff --git a/src/fr/devinsy/xidyn/TagAttributes.java b/src/fr/devinsy/xidyn/data/TagAttributes.java similarity index 91% rename from src/fr/devinsy/xidyn/TagAttributes.java rename to src/fr/devinsy/xidyn/data/TagAttributes.java index fdd2e7f..d7cb754 100644 --- a/src/fr/devinsy/xidyn/TagAttributes.java +++ b/src/fr/devinsy/xidyn/data/TagAttributes.java @@ -1,4 +1,4 @@ -package fr.devinsy.xidyn; +package fr.devinsy.xidyn.data; import java.util.HashMap; diff --git a/src/fr/devinsy/xidyn/TagData.java b/src/fr/devinsy/xidyn/data/TagData.java similarity index 83% rename from src/fr/devinsy/xidyn/TagData.java rename to src/fr/devinsy/xidyn/data/TagData.java index df7c3cb..aa7d332 100644 --- a/src/fr/devinsy/xidyn/TagData.java +++ b/src/fr/devinsy/xidyn/data/TagData.java @@ -1,4 +1,4 @@ -package fr.devinsy.xidyn; +package fr.devinsy.xidyn.data; /* * Xid uses three class to describe data: diff --git a/src/fr/devinsy/xidyn/TagDataListById.java b/src/fr/devinsy/xidyn/data/TagDataListById.java similarity index 86% rename from src/fr/devinsy/xidyn/TagDataListById.java rename to src/fr/devinsy/xidyn/data/TagDataListById.java index 2870f34..10bc51a 100644 --- a/src/fr/devinsy/xidyn/TagDataListById.java +++ b/src/fr/devinsy/xidyn/data/TagDataListById.java @@ -1,4 +1,4 @@ -package fr.devinsy.xidyn; +package fr.devinsy.xidyn.data; import java.util.HashMap; diff --git a/src/fr/devinsy/xidyn/TagDataListByIndex.java b/src/fr/devinsy/xidyn/data/TagDataListByIndex.java similarity index 82% rename from src/fr/devinsy/xidyn/TagDataListByIndex.java rename to src/fr/devinsy/xidyn/data/TagDataListByIndex.java index ea11a9d..f67ffbc 100644 --- a/src/fr/devinsy/xidyn/TagDataListByIndex.java +++ b/src/fr/devinsy/xidyn/data/TagDataListByIndex.java @@ -1,4 +1,4 @@ -package fr.devinsy.xidyn; +package fr.devinsy.xidyn.data; import java.util.Vector; diff --git a/src/fr/devinsy/xidyn/TagDataManager.java b/src/fr/devinsy/xidyn/data/TagDataManager.java similarity index 94% rename from src/fr/devinsy/xidyn/TagDataManager.java rename to src/fr/devinsy/xidyn/data/TagDataManager.java index 3a4b193..78dd9c7 100644 --- a/src/fr/devinsy/xidyn/TagDataManager.java +++ b/src/fr/devinsy/xidyn/data/TagDataManager.java @@ -1,4 +1,4 @@ -package fr.devinsy.xidyn; +package fr.devinsy.xidyn.data; /* * diff --git a/src/fr/devinsy/xidyn/Presenter.java b/src/fr/devinsy/xidyn/presenters/DomPresenter.java similarity index 78% rename from src/fr/devinsy/xidyn/Presenter.java rename to src/fr/devinsy/xidyn/presenters/DomPresenter.java index 3d799d2..725877d 100644 --- a/src/fr/devinsy/xidyn/Presenter.java +++ b/src/fr/devinsy/xidyn/presenters/DomPresenter.java @@ -1,9 +1,10 @@ -package fr.devinsy.xidyn; +package fr.devinsy.xidyn.presenters; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.io.StringWriter; import java.io.Writer; import java.util.HashMap; import java.util.Iterator; @@ -24,22 +25,140 @@ import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; +import fr.devinsy.xidyn.data.SimpleTagData; +import fr.devinsy.xidyn.data.TagAttributes; +import fr.devinsy.xidyn.data.TagData; +import fr.devinsy.xidyn.data.TagDataListById; +import fr.devinsy.xidyn.data.TagDataListByIndex; +import fr.devinsy.xidyn.data.TagDataManager; + /** * */ -public class Presenter +public class DomPresenter extends Presenter { - static private Logger logger = LoggerFactory.getLogger(Presenter.class); static final public char INDEX_SEPARATOR = '_'; + static private Logger logger = LoggerFactory.getLogger(DomPresenter.class); + protected Document doc; /** * */ - static protected void addMetaTag(final Document doc, final String name, final String content) + public DomPresenter() + { + this.doc = null; + } + + /** + * + */ + public DomPresenter(final Document doc) + { + setSource(doc); + } + + /** + * + */ + @Override + public StringBuffer dynamize(final TagDataListById datas) throws Exception + { + StringBuffer result; + + if (this.doc == null) + { + String errorMessage = "source not defined"; + logger.error(errorMessage); + result = null; + throw new Exception(errorMessage); + } + else + { + // Build the web page. + StringWriter htmlCode = new StringWriter(); + DomPresenter.dynamize(htmlCode, this.doc, datas); + result = new StringBuffer(htmlCode.toString()); + } + + // + return (result); + } + + /** + * + */ + @Override + public StringBuffer dynamize(final TagDataManager datas) throws Exception + { + return (dynamize(datas.getIdsDataById())); + } + + /** + * + */ + public Document getDOM() + { + Document result; + + result = this.doc; + + // + return (result); + } + + /** + * + */ + @Override + public Object getSource() + { + Object result; + + result = this.doc; + + // + return (result); + } + + /** + * + */ + @Override + public boolean isOutdated() + { + boolean result; + + result = false; + + // + return result; + } + + /** + * + */ + public void setDOM(final Document doc) + { + this.doc = doc; + } + + /** + * + */ + public void setSource(final Document doc) + { + this.doc = doc; + DomPresenter.addMetaTag(this.doc, "generator", "XIDYN 1.0.0"); + } + + /** + * + */ + static public void addMetaTag(final Document doc, final String name, final String content) { // Find head tag. - Node headNode = Presenter.findHeadNode(doc); + Node headNode = findHeadNode(doc); Node metaNode = doc.createElement("meta"); @@ -57,7 +176,7 @@ public class Presenter /** * */ - static protected Document buildTree(final InputStream source) throws Exception + static public Document buildDom(final InputStream source) throws Exception { Document result; @@ -131,80 +250,39 @@ public class Presenter return (result); } + /** + * Xidyn a file with data. + */ + static public StringBuffer dynamize(final Document doc, final TagDataListById datas) throws Exception + { + StringBuffer result; + + StringWriter htmlCode = new StringWriter(); + DomPresenter.dynamize(htmlCode, doc, datas); + result = htmlCode.getBuffer(); + + // + return (result); + } + /** * Dynamize a file with data. */ static public void dynamize(final Writer result, final Document doc, final TagDataListById data) throws Exception { - Presenter.process(result, doc, data); - } - - /** - * Good estimation of the target length able to optimize performance. - */ - static int estimatedTargetLength(final long sourceLength) - { - int result; - - if (sourceLength < 1000) - { - result = (int) (sourceLength * 5); - } - else if (sourceLength < 50000) - { - result = (int) (sourceLength * 2); - } - else if (sourceLength < 800000) - { - result = (100000); - } - else - { - result = (int) (sourceLength + 30000); - } - - // - return (result); - } - - /** - * - */ - static public String extractBodyContent(final StringBuffer data) - { - String result = null; - - // Extract the body content. - String dataLowerCase = data.toString().toLowerCase(); - - int startBody = dataLowerCase.indexOf(""); - int endBody = dataLowerCase.indexOf(""); - - // Note: as failed search is improbable, no care about complexity - // in failed search case. - if ((startBody == -1) || (endBody == -1)) - { - result = null; - } - else - { - result = data.substring(startBody + 6, endBody).trim(); - } - - // - return (result); + process(result, doc, data); } /** * Define in Presenter cause needs this possibility. */ - static protected Document fileToTree(final String fileName) throws Exception + static public Document fileToDom(final String fileName) throws Exception { Document result; try { - result = buildTree(new FileInputStream(new File(fileName))); + result = buildDom(new FileInputStream(new File(fileName))); } catch (IOException exception) { @@ -225,7 +303,7 @@ public class Presenter * Document node. * @return The head tag node */ - static protected Node findHeadNode(final Node node) + private static Node findHeadNode(final Node node) { Node headNode = null; @@ -274,7 +352,7 @@ public class Presenter /** * */ - static public String getClassAttributeValue(final Node node) + private static String getClassAttributeValue(final Node node) { String result; @@ -306,10 +384,9 @@ public class Presenter * * @param node */ - static protected String getElementText(final Node node) + static private String getElementText(final Node node) { String result; - result = ""; // Grrrr, Java ... NodeList children = node.getChildNodes(); if ((children == null) || (children.getLength() == 0)) @@ -319,6 +396,7 @@ public class Presenter else { boolean ended = false; + result = ""; // Grrrr, Java ... int childCounter = 0; int childCount = children.getLength(); while (!ended) @@ -333,9 +411,8 @@ public class Presenter Node child = children.item(childCounter); if (child.getNodeType() == Node.TEXT_NODE) { - result = newLinesToSpaces(child.getNodeValue()); // STU - // (+=, - // newLines...) + // STU (+=, newLines...) + result = newLinesToSpaces(child.getNodeValue()); ended = true; } else @@ -353,7 +430,7 @@ public class Presenter /** * */ - static public TagAttributes mergeAttributes(final TagAttributes target, final TagAttributes source) + static private TagAttributes mergeAttributes(final TagAttributes target, final TagAttributes source) { TagAttributes result; @@ -399,13 +476,13 @@ public class Presenter /** * Converts New Line characters to spaces. This is used when for example the - * text in a div tag goes over serveral lines. + * text in a div tag goes over several lines. * * @param text * String * @return String */ - static protected String newLinesToSpaces(final String text) + static private String newLinesToSpaces(final String text) { StringBuffer result = new StringBuffer(text); @@ -424,16 +501,16 @@ public class Presenter /** * */ - static protected 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 datas) throws Exception { - Presenter.process(result, node, datas, ""); + DomPresenter.process(result, node, datas, ""); } /** * Recursive method that processes a node and any child nodes. * */ - static protected void process(final Writer result, final Node node, final TagDataListById datas, final String suffix) throws Exception + static private void process(final Writer result, final Node node, final TagDataListById datas, final String suffix) throws Exception { logger.debug("process - started"); String TRANSITIONAL_DTD = "xhtml1-transitional.dtd"; @@ -471,7 +548,7 @@ public class Presenter // Log.write(Log.TRACE,"systemId = " + systemId); } - Presenter.process(result, ((Document) node).getDocumentElement(), datas, suffix); + DomPresenter.process(result, ((Document) node).getDocumentElement(), datas, suffix); break; } @@ -485,11 +562,11 @@ public class Presenter if (idAttr != null) { - Presenter.processElementWithId(result, node, attrs, idAttr, datas, suffix); + DomPresenter.processElementWithId(result, node, attrs, idAttr, datas, suffix); } else { - Presenter.processElementBasically(result, node, datas, suffix); + DomPresenter.processElementBasically(result, node, datas, suffix); } break; @@ -522,7 +599,7 @@ public class Presenter case Node.TEXT_NODE: { logger.debug("case Node.TEXTE_NODE"); - result.append(restoreEntities(new StringBuffer(node.getNodeValue()))); + result.append(Presenter.restoreEntities(new StringBuffer(node.getNodeValue()))); break; } @@ -554,7 +631,7 @@ public class Presenter /** * */ - static protected StringBuffer processAttributes(final NamedNodeMap attrs) + static private StringBuffer processAttributes(final NamedNodeMap attrs) { StringBuffer result; @@ -567,7 +644,7 @@ public class Presenter /** * */ - static protected StringBuffer processAttributes(final NamedNodeMap attrs, final TagAttributes dataAttributes) + static private StringBuffer processAttributes(final NamedNodeMap attrs, final TagAttributes dataAttributes) { StringBuffer result; @@ -580,7 +657,7 @@ public class Presenter /** * */ - static protected StringBuffer processAttributes(final NamedNodeMap attrs, final TagAttributes dataAttributes, final String suffix) + static private StringBuffer processAttributes(final NamedNodeMap attrs, final TagAttributes dataAttributes, final String suffix) { StringBuffer result; @@ -630,7 +707,7 @@ public class Presenter if ((attribute.getKey().equals("id")) && (suffix.length() != 0)) { - result.append(" " + attribute.getKey() + "=\"" + attribute.getValue() + Presenter.INDEX_SEPARATOR + suffix + "\""); + result.append(" " + attribute.getKey() + "=\"" + attribute.getValue() + INDEX_SEPARATOR + suffix + "\""); } else { @@ -645,7 +722,7 @@ public class Presenter /** * */ - static protected StringBuffer processAttributes(final NamedNodeMap attrs, final TagAttributes dataAttributes, final TagAttributes namedDataAttributes, final String suffix) + static private StringBuffer processAttributes(final NamedNodeMap attrs, final TagAttributes dataAttributes, final TagAttributes namedDataAttributes, final String suffix) { StringBuffer result; @@ -658,7 +735,7 @@ public class Presenter /** * */ - static protected void processChildren(final Writer result, final Node node, final TagDataListById datas) throws Exception + static private void processChildren(final Writer result, final Node node, final TagDataListById datas) throws Exception { processChildren(result, node, datas, ""); } @@ -666,7 +743,7 @@ public class Presenter /** * */ - static protected void processChildren(final Writer result, final Node node, final TagDataListById datas, final String suffix) throws Exception + private static void processChildren(final Writer result, final Node node, final TagDataListById datas, final String suffix) throws Exception { // Get the iteration strategy. SimpleTagData.IterationStrategy strategy; @@ -795,9 +872,9 @@ public class Presenter } /** - * + * TODO remove? */ - static void processElementBasically(final Writer result, final Node node, final TagDataListById datas) throws Exception + private static void processElementBasically(final Writer result, final Node node, final TagDataListById datas) throws Exception { processElementBasically(result, node, datas, ""); } @@ -805,7 +882,7 @@ public class Presenter /** * */ - static void processElementBasically(final Writer result, final Node node, final TagDataListById datas, final String suffix) throws Exception + private static void processElementBasically(final Writer result, final Node node, final TagDataListById datas, final String suffix) throws Exception { logger.debug("processElementBasically - started [{}]", node.getNodeName()); @@ -848,7 +925,7 @@ public class Presenter * @param idAttr * The ID. */ - static protected void processElementWithId(final Writer result, final Node node, final NamedNodeMap attrs, final Node idAttr, final TagDataListById datas) throws Exception + static private void processElementWithId(final Writer result, final Node node, final NamedNodeMap attrs, final Node idAttr, final TagDataListById datas) throws Exception { processElementWithId(result, node, attrs, idAttr, datas, ""); @@ -865,7 +942,7 @@ public class Presenter * @param idAttr * The ID. */ - static protected void processElementWithId(final Writer result, final Node node, final NamedNodeMap attrs, final Node idAttr, final TagDataListById datas, final String suffix) throws Exception + static private void processElementWithId(final Writer result, final Node node, final NamedNodeMap attrs, final Node idAttr, final TagDataListById datas, final String suffix) throws Exception { String tag = node.getNodeName(); @@ -878,7 +955,7 @@ public class Presenter if (dataCore == null) { - Presenter.processElementBasically(result, node, datas, suffix); + DomPresenter.processElementBasically(result, node, datas, suffix); } else if (dataCore instanceof SimpleTagData) { @@ -972,7 +1049,7 @@ public class Presenter // Manage a Hashmap. TagDataListById data = (TagDataListById) tags.elementAt(nLine); - Presenter.processElementWithId(result, node, attrs, idAttr, data, Integer.toString(nLine)); + DomPresenter.processElementWithId(result, node, attrs, idAttr, data, Integer.toString(nLine)); result.append('\n'); } } @@ -985,61 +1062,4 @@ public class Presenter // logger.debug("Exit"); } - - /** - * Any ampersand lt;, ampersand gt; and ampersand amp; sequences in text - * nodes get read in as symbols. This method converts them back to entities. - * - * @param s - * String that is to have the entities restored.. - * @return The processed string. - */ - static public String restoreEntities(final StringBuffer s) - { - String result; - - if (s == null) - { - result = null; - } - else - { - StringBuffer str = new StringBuffer(); - - int len = (s != null) ? s.length() : 0; - for (int i = 0; i < len; i++) - { - char ch = s.charAt(i); - switch (ch) - { - case '<': - { - str.append("<"); - break; - } - - case '>': - { - str.append(">"); - break; - } - - case '&': - { - str.append("&"); - break; - } - - default: - { - str.append(ch); - } - } - } - result = str.toString(); - } - - // - return (result);// import javax.xml.parsers.*; - } } diff --git a/src/fr/devinsy/xidyn/FilePresenter.java b/src/fr/devinsy/xidyn/presenters/FilePresenter.java similarity index 87% rename from src/fr/devinsy/xidyn/FilePresenter.java rename to src/fr/devinsy/xidyn/presenters/FilePresenter.java index 6c78b42..55c9bde 100644 --- a/src/fr/devinsy/xidyn/FilePresenter.java +++ b/src/fr/devinsy/xidyn/presenters/FilePresenter.java @@ -1,4 +1,4 @@ -package fr.devinsy.xidyn; +package fr.devinsy.xidyn.presenters; import java.io.BufferedReader; import java.io.File; @@ -9,12 +9,15 @@ import java.net.URI; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import fr.devinsy.xidyn.data.TagDataListById; +import fr.devinsy.xidyn.data.TagDataManager; + /** * */ public class FilePresenter extends DomPresenter { - static private Logger logger = LoggerFactory.getLogger(FilePresenter.class); + static Logger logger = LoggerFactory.getLogger(FilePresenter.class); private String sourceFilePathname; private long sourceFileTime; @@ -75,11 +78,11 @@ public class FilePresenter extends DomPresenter else if ((this.doc == null) || (this.sourceFileTime != source.lastModified())) { this.sourceFileTime = source.lastModified(); - this.doc = Presenter.fileToTree(this.sourceFilePathname); + this.doc = DomPresenter.fileToDom(this.sourceFilePathname); this.doctype = getDoctype(this.sourceFilePathname); if (this.doc != null) { - Presenter.addMetaTag(doc, "generator", "XID 0.0"); + DomPresenter.addMetaTag(doc, "generator", "XID 0.0"); } } @@ -88,7 +91,7 @@ public class FilePresenter extends DomPresenter htmlCode.write(doctype); htmlCode.write('\n'); - Presenter.dynamize(htmlCode, doc, datas); + DomPresenter.dynamize(htmlCode, doc, datas); result = htmlCode.getBuffer(); // @@ -118,6 +121,21 @@ public class FilePresenter extends DomPresenter return (result); } + /** + * + */ + @Override + public boolean isOutdated() + { + boolean result; + + // TODO + result = false; + + // + return result; + } + /** * */ diff --git a/src/fr/devinsy/xidyn/FilePresenters.java b/src/fr/devinsy/xidyn/presenters/FilePresenters.java similarity index 98% rename from src/fr/devinsy/xidyn/FilePresenters.java rename to src/fr/devinsy/xidyn/presenters/FilePresenters.java index c3b0467..50560cc 100644 --- a/src/fr/devinsy/xidyn/FilePresenters.java +++ b/src/fr/devinsy/xidyn/presenters/FilePresenters.java @@ -4,7 +4,7 @@ * as published by the Free Software Foundation version 2 or any later version. */ -package fr.devinsy.xidyn; +package fr.devinsy.xidyn.presenters; import java.util.Vector; diff --git a/src/fr/devinsy/xidyn/FilePresentersProxy.java b/src/fr/devinsy/xidyn/presenters/FilePresentersProxy.java similarity index 95% rename from src/fr/devinsy/xidyn/FilePresentersProxy.java rename to src/fr/devinsy/xidyn/presenters/FilePresentersProxy.java index d1726e5..ef1c902 100644 --- a/src/fr/devinsy/xidyn/FilePresentersProxy.java +++ b/src/fr/devinsy/xidyn/presenters/FilePresentersProxy.java @@ -1,4 +1,4 @@ -package fr.devinsy.xidyn; +package fr.devinsy.xidyn.presenters; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/fr/devinsy/xidyn/ParserErrorHandler.java b/src/fr/devinsy/xidyn/presenters/ParserErrorHandler.java similarity index 93% rename from src/fr/devinsy/xidyn/ParserErrorHandler.java rename to src/fr/devinsy/xidyn/presenters/ParserErrorHandler.java index 6a606f2..d6facb3 100644 --- a/src/fr/devinsy/xidyn/ParserErrorHandler.java +++ b/src/fr/devinsy/xidyn/presenters/ParserErrorHandler.java @@ -1,4 +1,4 @@ -package fr.devinsy.xidyn; +package fr.devinsy.xidyn.presenters; import java.util.Vector; diff --git a/src/fr/devinsy/xidyn/presenters/Presenter.java b/src/fr/devinsy/xidyn/presenters/Presenter.java new file mode 100644 index 0000000..3dc246d --- /dev/null +++ b/src/fr/devinsy/xidyn/presenters/Presenter.java @@ -0,0 +1,151 @@ +package fr.devinsy.xidyn.presenters; + +import fr.devinsy.xidyn.data.TagDataListById; +import fr.devinsy.xidyn.data.TagDataManager; + +/** + * + */ +public abstract class Presenter +{ + /** + * + * @param datas + * @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; + + /** + * + * @return + */ + public abstract Object getSource(); + + /** + * + * @return + */ + public abstract boolean isOutdated(); + + /** + * Good estimation of the target length able to optimize performance. + */ + public static int estimatedTargetLength(final long sourceLength) + { + int result; + + if (sourceLength < 1000) + { + result = (int) (sourceLength * 5); + } + else if (sourceLength < 50000) + { + result = (int) (sourceLength * 2); + } + else if (sourceLength < 800000) + { + result = (100000); + } + else + { + result = (int) (sourceLength + 30000); + } + + // + return (result); + } + + /** + * + */ + static public String extractBodyContent(final StringBuffer data) + { + String result = null; + + // Extract the body content. + String dataLowerCase = data.toString().toLowerCase(); + + int startBody = dataLowerCase.indexOf(""); + int endBody = dataLowerCase.indexOf(""); + + // Note: as failed search is improbable, no care about complexity + // in failed search case. + if ((startBody == -1) || (endBody == -1)) + { + result = null; + } + else + { + result = data.substring(startBody + 6, endBody).trim(); + } + + // + return (result); + } + + /** + * Any ampersand lt;, ampersand gt; and ampersand amp; sequences in text + * nodes get read in as symbols. This method converts them back to entities. + * + * @param source + * String that is to have the entities restored.. + * @return The processed string. + */ + static public String restoreEntities(final StringBuffer source) + { + String result; + + if (source == null) + { + result = null; + } + else + { + StringBuffer buffer = new StringBuffer(source.length() * 2); + + int len = (source != null) ? source.length() : 0; + for (int index = 0; index < len; index++) + { + char letter = source.charAt(index); + switch (letter) + { + case '<': + { + buffer.append("<"); + break; + } + + case '>': + { + buffer.append(">"); + break; + } + + case '&': + { + buffer.append("&"); + break; + } + + default: + { + buffer.append(letter); + } + } + } + result = buffer.toString(); + } + + // + return (result); + } +} diff --git a/src/fr/devinsy/xidyn/StringPresenter.java b/src/fr/devinsy/xidyn/presenters/StringPresenter.java similarity index 91% rename from src/fr/devinsy/xidyn/StringPresenter.java rename to src/fr/devinsy/xidyn/presenters/StringPresenter.java index 3227f01..ec2e868 100644 --- a/src/fr/devinsy/xidyn/StringPresenter.java +++ b/src/fr/devinsy/xidyn/presenters/StringPresenter.java @@ -1,4 +1,4 @@ -package fr.devinsy.xidyn; +package fr.devinsy.xidyn.presenters; import java.io.ByteArrayInputStream; import java.io.StringWriter; @@ -6,6 +6,9 @@ import java.io.StringWriter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import fr.devinsy.xidyn.data.TagDataListById; +import fr.devinsy.xidyn.data.TagDataManager; + /** * */ @@ -62,7 +65,7 @@ public class StringPresenter extends DomPresenter // 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())); + doc = DomPresenter.buildDom(new ByteArrayInputStream(htmlSource.getBytes())); } StringWriter htmlCode = new StringWriter(Presenter.estimatedTargetLength(this.html.length())); @@ -70,7 +73,7 @@ public class StringPresenter extends DomPresenter { htmlCode.write(this.html.substring(0, this.html.indexOf('>'))); } - Presenter.dynamize(htmlCode, doc, datas); + DomPresenter.dynamize(htmlCode, doc, datas); StringBuffer htmlTarget = htmlCode.getBuffer(); if (htmlTarget == null) diff --git a/src/fr/devinsy/xidyn/URLPresenter.java b/src/fr/devinsy/xidyn/presenters/URLPresenter.java similarity index 93% rename from src/fr/devinsy/xidyn/URLPresenter.java rename to src/fr/devinsy/xidyn/presenters/URLPresenter.java index e07b15d..4777bd1 100644 --- a/src/fr/devinsy/xidyn/URLPresenter.java +++ b/src/fr/devinsy/xidyn/presenters/URLPresenter.java @@ -1,4 +1,4 @@ -package fr.devinsy.xidyn; +package fr.devinsy.xidyn.presenters; import java.io.BufferedReader; import java.io.FileReader; @@ -10,6 +10,9 @@ import java.net.URL; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import fr.devinsy.xidyn.data.TagDataListById; +import fr.devinsy.xidyn.data.TagDataManager; + /** * */ @@ -94,11 +97,11 @@ public class URLPresenter extends DomPresenter if ((this.doc == null) || (this.sourceFileTime != lastModified)) { this.sourceFileTime = lastModified; - this.doc = Presenter.buildTree(this.sourceURL.openStream()); + this.doc = DomPresenter.buildDom(this.sourceURL.openStream()); this.doctype = ""; // TODO Made generic. if (this.doc != null) { - Presenter.addMetaTag(doc, "generator", "XID 0.0"); + DomPresenter.addMetaTag(doc, "generator", "XID 0.0"); } } @@ -107,7 +110,7 @@ public class URLPresenter extends DomPresenter htmlCode.write(doctype); htmlCode.write('\n'); - Presenter.dynamize(htmlCode, doc, datas); + DomPresenter.dynamize(htmlCode, doc, datas); result = htmlCode.getBuffer(); } diff --git a/test/XidynTest.java b/test/XidynTest.java index 7045c1a..d72157f 100644 --- a/test/XidynTest.java +++ b/test/XidynTest.java @@ -6,9 +6,9 @@ import org.apache.log4j.Level; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import fr.devinsy.xidyn.SimpleTagData; -import fr.devinsy.xidyn.StringPresenter; -import fr.devinsy.xidyn.TagDataManager; +import fr.devinsy.xidyn.data.SimpleTagData; +import fr.devinsy.xidyn.data.TagDataManager; +import fr.devinsy.xidyn.presenters.StringPresenter; /** *