diff --git a/src/fr/devinsy/xidyn/presenters/DomPresenter.java b/src/fr/devinsy/xidyn/presenters/DomPresenter.java index 769d288..6ef2e72 100644 --- a/src/fr/devinsy/xidyn/presenters/DomPresenter.java +++ b/src/fr/devinsy/xidyn/presenters/DomPresenter.java @@ -24,7 +24,6 @@ import org.slf4j.Logger; 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.utils.XidynUtils; @@ -197,38 +196,4 @@ public class DomPresenter implements Presenter { } - - /** - * - * @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; - } } diff --git a/src/fr/devinsy/xidyn/presenters/FilePresenter.java b/src/fr/devinsy/xidyn/presenters/FilePresenter.java index 9300f8d..81a5fd8 100644 --- a/src/fr/devinsy/xidyn/presenters/FilePresenter.java +++ b/src/fr/devinsy/xidyn/presenters/FilePresenter.java @@ -262,34 +262,4 @@ public class FilePresenter extends StringPresenter } } } - - /** - * Dynamize a file without data. - */ - public static StringBuffer dynamize(final String filePathname) throws Exception - { - StringBuffer result; - - FilePresenter presenter = new FilePresenter(filePathname); - - result = presenter.dynamize((TagDataManager) null); - - // - return (result); - } - - /** - * Dynamize a file. - */ - public static StringBuffer dynamize(final String filePathname, final TagDataManager datas) throws Exception - { - StringBuffer result; - - FilePresenter presenter = new FilePresenter(filePathname); - - result = presenter.dynamize(datas); - - // - return (result); - } } diff --git a/src/fr/devinsy/xidyn/presenters/GenericPresenter.java b/src/fr/devinsy/xidyn/presenters/GenericPresenter.java index 0bb7eae..248fb56 100644 --- a/src/fr/devinsy/xidyn/presenters/GenericPresenter.java +++ b/src/fr/devinsy/xidyn/presenters/GenericPresenter.java @@ -21,6 +21,8 @@ package fr.devinsy.xidyn.presenters; import java.io.File; import java.net.URL; +import org.w3c.dom.Document; + import fr.devinsy.xidyn.data.TagDataManager; /** @@ -32,6 +34,21 @@ public class GenericPresenter implements Presenter private Presenter presenter; + /** + * + */ + public GenericPresenter(final Document source) + { + if (source == null) + { + throw new NullPointerException("source is null"); + } + else + { + this.presenter = PresenterFactory.create(source); + } + } + /** * */ @@ -43,7 +60,7 @@ public class GenericPresenter implements Presenter } else { - this.presenter = PresenterFactory.get(source); + this.presenter = PresenterFactory.create(source); } } @@ -58,7 +75,7 @@ public class GenericPresenter implements Presenter } else { - this.presenter = PresenterFactory.get(source); + this.presenter = PresenterFactory.create(source); } } @@ -73,7 +90,7 @@ public class GenericPresenter implements Presenter } else { - this.presenter = PresenterFactory.get(source); + this.presenter = PresenterFactory.create(source); } } @@ -147,6 +164,22 @@ public class GenericPresenter implements Presenter return result; } + /** + * + * @param source + */ + public void setSource(final Document source) + { + if (source == null) + { + throw new NullPointerException("source is null"); + } + else + { + this.presenter = PresenterFactory.create(source); + } + } + /** * * @param source @@ -159,7 +192,7 @@ public class GenericPresenter implements Presenter } else { - this.presenter = PresenterFactory.get(source); + this.presenter = PresenterFactory.create(source); } } @@ -175,7 +208,7 @@ public class GenericPresenter implements Presenter } else { - this.presenter = PresenterFactory.get(source); + this.presenter = PresenterFactory.create(source); } } @@ -191,7 +224,7 @@ public class GenericPresenter implements Presenter } else { - this.presenter = PresenterFactory.get(source); + this.presenter = PresenterFactory.create(source); } } diff --git a/src/fr/devinsy/xidyn/presenters/PresenterFactory.java b/src/fr/devinsy/xidyn/presenters/PresenterFactory.java index f842f74..7e532c1 100644 --- a/src/fr/devinsy/xidyn/presenters/PresenterFactory.java +++ b/src/fr/devinsy/xidyn/presenters/PresenterFactory.java @@ -23,20 +23,170 @@ import java.net.URL; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.w3c.dom.Document; + +import fr.devinsy.xidyn.utils.cache.Cache; /** * */ public class PresenterFactory { + /** + * http://thecodersbreakfast.net/index.php?post/2008/02/25/26-de-la-bonne- + * implementation-du-singleton-en-java + */ + private static class SingletonHolder + { + private final static PresenterFactory INSTANCE = new PresenterFactory(); + } + private static Logger logger = LoggerFactory.getLogger(PresenterFactory.class); + private Cache cache; + + /** + * + */ + private PresenterFactory() + { + this.cache = new Cache(); + } + + /** + * + */ + public void clear() + { + this.cache.clear(); + } + /** * * @param source * @return */ - public static Presenter get(final File source) + public Presenter get(final Document source) + { + Presenter result; + + result = this.cache.get(source); + + if (result == null) + { + result = create(source); + this.cache.put(source, result); + } + + // + return result; + } + + /** + * + * @param source + * @return + */ + public Presenter get(final File source) + { + Presenter result; + + result = this.cache.get(source); + + if (result == null) + { + result = create(source); + this.cache.put(source, result); + } + + // + return result; + } + + /** + * + * @param source + * @return + */ + public Presenter get(final String source) + { + Presenter result; + + result = this.cache.get(source); + + if (result == null) + { + result = create(source); + this.cache.put(source, result); + } + + // + return result; + } + + /** + * + * @param source + * @return + */ + public Presenter get(final URL source) + { + Presenter result; + + result = this.cache.get(source); + + if (result == null) + { + result = create(source); + this.cache.put(source, result); + } + + // + return result; + } + + /** + * + * @return + */ + public int size() + { + int result; + + result = this.cache.size(); + + // + return result; + } + + /** + * + * @param source + * @return + */ + public static Presenter create(final Document source) + { + Presenter result; + + if (source == null) + { + result = null; + } + else + { + result = new DomPresenter(source); + } + + // + return result; + } + + /** + * + * @param source + * @return + */ + public static Presenter create(final File source) { Presenter result; @@ -58,7 +208,7 @@ public class PresenterFactory * @param source * @return */ - public static Presenter get(final String source) + public static Presenter create(final String source) { Presenter result; @@ -99,7 +249,7 @@ public class PresenterFactory * @param source * @return */ - public static Presenter get(final URL source) + public static Presenter create(final URL source) { Presenter result; @@ -115,4 +265,13 @@ public class PresenterFactory // return result; } + + /** + * + * @return + */ + public static PresenterFactory instance() + { + return SingletonHolder.INSTANCE; + } } diff --git a/src/fr/devinsy/xidyn/presenters/PresenterUtils.java b/src/fr/devinsy/xidyn/presenters/PresenterUtils.java new file mode 100644 index 0000000..2be9362 --- /dev/null +++ b/src/fr/devinsy/xidyn/presenters/PresenterUtils.java @@ -0,0 +1,152 @@ +/** + * Copyright (C) 2006-2016 Christian Pierre MOMON + * + * This file is part of Xidyn. + * + * Xidyn is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Xidyn is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Xidyn. If not, see + */ +package fr.devinsy.xidyn.presenters; + +import java.io.File; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.Document; + +import fr.devinsy.xidyn.data.TagDataListById; +import fr.devinsy.xidyn.data.TagDataManager; + +/** + * + */ +public class PresenterUtils +{ + private static Logger logger = LoggerFactory.getLogger(PresenterUtils.class); + + /** + * + */ + private PresenterUtils() + { + } + + /** + * + * @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; + } + + /** + * Dynamize a file without data. + */ + public static StringBuffer dynamize(final File source) throws Exception + { + StringBuffer result; + + FilePresenter presenter = new FilePresenter(source); + + result = presenter.dynamize((TagDataManager) null); + + // + return (result); + } + + /** + * Dynamize a file. + */ + public static StringBuffer dynamize(final File source, final TagDataManager datas) throws Exception + { + StringBuffer result; + + FilePresenter presenter = new FilePresenter(source); + + result = presenter.dynamize(datas); + + // + return (result); + } + + /** + * Dynamize a string with HTML in, or with file path name in, or with URL + * in. + */ + public static StringBuffer dynamize(final String source, final TagDataManager datas) throws Exception + { + StringBuffer result; + + Presenter presenter = PresenterFactory.create(source); + + result = presenter.dynamize(datas); + + // + return (result); + } + + /** + * + * @param source + * @return + */ + public static boolean hasHtmlTag(final String source) + { + boolean result; + + if (source == null) + { + result = false; + } + else + { + if (source.matches("<[hH][tT][mM][lL]>")) + { + result = true; + } + else + { + result = false; + } + } + + // + return result; + } +} diff --git a/src/fr/devinsy/xidyn/presenters/StringPresenter.java b/src/fr/devinsy/xidyn/presenters/StringPresenter.java index 56b741f..1da1fe3 100644 --- a/src/fr/devinsy/xidyn/presenters/StringPresenter.java +++ b/src/fr/devinsy/xidyn/presenters/StringPresenter.java @@ -232,48 +232,4 @@ public class StringPresenter implements Presenter { // Nothing to do. } - - /** - * Dynamize a string with HTML in. - */ - public static StringBuffer dynamize(final String html, final TagDataManager datas) throws Exception - { - StringBuffer result; - - StringPresenter presenter = new StringPresenter(html); - - result = presenter.dynamize(datas); - - // - return (result); - } - - /** - * - * @param source - * @return - */ - public static boolean hasHtmlTag(final String source) - { - boolean result; - - if (source == null) - { - result = false; - } - else - { - if (source.matches("<[hH][tT][mM][lL]>")) - { - result = true; - } - else - { - result = false; - } - } - - // - return result; - } } diff --git a/src/fr/devinsy/xidyn/presenters/TranslatorPresenter.java b/src/fr/devinsy/xidyn/presenters/TranslatorPresenter.java index 1fa532f..e2d891e 100644 --- a/src/fr/devinsy/xidyn/presenters/TranslatorPresenter.java +++ b/src/fr/devinsy/xidyn/presenters/TranslatorPresenter.java @@ -212,7 +212,7 @@ public class TranslatorPresenter implements Presenter if (language == null) { // - result = PresenterFactory.get(this.defaultSource); + result = PresenterFactory.create(this.defaultSource); if (result.isAvailable()) { @@ -226,7 +226,7 @@ public class TranslatorPresenter implements Presenter else { String adaptedSource = FileTools.addBeforeExtension(this.defaultSource, "-" + language); - result = PresenterFactory.get(adaptedSource); + result = PresenterFactory.create(adaptedSource); if (result.isAvailable()) { diff --git a/src/fr/devinsy/xidyn/presenters/URLPresenter.java b/src/fr/devinsy/xidyn/presenters/URLPresenter.java index 8d87e07..091379f 100644 --- a/src/fr/devinsy/xidyn/presenters/URLPresenter.java +++ b/src/fr/devinsy/xidyn/presenters/URLPresenter.java @@ -350,34 +350,4 @@ public class URLPresenter extends StringPresenter } } } - - /** - * Dynamize a file without data. - */ - public static StringBuffer dynamize(final String filePathname) throws Exception - { - StringBuffer result; - - URLPresenter presenter = new URLPresenter(filePathname); - - result = presenter.dynamize((TagDataManager) null); - - // - return (result); - } - - /** - * Dynamize a file. - */ - public static StringBuffer dynamize(final String filePathname, final TagDataManager datas) throws Exception - { - StringBuffer result; - - URLPresenter presenter = new URLPresenter(filePathname); - - result = presenter.dynamize(datas); - - // - return (result); - } }