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(Reader input) throws IOException 

Source Link

Document

Get the contents of a Reader as a list of Strings, one entry per line.

Usage

From source file:ml.shifu.shifu.util.CommonUtils.java

public static List<String> readConfFileIntoList(String configFile, SourceType sourceType) throws IOException {
    List<String> fileLines = new ArrayList<String>();

    if (StringUtils.isBlank(configFile) || !ShifuFileUtils.isFileExists(configFile, sourceType)) {
        return fileLines;
    }// w  w w  . j  ava  2 s.co m

    List<String> strList = null;
    Reader reader = null;
    try {
        reader = ShifuFileUtils.getReader(configFile, sourceType);
        strList = IOUtils.readLines(reader);
    } finally {
        IOUtils.closeQuietly(reader);
    }

    if (CollectionUtils.isNotEmpty(strList)) {
        for (String line : strList) { // skip empty line and line start with "#"
            if (StringUtils.isBlank(line) || line.trim().startsWith("#")) {
                continue;
            }
            fileLines.add(StringUtils.trim(line));
        }
    }

    return fileLines;
}

From source file:edu.ucsd.crbs.cws.cluster.submission.TestJobCmdScriptCreatorImpl.java

@Test
public void testCreateAndRunScriptWhereUpdateFailsOnceAndThenWorks() throws Exception {
    assumeTrue(SystemUtils.IS_OS_UNIX);/* www  . j a  v a 2  s .  c  o  m*/
    File baseDirectory = Folder.newFolder();
    File tempDirectory = new File(baseDirectory + File.separator + "subdir");
    File outputsDir = new File(tempDirectory + File.separator + Constants.OUTPUTS_DIR_NAME);
    assertTrue(outputsDir.mkdirs());

    JobEmailNotificationData emailNotifyData = createJobEmailNotificationData();

    JobBinaries jb = new JobBinaries();
    jb.setKeplerScript(getAndCheckForTrueBinaryFile().getAbsolutePath());
    jb.setRegisterUpdateJar("register.jar");
    jb.setMailCommand("cat > email.${finishedMessage};/bin/echo");
    jb.setJavaCommand("if [ $cntr -eq 0 ] ; then " + getAndCheckForFalseBinaryFile().getAbsolutePath()
            + "; else " + getAndCheckForTrueBinaryFile().getAbsolutePath() + "; fi #");
    jb.setRetryCount(3);

    JobCmdScriptCreatorImpl scriptCreator = new JobCmdScriptCreatorImpl("/workflowsdir", jb, emailNotifyData);

    Job j = new Job();
    Workflow w = new Workflow();
    w.setId(new Long(5));
    j.setWorkflow(w);

    String jobCmd = scriptCreator.create(tempDirectory.getAbsolutePath(), j, new Long(10));

    assertTrue(jobCmd != null);
    assertTrue(
            jobCmd.equals(outputsDir.getAbsolutePath() + File.separator + JobCmdScriptCreatorImpl.JOB_CMD_SH));
    File checkCmdFile = new File(jobCmd);
    assertTrue(checkCmdFile.canExecute());

    RunCommandLineProcessImpl rclpi = new RunCommandLineProcessImpl();
    rclpi.setWorkingDirectory(outputsDir.getAbsolutePath());

    String result = rclpi.runCommandLineProcess(jobCmd);
    assertTrue(result.contains("Update of workspace path try 1 of 3 failed.  Sleeping 0 seconds and retrying"));

    String logFile = tempDirectory.getAbsoluteFile() + File.separator + "job...log";
    File checkLogFile = new File(logFile);
    assertTrue(logFile + " and we ran " + jobCmd, checkLogFile.exists());
    List<String> lines = IOUtils.readLines(new FileReader(logFile));
    boolean exitFound = false;
    for (String line : lines) {
        if (line.startsWith("exitcode: ")) {
            assertTrue(line, line.equals("exitcode: 0"));
            exitFound = true;
        }
    }
    assertTrue(exitFound);
}

From source file:net.tracknalysis.tracklogger.export.AbstractSessionToTrackLoggerCsvExporterTest.java

@Test
public void testExport() throws Exception {
    exporter.export(1);//from  w ww .j av  a  2 s  .c  om

    List<String> lines = IOUtils
            .readLines(new FileInputStream(new File("target", "1-1969.12.31-19.00.00.csv")));
    assertEquals(9, lines.size());

    assertTrue(lines.get(0).startsWith("Session Title: 1 - "));
    assertEquals("Split Marker Set: Split Marker Name!", lines.get(1));
    assertTrue(lines.get(2).startsWith("Export Time: "));
    assertEquals("Format: TrackLogger CSV 1.0b", lines.get(3));
    assertEquals(
            "running_time,synch_timestamp,accel_capture_timestamp,longitudinal_accel,lateral_accel,vertical_accel,location_capture_timestamp,latitude,longitude,altitude,speed,bearing,ecu_capture_timestamp,rpm,map,mgp,throttle_position,afr,mat,clt,ignition_advance,battery_voltage,lap_capture_timestamp,lap,split",
            lines.get(5));
    assertEquals(
            "0,1,2,3.00000000000000000000,4.00000000000000000000,5.00000000000000000000,6,7.00000000000000000000,8.00000000000000000000,9.00000000000000000000,10.00000000000000000000,11.00000000000000000000,12,13,14.00000000000000000000,24.00000000000000000000,15.00000000000000000000,16.00000000000000000000,17.00000000000000000000,18.00000000000000000000,19.00000000000000000000,20.00000000000000000000,21,22,23",
            lines.get(6));
    assertEquals(
            "86399998,86399999,12,13.00000000000000000000,14.00000000000000000000,15.00000000000000000000,16,17.00000000000000000000,18.00000000000000000000,19.00000000000000000000,110.00000000000000000000,111.00000000000000000000,112,113,114.00000000000000000000,124.00000000000000000000,115.00000000000000000000,116.00000000000000000000,117.00000000000000000000,118.00000000000000000000,119.00000000000000000000,120.00000000000000000000,121,122,123",
            lines.get(7));
    assertEquals(
            "86400000,1,12,13.00000000000000000000,14.00000000000000000000,15.00000000000000000000,16,17.00000000000000000000,18.00000000000000000000,19.00000000000000000000,110.00000000000000000000,111.00000000000000000000,112,113,114.00000000000000000000,124.00000000000000000000,115.00000000000000000000,116.00000000000000000000,117.00000000000000000000,118.00000000000000000000,119.00000000000000000000,120.00000000000000000000,121,122,123",
            lines.get(8));
}

From source file:nl.surfsara.newsreader.pipeline.PipelineLayout.java

private void init(String layouFile) throws FileNotFoundException, IOException, ClassNotFoundException {
    List<String> lines = IOUtils.readLines(new FileReader(new File(layoutFile)));
    StringBuilder jsonString = new StringBuilder();
    for (String s : lines) {
        jsonString.append(s);// ww w  . ja  v  a  2s  .  c  om
    }
    // TODO move field reference strings to constants/enums or a static class  
    JSONObject jo = new JSONObject(jsonString.toString());
    pipelineid = (String) jo.get("id");
    pipelineversion = (String) jo.get("version");
    description = (String) jo.get("description");
    JSONArray ja = jo.getJSONArray("layout");
    steps = new ArrayList<PipelineStep>();
    for (int i = 0; i < ja.length(); i++) {
        JSONObject jsonObject = ja.getJSONObject(i);
        String name = jsonObject.getString("name");
        String className = jsonObject.getString("class");
        long timeOut = jsonObject.getLong("timeout");
        int numErrorLine = jsonObject.getInt("numErrorLines");

        @SuppressWarnings("unchecked")
        PipelineStep step = new PipelineStep(name, ((Class<? extends Module>) Class.forName(className)),
                timeOut, numErrorLine);
        steps.add(step);
    }
}

From source file:opennlp.tools.languagemodel.NgramLanguageModelTest.java

@Test
public void testTrigramLanguageModelCreationFromText() throws Exception {
    int ngramSize = 3;
    NGramLanguageModel languageModel = new NGramLanguageModel(ngramSize);
    InputStream stream = getClass().getResourceAsStream("/opennlp/tools/languagemodel/sentences.txt");
    for (String line : IOUtils.readLines(stream)) {
        String[] array = line.split(" ");
        List<String> split = Arrays.asList(array);
        List<String> generatedStrings = NGramGenerator.generate(split, ngramSize, " ");
        for (String generatedString : generatedStrings) {
            String[] tokens = generatedString.split(" ");
            if (tokens.length > 0) {
                languageModel.add(new StringList(tokens), 1, ngramSize);
            }// www.  ja va 2s .  c  o  m
        }
    }
    StringList tokens = languageModel.predictNextTokens(new StringList("neural", "network", "language"));
    Assert.assertNotNull(tokens);
    Assert.assertEquals(new StringList("models"), tokens);
    double p1 = languageModel.calculateProbability(new StringList("neural", "network", "language", "models"));
    double p2 = languageModel.calculateProbability(new StringList("neural", "network", "language", "model"));
    Assert.assertTrue(p1 > p2);
}

From source file:org.aksw.simba.cetus.tools.ClassModelCreator.java

@Deprecated
public static boolean inferClassRelations_OLD(Model classModel) {
    InputStream is = AbstractNIFParser.class.getClassLoader().getResourceAsStream(TYPE_INFERENCE_RULES);
    List<String> lines;
    try {//from w  w  w .  j  a va2 s.c  o m
        lines = IOUtils.readLines(is);
    } catch (IOException e) {
        LOGGER.error("Couldn't load type inferencer rules from resource \"" + TYPE_INFERENCE_RULES
                + "\". Working on the standard model.", e);
        return false;
    }
    IOUtils.closeQuietly(is);
    StringBuilder sb = new StringBuilder();
    for (String line : lines) {
        sb.append(line);
    }

    Reasoner reasoner = new GenericRuleReasoner(Rule.parseRules(sb.toString()));
    InfModel infModel = ModelFactory.createInfModel(reasoner, classModel);
    classModel.add(infModel);
    return true;
}

From source file:org.aksw.simba.tapioca.data.VocabularyBlacklist.java

protected static Set<String> loadList(String listName) {
    InputStream is = VocabularyBlacklist.class.getClassLoader().getResourceAsStream(listName);
    if (is == null) {
        LOGGER.error("Couldn't load list from resources. Returning null.");
        return new HashSet<String>();
    }/*from w ww .j ava  2 s. c o m*/
    Set<String> list = null;
    try {
        List<String> lines = IOUtils.readLines(is);
        list = new HashSet<String>(lines);
    } catch (IOException e) {
        LOGGER.error("Couldn't load list from resources. Returning null.", e);
    } finally {
        IOUtils.closeQuietly(is);
    }
    return list;
}

From source file:org.aksw.simba.tapioca.extraction.voidex.SpecialClassExtractor.java

protected static Set<Node> loadList(String listName) {
    InputStream is = SpecialClassExtractor.class.getClassLoader().getResourceAsStream(listName);
    if (is == null) {
        LOGGER.error("Couldn't load list " + listName + " from resources. Returning empty list.");
        return new HashSet<Node>();
    }//from   w w  w . jav a  2  s.c  o m
    List<String> lines;
    try {
        lines = IOUtils.readLines(is);
    } catch (IOException e) {
        LOGGER.error("Couldn't load list from resources. Returning empty list.", e);
        return new HashSet<Node>();
    }
    Set<Node> resourceList = new HashSet<Node>((int) 2 * lines.size());
    for (String line : lines) {
        resourceList.add(ResourceFactory.createResource(line.trim()).asNode());
    }
    return resourceList;
}

From source file:org.aludratest.codecheck.rule.pmd.annot.NoTrailingCommaInAnnotationArray.java

private void checkForTrailingComma(ASTMemberValueArrayInitializer node, RuleContext context) {
    int beginLine = node.getBeginLine();
    int endLine = node.getEndLine();
    int beginColumn = node.getBeginColumn();
    int endColumn = node.getEndColumn();

    InputStream in = null;/*from   w w  w  .ja v  a2s . co  m*/
    try {
        File file = context.getSourceCodeFile();
        if (file == null || !file.isFile()) {
            // construct a URL from source code file name
            in = getSourceCodeInputStream(context);
            if (in == null) {
                context.getReport()
                        .addError(new ProcessingError(
                                "Cannot find source code file " + context.getSourceCodeFilename(),
                                context.getSourceCodeFilename()));
                return;
            }
        } else {
            in = new FileInputStream(file);
        }
        List<String> lines = IOUtils.readLines(in);
        StringBuilder sbContent = new StringBuilder();
        for (int line = beginLine; line <= endLine; line++) {
            if (line > lines.size()) {
                throw new IOException("Unexpected end of file " + file.getAbsolutePath());
            }

            String toAppend = null;

            // replace tab by 8 spaces (relatively hard coded in PMD)
            String spaces = "        ";
            String thisLine = lines.get(line - 1).replace("\t", spaces);

            if (line == beginLine && line == endLine) {
                toAppend = thisLine.substring(beginColumn - 1, endColumn);
            } else if (line == beginLine) {
                toAppend = thisLine.substring(beginColumn - 1);
            } else if (line == endLine) {
                toAppend = thisLine.substring(0, endColumn);
            } else {
                toAppend = thisLine;
            }

            // strip comments from line. Yeah, we have to... and yes, it is not 100% safe.
            // for example: /* // */, MyClass.class, } would be undetected.
            toAppend = stripLineComments(toAppend);

            sbContent.append(toAppend);
        }

        String arrayCode = sbContent.toString();
        // strip comments from code. Yeah, we have to...
        arrayCode = arrayCode.replaceAll("/\\*([^/]|/[^\\*])*\\*/", "");

        if (arrayCode.matches(".*,\\s*\\}\\s*")) {
            addViolationWithMessage(context, node,
                    "Annotation Array initializers should not contain a trailing comma");
        }
    } catch (IOException e) {
        context.getReport().addError(new ProcessingError(e.getMessage(), context.getSourceCodeFilename()));
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException ee) {
            // ignore
        }
    }
}

From source file:org.apache.ambari.server.controller.AmbariManagementControllerImpl.java

/**
 * Verifies single repository, see {{@link #verifyRepositories(Set)}.
 *
 * @param request request//from www  .jav  a 2s .  c o m
 * @throws AmbariException if verification fails
 */
private void verifyRepository(RepositoryRequest request) throws AmbariException {
    URLStreamProvider usp = new URLStreamProvider(REPO_URL_CONNECT_TIMEOUT, REPO_URL_READ_TIMEOUT, null, null,
            null);

    RepositoryInfo repositoryInfo = ambariMetaInfo.getRepository(request.getStackName(),
            request.getStackVersion(), request.getOsType(), request.getRepoId());
    String repoName = repositoryInfo.getRepoName();

    String errorMessage = null;

    String[] suffixes = configs.getRepoValidationSuffixes(request.getOsType());
    for (String suffix : suffixes) {
        String formatted_suffix = String.format(suffix, repoName);
        String spec = request.getBaseUrl().trim();

        // This logic is to identify if the end of baseurl has a slash ('/') and/or the beginning of suffix String (e.g. "/repodata/repomd.xml")
        // has a slash and they can form a good url.
        // e.g. "http://baseurl.com/" + "/repodata/repomd.xml" becomes "http://baseurl.com/repodata/repomd.xml" but not "http://baseurl.com//repodata/repomd.xml"
        if (spec.charAt(spec.length() - 1) != '/' && formatted_suffix.charAt(0) != '/') {
            spec = spec + "/" + formatted_suffix;
        } else if (spec.charAt(spec.length() - 1) == '/' && formatted_suffix.charAt(0) == '/') {
            spec = spec + formatted_suffix.substring(1);
        } else {
            spec = spec + formatted_suffix;
        }

        // if spec contains "file://" then check local file system.
        final String FILE_SCHEME = "file://";
        if (spec.toLowerCase().startsWith(FILE_SCHEME)) {
            String filePath = spec.substring(FILE_SCHEME.length());
            File f = new File(filePath);
            if (!f.exists()) {
                errorMessage = "Could not access base url . " + spec + " . ";
                break;
            }

        } else {
            try {
                IOUtils.readLines(usp.readFrom(spec));
            } catch (IOException ioe) {
                errorMessage = "Could not access base url . " + request.getBaseUrl() + " . ";
                if (LOG.isDebugEnabled()) {
                    errorMessage += ioe;
                } else {
                    errorMessage += ioe.getMessage();
                }
                break;
            }
        }
    }

    if (errorMessage != null) {
        LOG.error(errorMessage);
        throw new IllegalArgumentException(errorMessage);
    }
}