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

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

Introduction

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

Prototype

public static List readLines(File file) throws IOException 

Source Link

Document

Reads the contents of a file line by line to a List of Strings using the default encoding for the VM.

Usage

From source file:de.tudarmstadt.ukp.dkpro.core.langdect.LanguageDetectorTest.java

@Ignore
@Test// ww w . j a  v  a  2 s .c  o  m
public void languageDetectorTest() throws Exception {
    String web1TBaseDir = new DkproContext().getWorkspace("web1t").getAbsolutePath();

    AnalysisEngine engine = createEngine(createEngineDescription(
            createEngineDescription(BreakIteratorSegmenter.class),
            createEngineDescription(LanguageDetector.class, LanguageDetector.PARAM_FREQUENCY_PROVIDER_RESOURCES,
                    Arrays.asList(
                            createExternalResourceDescription(Web1TFrequencyCountResource.class,
                                    Web1TFrequencyCountResource.PARAM_INDEX_PATH, web1TBaseDir + "/en",
                                    Web1TFrequencyCountResource.PARAM_MIN_NGRAM_LEVEL, "1",
                                    Web1TFrequencyCountResource.PARAM_MAX_NGRAM_LEVEL, "3"),
                            createExternalResourceDescription(Web1TFrequencyCountResource.class,
                                    Web1TFrequencyCountResource.PARAM_INDEX_PATH, web1TBaseDir + "/de",
                                    Web1TFrequencyCountResource.PARAM_MIN_NGRAM_LEVEL, "1",
                                    Web1TFrequencyCountResource.PARAM_MAX_NGRAM_LEVEL, "3")))));

    for (String line : FileUtils.readLines(new File("src/test/resources/langdect/test.txt"))) {
        String[] parts = line.split("\t");
        String text = parts[0];
        String language = parts[1];

        JCas aJCas = engine.newJCas();
        aJCas.setDocumentText(text);
        engine.process(aJCas);

        String[] languageParts = aJCas.getDocumentLanguage().split("/");
        String casLanguage = languageParts[languageParts.length - 1];

        assertEquals(language, casLanguage);
    }
}

From source file:edu.ku.brc.dbsupport.SchemaUpdateService.java

/**
 * @param conn//from w  w w . j  ava2  s .c o m
 * @param fileName
 * @return
 * @throws Exception
 */
@SuppressWarnings("unchecked")
public static boolean createDBTablesFromSQLFile(final Connection conn, final String fileName) throws Exception {
    File outFile = XMLHelper.getConfigDir(fileName);
    if (outFile != null && outFile.exists()) {
        StringBuilder sb = new StringBuilder();
        Statement stmt = conn.createStatement();
        List<?> list = FileUtils.readLines(outFile);

        for (String line : (List<String>) list) {
            String tLine = line.trim();
            sb.append(tLine);

            if (tLine.endsWith(";")) {
                System.out.println(sb.toString());
                stmt.executeUpdate(sb.toString());
                sb.setLength(0);
            }
        }
        stmt.close();
        return true;
    }
    return false;
}

From source file:com.gargoylesoftware.htmlunit.source.BrowserVersionFeaturesSource.java

private void rename(final File file, final String oldName, final String newName) throws IOException {
    final List<String> lines = FileUtils.readLines(file);
    boolean modified = false;
    for (int i = 0; i < lines.size(); i++) {
        String line = lines.get(i);
        if (line.equals(oldName) || line.contains(oldName + ")") || line.contains(oldName + ",")) {
            line = line.replace(oldName, newName);
            lines.set(i, line);/*from   ww  w. j a  v a2  s .c o m*/
            modified = true;
        }
    }
    if (modified) {
        FileUtils.writeLines(file, lines);
    }
}

From source file:com.btisystems.pronx.ems.AppIntegrationTest.java

/**
 * Strips out the Java version from the notification meta data. This isn't
 * needed when diffing the contents.//from w w w  .  j a  va  2s. c  o  m
 *
 * @param file
 * @throws IOException
 */
private void trimNotificationMeta(File file) throws IOException {
    List<String> lines = FileUtils.readLines(file);
    List<String> updatedLines = new ArrayList<>();
    for (final String line : lines) {
        if (!line.startsWith("<java version=")) {
            updatedLines.add(line);
        }
    }
    FileUtils.writeLines(file, updatedLines, false);
}

From source file:hu.bme.mit.sette.tools.spf.SpfParser.java

@Override
protected void parseSnippet(Snippet snippet, SnippetInputsXml inputsXml) throws Exception {
    File outputFile = RunnerProjectUtils.getSnippetOutputFile(getRunnerProjectSettings(), snippet);
    File errorFile = RunnerProjectUtils.getSnippetErrorFile(getRunnerProjectSettings(), snippet);

    if (errorFile.exists()) {

        // TODO make this section simple and clear

        List<String> lines = FileUtils.readLines(errorFile);

        String firstLine = lines.get(0);

        if (firstLine.startsWith("java.lang.RuntimeException: ## Error: Operation not supported!")) {
            inputsXml.setResultType(ResultType.NA);
        } else if (firstLine.startsWith("java.lang.NullPointerException")) {
            inputsXml.setResultType(ResultType.EX);
        } else if (firstLine
                .startsWith("java.lang.RuntimeException: ## Error: symbolic log10 not implemented")) {
            inputsXml.setResultType(ResultType.NA);
        } else if (firstLine.startsWith("***********Warning: everything false")) {
            // TODO enhance
            // now skip
        } else if (snippet.getMethod().toString().contains("_Constants")
                || snippet.getMethod().toString().contains(".always()")) {
            // TODO JPF/SPF compilation differences between javac and ecj:
            // https://groups.google.com/forum/#!topic/java-pathfinder/jhOkvLx-SKE
            // now just accept
        } else {//from  w  w  w  .  j  a va  2 s  .  c o  m
            // TODO error handling

            // this is debug (only if unhandled error)
            System.err.println("=============================");
            System.err.println(snippet.getMethod());
            System.err.println("=============================");

            for (String line : lines) {
                System.err.println(line);
            }
            System.err.println("=============================");

            // TODO error handling
            throw new RuntimeException("PARSER PROBLEM, UNHANDLED ERROR");
        }
    }

    if (inputsXml.getResultType() == null) {
        // TODO enhance
        inputsXml.setResultType(ResultType.S);

        if (snippet.getMethod().toString().contains("_Constants")
                || snippet.getMethod().toString().contains(".always()")) {
            // TODO JPF/SPF compilation differences between javac and ecj:
            // https://groups.google.com/forum/#!topic/java-pathfinder/jhOkvLx-SKE
            // now just accept

            // no inputs for constant tests, just call them once
            inputsXml.getGeneratedInputs().add(new InputElement());
        } else {
            LineIterator lines = FileUtils.lineIterator(outputFile);

            // find input lines

            List<String> inputLines = new ArrayList<>();
            boolean shouldCollect = false;
            while (lines.hasNext()) {
                String line = lines.next();
                if (line.trim()
                        .equals("====================================================== Method Summaries")) {
                    shouldCollect = true;
                } else if (shouldCollect) {
                    if (line.startsWith("======================================================")) {
                        // start of next section
                        shouldCollect = false;
                        break;
                    } else {
                        if (!StringUtils.isBlank(line)) {
                            inputLines.add(line.trim());
                        }
                    }
                }
            }

            // close iterator
            lines.close();

            // remove duplicates
            inputLines = new ArrayList<>(new LinkedHashSet<>(inputLines));

            System.out.println(snippet.getMethod());

            String firstLine = inputLines.get(0);
            assert (firstLine.startsWith("Inputs: "));
            firstLine = firstLine.substring(7).trim();
            String[] parameterStrings = StringUtils.split(firstLine, ',');
            ParameterType[] parameterTypes = new ParameterType[parameterStrings.length];

            if (inputLines.size() == 2 && inputLines.get(1).startsWith("No path conditions for")) {
                InputElement input = new InputElement();
                for (int i = 0; i < parameterStrings.length; i++) {
                    // no path conditions, only considering the "default"
                    // inputs
                    Class<?> type = snippet.getMethod().getParameterTypes()[i];
                    parameterTypes[i] = getParameterType(type);
                    input.getParameters()
                            .add(new ParameterElement(parameterTypes[i], getDefaultParameterString(type)));
                }
                inputsXml.getGeneratedInputs().add(input);
            } else {
                // parse parameter types

                Class<?>[] paramsJavaClass = snippet.getMethod().getParameterTypes();

                for (int i = 0; i < parameterStrings.length; i++) {
                    String parameterString = parameterStrings[i];
                    Class<?> pjc = ClassUtils.primitiveToWrapper(paramsJavaClass[i]);

                    if (parameterString.endsWith("SYMINT")) {
                        if (pjc == Boolean.class) {
                            parameterTypes[i] = ParameterType.BOOLEAN;
                        } else if (pjc == Byte.class) {
                            parameterTypes[i] = ParameterType.BYTE;
                        } else if (pjc == Short.class) {
                            parameterTypes[i] = ParameterType.SHORT;
                        } else if (pjc == Integer.class) {
                            parameterTypes[i] = ParameterType.INT;
                        } else if (pjc == Long.class) {
                            parameterTypes[i] = ParameterType.LONG;
                        } else {
                            // int for something else
                            parameterTypes[i] = ParameterType.INT;
                        }
                    } else if (parameterString.endsWith("SYMREAL")) {
                        if (pjc == Float.class) {
                            parameterTypes[i] = ParameterType.FLOAT;
                        } else if (pjc == Float.class) {
                            parameterTypes[i] = ParameterType.DOUBLE;
                        } else {
                            // int for something else
                            parameterTypes[i] = ParameterType.DOUBLE;
                        }
                    } else if (parameterString.endsWith("SYMSTRING")) {
                        parameterTypes[i] = ParameterType.EXPRESSION;
                    } else {
                        // TODO error handling
                        // int for something else
                        System.err.println(parameterString);
                        throw new RuntimeException("PARSER PROBLEM");
                    }
                }

                // example
                // inheritsAPIGuessTwoPrimitives(11,-2147483648(don't care))
                // -->
                // "java.lang.IllegalArgumentException..."
                // inheritsAPIGuessTwoPrimitives(9,11) -->
                // "java.lang.IllegalArgumentException..."
                // inheritsAPIGuessTwoPrimitives(7,9) -->
                // "java.lang.RuntimeException: Out of range..."
                // inheritsAPIGuessTwoPrimitives(4,1) --> Return Value: 1
                // inheritsAPIGuessTwoPrimitives(0,0) --> Return Value: 0
                // inheritsAPIGuessTwoPrimitives(9,-88) -->
                // "java.lang.IllegalArgumentException..."
                // inheritsAPIGuessTwoPrimitives(-88,-2147483648(don't
                // care))
                // --> "java.lang.IllegalArgumentException..."

                String ps = String.format("^%s\\((.*)\\)\\s+-->\\s+(.*)$", snippet.getMethod().getName());

                // ps = String.format("^%s(.*)\\s+-->\\s+(.*)$",
                // snippet.getMethod()
                // .getName());
                ps = String.format("^(%s\\.)?%s(.*)\\s+-->\\s+(.*)$",
                        snippet.getContainer().getJavaClass().getName(), snippet.getMethod().getName());
                Pattern p = Pattern.compile(ps);

                // parse inputs
                int i = -1;
                for (String line : inputLines) {
                    i++;

                    if (i == 0) {
                        // first line
                        continue;
                    } else if (StringUtils.isEmpty(line)) {
                        continue;
                    }

                    Matcher m = p.matcher(line);

                    if (m.matches()) {
                        String paramsString = StringUtils.substring(m.group(2).trim(), 1, -1);
                        String resultString = m.group(3).trim();

                        paramsString = StringUtils.replace(paramsString, "(don't care)", "");

                        String[] paramsStrings = StringUtils.split(paramsString, ',');

                        InputElement input = new InputElement();

                        // if index error -> lesser inputs than parameters
                        for (int j = 0; j < parameterTypes.length; j++) {
                            if (parameterTypes[j] == ParameterType.BOOLEAN
                                    && paramsStrings[j].contains("-2147483648")) {
                                // don't care -> 0
                                paramsStrings[j] = "false";
                            }

                            ParameterElement pe = new ParameterElement(parameterTypes[j],
                                    paramsStrings[j].trim());

                            try {
                                // just check the type format
                                pe.validate();
                            } catch (Exception e) {
                                // TODO error handling
                                System.out.println(parameterTypes[j]);
                                System.out.println(paramsStrings[j]);
                                System.out.println(pe.getType());
                                System.out.println(pe.getValue());
                                e.printStackTrace();

                                System.err.println("=============================");
                                System.err.println(snippet.getMethod());
                                System.err.println("=============================");
                                for (String lll : inputLines) {
                                    System.err.println(lll);
                                }
                                System.err.println("=============================");

                                System.exit(-1);
                            }

                            input.getParameters().add(pe);
                        }

                        if (resultString.startsWith("Return Value:")) {
                            // has retval, nothing to do
                        } else {
                            // exception; example (" is present inside the
                            // string!!!):
                            // "java.lang.ArithmeticException: div by 0..."
                            // "java.lang.IndexOutOfBoundsException: Index: 1, Size: 5..."

                            int pos = resultString.indexOf(':');
                            if (pos < 0) {
                                // not found :, search for ...
                                pos = resultString.indexOf("...");
                            }

                            String ex = resultString.substring(1, pos);
                            input.setExpected(ex);

                            // System.err.println(resultString);
                            // System.err.println(ex);
                            // // input.setExpected(expected);
                        }

                        inputsXml.getGeneratedInputs().add(input);
                    } else {
                        System.err.println("NO MATCH");
                        System.err.println(ps);
                        System.err.println(line);
                        throw new Exception("NO MATCH: " + line);
                    }
                }
            }
        }
        inputsXml.validate();
    }
}

From source file:com.uksf.mf.core.utility.sqm.SqmFixer.java

/**
 * Checks sqm validity then fixes//from   w w w. j  ava2 s.co  m
 */
private void processSqm() {
    try {
        List<String> rawSqm = FileUtils.readLines(activeFile);
        COUNT++;
        if (SqmChecker.checkSQM(activeFile, rawSqm)) {
            LogHandler.logSeverity(INFO, "Sqm file is valid, fixing");
            List<String> sqmIn = FileUtils.readLines(activeFile, "utf-8");
            List<String> sqmOut = fixSqm(sqmIn);
            if (sqmOut != null && sqmOut.size() > 0) {
                LogHandler.logSeverity(INFO, "Sqm file fixed, saving");
                saveSqm(sqmOut);
                LogHandler.logSeverity(INFO, "Sqm file saved to '" + activeFile.getAbsolutePath() + "'");
                FIXEDCOUNT++;
            }
        }
    } catch (IOException e) {
        Core.nonFatalError(e);
    }
}

From source file:com.tascape.qa.th.AbstractTestRunner.java

protected void generateHtml(Path logFile) {
    Pattern http = Pattern.compile("((http|https)://\\S+)");

    Path html = logFile.getParent().resolve("log.html");
    LOG.trace("creating file {}", html);
    try (PrintWriter pw = new PrintWriter(html.toFile())) {
        pw.println("<html><body><pre>");
        pw.println("<a href='../'>Suite Log Directory</a><br /><a href='./'>Test Log Directory</a>");
        pw.println();/*  ww w .  java  2 s  . c om*/
        pw.println(logFile);
        pw.println();
        List<String> lines = FileUtils.readLines(logFile.toFile());
        List<File> files = new ArrayList<>(Arrays.asList(logFile.getParent().toFile().listFiles()));

        for (String line : lines) {
            String newline = line.replaceAll(">", "&gt;");
            newline = newline.replaceAll("<", "&lt;");
            if (newline.contains(" WARN  ")) {
                newline = "<b>" + newline + "</b> ";
            } else if (newline.contains(" ERROR ") || newline.contains("Failure in test")
                    || newline.contains("AssertionError")) {
                newline = "<font color='red'><b>" + newline + "</b></font> ";
            } else {
                Matcher m = http.matcher(line);
                if (m.find()) {
                    String url = m.group(1);
                    String a = String.format("<a href='%s'>%s</a>", url, url);
                    newline = newline.replace(url, a);
                }
            }
            pw.println(newline);
            for (File file : files) {
                String path = file.getAbsolutePath();
                String name = file.getName();
                if (newline.contains(path)) {
                    if (name.endsWith(".png")) {
                        pw.printf("<a href=\"%s\" target=\"_blank\"><img src=\"%s\" width=\"360px\"/></a>",
                                name, name);
                    }
                    String a = String.format("<a href=\"%s\" target=\"_blank\">%s</a>", name, name);
                    int len = newline.indexOf("    ");
                    pw.printf((len > 0 ? newline.substring(0, len + 5) : "") + a);
                    pw.println();
                    files.remove(file);
                    break;
                }
            }
        }

        pw.println("</pre></body></html>");
        logFile.toFile().delete();
    } catch (IOException ex) {
        LOG.warn(ex.getMessage());
    }
}

From source file:br.com.involves.converter.ConverterTest.java

@Test
public void testConverterToJsonNull() throws IllegalArgumentException, IllegalAccessException, IOException {
    JsonConverter converter = new JsonConverter();
    File expected = new File("src/test/resources/personOutputNullExpected.json");
    File output = folder.newFile("personOutputNullOutput.json");

    Person person = new Person(null, 0, ' ', null, null);

    converter.convert(person, output);//from   w  w w  . ja v a 2  s.co  m
    Assert.assertTrue(output.exists());
    Assert.assertEquals(FileUtils.readLines(expected), FileUtils.readLines(output));
}

From source file:com.strandls.alchemy.rest.client.stubgenerator.RestProxyGeneratorTest.java

/**
 * Run the task./*  w ww  . j av  a 2 s . co  m*/
 *
 * @param outputDir
 *            the output directory.
 * @param destinationPackage
 *            the destination package name.
 * @throws IOException
 */
private void runProxyGenerateTask(final File outputDir, final String destinationPackage) throws IOException {
    final BuildLogger logger = new DefaultLogger();
    logger.setMessageOutputLevel(Project.MSG_INFO);
    logger.setOutputPrintStream(System.out);
    logger.setErrorPrintStream(System.out);
    logger.setEmacsMode(true);

    final ProjectHelper ph = ProjectHelper.getProjectHelper();
    final Project p = new Project();
    p.addBuildListener(logger);
    p.init();
    p.addReference("ant.projectHelper", ph);

    final URL[] urls = ((URLClassLoader) (Thread.currentThread().getContextClassLoader())).getURLs();
    final StringBuffer classPath = new StringBuffer();
    for (final URL url : urls) {
        classPath.append(new File(url.getPath()));
        classPath.append(System.getProperty("path.separator"));
    }
    classPath.append("src/test/resources/test-webservices.jar");

    // set properties
    p.setUserProperty("client.src.dir", outputDir.getAbsolutePath());
    p.setUserProperty("task.classpath", classPath.toString());

    if (destinationPackage != null) {
        p.setUserProperty("destinationPackage", destinationPackage);
    }

    ph.parse(p, new File("src/test/resources/restProxyGenBuild.xml"));
    p.executeTarget("restProxyGen");

    // ensure all files are generated
    final Set<String> expectedFiles = new HashSet<>(
            FileUtils.readLines(new File("src/test/resources/test-weservices-files.txt")));
    final File packageDir = new File(outputDir, destinationPackage.replaceAll("\\.", File.separator));
    final Set<String> actualFiles = new HashSet<String>();
    for (final File file : packageDir.listFiles()) {
        if (file.isFile()) {
            actualFiles.add(file.getName());
        }
    }

    assertEquals(expectedFiles, actualFiles);
}

From source file:com.martinwunderlich.nlp.io.LexisNexisParser.java

public List<LexisNexisDocument> parse(File parseFile) {
    List<LexisNexisDocument> resultList = new ArrayList<>();
    List<String> lines = null;
    try {/*from   w w w.j  a va  2 s . c  om*/
        lines = FileUtils.readLines(parseFile);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

    LexisNexisDocument currentDoc = null;

    boolean inBody = false;
    boolean afterBody = false;
    boolean inHeader = false;
    boolean afterHeadline = false;
    boolean inCopyright = false;

    StringBuilder bodyBuilder = null;
    StringBuilder copyRightBuilder = null;

    int emptyHeaderLines = 0; // keeps track of consecutive empty lines in header: two or more of these after the headline mean that the body texts starts (argh).

    for (String line : lines) {
        if (line.isEmpty() && !inBody) {
            emptyHeaderLines++;
            continue; // skip empty lines outside of body
        }

        if (isDocumentStart(line)) {
            if (currentDoc != null) {
                currentDoc.setText(bodyBuilder.toString());
                currentDoc.setCopyright(copyRightBuilder.toString());
                resultList.add(currentDoc);
            }

            // Init
            bodyBuilder = new StringBuilder();
            copyRightBuilder = new StringBuilder();
            currentDoc = new LexisNexisDocument();
            inHeader = true;
            afterHeadline = false;
            inBody = false;
            inCopyright = false;
            emptyHeaderLines = 0;

            continue;
        }

        if (inCopyright || isCopyrightStart(line)) {
            copyRightBuilder.append(line.trim());
            if (!inCopyright)
                inCopyright = true;
            continue;
        }

        String type = getMetaLineType(line);
        if (type != null) {
            addMetaLine(line, type, currentDoc);
            emptyHeaderLines = 0;
            if (inBody) {
                inBody = false;
                afterBody = true;
            }
        } else {
            if (inBody) {
                if (line.isEmpty())
                    bodyBuilder.append("\n");
                else
                    bodyBuilder.append(line.trim().replaceAll("\n", " "));
            } else if (inHeader && !afterHeadline) { // in header and line is not meta? this must be the headline
                currentDoc.setHeadline(line.trim());
                afterHeadline = true;
                emptyHeaderLines = 0;
            } else if (emptyHeaderLines >= 2 && !afterBody) { // first line of body text found
                inBody = true;
                inHeader = false;
                bodyBuilder.append(line.trim().replaceAll("\n", " "));
            }
        }
    }

    // Store final doc in list
    if (currentDoc != null) {
        currentDoc.setText(bodyBuilder.toString());
        resultList.add(currentDoc);
    }

    return resultList;
}