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.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.commons.jxpath.AbstractFactory;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.Pointer;
import org.apache.commons.lang.text.StrSubstitutor;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Helper class for working with JSON configuration. Uses the JXPath library to
* use XPath syntax to access JSON nodes.
*
* @author Oliver Lucido
*/
@SuppressWarnings("unchecked")
public class JsonConfigHelper {
/** Logging */
@SuppressWarnings("unused")
private Logger log = LoggerFactory.getLogger(JsonConfigHelper.class);
/** JXPath factory for creating JSON nodes */
private class JsonMapFactory extends AbstractFactory {
@Override
public boolean createObject(JXPathContext context, Pointer pointer,
Object parent, String name, int index) {
if (parent instanceof Map) {
((Map) parent).put(name,
new LinkedHashMap());
return true;
}
return false;
}
}
/** JSON root node */
private Map rootNode;
/** JXPath context */
private JXPathContext jxPath;
/**
* Creates an empty JSON configuration
*/
public JsonConfigHelper() {
rootNode = new LinkedHashMap();
}
/**
* Creates a JSON configuration from a map. This is normally used to create
* an instance for a subNode returned from one of the get methods.
*
* @param rootNode a JSON structured map
*/
public JsonConfigHelper(Map rootNode) {
this.rootNode = rootNode;
}
/**
* Creates a JSON configuration from the specified string
*
* @param jsonContent a JSON content string
* @throws IOException if there was an error parsing or reading the content
*/
public JsonConfigHelper(String jsonContent) throws IOException {
rootNode = new ObjectMapper().readValue(jsonContent, Map.class);
}
/**
* 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 JsonConfigHelper(File jsonFile) throws IOException {
rootNode = new ObjectMapper().readValue(jsonFile, Map.class);
}
/**
* 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 JsonConfigHelper(InputStream jsonIn) throws IOException {
rootNode = new ObjectMapper().readValue(jsonIn, Map.class);
}
/**
* Creates a JSON configuration from the specified reader
*
* @param jsonReader a reader for a JSON file
* @throws IOException if there was an error parsing or reading the reader
*/
public JsonConfigHelper(Reader jsonReader) throws IOException {
rootNode = new ObjectMapper().readValue(jsonReader, Map.class);
}
/**
* Gets a JXPath context for selecting and creating JSON nodes and values
*
* @return a JXPath context
*/
private JXPathContext getJXPath() {
if (jxPath == null) {
jxPath = JXPathContext.newContext(rootNode);
jxPath.setFactory(new JsonMapFactory());
jxPath.setLenient(true);
}
return jxPath;
}
/**
* 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) {
Object valueNode = null;
try {
valueNode = getJXPath().getValue(path);
} catch (Exception e) {
}
String value = valueNode == null ? defaultValue : valueNode.toString();
return StrSubstitutor.replaceSystemProperties(value);
}
/**
* Get the value of specified node, with a specified default if it's not
* found
*
* @param path
* @param defaultValue
* @return node value or default Value if not found WITHOUT string
* substitution
*/
public String getPlainText(String path, String defaultValue) {
Object valueNode = null;
try {
valueNode = getJXPath().getValue(path);
} catch (Exception e) {
}
String value = valueNode == null ? defaultValue : valueNode.toString();
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