Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* The Fascinator - Common Library
* Copyright (C) 2008-2009 University of Southern Queensland
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package com.googlecode.fascinator.common;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Manages JSON configuration. Configuration values are read from a specified
* JSON and if not found, the system-wide JSON will also be searched.
*
* @author Oliver Lucido
*/
public class JsonConfig {
/** Logging */
private static Logger log = LoggerFactory.getLogger(JsonConfig.class);
/** Default configuration directory */
private static final String CONFIG_DIR = FascinatorHome.getPath();
/** Default system configuration file name */
private static final String SYSTEM_CONFIG_FILE = "system-config.json";
/** JSON system config */
private JsonConfigHelper systemConfig;
/** JSON user config */
private JsonConfigHelper userConfig;
/**
* Creates a config with only the system settings
*
* @throws IOException if these was an error parsing or reading the system
* JSON file
*/
public JsonConfig() throws IOException {
this((InputStream) null);
}
/**
* Creates a JSON configuration from the specified file
*
* @param jsonFile a JSON file
* @throws IOException if there was an error parsing or reading the file
*/
public JsonConfig(File jsonFile) throws IOException {
this(new FileInputStream(jsonFile));
}
/**
* Creates a JSON configuration from the specified input stream
*
* @param jsonIn a JSON stream
* @throws IOException if there was an error parsing or reading the stream
*/
public JsonConfig(InputStream jsonIn) throws IOException {
if (jsonIn == null) {
userConfig = new JsonConfigHelper();
} else {
userConfig = new JsonConfigHelper(jsonIn);
}
systemConfig = new JsonConfigHelper(getSystemFile());
}
/**
* Create a JSON configuration from the specified input string
*
* @param jsonString
* @throws IOException
*/
public JsonConfig(String jsonString) throws IOException {
this(new ByteArrayInputStream(jsonString.getBytes("UTF-8")));
}
/**
* Gets the value of the specified node
*
* @param path XPath to node
* @return node value or null if not found
*/
public String get(String path) {
return get(path, null);
}
/**
* Gets the value of the specified node, with a specified default if the not
* was not found
*
* @param path XPath to node
* @param defaultValue value to return if the node was not found
* @return node value or defaultValue if not found
*/
public String get(String path, String defaultValue) {
String value = userConfig.get(path);
if (value == null) {
value = systemConfig.get(path, defaultValue);
}
return value;
}
/**
* Gets values of the specified node as a list. Use this method for JSON
* arrays.
*
* @param path XPath to node
* @return value list, possibly empty
*/
public List