Example usage for org.apache.commons.io IOUtils readLines

List of usage examples for org.apache.commons.io IOUtils readLines

Introduction

In this page you can find the example usage for org.apache.commons.io IOUtils readLines.

Prototype

public static List readLines(InputStream input, String encoding) throws IOException 

Source Link

Document

Get the contents of an InputStream as a list of Strings, one entry per line, using the specified character encoding.

Usage

From source file:com.thinkbiganalytics.spark.repl.SparkScriptEngine.java

/**
 * Gets the list of patterns that should prevent a script from executing.
 *
 * @return the deny patterns list/*from w  w  w.j  a  v  a 2 s .  co  m*/
 * @throws IllegalStateException if the spark-deny-patterns.conf file cannot be found
 */
@Nonnull
private List<Pattern> getDenyPatterns() {
    if (denyPatterns == null) {
        // Load custom or default deny patterns
        String resourceName = "spark-deny-patterns.conf";
        InputStream resourceStream = getClass().getResourceAsStream("/" + resourceName);
        if (resourceStream == null) {
            resourceName = "spark-deny-patterns.default.conf";
            resourceStream = getClass().getResourceAsStream(resourceName);
        }

        // Parse lines
        final List<String> denyPatternLines;
        if (resourceStream != null) {
            try {
                denyPatternLines = IOUtils.readLines(resourceStream, "UTF-8");
                log.info("Loaded Spark deny patterns from {}.", resourceName);
            } catch (final IOException e) {
                throw new IllegalStateException("Unable to load " + resourceName, e);
            }
        } else {
            log.info("Missing default Spark deny patterns.");
            denyPatternLines = Collections.emptyList();
        }

        // Compile patterns
        denyPatterns = new ArrayList<>();
        for (final String line : denyPatternLines) {
            final String trimLine = line.trim();
            if (!line.startsWith("#") && !trimLine.isEmpty()) {
                denyPatterns.add(Pattern.compile(line));
            }
        }
    }
    return denyPatterns;
}

From source file:com.dangdang.config.service.web.mb.PropertyGroupManagedBean.java

/**
 * @param inputstream/*from  ww w .  ja  va2 s.  c om*/
 * @return
 * @throws IOException
 */
private List<PropertyItemVO> parseInputFile(InputStream inputstream) throws IOException {
    List<String> lines = IOUtils.readLines(inputstream, Charsets.UTF_8.name());
    List<PropertyItemVO> items = Lists.newArrayList();
    String previousLine = null;
    for (int i = 1; i < lines.size(); i++) {
        String line = lines.get(i);
        if (!line.startsWith("#")) {
            Iterable<String> parts = PROPERTY_SPLITTER.split(line);
            if (Iterables.size(parts) == 2) {
                PropertyItemVO item = new PropertyItemVO(Iterables.getFirst(parts, null),
                        Iterables.getLast(parts));
                if (previousLine != null && previousLine.startsWith("#")) {
                    item.setComment(StringUtils.trimLeadingCharacter(previousLine, '#').trim());
                }
                items.add(item);
            }
        }

        previousLine = line;
    }
    return items;
}

From source file:licenseUtil.LicensingList.java

public static Map<String, String> constructLicenseUrlFileMapping() {

    Map<String, String> result = new HashMap<>();
    try {//from   ww w. ja va  2 s.co  m
        List<String> files = IOUtils.readLines(
                LicensingList.class.getClassLoader().getResourceAsStream(licenseTextDirectory),
                StandardCharsets.UTF_8);

        LinkExtractor linkExtractor = LinkExtractor.builder().build();
        for (String licenseFN : files) {
            InputStream inputStream;
            String fn = "/" + licenseTextDirectory + licenseFN;
            inputStream = LicensingList.class.getResourceAsStream(fn);

            String link;
            if (inputStream != null) {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                boolean foundUrl = false;
                boolean foundListPlaceholder = false;
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    if (line.toLowerCase().contains(libraryListPlaceholder)) {
                        foundListPlaceholder = true;
                        break;
                    }
                    //Iterable<LinkSpan> links = linkExtractor.extractLinks(line);
                    for (LinkSpan linkSpan : linkExtractor.extractLinks(line)) {
                        // get substring containing the link and prune protocol
                        link = line.substring(linkSpan.getBeginIndex(), linkSpan.getEndIndex())
                                .replaceFirst("^[^:]+://", "");
                        result.put(link, licenseFN);
                        foundUrl = true;
                    }
                }
                bufferedReader.close();

                if (!foundUrl)
                    logger.warn("No license URL found in " + licenseFN + " template file.");
                if (!foundListPlaceholder) {
                    String msg = "Library list placeholder (" + libraryListPlaceholder + ") not found in "
                            + licenseFN + " template file.";
                    logger.error(msg);
                    throw new RuntimeException(msg);
                }
            }
        }
    } catch (final IOException ex) {
        throw new RuntimeException(ex);
    }
    return result;
}

From source file:gobblin.config.store.hdfs.SimpleHadoopFilesystemConfigStore.java

/**
 * Retrieves all the {@link ConfigKeyPath}s that are imported by the given {@link ConfigKeyPath}. This method does this
 * by reading the {@link #INCLUDES_CONF_FILE_NAME} file associated with the dataset specified by the given
 * {@link ConfigKeyPath}. If the {@link Path} described by the {@link ConfigKeyPath} does not exist, then an empty
 * {@link List} is returned./*from  w ww  . ja  v  a 2 s .com*/
 *
 * @param  configKey      the config key path whose tags are needed
 * @param  version        the configuration version in the configuration store.
 *
 * @return a {@link List} of {@link ConfigKeyPath}s where each entry is a {@link ConfigKeyPath} imported by the dataset
 * specified by the configKey.
 *
 * @throws VersionDoesNotExistException if the version specified cannot be found in the {@link ConfigStore}.
 */
public List<ConfigKeyPath> getOwnImports(ConfigKeyPath configKey, String version,
        Optional<Config> runtimeConfig) throws VersionDoesNotExistException {
    Preconditions.checkNotNull(configKey, "configKey cannot be null!");
    Preconditions.checkArgument(!Strings.isNullOrEmpty(version), "version cannot be null or empty!");

    List<ConfigKeyPath> configKeyPaths = new ArrayList<>();
    Path datasetDir = getDatasetDirForKey(configKey, version);
    Path includesFile = new Path(datasetDir, INCLUDES_CONF_FILE_NAME);

    try {
        if (!this.fs.exists(includesFile)) {
            return configKeyPaths;
        }

        FileStatus includesFileStatus = this.fs.getFileStatus(includesFile);
        if (!includesFileStatus.isDirectory()) {
            try (InputStream includesConfInStream = this.fs.open(includesFileStatus.getPath())) {
                /*
                 * The includes returned are used to build a fallback chain.
                 * With the natural order, if a key found in the first include it is not be overriden by the next include.
                 * By reversing the list, the Typesafe fallbacks are constructed bottom up.
                 */
                configKeyPaths.addAll(Lists.newArrayList(Iterables.transform(
                        Lists.reverse(resolveIncludesList(
                                IOUtils.readLines(includesConfInStream, Charsets.UTF_8), runtimeConfig)),
                        new IncludesToConfigKey())));
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(String.format("Error while getting config for configKey: \"%s\"", configKey),
                e);
    }

    return configKeyPaths;
}

From source file:com.jsmartframework.web.manager.BeanHelper.java

private void setExposeVarMapping(Field field, VarMapping varMapping) {
    try {/*from w ww  .j a v  a2  s .co  m*/
        List<String> properties = IOUtils.readLines(getClass().getClassLoader().getResourceAsStream("/"),
                ENCODING);
        for (String propertiesName : properties) {

            if (propertiesName.startsWith(varMapping.i18n()) && propertiesName.endsWith(".properties")) {
                String language = CONFIG.getContent().getDefaultLanguage();

                Matcher matcher = PROPERTIES_NAME_PATTERN.matcher(propertiesName);
                if (matcher.find()) {
                    language = matcher.group(1);
                }
                setExposeVarMapping(field, varMapping, language);
            }
        }
        setExposeVarMapping(field, varMapping, Locale.getDefault().getLanguage());

    } catch (IOException ex) {
        LOGGER.log(Level.SEVERE, "Error reading list of resource properties", ex);
    }
}

From source file:de.huxhorn.lilith.swing.preferences.PreferencesDialog.java

public void editDetailsFormatter() {
    Console console = new Console();
    File messageViewRoot = applicationPreferences.getDetailsViewRoot();
    File messageViewGroovyFile = new File(messageViewRoot, ApplicationPreferences.DETAILS_VIEW_GROOVY_FILENAME);

    EventWrapper<LoggingEvent> eventWrapper = new EventWrapper<>(
            new SourceIdentifier("identifier", "secondaryIdentifier"), 17, new LoggingEvent());
    console.setVariable("eventWrapper", eventWrapper);

    console.setCurrentFileChooserDir(messageViewRoot);
    String text = "";
    if (!messageViewGroovyFile.isFile()) {
        applicationPreferences.initDetailsViewRoot(true);
    }//from   www  .ja  va2s.co m
    if (messageViewGroovyFile.isFile()) {
        InputStream is;
        try {
            is = new FileInputStream(messageViewGroovyFile);
            List lines = IOUtils.readLines(is, StandardCharsets.UTF_8);
            boolean isFirst = true;
            StringBuilder textBuffer = new StringBuilder();
            for (Object o : lines) {
                String s = (String) o;
                if (isFirst) {
                    isFirst = false;
                } else {
                    textBuffer.append("\n");
                }
                textBuffer.append(s);
            }
            text = textBuffer.toString();
        } catch (IOException e) {
            if (logger.isInfoEnabled()) {
                logger.info("Exception while reading '" + messageViewGroovyFile.getAbsolutePath() + "'.", e);
            }
        }
    } else {
        if (logger.isWarnEnabled())
            logger.warn("Failed to initialize detailsView file '{}'!", messageViewGroovyFile.getAbsolutePath());
    }
    console.run(); // initializes everything

    console.setScriptFile(messageViewGroovyFile);
    JTextPane inputArea = console.getInputArea();
    //inputArea.setText(text);
    Document doc = inputArea.getDocument();
    try {
        doc.remove(0, doc.getLength());
        doc.insertString(0, text, null);
    } catch (BadLocationException e) {
        if (logger.isWarnEnabled())
            logger.warn("Exception while setting source!", e);
    }
    console.setDirty(false);
    inputArea.setCaretPosition(0);
    inputArea.requestFocusInWindow();
}

From source file:com.baasbox.db.DbHelper.java

public static void populateDB() throws IOException {
    ODatabaseRecordTx db = getConnection();
    //DO NOT DELETE THE FOLLOWING LINE!
    OrientGraphNoTx dbg = new OrientGraphNoTx(getODatabaseDocumentTxConnection());
    BaasBoxLogger.info("Populating the db...");
    InputStream is;//from  w w  w.java2  s  .c o m
    if (Play.application().isProd())
        is = Play.application().resourceAsStream(SCRIPT_FILE_NAME);
    else
        is = new FileInputStream(Play.application().getFile("conf/" + SCRIPT_FILE_NAME));
    List<String> script = IOUtils.readLines(is, "UTF-8");
    is.close();

    for (String line : script) {
        if (BaasBoxLogger.isDebugEnabled())
            BaasBoxLogger.debug(line);
        if (!line.startsWith("--") && !line.trim().isEmpty()) { //skip comments
            db.command(new OCommandSQL(line.replace(';', ' '))).execute();
        }
    }
    Internal.DB_VERSION._setValue(BBConfiguration.getDBVersion());
    String uniqueId = "";
    try {
        UUID u = new UUID();
        uniqueId = new String(Base64.encodeBase64(u.toString().getBytes()));
    } catch (Exception e) {
        java.util.UUID u = java.util.UUID.randomUUID();
        uniqueId = new String(Base64.encodeBase64(u.toString().getBytes()));
    }
    Internal.INSTALLATION_ID._setValue(uniqueId);
    BaasBoxLogger.info("Unique installation id is: " + uniqueId);
    BaasBoxLogger.info("...done");
}

From source file:net.minecraftforge.fml.common.FMLCommonHandler.java

/**
 * Loads a lang file, first searching for a marker to enable the 'extended' format {escape charaters}
 * If the marker is not found it simply returns and let the vanilla code load things.
 * The Marker is 'PARSE_ESCAPES' by itself on a line starting with '#' as such:
 * #PARSE_ESCAPES/* w  w  w  .  jav  a2 s.  co  m*/
 *
 * @param table The Map to load each key/value pair into.
 * @param inputstream Input stream containing the lang file.
 * @return A new InputStream that vanilla uses to load normal Lang files, Null if this is a 'enhanced' file and loading is done.
 */
public InputStream loadLanguage(Map<String, String> table, InputStream inputstream) throws IOException {
    byte[] data = IOUtils.toByteArray(inputstream);

    boolean isEnhanced = false;
    for (String line : IOUtils.readLines(new ByteArrayInputStream(data), Charsets.UTF_8)) {
        if (!line.isEmpty() && line.charAt(0) == '#') {
            line = line.substring(1).trim();
            if (line.equals("PARSE_ESCAPES")) {
                isEnhanced = true;
                break;
            }
        }
    }

    if (!isEnhanced)
        return new ByteArrayInputStream(data);

    Properties props = new Properties();
    props.load(new InputStreamReader(new ByteArrayInputStream(data), Charsets.UTF_8));
    for (Entry e : props.entrySet()) {
        table.put((String) e.getKey(), (String) e.getValue());
    }
    props.clear();
    return null;
}

From source file:com.thoughtworks.go.domain.materials.git.GitCommandTest.java

@Test
void shouldParseGitOutputCorrectly() throws IOException {
    List<String> stringList;
    try (InputStream resourceAsStream = getClass().getResourceAsStream("git_sample_output.text")) {
        stringList = IOUtils.readLines(resourceAsStream, UTF_8);
    }/*from  w w  w . j av  a2 s.  co m*/

    GitModificationParser parser = new GitModificationParser();
    List<Modification> mods = parser.parse(stringList);
    assertThat(mods).hasSize(3);

    Modification mod = mods.get(2);
    assertThat(mod.getRevision()).isEqualTo("46cceff864c830bbeab0a7aaa31707ae2302762f");
    assertThat(mod.getModifiedTime()).isEqualTo(DateUtils.parseISO8601("2009-08-11 12:37:09 -0700"));
    assertThat(mod.getUserDisplayName()).isEqualTo("Cruise Developer <cruise@cruise-sf3.(none)>");
    assertThat(mod.getComment()).isEqualTo("author:cruise <cceuser@CceDev01.(none)>\n"
            + "node:ecfab84dd4953105e3301c5992528c2d381c1b8a\n" + "date:2008-12-31 14:32:40 +0800\n"
            + "description:Moving rakefile to build subdirectory for #2266\n" + "\n"
            + "author:CceUser <cceuser@CceDev01.(none)>\n" + "node:fd16efeb70fcdbe63338c49995ce9ff7659e6e77\n"
            + "date:2008-12-31 14:17:06 +0800\n" + "description:Adding rakefile");
}

From source file:eionet.cr.web.action.factsheet.FactsheetActionBean.java

/**
 *
 * @return//from ww  w.  ja va  2s . c  om
 */
private static Set<String> createEditableTypes() {

    LinkedHashSet<String> result = new LinkedHashSet<String>();

    InputStream inputStream = null;
    try {
        inputStream = FactsheetActionBean.class.getClassLoader()
                .getResourceAsStream(FULLY_EDITABLE_TYPES_FILE_NAME);
        List<String> lines = IOUtils.readLines(inputStream, "UTF-8");
        for (String line : lines) {
            result.add(line.trim());
        }
    } catch (IOException e) {
        LOGGER.error("Failed reading lines from " + FULLY_EDITABLE_TYPES_FILE_NAME, e);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }

    LOGGER.debug("Found these fully editable types: " + result);

    return result;
}