Huge code improvment: add isAvailable method, add GenericPresenter

class, add PresenterFactory class, add TranslatorPresenter class.
This commit is contained in:
Christian P. MOMON 2013-08-27 16:35:18 +02:00
parent e8b98608a2
commit 3b5bac5213
12 changed files with 661 additions and 16 deletions

View file

@ -22,5 +22,6 @@
<attribute name="owner.project.facets" value="java"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="lib/devinsy-utils-0.2.4.jar"/>
<classpathentry kind="output" path="build/classes"/>
</classpath>

View file

@ -1,5 +1,6 @@
Description of used libraries:
- activation: STU
- devinsy-utils : useful tools (StringList...)
- hamcrest-core: required by junit
- junit: unit tests API
- log4j: log API

Binary file not shown.

BIN
lib/devinsy-utils-0.2.4.jar Normal file

Binary file not shown.

View file

@ -116,6 +116,27 @@ public class DomPresenter implements Presenter
return (result);
}
/**
* {@inheritDoc}
*/
@Override
public boolean isAvailable()
{
boolean result;
if (this.dom == null)
{
result = false;
}
else
{
result = true;
}
//
return result;
}
/**
* {@inheritDoc}
*/
@ -153,6 +174,7 @@ public class DomPresenter implements Presenter
*
* @throws Exception
*/
@Override
public void update() throws Exception
{

View file

@ -112,6 +112,27 @@ public class FilePresenter extends StringPresenter
return (result);
}
/**
* {@inheritDoc}
*/
@Override
public boolean isAvailable()
{
boolean result;
if ((this.source == null) || (!this.source.exists()))
{
result = false;
}
else
{
result = true;
}
//
return result;
}
/**
* {@inheritDoc}
*/
@ -197,6 +218,7 @@ public class FilePresenter extends StringPresenter
/**
* @throws Exception
*/
@Override
public void update() throws Exception
{
//

View file

@ -0,0 +1,203 @@
package fr.devinsy.xidyn.presenters;
import java.io.File;
import java.net.URL;
import fr.devinsy.xidyn.data.TagDataManager;
/**
*
*/
public class GenericPresenter implements Presenter
{
private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(GenericPresenter.class);
private Presenter presenter;
/**
*
*/
public GenericPresenter(final File source)
{
if (source == null)
{
throw new NullPointerException("source is null");
}
else
{
this.presenter = PresenterFactory.get(source);
}
}
/**
*
*/
public GenericPresenter(final String source)
{
if (source == null)
{
throw new NullPointerException("source is null");
}
else
{
this.presenter = PresenterFactory.get(source);
}
}
/**
*
*/
public GenericPresenter(final URL source)
{
if (source == null)
{
throw new NullPointerException("source is null");
}
else
{
this.presenter = PresenterFactory.get(source);
}
}
/**
*
*/
@Override
public StringBuffer dynamize() throws Exception
{
StringBuffer result;
result = this.presenter.dynamize();
//
return result;
}
/**
*
*/
@Override
public StringBuffer dynamize(final TagDataManager datas) throws Exception
{
StringBuffer result;
result = this.presenter.dynamize(datas);
//
return result;
}
/**
*
*/
@Override
public Object getSource()
{
Object result;
result = this.presenter.getSource();
//
return result;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isAvailable()
{
boolean result;
result = this.presenter.isAvailable();
//
return result;
}
/**
*
*/
@Override
public boolean isOutdated() throws Exception
{
boolean result;
result = this.presenter.isOutdated();
//
return result;
}
/**
*
* @param source
*/
public void setSource(final File source)
{
if (source == null)
{
throw new NullPointerException("source is null");
}
else
{
this.presenter = PresenterFactory.get(source);
}
}
/**
*
* @param source
*/
public void setSource(final String source)
{
if (source == null)
{
throw new NullPointerException("source is null");
}
else
{
this.presenter = PresenterFactory.get(source);
}
}
/**
*
* @param source
*/
public void setSource(final URL source)
{
if (source == null)
{
throw new NullPointerException("source is null");
}
else
{
this.presenter = PresenterFactory.get(source);
}
}
/**
*
*/
public String toString(final String language) throws Exception
{
String result;
result = this.presenter.toString();
//
return result;
}
/**
*
*/
@Override
public void update() throws Exception
{
this.presenter.update();
}
}
// ////////////////////////////////////////////////////////////////////////

View file

@ -10,7 +10,8 @@ import fr.devinsy.xidyn.data.TagDataManager;
* <li>be aware to avoid copy action (performance)
* </ul>
*/
public interface Presenter {
public interface Presenter
{
/**
*
@ -34,9 +35,21 @@ public interface Presenter {
*/
public Object getSource();
/**
* This method check if the source exists and it is readable.
*
* @return
*/
public boolean isAvailable();
/**
*
* @return
*/
public boolean isOutdated() throws Exception;
/**
*
*/
public void update() throws Exception;
}

View file

@ -0,0 +1,100 @@
package fr.devinsy.xidyn.presenters;
import java.io.File;
import java.net.URL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
*/
public class PresenterFactory
{
static private Logger logger = LoggerFactory.getLogger(PresenterFactory.class);
/**
*
* @param source
* @return
*/
public static Presenter get(final File source)
{
Presenter result;
if (source == null)
{
result = null;
}
else
{
result = new FilePresenter(source);
}
//
return result;
}
/**
*
* @param source
* @return
*/
public static Presenter get(final String source)
{
Presenter result;
if (source == null)
{
result = null;
}
else if (source.startsWith("file://"))
{
result = new FilePresenter(source);
}
else if (source.matches(".+://.+"))
{
result = new URLPresenter(source);
}
else if (source.startsWith("/"))
{
if (new File(source).exists())
{
result = new FilePresenter(source);
}
else
{
result = new URLPresenter(source);
}
}
else
{
result = new StringPresenter(source);
}
//
return result;
}
/**
*
* @param source
* @return
*/
public static Presenter get(final URL source)
{
Presenter result;
if (source == null)
{
result = null;
}
else
{
result = new URLPresenter(source);
}
//
return result;
}
}

View file

@ -154,6 +154,27 @@ public class StringPresenter implements Presenter
return (result);
}
/**
* {@inheritDoc}
*/
@Override
public boolean isAvailable()
{
boolean result;
if (this.source == null)
{
result = false;
}
else
{
result = true;
}
//
return result;
}
/**
* {@inheritDoc}
*/
@ -185,6 +206,15 @@ public class StringPresenter implements Presenter
this.dom = null;
}
/**
*
*/
@Override
public void update() throws Exception
{
// Nothing to do.
}
/**
* Dynamize a string with HTML in.
*/

View file

@ -0,0 +1,194 @@
package fr.devinsy.xidyn.presenters;
import java.util.HashMap;
import fr.devinsy.util.FileTools;
import fr.devinsy.xidyn.data.TagDataManager;
/**
*
*/
public class TranslatorPresenter implements Presenter
{
private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(TranslatorPresenter.class);
private String defaultSource;
private HashMap<String, Presenter> presenters;
/**
*
*/
public TranslatorPresenter(final String defaultSource)
{
if (defaultSource == null)
{
throw new NullPointerException("defaultSource is null");
}
else
{
this.defaultSource = defaultSource;
this.presenters = new HashMap<String, Presenter>();
}
}
/**
*
*/
@Override
public StringBuffer dynamize() throws Exception
{
StringBuffer result;
Presenter presenter = getPresenter(null);
if (presenter == null)
{
result = null;
}
else
{
result = presenter.dynamize();
}
//
return result;
}
/**
*
*/
@Override
public StringBuffer dynamize(final TagDataManager datas) throws Exception
{
StringBuffer result;
Presenter presenter = getPresenter(null);
if (presenter == null)
{
result = null;
}
else
{
result = presenter.dynamize(datas);
}
//
return result;
}
/**
*
*/
public Presenter getPresenter(final String language) throws Exception
{
Presenter result;
//
result = this.presenters.get(language);
//
if (result == null)
{
//
if (language == null)
{
//
result = PresenterFactory.get(this.defaultSource);
if (result.isAvailable())
{
this.presenters.put(language, result);
}
else
{
throw new Exception("Undefined default language file.");
}
}
else
{
String adaptedSource = FileTools.addBeforeExtension(defaultSource, "-" + language);
result = PresenterFactory.get(adaptedSource);
if (result.isAvailable())
{
this.presenters.put(language, result);
}
else
{
result = getPresenter(null);
}
}
}
//
return result;
}
/**
*
*/
@Override
public Object getSource()
{
String result;
result = this.defaultSource;
//
return result;
}
@Override
public boolean isAvailable()
{
boolean result;
try
{
result = getPresenter(null).isAvailable();
}
catch (Exception exception)
{
result = false;
}
//
return result;
}
/**
*
*/
@Override
public boolean isOutdated() throws Exception
{
return false;
}
/**
*
*/
public String toString(final String language) throws Exception
{
String result;
Presenter presenter = getPresenter(language);
if (presenter == null)
{
result = null;
}
else
{
result = presenter.toString();
}
//
return result;
}
@Override
public void update() throws Exception
{
}
}
// ////////////////////////////////////////////////////////////////////////

View file

@ -94,14 +94,7 @@ public class URLPresenter extends StringPresenter
{
String result;
if (this.source == null)
{
result = null;
}
else
{
result = this.source.toString();
}
result = this.sourcePathname;
//
return (result);
@ -120,6 +113,55 @@ public class URLPresenter extends StringPresenter
return (result);
}
/**
* {@inheritDoc}
*/
@Override
public boolean isAvailable()
{
boolean result;
if (this.source == null)
{
result = false;
}
else if (sourcePathname.startsWith("/"))
{
/*
* In case of Jar resources, if resource does not exist then
* this.source is null. So, if we are here in the code then
* this.source is not null and so resource is available.
*/
result = true;
}
else
{
/*
* The source is an URL with protocol. Open a stream is the only way to test if the resource is available.
*/
try
{
this.source.openStream();
result = true;
}
catch (final IOException exception)
{
/*
* On URL.openStream:
* <ul>
* <li>If host does not exist then a java.net.UnknownHostException is throwed.</li>
* <li>If host exists but not the file does not exist then a java.io.FileNotFoundException is throwed.</li>
* </ul>
* These exceptions are IOException.
*/
result = false;
}
}
//
return result;
}
/**
* {@inheritDoc}
*
@ -220,7 +262,7 @@ public class URLPresenter extends StringPresenter
//
result = super.getSource();
}
catch (final IOException exception)
catch (final Exception exception)
{
result = null;
}
@ -232,9 +274,10 @@ public class URLPresenter extends StringPresenter
/**
* No need to be synchronized.
*
* @throws IOException
* @throws Exception
*/
public void update() throws IOException
@Override
public void update() throws Exception
{
//
if (this.source == null)
@ -245,11 +288,27 @@ public class URLPresenter extends StringPresenter
}
else
{
long currentSourceTime = this.source.openConnection().getLastModified();
if ((super.getSource() == null) || (this.sourceTime != currentSourceTime))
try
{
super.setSource(XidynUtils.load(this.source));
this.sourceTime = currentSourceTime;
long currentSourceTime = this.source.openConnection().getLastModified();
if ((super.getSource() == null) || (this.sourceTime != currentSourceTime))
{
super.setSource(XidynUtils.load(this.source));
this.sourceTime = currentSourceTime;
}
}
catch (final IOException exception)
{
/* On URL.openStream:
* <ul>
* <li>If host does not exist then a java.net.UnknownHostException is throwed.</li>
* <li>If host exists but not the file does not exist then a java.io.FileNotFoundException is throwed.</li>
* </ul>
* These exceptions are IOException.
*/
String errorMessage = "source file defined but not readable (" + this.source.toString() + ")";
logger.error(errorMessage);
throw new Exception(errorMessage);
}
}
}