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

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

Introduction

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

Prototype

public static LineIterator lineIterator(File file) throws IOException 

Source Link

Document

Returns an Iterator for the lines in a File using the default encoding for the VM.

Usage

From source file:org.tanaguru.rules.doc.utils.updateAw22toRgaa30.CopyFiles.java

public void copyRuleClassWithValidState(String originalTest, String targetTest, ResourceBundle rb)
        throws IOException {
    String testCode = originalTest.split("-")[1];
    String newTestCode = targetTest.split("-")[1];
    String[] classCode = normalize2DigitsOld(testCode);
    String[] classCode0 = normalize2Digits(newTestCode);
    File classFile = FileUtils.getFile(rb.getString("export.pathUntilClassCode") + "/src/main/java/"
            + rb.getString("export.groupId").replace(".", "/") + "/tanaguru/rules/"
            + rb.getString("export.originalReferentialPackage") + "/"
            + rb.getString("export.originalReferential") + "Rule" + classCode[0] + classCode[1] + classCode[2]
            + ".java");
    LineIterator oldRefFile = FileUtils.lineIterator(classFile);
    StringBuilder strb = new StringBuilder();
    while (oldRefFile.hasNext()) {
        strb.append(oldRefFile.next()//w w  w  .  j  a v a 2  s. c  om
                .replace(rb.getString("export.originalReferential"), rb.getString("export.targetReferential"))
                .replace(rb.getString("export.originalReferentialPackage"),
                        rb.getString("export.targetReferentialPackage"))
                .replace("2013", "2014")
                .replace(rb.getString("export.originalLink"), rb.getString("export.targetLink"))
                .replace(rb.getString("export.originalReferentialName"),
                        rb.getString("export.targetReferentialName"))
                .replace(classCode[0] + classCode[1] + classCode[2],
                        classCode0[0] + classCode0[1] + classCode0[2]));
        strb.append("\n");
    }
    FileUtils.writeStringToFile(new File(rb.getString("export.pathUntilNewCode")
            + "/src/main/java/org/opens/tanaguru/rules/" + rb.getString("export.targetReferentialPackage") + "/"
            + rb.getString("export.targetReferential") + "Rule" + classCode0[0] + classCode0[1] + classCode0[2]
            + ".java"), strb.toString());
}

From source file:org.tanaguru.rules.doc.utils.updateAw22toRgaa30.CopyFiles.java

public void copyRuleUnitTestWithValidState(String originalTest, String targetTest, ResourceBundle rb,
        boolean notTested) throws IOException {
    String testCode = originalTest.split("-")[1];
    String newTestCode = targetTest.split("-")[1];
    String[] classCode = normalize2DigitsOld(testCode);
    String[] classCodeFull = normalize2Digits(testCode);
    String oldTestCaseCode = getOldTestCaseCode(classCodeFull);
    String[] classCode0 = normalize2Digits(newTestCode);
    File classFile = FileUtils.getFile(rb.getString("export.pathUntilClassCode") + "/src/test/java/"
            + rb.getString("export.groupId").replace(".", "/") + "/tanaguru/rules/"
            + rb.getString("export.originalReferentialPackage") + "/"
            + rb.getString("export.originalReferential") + "Rule" + classCode[0] + classCode[1] + classCode[2]
            + "Test.java");
    Pattern p = Pattern.compile("^.*([0-9]{5})");
    Pattern p2 = Pattern.compile("^.*([0-9]{6})");
    LineIterator oldRefFile = FileUtils.lineIterator(classFile);
    StringBuilder strb = new StringBuilder();
    System.out.println(testCode);
    while (oldRefFile.hasNext()) {
        String line = oldRefFile.next();
        line = line//from   w  w  w .  j av  a 2s  . c o  m
                .replace(rb.getString("export.originalReferential"), rb.getString("export.targetReferential"))
                .replace(rb.getString("export.originalReferential").toUpperCase(),
                        rb.getString("export.targetReferential"))
                .replace(rb.getString("export.originalReferentialPackage"),
                        rb.getString("export.targetReferentialPackage"))
                .replace("2013", "2014")
                .replace(rb.getString("export.originalLink"), rb.getString("export.targetLink"))
                .replace(rb.getString("export.originalReferentialName"),
                        rb.getString("export.targetReferentialName"))
                .replace(classCode[0] + classCode[1] + classCode[2],
                        classCode0[0] + classCode0[1] + classCode0[2])
                .replace(oldTestCaseCode, classCode0[0] + "." + classCode0[1] + "." + classCode0[2])
                .replace(testCode, classCode0[0] + "." + classCode0[1] + "." + classCode0[2]);
        Matcher m = p.matcher(line);
        if (m.find()) {
            Matcher m2 = p2.matcher(line);
            if (!m2.find()) {
                line = line.replace(m.group(1), getAllDigitsTestCode(m.group(1)));
            }
        }
        strb.append(line);
        strb.append("\n");
    }
    String result = strb.toString();
    if (notTested == true) {
        if (strb.toString().contains("PASSED")) {
            result = strb.toString().replace("PASSED", "NOT_TESTED");
        } else if (strb.toString().contains("FAILED")) {
            result = strb.toString().replace("FAILED", "NOT_TESTED");
        } else if (strb.toString().contains("NOT_APPLICABLE")) {
            result = strb.toString().replace("NOT_APPLICABLE", "NOT_TESTED");
        }
    }
    FileUtils.writeStringToFile(new File(rb.getString("export.pathUntilNewCode")
            + "/src/test/java/org/opens/tanaguru/rules/" + rb.getString("export.targetReferentialPackage") + "/"
            + rb.getString("export.targetReferential") + "Rule" + classCode0[0] + classCode0[1] + classCode0[2]
            + "Test.java"), result);
}

From source file:org.tanaguru.rules.doc.utils.updateAw22toRgaa30.CopyFiles.java

public void copyTestCasesWithValidState(String originalTest, String targetTest, ResourceBundle rb)
        throws IOException {
    String testCode = originalTest.split("-")[1];
    String newTestCode = targetTest.split("-")[1];
    String[] classCode = normalize2DigitsOld(testCode);
    String[] classCode0 = normalize2Digits(newTestCode);
    File directory = FileUtils.getFile(rb.getString("export.pathUntilClassCode")
            + "/src/test/resources/testcases/" + rb.getString("export.originalReferentialPackage") + "/"
            + rb.getString("export.originalReferential") + "Rule" + classCode[0] + classCode[1] + classCode[2]);
    String[] extensions = { "html", "json", "css" };
    Collection<File> files = FileUtils.listFiles(directory, extensions, true);
    for (File testCases : files) {
        String[] state = testCases.getName().split("-");
        LineIterator oldRefFile = FileUtils.lineIterator(testCases);
        StringBuilder strb = new StringBuilder();
        while (oldRefFile.hasNext()) {
            strb.append(oldRefFile.next()
                    .replace(rb.getString("export.originalReferential"),
                            rb.getString("export.targetReferential"))
                    .replace(rb.getString("export.originalReferential").toUpperCase(),
                            rb.getString("export.targetReferential"))
                    .replace(rb.getString("export.originalReferentialPackage"),
                            rb.getString("export.targetReferentialPackage"))
                    .replace("2013", "2014")
                    .replace(rb.getString("export.originalLink"), rb.getString("export.targetLink"))
                    .replace(rb.getString("export.originalReferentialName"),
                            rb.getString("export.targetReferentialName"))
                    .replace(classCode[0] + classCode[1] + classCode[2],
                            classCode0[0] + classCode0[1] + classCode0[2])
                    .replace(testCode, classCode0[0] + "." + classCode0[1] + "." + classCode0[2]));
            strb.append("\n");
        }/*from  w  ww .  j a v a2 s.com*/
        if (testCases.getName().endsWith(".css")) {
            FileUtils
                    .writeStringToFile(
                            new File(rb.getString("export.pathUntilNewCode") + "/src/test/resources/testcases/"
                                    + rb.getString("export.targetReferentialPackage") + "/"
                                    + rb.getString("export.targetReferential") + "Rule" + classCode0[0]
                                    + classCode0[1] + classCode0[2] + "/css/"
                                    + rb.getString("export.targetReferential") + ".Test." + classCode0[0] + "."
                                    + classCode0[1] + "." + classCode0[2] + "-" + state[1] + "-" + state[2]),
                            strb.toString());

        } else {
            FileUtils.writeStringToFile(new File(rb.getString("export.pathUntilNewCode")
                    + "/src/test/resources/testcases/" + rb.getString("export.targetReferentialPackage") + "/"
                    + rb.getString("export.targetReferential") + "Rule" + classCode0[0] + classCode0[1]
                    + classCode0[2] + "/" + rb.getString("export.targetReferential") + ".Test." + classCode0[0]
                    + "." + classCode0[1] + "." + classCode0[2] + "-" + state[1] + "-" + state[2]),
                    strb.toString());
        }
    }
}

From source file:temp.ReadEcoStrata.java

@Test
public void printStrata() throws IOException {
    LineIterator it = FileUtils.lineIterator(new File("F:\\MetteMauritsen\\ECOSTRATA.csv"));
    it.nextLine(); // skip header
    Map<String, List<Coordinate>> coords = new HashMap<>();
    it.nextLine();/*from w ww .  j av  a  2s . c o m*/
    while (it.hasNext()) {
        String line = it.nextLine().trim().replace("\"", "");
        String elms[] = line.split(";");
        List<Coordinate> c = coords.get(elms[0]);
        if (c == null) {
            c = new ArrayList<>();
            coords.put(elms[0], c);
        }
        c.add(new Coordinate(Conversion.safeStringtoDoubleNULL(elms[2]),
                Conversion.safeStringtoDoubleNULL(elms[1])));
    }
    for (String strata : coords.keySet().stream().sorted((String s1, String s2) -> {
        return ImrSort.compareTranslative(s1, s2);
    }).collect(Collectors.toList())) {
        MultiPolygon mp = JTSUtils
                .createMultiPolygon(Arrays.asList(JTSUtils.createLineString(coords.get(strata))));
        System.out.println(strata + "\t" + mp);
    }
}

From source file:temp.ReadKolmuleStrata.java

public static void printStrata() throws IOException {
    LineIterator it = FileUtils
            .lineIterator(new File("\\\\delphi\\felles\\alle\\smund\\stox\\strata_kolmule\\strata.txt"));
    it.nextLine(); // skip header
    Map<String, List<Coordinate>> coords = new HashMap<>();
    while (it.hasNext()) {
        String line = it.nextLine().trim();
        String elms[] = line.split("\t");
        List<Coordinate> c = coords.get(elms[0]);
        if (c == null) {
            c = new ArrayList<>();
            coords.put(elms[0], c);/*ww  w .ja v  a  2  s  .  c  o m*/
        }
        c.add(new Coordinate(Conversion.safeStringtoDoubleNULL(elms[2]),
                Conversion.safeStringtoDoubleNULL(elms[1])));
    }
    for (String strata : coords.keySet()) {
        MultiPolygon mp = JTSUtils
                .createMultiPolygon(Arrays.asList(JTSUtils.createLineString(coords.get(strata))));
        System.out.println(strata + "\t" + mp);
    }
}

From source file:temp.ReadNorskehavstoktStrata.java

public static void printStrata(Integer i) throws IOException {
    LineIterator it = FileUtils.lineIterator(
            new File("\\\\delphi\\felles\\alle\\smund\\stox\\strata_norskehavstoktet\\stratum" + i + ".txt"));
    it.nextLine(); // skip header
    List<Coordinate> coords = new ArrayList<>();
    while (it.hasNext()) {
        String line = it.nextLine().trim();
        String elms[] = line.split("\t");
        coords.add(new Coordinate(Conversion.safeStringtoDoubleNULL(elms[1]),
                Conversion.safeStringtoDoubleNULL(elms[0])));
    }// w  w w.  j  av a  2  s.  c  om
    MultiPolygon mp = JTSUtils.createMultiPolygon(Arrays.asList(JTSUtils.createLineString(coords)));
    System.out.println(i + "\t" + mp);
}

From source file:temp.RectanglePSU.java

public static void main(String[] args) throws IOException {
    System.out.println("test");
    LineIterator it = FileUtils.lineIterator(
            new File("\\\\delphi\\felles\\alle\\smund\\stox\\Beam5.2\\gis\\gisdat\\TMP\\nabmain.txt"));
    // Loop through the lines
    List<LineString> ls = new ArrayList<>();
    //List<Geometry> gl = new ArrayList<>();
    Map<String, List<Coordinate[]>> mpumap = new HashMap<>();
    it.nextLine();//w  w w  .ja  va2  s.c  om
    while (it.hasNext()) {
        String line = it.nextLine().trim();
        String elms[] = line.split("\t");
        String rect = elms[0];
        String mainarea = elms[1].trim();
        if (mainarea.isEmpty() || mainarea.equals("99")) {
            continue;
        }
        Coordinate[] coords = RectangleUtil.getCoordsByRectangleKey(rect);
        //Geometry mp = JTSUtils.createPolygon(JTSUtils.createLineString(coords));
        List<Coordinate[]> mpu = mpumap.get(mainarea);
        if (mpu == null) {
            mpu = new ArrayList<>();
            mpumap.put(mainarea, mpu);
        }
        mpu.add(coords);
    }
    GeometryFactory gf = new GeometryFactory();
    for (Entry<String, List<Coordinate[]>> e : mpumap.entrySet()) {
        List<Coordinate[]> mpu = e.getValue();
        Geometry[] gl = new Geometry[mpu.size()];
        for (int i = 0; i < mpu.size(); i++) {
            gl[i] = JTSUtils.createPolygon(JTSUtils.createLineString(mpu.get(i)));
        }
        GeometryCollection polygonCollection = gf.createGeometryCollection(gl);
        Geometry union = polygonCollection.buffer(0);
        if (union instanceof Polygon) {
            union = new MultiPolygon(new Polygon[] { (Polygon) union }, gf);
        }
        System.out.println(e.getKey() + "\t" + union);
        /*for (int i = 0; i < union.getNumGeometries(); i++) {
         Geometry m = union.getGeometryN(i);
         System.out.println(e.getKey() + " " + m);
         }*/
    }
}

From source file:tpt.dbweb.cat.io.ConllWriter.java

/**
 * Reads a conll file.//  ww w . j a v  a 2s  . c  o m
 * @param file
 * @return map of document ids to a table (list of rows; a row is a list of columns)
 */
public static Map<String, List<List<String>>> readConllDocuments(Path file) {
    Map<String, List<List<String>>> result = new HashMap<>();
    List<List<String>> docList = new ArrayList<>();
    String docid = null;
    LineIterator it;
    try {
        it = FileUtils.lineIterator(file.toFile());
        while (it.hasNext()) {
            String line = it.next();
            if (line.startsWith("#")) {
                if (line.startsWith("#begin document")) {
                    docList = new ArrayList<>();
                    docid = line.substring("#begin document".length() + 1);
                } else if (line.startsWith("#end document")) {
                    if (docid == null) {
                        log.error("doc id is null, cannot read conll file {}", file);
                    } else {
                        result.put(docid, docList);
                    }
                    docid = null;
                }
                continue;
            }
            String[] cols = line.split("\\s+");
            if (cols.length == 0 || (cols.length == 1 && "".equals(cols[0]))) {
                docList.add(Arrays.asList());
            } else {
                docList.add(Arrays.asList(cols));
            }
            // extract column for words
            // align output line by line to these words // TODO: move comment to right place
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:voldemort.utils.app.VoldemortApp.java

protected List<HostNamePair> getHostNamesPairsFromFile(File file) {
    List<HostNamePair> hostNamePairs = new ArrayList<HostNamePair>();

    // This was recently changed from using Properties to performing the
    // file parsing manually. This is due to the fact that we want to
    // maintain the ordering in the file. Line N in the host names file
    // should correspond to node ID N-1. Previously they were in hashed
    // order, so certain tools that auto-configure a cluster based on
    // this same hosts file were creating invalid configurations between
    // the cluster.xml and server.properties (node.id) files.
    LineIterator li = null;//from  w  w w  . j a v a 2 s  .com
    int lineNumber = 0;

    try {
        li = FileUtils.lineIterator(file);

        while (li.hasNext()) {
            String rawLine = String.valueOf(li.next()).trim();
            lineNumber++;

            // Strip comments
            int hashIndex = rawLine.indexOf("#");

            if (hashIndex != -1)
                rawLine = rawLine.substring(0, hashIndex).trim();

            // Whitespace
            if (rawLine.length() == 0)
                continue;

            String[] line = StringUtils.split(rawLine, " \t=:");

            if (line.length < 1 || line.length > 2) {
                System.err.println("Invalid entry (line " + lineNumber + ") in " + file.getAbsolutePath() + ": "
                        + rawLine);
                System.exit(2);
            }

            String externalHostName = line[0];
            String internalHostName = line.length > 1 ? line[1] : externalHostName;
            hostNamePairs.add(new HostNamePair(externalHostName, internalHostName));
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (li != null)
            li.close();
    }

    return hostNamePairs;
}