public static RestxConfig of(Iterable<ConfigElement> configElements) { return new StdRestxConfig(configElements); }
@Override public RestxConfig get() { List<ConfigElement> elements = new ArrayList<>(); loadAllFromFile(elements, path); return StdRestxConfig.of(elements); } };
protected void loadResourceInto(String name, List<ConfigElement> elements) { URL r; r = Thread.currentThread().getContextClassLoader().getResource(name); if (r != null) { try { Iterable<ConfigElement> loadedElements = StdRestxConfig.parse("classpath:" + name, Resources.asCharSource(r, Charsets.UTF_8)).elements(); Iterables.addAll(elements, loadedElements); logger.debug("loaded {} elements from {}", Iterables.size(loadedElements), name); } catch (IOException e) { logger.warn("can't load " + name + ": " + e.getMessage(), e); } } else { logger.debug("no settings loaded from {}: resource not available", name); } } }
protected void loadFileInto(Path path, List<ConfigElement> elements) { File file = path.toFile().getAbsoluteFile(); if (!file.exists()) { logger.debug("no settings loaded from {}: file not available", file); return; } if (!file.isFile()) { logger.warn("no settings loaded from {}: this is not a file", file); return; } try { Iterable<ConfigElement> loadedElements = StdRestxConfig.parse("file://" + file, asCharSource(file, Charsets.UTF_8)).elements(); Iterables.addAll(elements, loadedElements); logger.debug("loaded {} elements from {}", Iterables.size(loadedElements), file); } catch (IOException e) { logger.warn("can't load " + file + ": " + e.getMessage(), e); } }
public static RestxConfig of(Iterable<ConfigElement> configElements) { return new StdRestxConfig(configElements); }
@Override public RestxConfig get() { List<ConfigElement> elements = new ArrayList<>(); loadAllFromResource(elements, resource); return StdRestxConfig.of(elements); } };
public static RestxConfig parse(String origin, CharSource charSource) throws IOException { List<ConfigElement> elements = new ArrayList<>(); StringBuilder doc = new StringBuilder(); ImmutableList<String> lines = charSource.readLines(); for (int lineCount = 0; lineCount < lines.size(); lineCount++) { String line = lines.get(lineCount); if (line.startsWith("#")) { doc.append(line.substring(1).trim()).append("\n"); } else if (!line.trim().isEmpty()) { int equalIndex = line.indexOf('='); if (equalIndex == -1) { throw new IOException("invalid config " + origin + " at line " + lineCount + ":" + " line does not contain the equals sign '='"); } String key = line.substring(0, equalIndex).trim(); StringBuilder value = new StringBuilder(); String lineValue = line.substring(equalIndex + 1).trim(); while(lineValue.endsWith("\\") && lineCount+1 < lines.size()) { value.append(lineValue.substring(0, lineValue.length() - "\\".length())); lineCount++; lineValue = lines.get(lineCount).trim(); } value.append(lineValue); elements.add(ConfigElement.of(origin, doc.toString().trim(), key, value.toString())); doc.setLength(0); } } return new StdRestxConfig(elements); }
public static RestxConfig parse(String origin, CharSource charSource) throws IOException { List<ConfigElement> elements = new ArrayList<>(); StringBuilder doc = new StringBuilder(); ImmutableList<String> lines = charSource.readLines(); for (int lineCount = 0; lineCount < lines.size(); lineCount++) { String line = lines.get(lineCount); if (line.startsWith("#")) { doc.append(line.substring(1).trim()).append("\n"); } else if (!line.trim().isEmpty()) { int equalIndex = line.indexOf('='); if (equalIndex == -1) { throw new IOException("invalid config " + origin + " at line " + lineCount + ":" + " line does not contain the equals sign '='"); } String key = line.substring(0, equalIndex).trim(); StringBuilder value = new StringBuilder(); String lineValue = line.substring(equalIndex + 1).trim(); while(lineValue.endsWith("\\") && lineCount+1 < lines.size()) { value.append(lineValue.substring(0, lineValue.length() - "\\".length())); lineCount++; lineValue = lines.get(lineCount).trim(); } value.append(lineValue); elements.add(ConfigElement.of(origin, doc.toString().trim(), key, value.toString())); doc.setLength(0); } } return new StdRestxConfig(elements); }