/** * Get list of JsonConfigHelper of the specified node * * @param path XPath to node * @return node list, possibly empty */ public List<JsonConfigHelper> getJsonList(String path) { List<Object> list = getList(path); List<JsonConfigHelper> newList = new ArrayList<JsonConfigHelper>(); for (Object obj : list) { if (obj instanceof Map) { newList.add(new JsonConfigHelper(((Map<String, Object>) obj))); } } return newList; }
/** * Gets the JSON Map of the specified node * * @param path XPath to node * @return node map, possibly empty */ public Map<String, JsonConfigHelper> getJsonMap(String path) { Map<String, JsonConfigHelper> value = userConfig.getJsonMap(path); if (value == null) { value = systemConfig.getJsonMap(path); } return value; }
Object node = getJXPath().getValue(path); Object refNode = getJXPath().getValue(refPath); Map<String, Object> refParent = getMap(refPath + "/.."); for (String key : refParent.keySet()) { if (value.equals(refNode)) { Map<String, Object> parent = getMap(path + "/.."); for (String nodeKey : parent.keySet()) { if (parent.get(nodeKey).equals(node)) { newMap.put(nodeKey, node); getJXPath().removePath(path); break; setMap(refPath.substring(0, refPath.lastIndexOf('/')), newMap);
/** * Set Map on the specified path * * @param path XPath to node * @param map node Map */ public void setJsonMap(String path, Map<String, JsonConfigHelper> map) { for (String key : map.keySet()) { JsonConfigHelper json = map.get(key); try { getJXPath().setValue(path + "/" + key, json.getMap("/")); } catch (Exception e) { getJXPath().createPathAndSetValue(path + "/" + key, json.getMap("/")); } } }
/** * To check if configuration file is outdated * * @return <code>true</code> if outdated, <code>false</code> otherwise */ public boolean isOutdated() { boolean outdated = false; String systemVersion = systemConfig.get("version"); try { JsonConfigHelper compiledConfig = new JsonConfigHelper(getClass() .getResourceAsStream("/" + SYSTEM_CONFIG_FILE)); String compiledVersion = compiledConfig.get("version"); outdated = !compiledVersion.equals(systemVersion); if (outdated) { log.debug("Configuration versions do not match! '{}' != '{}'", systemVersion, compiledVersion); } } catch (IOException ioe) { log.error("Failed to parse compiled configuration!", ioe); } return outdated; } }
/** * Set Multiple nested map on the specified path * * @param path XPath to node * @param json node Map */ public void setMultiMap(String path, Map<String, Object> json) { for (String key : json.keySet()) { if (json.get(key) instanceof Map) { setMultiMap(path + "/" + key, (Map<String, Object>) json.get(key)); } else { getJXPath().createPathAndSetValue(path + "/" + key, json.get(key)); } } }
/** * Get the JSON Map of the specified node * * @param path XPath to node * @return node map, possibly empty */ public Map<String, JsonConfigHelper> getJsonMap(String path) { Map<String, JsonConfigHelper> jsonMap = new LinkedHashMap<String, JsonConfigHelper>(); Object valueNode; try { valueNode = getJXPath().getValue(path); } catch (Exception e) { return null; } if (valueNode instanceof Map) { Map<String, Object> map = (Map<String, Object>) valueNode; for (String key : map.keySet()) { jsonMap.put( key, new JsonConfigHelper((Map<String, Object>) map.get(key))); } } return jsonMap; }
/** * 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); }
/** * Sets the value of the specified node. If the node doesn't exist it is * created. The value can be set in the system configuration if necessary. * * @param path XPath to node * @param value value to set * @param system set the value in the system configuration */ public void set(String path, String value, boolean system) { if (system) { systemConfig.set(path, value); } else { userConfig.set(path, value); } }
/** * Gets a map of the child nodes of the specified node * * @param path XPath to node * @return node map, possibly empty */ public Map<String, Object> getMap(String path) { Map<String, Object> valueMap = userConfig.getMap(path); if (valueMap.isEmpty()) { valueMap = systemConfig.getMap(path); } return valueMap; }
/** * Gets a map of the child (and the 2nd level children) nodes of the * specified node * * @param path XPath to node * @return node map, possibly empty */ public Map<String, Object> getMapWithChild(String path) { Map<String, Object> valueMap = userConfig.getMapWithChild(path); if (valueMap.isEmpty()) { valueMap = systemConfig.getMapWithChild(path); } return valueMap; }
/** * 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<Object> getList(String path) { List<Object> valueList = userConfig.getList(path); if (valueList.isEmpty()) { valueList = systemConfig.getList(path); } return valueList; }
/** * 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()); }
Object node = getJXPath().getValue(path); Object refNode = getJXPath().getValue(refPath); Map<String, Object> refParent = getMap(refPath + "/.."); for (String key : refParent.keySet()) { Map<String, Object> parent = getMap(path + "/.."); for (String nodeKey : parent.keySet()) { if (parent.get(nodeKey).equals(node)) { newMap.put(nodeKey, node); getJXPath().removePath(path); break; setMap(refPath.substring(0, refPath.lastIndexOf('/')), newMap);
/** * Sets the value of the specified node. If the node doesn't exist it is * created. * * @param path XPath to node * @param value value to set */ public void set(String path, String value) { try { getJXPath().setValue(path, value); } catch (Exception e) { getJXPath().createPathAndSetValue(path, value); } }
/** * Set Map on the specified path * * @param path XPath to node * @param map node Map */ public void setJsonList(String path, List<JsonConfigHelper> jsonList) { Object valueNode = getJXPath().getValue(path); List<Object> valueList = new ArrayList<Object>(); if (valueNode == null) { getJXPath().createPathAndSetValue(path, valueList); } for (JsonConfigHelper json : jsonList) { valueList.add(json.getMap("/")); } }
/** * 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; }
/** * Move node from one path to another path in the JSON * * @param source XPath to node * @param dest XPath to node */ public void move(String source, String dest) { Object copyValue = getJXPath().getValue(source); getJXPath().removePath(source); try { getJXPath().setValue(dest, copyValue); } catch (Exception e) { getJXPath().createPathAndSetValue(dest, copyValue); } }