/** * Get the configurations from the configurations File as a {@link JsonElement} * * @return {@link JsonElement} The configuration tree contained inside the configuration File. If * the File doesn't exist or an error occurred, the default configuration tree is returned */ private JsonElement getConfigurationTreeFromFile() { if (mConfigurationFile.exists()) { // Check, if the configuration File exists on disk JsonParser parser = new JsonParser(); try { JsonElement result = parser.parse(new FileReader(mConfigurationFile)); if (result.isJsonObject()) { // Check if content is a valid JsonObject return result; } } catch (FileNotFoundException e) { e.printStackTrace(); } } return mDefaultConfigurations; // If the configurations File doesn't exist on disk, return the // default configurations }
/** * Method to recursively convert a Json tree, consisting of {@link JsonElement}s, to a to a * configuration tree, consisting of {@link ConfigurationElement}s * * @param parentConfiguration {@link ConfigurationElement} The parent configuration of the new * configurations added in this method call * @param parentJsonElement {@link JsonElement} The parent of the {@link JsonElement}s which will * be converted to {@link ConfigurationElement}s */ private void recConvertJsonTreeToConfigurationTree( ConfigurationElement parentConfiguration, JsonElement parentJsonElement) { if (parentJsonElement.isJsonObject()) { Set<Map.Entry<String, JsonElement>> childConfigurations = ((JsonObject) parentJsonElement).entrySet(); for (Map.Entry<String, JsonElement> childConfiguration : childConfigurations) { if (childConfiguration.getValue().isJsonObject()) { ConfigurationElement newConfigurationSection = new ConfigurationElement(childConfiguration.getKey()); parentConfiguration.addChildConfiguration(newConfigurationSection); recConvertJsonTreeToConfigurationTree( newConfigurationSection, childConfiguration.getValue()); } else { parentConfiguration.addChildConfiguration( new ConfigurationElement( childConfiguration.getKey(), new JsonConfigurationValue(childConfiguration.getValue()))); } } } }