view artifact-database/src/main/java/de/intevation/artifactdatabase/Config.java @ 67:ed03cc0e5800

Added method to XMLUtils to copy an XML document to an output stream. artifacts/trunk@553 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 17 Jan 2010 14:00:06 +0000
parents 911c684c8a1c
children 8447467cef86
line wrap: on
line source
package de.intevation.artifactdatabase;

import java.io.File;
import java.io.IOException;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;

import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import javax.xml.namespace.QName;

import javax.xml.xpath.XPathConstants;

import org.apache.log4j.Logger;

/**
 *  @author Sascha L. Teichmann
 */
public final class Config
{
    private static Logger logger = Logger.getLogger(Config.class);

    public static final String CONFIG_DIR = "artifact.database.dir";

    public static final File CONFIG_DIR_DEFAULT =
        new File(new File(System.getProperty("user.home",
            System.getProperty("user.dir", "."))), ".artifactdb");

    public static final String CONFIG_FILE  = "conf.xml";

    public static final String CONFIG_DIR_PLACEHOLDER = "${artifacts.config.dir}";

    private static Document config;

    private Config() {
    }

    public static synchronized final Document getConfig() {
        if (config == null) {
            config = loadConfig();
        }
        return config;
    }

    public static File getConfigDirectory() {
        String configDirString = System.getProperty(CONFIG_DIR);

        File configDir = configDirString != null
            ? new File(configDirString)
            : CONFIG_DIR_DEFAULT;

        if (!configDir.isDirectory()) {
            logger.warn("'" + configDir + "' is not a directory.");
            configDir = CONFIG_DIR_DEFAULT;
        }

        return configDir;
    }

    public static String replaceConfigDir(String path) {
        String configDir = getConfigDirectory().getAbsolutePath();
        return path.replace(CONFIG_DIR_PLACEHOLDER, configDir);
    }

    private static Document loadConfig() {

        File configDir = getConfigDirectory();

        File file = new File(configDir, CONFIG_FILE);

        if (!file.canRead() && !file.isFile()) {
            logger.error("Cannot read config file '"
                + file + "'.");
            return null;
        }

        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(false); // XXX: This may change in future.
            return factory.newDocumentBuilder().parse(file);
        }
        catch (SAXException se) {
            logger.error(se.getLocalizedMessage(), se);
        }
        catch (ParserConfigurationException pce) {
            logger.error(pce.getLocalizedMessage(), pce);
        }
        catch (IOException ioe) {
            logger.error(ioe.getLocalizedMessage());
        }

        return null;
    }

    public static final Object getXPath(
        Object root, String query, QName returnType
    ) {
        return XMLUtils.xpath(root, query, returnType);
    }

    public static final Object getXPath(String query, QName returnType) {
        return XMLUtils.xpath(getConfig(), query, returnType);
    }

    public static final NodeList getNodeSetXPath(String query) {
        return (NodeList)getXPath(query, XPathConstants.NODESET);
    }

    public static final Node getNodeXPath(String query) {
        return (Node)getXPath(query, XPathConstants.NODE);
    }

    public static final String getStringXPath(String xpath) {
        return getStringXPath(xpath, null);
    }

    public static final String getStringXPath(String query, String def) {
        String s = (String)getXPath(query, XPathConstants.STRING);
        return s == null || s.length() == 0
            ? def
            : s;
    }

    public static final NodeList getNodeSetXPath(Object root, String query) {
        return (NodeList)getXPath(root, query, XPathConstants.NODESET);
    }

    public static final Node getNodeXPath(Object root, String query) {
        return (Node)getXPath(root, query, XPathConstants.NODE);
    }

    public static final String getStringXPath(Object root, String xpath) {
        return getStringXPath(root, xpath, null);
    }

    public static final String getStringXPath(
        Object root, String query, String def
    ) {
        String s = (String)getXPath(root, query, XPathConstants.STRING);
        return s == null || s.length() == 0
            ? def
            : s;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org