Refactor hugly.

This commit is contained in:
Christian P. MOMON 2013-07-13 18:46:25 +02:00
parent 92cad653b2
commit db09c5a330
17 changed files with 375 additions and 306 deletions

View file

@ -6,9 +6,9 @@ import org.apache.log4j.Level;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import fr.devinsy.xidyn.SimpleTagData; import fr.devinsy.xidyn.data.SimpleTagData;
import fr.devinsy.xidyn.StringPresenter; import fr.devinsy.xidyn.data.TagDataManager;
import fr.devinsy.xidyn.TagDataManager; import fr.devinsy.xidyn.presenters.StringPresenter;
/** /**
* *

View file

@ -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);
}
}

View file

@ -1,4 +1,4 @@
package fr.devinsy.xidyn; package fr.devinsy.xidyn.data;
import java.io.Serializable; import java.io.Serializable;

View file

@ -1,4 +1,4 @@
package fr.devinsy.xidyn; package fr.devinsy.xidyn.data;
import java.util.HashMap; import java.util.HashMap;

View file

@ -1,4 +1,4 @@
package fr.devinsy.xidyn; package fr.devinsy.xidyn.data;
/* /*
* Xid uses three class to describe data: * Xid uses three class to describe data:

View file

@ -1,4 +1,4 @@
package fr.devinsy.xidyn; package fr.devinsy.xidyn.data;
import java.util.HashMap; import java.util.HashMap;

View file

@ -1,4 +1,4 @@
package fr.devinsy.xidyn; package fr.devinsy.xidyn.data;
import java.util.Vector; import java.util.Vector;

View file

@ -1,4 +1,4 @@
package fr.devinsy.xidyn; package fr.devinsy.xidyn.data;
/* /*
* *

View file

@ -1,9 +1,10 @@
package fr.devinsy.xidyn; package fr.devinsy.xidyn.presenters;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.StringWriter;
import java.io.Writer; import java.io.Writer;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
@ -24,22 +25,140 @@ import org.w3c.dom.Node;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
import org.xml.sax.SAXException; 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 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. // Find head tag.
Node headNode = Presenter.findHeadNode(doc); Node headNode = findHeadNode(doc);
Node metaNode = doc.createElement("meta"); 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; Document result;
@ -131,80 +250,39 @@ public class Presenter
return (result); 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. * Dynamize a file with data.
*/ */
static public void dynamize(final Writer result, final Document doc, final TagDataListById data) throws Exception static public void dynamize(final Writer result, final Document doc, final TagDataListById data) throws Exception
{ {
Presenter.process(result, doc, data); 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("<body>");
int endBody = dataLowerCase.indexOf("</body>");
// 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);
} }
/** /**
* Define in Presenter cause <object> needs this possibility. * Define in Presenter cause <object> needs this possibility.
*/ */
static protected Document fileToTree(final String fileName) throws Exception static public Document fileToDom(final String fileName) throws Exception
{ {
Document result; Document result;
try try
{ {
result = buildTree(new FileInputStream(new File(fileName))); result = buildDom(new FileInputStream(new File(fileName)));
} }
catch (IOException exception) catch (IOException exception)
{ {
@ -225,7 +303,7 @@ public class Presenter
* Document node. * Document node.
* @return The head tag node * @return The head tag node
*/ */
static protected Node findHeadNode(final Node node) private static Node findHeadNode(final Node node)
{ {
Node headNode = null; 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; String result;
@ -306,10 +384,9 @@ public class Presenter
* *
* @param node * @param node
*/ */
static protected String getElementText(final Node node) static private String getElementText(final Node node)
{ {
String result; String result;
result = ""; // Grrrr, Java ...
NodeList children = node.getChildNodes(); NodeList children = node.getChildNodes();
if ((children == null) || (children.getLength() == 0)) if ((children == null) || (children.getLength() == 0))
@ -319,6 +396,7 @@ public class Presenter
else else
{ {
boolean ended = false; boolean ended = false;
result = ""; // Grrrr, Java ...
int childCounter = 0; int childCounter = 0;
int childCount = children.getLength(); int childCount = children.getLength();
while (!ended) while (!ended)
@ -333,9 +411,8 @@ public class Presenter
Node child = children.item(childCounter); Node child = children.item(childCounter);
if (child.getNodeType() == Node.TEXT_NODE) if (child.getNodeType() == Node.TEXT_NODE)
{ {
result = newLinesToSpaces(child.getNodeValue()); // STU // STU (+=, newLines...)
// (+=, result = newLinesToSpaces(child.getNodeValue());
// newLines...)
ended = true; ended = true;
} }
else 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; TagAttributes result;
@ -399,13 +476,13 @@ public class Presenter
/** /**
* Converts New Line characters to spaces. This is used when for example the * 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 * @param text
* String * String
* @return String * @return String
*/ */
static protected String newLinesToSpaces(final String text) static private String newLinesToSpaces(final String text)
{ {
StringBuffer result = new StringBuffer(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. * 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"); logger.debug("process - started");
String TRANSITIONAL_DTD = "xhtml1-transitional.dtd"; String TRANSITIONAL_DTD = "xhtml1-transitional.dtd";
@ -471,7 +548,7 @@ public class Presenter
// Log.write(Log.TRACE,"systemId = " + systemId); // Log.write(Log.TRACE,"systemId = " + systemId);
} }
Presenter.process(result, ((Document) node).getDocumentElement(), datas, suffix); DomPresenter.process(result, ((Document) node).getDocumentElement(), datas, suffix);
break; break;
} }
@ -485,11 +562,11 @@ public class Presenter
if (idAttr != null) if (idAttr != null)
{ {
Presenter.processElementWithId(result, node, attrs, idAttr, datas, suffix); DomPresenter.processElementWithId(result, node, attrs, idAttr, datas, suffix);
} }
else else
{ {
Presenter.processElementBasically(result, node, datas, suffix); DomPresenter.processElementBasically(result, node, datas, suffix);
} }
break; break;
@ -522,7 +599,7 @@ public class Presenter
case Node.TEXT_NODE: case Node.TEXT_NODE:
{ {
logger.debug("case Node.TEXTE_NODE"); logger.debug("case Node.TEXTE_NODE");
result.append(restoreEntities(new StringBuffer(node.getNodeValue()))); result.append(Presenter.restoreEntities(new StringBuffer(node.getNodeValue())));
break; break;
} }
@ -554,7 +631,7 @@ public class Presenter
/** /**
* *
*/ */
static protected StringBuffer processAttributes(final NamedNodeMap attrs) static private StringBuffer processAttributes(final NamedNodeMap attrs)
{ {
StringBuffer result; 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; 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; StringBuffer result;
@ -630,7 +707,7 @@ public class Presenter
if ((attribute.getKey().equals("id")) && (suffix.length() != 0)) 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 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; 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, ""); 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. // Get the iteration strategy.
SimpleTagData.IterationStrategy 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, ""); 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()); logger.debug("processElementBasically - started [{}]", node.getNodeName());
@ -848,7 +925,7 @@ public class Presenter
* @param idAttr * @param idAttr
* The ID. * 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, ""); processElementWithId(result, node, attrs, idAttr, datas, "");
@ -865,7 +942,7 @@ public class Presenter
* @param idAttr * @param idAttr
* The ID. * 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(); String tag = node.getNodeName();
@ -878,7 +955,7 @@ public class Presenter
if (dataCore == null) if (dataCore == null)
{ {
Presenter.processElementBasically(result, node, datas, suffix); DomPresenter.processElementBasically(result, node, datas, suffix);
} }
else if (dataCore instanceof SimpleTagData) else if (dataCore instanceof SimpleTagData)
{ {
@ -972,7 +1049,7 @@ public class Presenter
// Manage a Hashmap. // Manage a Hashmap.
TagDataListById data = (TagDataListById) tags.elementAt(nLine); 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'); result.append('\n');
} }
} }
@ -985,61 +1062,4 @@ public class Presenter
// //
logger.debug("Exit"); 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("&lt;");
break;
}
case '>':
{
str.append("&gt;");
break;
}
case '&':
{
str.append("&amp;");
break;
}
default:
{
str.append(ch);
}
}
}
result = str.toString();
}
//
return (result);// import javax.xml.parsers.*;
}
} }

View file

@ -1,4 +1,4 @@
package fr.devinsy.xidyn; package fr.devinsy.xidyn.presenters;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
@ -9,12 +9,15 @@ import java.net.URI;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import fr.devinsy.xidyn.data.TagDataListById;
import fr.devinsy.xidyn.data.TagDataManager;
/** /**
* *
*/ */
public class FilePresenter extends DomPresenter public class FilePresenter extends DomPresenter
{ {
static private Logger logger = LoggerFactory.getLogger(FilePresenter.class); static Logger logger = LoggerFactory.getLogger(FilePresenter.class);
private String sourceFilePathname; private String sourceFilePathname;
private long sourceFileTime; private long sourceFileTime;
@ -75,11 +78,11 @@ public class FilePresenter extends DomPresenter
else if ((this.doc == null) || (this.sourceFileTime != source.lastModified())) else if ((this.doc == null) || (this.sourceFileTime != source.lastModified()))
{ {
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); this.doctype = getDoctype(this.sourceFilePathname);
if (this.doc != null) 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(doctype);
htmlCode.write('\n'); htmlCode.write('\n');
Presenter.dynamize(htmlCode, doc, datas); DomPresenter.dynamize(htmlCode, doc, datas);
result = htmlCode.getBuffer(); result = htmlCode.getBuffer();
// //
@ -118,6 +121,21 @@ public class FilePresenter extends DomPresenter
return (result); return (result);
} }
/**
*
*/
@Override
public boolean isOutdated()
{
boolean result;
// TODO
result = false;
//
return result;
}
/** /**
* *
*/ */

View file

@ -4,7 +4,7 @@
* as published by the Free Software Foundation version 2 or any later version. * 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; import java.util.Vector;

View file

@ -1,4 +1,4 @@
package fr.devinsy.xidyn; package fr.devinsy.xidyn.presenters;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;

View file

@ -1,4 +1,4 @@
package fr.devinsy.xidyn; package fr.devinsy.xidyn.presenters;
import java.util.Vector; import java.util.Vector;

View file

@ -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<E>
{
/**
*
* @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("<body>");
int endBody = dataLowerCase.indexOf("</body>");
// 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("&lt;");
break;
}
case '>':
{
buffer.append("&gt;");
break;
}
case '&':
{
buffer.append("&amp;");
break;
}
default:
{
buffer.append(letter);
}
}
}
result = buffer.toString();
}
//
return (result);
}
}

View file

@ -1,4 +1,4 @@
package fr.devinsy.xidyn; package fr.devinsy.xidyn.presenters;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.StringWriter; import java.io.StringWriter;
@ -6,6 +6,9 @@ import java.io.StringWriter;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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. // StringBufferInputStream is deprecated so we use another solution.
// (see // (see
// http://www.developpez.net/forums/archive/index.php/t-14101.html). // 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())); 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('>'))); htmlCode.write(this.html.substring(0, this.html.indexOf('>')));
} }
Presenter.dynamize(htmlCode, doc, datas); DomPresenter.dynamize(htmlCode, doc, datas);
StringBuffer htmlTarget = htmlCode.getBuffer(); StringBuffer htmlTarget = htmlCode.getBuffer();
if (htmlTarget == null) if (htmlTarget == null)

View file

@ -1,4 +1,4 @@
package fr.devinsy.xidyn; package fr.devinsy.xidyn.presenters;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileReader; import java.io.FileReader;
@ -10,6 +10,9 @@ import java.net.URL;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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)) if ((this.doc == null) || (this.sourceFileTime != lastModified))
{ {
this.sourceFileTime = lastModified; this.sourceFileTime = lastModified;
this.doc = Presenter.buildTree(this.sourceURL.openStream()); this.doc = DomPresenter.buildDom(this.sourceURL.openStream());
this.doctype = "<!DOCTYPE HTML>"; // TODO Made generic. this.doctype = "<!DOCTYPE HTML>"; // TODO Made generic.
if (this.doc != null) 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(doctype);
htmlCode.write('\n'); htmlCode.write('\n');
Presenter.dynamize(htmlCode, doc, datas); DomPresenter.dynamize(htmlCode, doc, datas);
result = htmlCode.getBuffer(); result = htmlCode.getBuffer();
} }

View file

@ -6,9 +6,9 @@ import org.apache.log4j.Level;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import fr.devinsy.xidyn.SimpleTagData; import fr.devinsy.xidyn.data.SimpleTagData;
import fr.devinsy.xidyn.StringPresenter; import fr.devinsy.xidyn.data.TagDataManager;
import fr.devinsy.xidyn.TagDataManager; import fr.devinsy.xidyn.presenters.StringPresenter;
/** /**
* *