use of org.apache.commons.io.LineIterator in project stanbol by apache.
the class DataFileResourceLoader method getLines.
public List<String> getLines(String resource) throws IOException {
List<String> lines = new ArrayList<String>();
LineIterator it = IOUtils.lineIterator(openResource(resource), "UTF-8");
while (it.hasNext()) {
String line = it.nextLine();
if (line != null && !line.isEmpty() && line.charAt(0) != '#') {
lines.add(line);
}
}
return lines;
}
use of org.apache.commons.io.LineIterator in project stanbol by apache.
the class IndexingConfig method loadConfig.
/**
* Loads an {@link Properties} configuration from the parsed file and
* returns it as Map
* @param configFile the file
* @param required if <code>true</code> an {@link IllegalArgumentException}
* will be thrown if the config was not present otherwise an empty map will
* be returned
* @return The configuration as Map
*/
private Map<String, Object> loadConfig(String configFile, boolean required) {
//Uses an own implementation to parse key=value configuration
//The problem with the java properties is that keys do not support
//UTF-8, but some configurations might want to use URLs as keys!
Map<String, Object> configMap = new HashMap<String, Object>();
try {
InputStream in = openConfig(configFile);
if (in != null) {
LineIterator lines = IOUtils.lineIterator(in, "UTF-8");
while (lines.hasNext()) {
String line = lines.next();
if (!line.isEmpty()) {
int indexOfEquals = line.indexOf('=');
String key = indexOfEquals > 0 ? line.substring(0, indexOfEquals).trim() : line.trim();
if (key.charAt(0) != '#' && key.charAt(0) != '!') {
//no comment
String value;
if (indexOfEquals > 0 && indexOfEquals < line.length() - 1) {
value = line.substring(indexOfEquals + 1, line.length());
} else {
value = null;
}
configMap.put(key, value);
}
// else ignore comments
}
//else ignore empty lines
}
} else if (required) {
throw new IllegalArgumentException("Unable to find configuration file '" + configFile + "'!");
} else {
//-> optional and not found -> return empty map
log.info("Unable to find optional configuration {}", configFile);
}
} catch (IOException e) {
if (required) {
throw new IllegalStateException("Unable to read configuration file '" + configFile + "'!", e);
} else {
log.warn("Unable to read configuration file '" + configFile + "'!", e);
}
}
// }
return configMap;
}
Aggregations