Improve and simplify code.

This commit is contained in:
Christian P. MOMON 2013-08-01 01:07:41 +02:00
parent 10f6cb40e6
commit 9c887e6cc4
6 changed files with 253 additions and 150 deletions

View file

@ -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); 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() public DomPresenter()
{ {
this.doc = null; this.doc = null;
this.isOutdated = false;
} }
/** /**
@ -34,22 +36,39 @@ public class DomPresenter extends DomPresenterCore implements Presenter
setSource(doc); setSource(doc);
} }
/**
* {@inheritDoc}
*/
@Override @Override
public StringBuffer dynamize() throws Exception public StringBuffer dynamize() throws Exception
{ {
// TODO Auto-generated method stub StringBuffer result;
return null;
if ((this.isOutdated) || (this.defaultHtmlTarget == null))
{
this.defaultHtmlTarget = dynamize(null);
this.isOutdated = false;
}
result = this.defaultHtmlTarget;
//
return result;
} }
/** /**
* * {@inheritDoc}
*/ */
@Override @Override
public StringBuffer dynamize(final TagDataListById datas) throws Exception public StringBuffer dynamize(final TagDataManager data) throws Exception
{ {
StringBuffer result; StringBuffer result;
if (this.doc == null) if (data == null)
{
result = dynamize(new TagDataManager());
}
else if (this.doc == null)
{ {
String errorMessage = "source not defined"; String errorMessage = "source not defined";
logger.error(errorMessage); logger.error(errorMessage);
@ -59,24 +78,16 @@ public class DomPresenter extends DomPresenterCore implements Presenter
else else
{ {
// Build the web page. // Build the web page.
StringWriter htmlCode = new StringWriter(); StringWriter writer = new StringWriter(20000);
DomPresenterCore.dynamize(htmlCode, this.doc, datas); DomPresenterCore.dynamize(writer, this.doc, data);
result = new StringBuffer(htmlCode.toString()); result = writer.getBuffer();
this.isOutdated = false;
} }
// //
return (result); 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 @Override
public Object getSource() public Object getSource()
@ -105,14 +116,14 @@ public class DomPresenter extends DomPresenterCore implements Presenter
} }
/** /**
* * {@inheritDoc}
*/ */
@Override @Override
public boolean isOutdated() throws Exception public boolean isOutdated() throws Exception
{ {
boolean result; boolean result;
result = false; result = this.isOutdated;
// //
return result; return result;
@ -124,6 +135,7 @@ public class DomPresenter extends DomPresenterCore implements Presenter
public void setDOM(final Document doc) public void setDOM(final Document doc)
{ {
this.doc = doc; this.doc = doc;
this.isOutdated = true;
} }
/** /**
@ -132,6 +144,51 @@ public class DomPresenter extends DomPresenterCore implements Presenter
public void setSource(final Document doc) public void setSource(final Document doc)
{ {
this.doc = 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;
}
} }

View file

@ -40,8 +40,9 @@ public class DomPresenterCore
{ {
static final public char INDEX_SEPARATOR = '_'; static final public char INDEX_SEPARATOR = '_';
static private Logger logger = LoggerFactory.getLogger(DomPresenterCore.class); 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) 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 static public Document buildDom(final InputStream source) throws Exception
{ {
@ -110,7 +111,7 @@ public class DomPresenterCore
} }
else else
{ {
DomPresenterCore.addMetaTag(result, "generator", "XID 0.0"); DomPresenterCore.addMetaTag(result, "generator", "XIDYN");
} }
} }
catch (ParserConfigurationException exception) 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; StringBuffer result;
StringWriter htmlCode = new StringWriter(); StringWriter htmlCode = new StringWriter();
DomPresenterCore.dynamize(htmlCode, doc, datas); DomPresenterCore.dynamize(htmlCode, doc, data);
result = htmlCode.getBuffer(); 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 static public void dynamize(final Writer result, final Document doc, final TagDataListById data) throws Exception
{ {
process(result, doc, data); 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 <object> needs this possibility. * Define in Presenter cause <object> 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, "");
} }
/** /**

View file

@ -8,18 +8,19 @@ import java.net.URI;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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.data.TagDataManager;
import fr.devinsy.xidyn.utils.XidynUtils; import fr.devinsy.xidyn.utils.XidynUtils;
/** /**
* *
*/ */
public class FilePresenter extends DomPresenter public class FilePresenter implements Presenter
{ {
static Logger logger = LoggerFactory.getLogger(FilePresenter.class); static Logger logger = LoggerFactory.getLogger(FilePresenter.class);
private Document doc;
private String sourceFilePathname; private String sourceFilePathname;
private long sourceFileTime; private long sourceFileTime;
private String doctype; private String doctype;
@ -49,10 +50,26 @@ public class FilePresenter extends DomPresenter
} }
/** /**
* No need to be synchronized. * {@inheritDoc}
*/ */
@Override @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; StringBuffer result;
@ -79,12 +96,12 @@ 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 = DomPresenter.fileToDom(this.sourceFilePathname); this.doc = DomPresenterCore.fileToDom(this.sourceFilePathname);
this.doctype = getDoctype(this.sourceFilePathname);
if (this.doc != null) 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. // Build the web page.
@ -92,7 +109,16 @@ public class FilePresenter extends DomPresenter
htmlCode.write(doctype); htmlCode.write(doctype);
htmlCode.write('\n'); 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(); result = htmlCode.getBuffer();
// //
@ -100,16 +126,7 @@ public class FilePresenter extends DomPresenter
} }
/** /**
* * {@inheritDoc}
*/
@Override
public StringBuffer dynamize(final TagDataManager datas) throws Exception
{
return (dynamize(datas.getIdsDataById()));
}
/**
*
*/ */
@Override @Override
public String getSource() public String getSource()
@ -123,7 +140,7 @@ public class FilePresenter extends DomPresenter
} }
/** /**
* * {@inheritDoc}
*/ */
@Override @Override
public boolean isOutdated() public boolean isOutdated()

View file

@ -1,13 +1,11 @@
package fr.devinsy.xidyn.presenters; package fr.devinsy.xidyn.presenters;
import fr.devinsy.xidyn.data.TagDataListById;
import fr.devinsy.xidyn.data.TagDataManager; import fr.devinsy.xidyn.data.TagDataManager;
/** /**
* *
*/ */
public interface Presenter public interface Presenter {
{
/** /**
* *
@ -15,7 +13,7 @@ public interface Presenter
* @return * @return
* @throws Exception * @throws Exception
*/ */
public abstract StringBuffer dynamize() throws Exception; public StringBuffer dynamize() throws Exception;
/** /**
* *
@ -23,25 +21,17 @@ public interface Presenter
* @return * @return
* @throws Exception * @throws Exception
*/ */
public abstract StringBuffer dynamize(final TagDataListById datas) throws Exception; public StringBuffer dynamize(final TagDataManager datas) throws Exception;
/**
*
* @param datas
* @return
* @throws Exception
*/
public abstract StringBuffer dynamize(final TagDataManager datas) throws Exception;
/** /**
* *
* @return * @return
*/ */
public abstract Object getSource(); public Object getSource();
/** /**
* *
* @return * @return
*/ */
public abstract boolean isOutdated() throws Exception; public boolean isOutdated() throws Exception;
} }

View file

@ -5,28 +5,27 @@ import java.io.StringWriter;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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.data.TagDataManager;
import fr.devinsy.xidyn.utils.XidynUtils; import fr.devinsy.xidyn.utils.XidynUtils;
/** /**
* *
*/ */
public class StringPresenter extends DomPresenter public class StringPresenter implements Presenter
{ {
static private Logger logger = LoggerFactory.getLogger(StringPresenter.class); static private Logger logger = LoggerFactory.getLogger(StringPresenter.class);
private String html; private String source;
private Document doc;
/** /**
* *
*/ */
public StringPresenter() public StringPresenter()
{ {
super(); setSource("");
this.doc = null;
this.html = null;
} }
/** /**
@ -34,68 +33,99 @@ public class StringPresenter extends DomPresenter
*/ */
public StringPresenter(final String html) public StringPresenter(final String html)
{ {
this.doc = null; setSource(html);
this.html = html;
} }
/** /**
* * {@inheritDoc}
*/ */
@Override @Override
public StringBuffer dynamize(final TagDataListById datas) throws Exception public StringBuffer dynamize() throws Exception
{ {
StringBuffer result; 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) if (this.doc == null)
{ {
// Build doc from this.html. // Build doc from this.html.
String htmlSource; String sourceHtml;
if ((this.html.startsWith("<!DOCTYPE")) || (this.html.startsWith("<!doctype")) || (this.html.startsWith("<html>")) || (this.html.startsWith("<HTML>"))) if (source == null)
{ {
htmlSource = html; String errorMessage = "source not defined";
logger.error(errorMessage);
result = null;
throw new Exception(errorMessage);
}
else if ((this.source.startsWith("<!DOCTYPE")) || (this.source.startsWith("<!doctype")) || (this.source.startsWith("<html>")) || (this.source.startsWith("<HTML>")))
{
sourceHtml = this.source;
} }
else else
{ {
StringBuffer buffer = new StringBuffer(html.length() + 100); StringBuffer buffer = new StringBuffer(source.length() + 100);
buffer.append("<html><head></head><body>\n"); buffer.append("<html><head></head><body>\n");
buffer.append(html); buffer.append(source);
buffer.append("</body></html>"); buffer.append("</body></html>");
htmlSource = buffer.toString(); sourceHtml = buffer.toString();
} }
// 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 = 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("<!DOCTYPE")) || (this.html.startsWith("<!doctype"))) if (datas == null)
{ {
htmlCode.write(this.html.substring(0, this.html.indexOf('>'))); result = dynamize();
}
DomPresenter.dynamize(htmlCode, doc, datas);
StringBuffer htmlTarget = htmlCode.getBuffer();
if (htmlTarget == null)
{
result = null;
}
else if ((this.html.startsWith("<!DOCTYPE")) || (this.html.startsWith("<!doctype")) || (this.html.startsWith("<html>")) || (this.html.startsWith("<HTML>")))
{
result = htmlTarget;
} }
else else
{ {
String bodyContent = XidynUtils.extractBodyContent(htmlTarget); //
StringWriter htmlCode = new StringWriter(XidynUtils.estimatedTargetLength(this.source.length()));
if ((this.source.startsWith("<!DOCTYPE")) || (this.source.startsWith("<!doctype")))
{
htmlCode.write(this.source.substring(0, this.source.indexOf('>')));
}
DomPresenterCore.dynamize(htmlCode, doc, datas);
StringBuffer htmlTarget = htmlCode.getBuffer();
if (bodyContent == null) //
if (htmlTarget == null)
{ {
result = null; result = null;
} }
else if ((this.source.startsWith("<!DOCTYPE")) || (this.source.startsWith("<!doctype")) || (this.source.startsWith("<html>")) || (this.source.startsWith("<HTML>")))
{
result = htmlTarget;
}
else 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
} }
/** /**
* * {@inheritDoc}
*/
@Override
public StringBuffer dynamize(final TagDataManager datas) throws Exception
{
return (dynamize(datas.getIdsDataById()));
}
/**
*
*/ */
@Override @Override
public String getSource() public String getSource()
{ {
String result; String result;
result = this.html; result = this.source;
// //
return (result); 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) public void setSource(final String html)
{ {
this.html = html; this.source = html;
this.doc = null; 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; StringBuffer result;
@ -149,12 +191,4 @@ public class StringPresenter extends DomPresenter
// //
return (result); return (result);
} }
/**
*
*/
static public StringBuffer dynamize(final String html, final TagDataManager datas) throws Exception
{
return (dynamize(html, datas.getIdsDataById()));
}
} }

View file

@ -3,25 +3,25 @@ package fr.devinsy.xidyn.presenters;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter; import java.io.StringWriter;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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.data.TagDataManager;
import fr.devinsy.xidyn.utils.XidynUtils; import fr.devinsy.xidyn.utils.XidynUtils;
/** /**
* *
*/ */
public class URLPresenter extends DomPresenter public class URLPresenter implements Presenter
{ {
static private Logger logger = LoggerFactory.getLogger(URLPresenter.class); static private Logger logger = LoggerFactory.getLogger(URLPresenter.class);
private Document doc;
private String sourcePathname; private String sourcePathname;
private URL sourceURL; private URL sourceURL;
private long sourceFileTime; private long sourceFileTime;
@ -59,7 +59,7 @@ public class URLPresenter extends DomPresenter
{ {
StringBuffer result; StringBuffer result;
result = dynamize((TagDataListById) null); result = dynamize((TagDataManager) null);
// //
return result; return result;
@ -69,7 +69,7 @@ public class URLPresenter extends DomPresenter
* No need to be synchronized. * No need to be synchronized.
*/ */
@Override @Override
public StringBuffer dynamize(final TagDataListById datas) throws Exception public StringBuffer dynamize(final TagDataManager datas) throws Exception
{ {
StringBuffer result; StringBuffer result;
@ -84,14 +84,7 @@ public class URLPresenter extends DomPresenter
} }
else if (datas == null) else if (datas == null)
{ {
result = new StringBuffer(sourceURL.openConnection().getContentLength()); result = dynamize(new TagDataManager());
BufferedReader in = new BufferedReader(new InputStreamReader(this.sourceURL.openStream()));
String line;
while ((line = in.readLine()) != null)
{
result.append(line);
}
in.close();
} }
else else
{ {
@ -99,12 +92,12 @@ 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 = DomPresenter.buildDom(this.sourceURL.openStream()); this.doc = DomPresenterCore.buildDom(this.sourceURL.openStream());
this.doctype = "<!DOCTYPE HTML>"; // TODO Made generic.
if (this.doc != null) if (this.doc != null)
{ {
DomPresenter.addMetaTag(doc, "generator", "XID 0.0"); DomPresenterCore.addMetaTag(doc, "generator", "XID 0.0");
} }
this.doctype = "<!DOCTYPE HTML>"; // TODO Made generic.
} }
// Build the web page. // Build the web page.
@ -112,7 +105,7 @@ public class URLPresenter extends DomPresenter
htmlCode.write(doctype); htmlCode.write(doctype);
htmlCode.write('\n'); htmlCode.write('\n');
DomPresenter.dynamize(htmlCode, doc, datas); DomPresenterCore.dynamize(htmlCode, doc, datas);
result = htmlCode.getBuffer(); result = htmlCode.getBuffer();
} }
@ -120,15 +113,6 @@ public class URLPresenter extends DomPresenter
return (result); return (result);
} }
/**
* {@inheritDoc}
*/
@Override
public StringBuffer dynamize(final TagDataManager datas) throws Exception
{
return (dynamize(datas.getIdsDataById()));
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -200,7 +184,6 @@ public class URLPresenter extends DomPresenter
/** /**
* *
* @param source * @param source
* @throws MalformedURLException
*/ */
public void setSource(final URL source) public void setSource(final URL source)
{ {