Mercurial > dive4elements > river
diff flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/DataCage.java @ 950:22bc87a42a0d
First step to factor out the "Datenkorb" logic into a service independent singleton.
flys-artifacts/trunk@2367 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Wed, 20 Jul 2011 09:56:39 +0000 |
parents | |
children | 92027887775a |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/DataCage.java Wed Jul 20 09:56:39 2011 +0000 @@ -0,0 +1,97 @@ +package de.intevation.flys.artifacts.services.meta; + +import java.io.InputStream; +import java.io.IOException; +import java.io.File; + +import java.io.FileInputStream; + +import org.apache.log4j.Logger; + +import org.w3c.dom.Document; + +import de.intevation.artifacts.common.utils.Config; +import de.intevation.artifacts.common.utils.XMLUtils; + +public class DataCage +{ + private static Logger log = Logger.getLogger(DataCage.class); + + public static final String XPATH_META_DATA_TEMPLATE = + "/artifact-database/metadata/@template"; + + private static DataCage INSTANCE; + + protected Builder builder; + + public DataCage() { + } + + public DataCage(Builder builder) { + this.builder = builder; + } + + public Builder getBuilder() { + return builder; + } + + public static synchronized DataCage getInstance() { + if (INSTANCE == null) { + INSTANCE = createDataCage(); + } + return INSTANCE; + } + + protected static Builder createBuilder(File file) { + log.debug("DataCage.createBuilder"); + + if (!file.isFile() || !file.canRead()) { + log.error("Cannot open template file '" + file + "'"); + return null; + } + + InputStream in = null; + + try { + in = new FileInputStream(file); + + Document template = XMLUtils.parseDocument(in); + + if (template == null) { + log.error("cannot parse meta data template"); + } + else { + return new Builder(template); + } + } + catch (IOException ioe) { + log.error(ioe); + } + finally { + if (in != null) { + try { + in.close(); + } + catch (IOException ioe) { + log.error(ioe); + } + } + } + return null; + } + + protected static DataCage createDataCage() { + log.debug("DataCage.createDataCage"); + + String path = Config.getStringXPath(XPATH_META_DATA_TEMPLATE); + if (path == null) { + log.error("no path to template file given"); + return null; + } + + path = Config.replaceConfigDir(path); + + return new DataCage(createBuilder(new File(path))); + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :