Search in sources :

Example 41 with LineIterator

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;
}
Also used : ArrayList(java.util.ArrayList) LineIterator(org.apache.commons.io.LineIterator)

Example 42 with LineIterator

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;
}
Also used : HashMap(java.util.HashMap) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) LineIterator(org.apache.commons.io.LineIterator)

Aggregations

LineIterator (org.apache.commons.io.LineIterator)42 IOException (java.io.IOException)24 File (java.io.File)13 InputStream (java.io.InputStream)12 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)8 StringReader (java.io.StringReader)7 FileIteratingFirehose (io.druid.data.input.impl.FileIteratingFirehose)5 BufferedReader (java.io.BufferedReader)5 InputStreamReader (java.io.InputStreamReader)5 Matcher (java.util.regex.Matcher)5 Pattern (java.util.regex.Pattern)5 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)4 FileNotFoundException (java.io.FileNotFoundException)4 FileWriter (java.io.FileWriter)3 Reader (java.io.Reader)3 URISyntaxException (java.net.URISyntaxException)3 DataAccessor (com.pratilipi.data.DataAccessor)2 BufferedWriter (java.io.BufferedWriter)2 FileReader (java.io.FileReader)2