Add URLPresenter. Improve FilePresenter. Normalize source() as

getSource().
This commit is contained in:
Christian P. MOMON 2013-07-01 01:33:27 +02:00
parent 47e9166092
commit 68006d388d
6 changed files with 269 additions and 26 deletions

View file

@ -99,7 +99,7 @@ public class DomPresenter extends Presenter
/**
*
*/
public Object source()
public Object getSource()
{
Object result;

View file

@ -4,6 +4,7 @@ import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.StringWriter;
import java.net.URI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -24,10 +25,15 @@ public class FilePresenter extends DomPresenter
*/
public FilePresenter()
{
this.sourceFilePathname = null;
this.sourceFileTime = 0;
this.doc = null;
this.doctype = "";
setSource((String) null);
}
/**
*
*/
public FilePresenter(final File source)
{
setSource(source);
}
/**
@ -35,10 +41,7 @@ public class FilePresenter extends DomPresenter
*/
public FilePresenter(final String filePathname)
{
this.sourceFilePathname = filePathname;
this.sourceFileTime = 0;
this.doc = null;
this.doctype = "";
setSource(filePathname);
}
/**
@ -52,7 +55,15 @@ public class FilePresenter extends DomPresenter
logger.info("dynamize file [" + this.sourceFilePathname + "]");
// Get the good tree.
File source = new File(this.sourceFilePathname);
File source;
if (this.sourceFilePathname.matches(".+://.+"))
{
source = new File(new URI(this.sourceFilePathname));
}
else
{
source = new File(this.sourceFilePathname);
}
if (!source.exists())
{
@ -93,22 +104,11 @@ public class FilePresenter extends DomPresenter
return (dynamize(datas.getIdsDataById()));
}
/**
*
*/
public void setSource(final String filePathname)
{
this.sourceFilePathname = filePathname;
this.sourceFileTime = 0;
this.doc = null;
this.doctype = "";
}
/**
*
*/
@Override
public String source()
public String getSource()
{
String result;
@ -118,6 +118,32 @@ public class FilePresenter extends DomPresenter
return (result);
}
/**
*
*/
public void setSource(final File source)
{
if (source == null)
{
setSource((String) null);
}
else
{
setSource(source.getAbsolutePath());
}
}
/**
*
*/
public void setSource(final String filePathname)
{
this.sourceFilePathname = filePathname;
this.sourceFileTime = 0;
this.doc = null;
this.doctype = "";
}
/**
* Dynamize a file without data.
*/

View file

@ -94,7 +94,7 @@ public class FilePresenters extends Vector<FilePresenter>
{
FilePresenter presenter = this.get(presenterIndex);
if (presenter.source().equals(source))
if (presenter.getSource().equals(source))
{
result = presenter;
ended = true;
@ -138,7 +138,7 @@ public class FilePresenters extends Vector<FilePresenter>
{
FilePresenter presenter = this.get(presenterIndex);
if (presenter.source().equals(source))
if (presenter.getSource().equals(source))
{
result = presenterIndex;
ended = true;

View file

@ -462,7 +462,7 @@ public class Presenter
// String publicId = dt.getPublicId();
String systemId = dt.getSystemId();
if (systemId.equals(TRANSITIONAL_DTD))
if ((systemId != null) && (systemId.equals(TRANSITIONAL_DTD)))
{
result.append(TRANSITIONAL_DOCTYPE);
}

View file

@ -117,7 +117,7 @@ public class StringPresenter extends DomPresenter
*
*/
@Override
public String source()
public String getSource()
{
String result;

View file

@ -0,0 +1,217 @@
package fr.devinsy.xidyn;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
*/
public class URLPresenter extends DomPresenter {
static private Logger logger = LoggerFactory.getLogger(URLPresenter.class);
private String sourcePathname;
private URL sourceURL;
private long sourceFileTime;
private String doctype;
/**
*
*/
public URLPresenter() {
setSource((URL) null);
}
/**
* @throws MalformedURLException
*
*/
public URLPresenter(final String source) {
setSource(source);
}
/**
*
*/
public URLPresenter(final URL source) {
setSource(source);
}
/**
* No need to be synchronized.
*/
public StringBuffer dynamize() throws Exception {
StringBuffer result;
result = dynamize((TagDataListById) null);
//
return result;
}
/**
* No need to be synchronized.
*/
@Override
public StringBuffer dynamize(final TagDataListById datas) throws Exception {
StringBuffer result;
logger.info("dynamize URL [" + this.sourcePathname + "]");
if (this.sourceURL == null) {
String errorMessage = "source file defined but not found (" + this.sourcePathname + ")";
logger.error(errorMessage);
result = null;
throw new Exception(errorMessage);
} else if (datas == null) {
result = new StringBuffer(sourceURL.openConnection().getContentLength());
BufferedReader in = new BufferedReader(new InputStreamReader(this.sourceURL.openStream()));
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
in.close();
} else {
long lastModified = sourceURL.openConnection().getLastModified();
if ((this.doc == null) || (this.sourceFileTime != lastModified)) {
this.sourceFileTime = lastModified;
this.doc = Presenter.buildTree(this.sourceURL.openStream());
this.doctype = "<!DOCTYPE HTML>"; // TODO Made generic.
if (this.doc != null) {
Presenter.addMetaTag(doc, "generator", "XID 0.0");
}
}
// Build the web page.
StringWriter htmlCode = new StringWriter(Presenter.estimatedTargetLength(sourceURL.openConnection().getContentLength()));
htmlCode.write(doctype);
htmlCode.write('\n');
Presenter.dynamize(htmlCode, doc, datas);
result = htmlCode.getBuffer();
}
//
return (result);
}
/**
*
*/
@Override
public StringBuffer dynamize(final TagDataManager datas) throws Exception {
return (dynamize(datas.getIdsDataById()));
}
/**
*
*/
@Override
public String getSource() {
String result;
result = this.sourceURL.toString();
//
return (result);
}
/**
*
*/
public void setSource(final String source) {
URL url;
if (source == null) {
url = null;
} else if (source.matches(".+://.+")) {
try {
url = new URL(source);
} catch (final MalformedURLException exception) {
logger.warn("UNKNOWN PROTOCOL [" + source + "]");
url = null;
}
} else {
url = URLPresenter.class.getResource(source);
}
setSource(url);
}
/**
*
* @param source
* @throws MalformedURLException
*/
public void setSource(final URL source) {
if (source == null) {
this.sourcePathname = null;
} else {
this.sourcePathname = source.toString();
}
this.sourceURL = source;
this.sourceFileTime = 0;
this.doc = null;
this.doctype = "";
}
/**
* Dynamize a file without data.
*/
static public StringBuffer dynamize(final String filePathname) throws Exception {
StringBuffer result;
URLPresenter presenter = new URLPresenter(filePathname);
result = presenter.dynamize((TagDataManager) null);
//
return (result);
}
/**
* Dynamize a file.
*/
static public StringBuffer dynamize(final String filePathname, final TagDataManager datas) throws Exception {
StringBuffer result;
URLPresenter presenter = new URLPresenter(filePathname);
result = presenter.dynamize(datas);
//
return (result);
}
/**
*
* @param filePathname
* @return
* @throws Exception
*/
static public String getDoctype(final String filePathname) throws Exception {
String result;
//
BufferedReader in = new BufferedReader(new FileReader(filePathname));
String doctype = in.readLine();
in.close();
logger.info("doctype=[" + doctype + "]");
//
if ((doctype.startsWith("<!DOCTYPE")) || (doctype.startsWith("<!doctype"))) {
result = doctype;
} else {
result = null;
}
//
return (result);
}
}