Example usage for org.apache.commons.io LineIterator hasNext

List of usage examples for org.apache.commons.io LineIterator hasNext

Introduction

In this page you can find the example usage for org.apache.commons.io LineIterator hasNext.

Prototype

public boolean hasNext() 

Source Link

Document

Indicates whether the Reader has more lines.

Usage

From source file:org.sonar.plugins.codesize.LineCounter.java

private int countFile(File file) {

    int lines = 0;
    LineIterator lineIterator = null;

    try {/*from  w ww .ja v  a2 s  . c o m*/
        lineIterator = FileUtils.lineIterator(file, defaultCharset.name());

        for (; lineIterator.hasNext(); lineIterator.next()) {
            lines++;
        }
    } catch (IOException e) {
        LOG.error("Could not open file", e);
    } finally {
        LineIterator.closeQuietly(lineIterator);
    }
    LOG.debug(file.getName() + ": " + lines);
    return lines;
}

From source file:org.sonar.plugins.xaml.XamlLineCouterSensor.java

private void addMeasures(SensorContext sensorContext, File file, org.sonar.api.resources.File xmlFile) {

    LineIterator iterator = null;
    int numLines = 0;
    int numEmptyLines = 0;

    try {//from w  w w .ja  va2 s .c  om
        iterator = FileUtils.lineIterator(file);

        while (iterator.hasNext()) {
            String line = iterator.nextLine();
            numLines++;
            if (StringUtils.isEmpty(line)) {
                numEmptyLines++;
            }
        }
    } catch (IOException e) {
        LOG.warn(e.getMessage());
    } finally {
        LineIterator.closeQuietly(iterator);
    }

    try {

        Log.debug("Count comment in " + file.getPath());

        int numCommentLines = new XamlLineCountParser().countLinesOfComment(FileUtils.openInputStream(file));
        sensorContext.saveMeasure(xmlFile, CoreMetrics.LINES, (double) numLines);
        sensorContext.saveMeasure(xmlFile, CoreMetrics.COMMENT_LINES, (double) numCommentLines);
        sensorContext.saveMeasure(xmlFile, CoreMetrics.NCLOC,
                (double) numLines - numEmptyLines - numCommentLines);
    } catch (Exception e) {
        LOG.debug("Fail to count lines in " + file.getPath(), e);
    }

    LOG.debug("LineCountSensor: " + xmlFile.getKey() + ":" + numLines + "," + numEmptyLines + "," + 0);
}

From source file:org.sonar.plugins.xml.LineCountSensor.java

private void addMeasures(SensorContext sensorContext, File file, org.sonar.api.resources.File xmlFile) {

    LineIterator iterator = null;
    int numLines = 0;
    int numEmptyLines = 0;

    try {//from w  w w .  j  av a2 s.c  o  m
        iterator = FileUtils.lineIterator(file);

        while (iterator.hasNext()) {
            String line = iterator.nextLine();
            numLines++;
            if (StringUtils.isEmpty(line)) {
                numEmptyLines++;
            }
        }
    } catch (IOException e) {
        LOG.warn(e.getMessage());
    } finally {
        LineIterator.closeQuietly(iterator);
    }

    try {

        Log.debug("Count comment in " + file.getPath());

        int numCommentLines = new LineCountParser().countLinesOfComment(FileUtils.openInputStream(file));
        sensorContext.saveMeasure(xmlFile, CoreMetrics.LINES, (double) numLines);
        sensorContext.saveMeasure(xmlFile, CoreMetrics.COMMENT_LINES, (double) numCommentLines);
        sensorContext.saveMeasure(xmlFile, CoreMetrics.NCLOC,
                (double) numLines - numEmptyLines - numCommentLines);
    } catch (Exception e) {
        LOG.debug("Fail to count lines in " + file.getPath(), e);
    }

    LOG.debug("LineCountSensor: " + xmlFile.getKey() + ":" + numLines + "," + numEmptyLines + "," + 0);
}

From source file:org.tanaguru.referentiel.creator.CodeGeneratorMojo.java

/**
 *
 * @return/*ww  w  .jav  a 2s. c om*/
 */
private Iterable<CSVRecord> getCsv() {
    // we parse the csv file to extract the first line and get the headers 
    LineIterator lineIterator;
    try {
        lineIterator = FileUtils.lineIterator(dataFile, Charset.defaultCharset().name());
    } catch (IOException ex) {
        Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex);
        lineIterator = null;
    }
    String[] csvHeaders = lineIterator.next().split(String.valueOf(delimiter));
    isCriterionPresent = extractCriterionFromCsvHeader(csvHeaders);
    try {
        extractAvailableLangsFromCsvHeader(csvHeaders);
    } catch (I18NLanguageNotFoundException ex) {
        Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }

    // from here we just add each line to a build to re-create the csv content
    // without the first line.
    StringBuilder strb = new StringBuilder();
    while (lineIterator.hasNext()) {
        strb.append(lineIterator.next());
        strb.append("\n");
    }
    Reader in;
    try {
        in = new StringReader(strb.toString());
        CSVFormat csvf = CSVFormat.newFormat(delimiter).withHeader(csvHeaders);
        return csvf.parse(in);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    } catch (IOException ex) {
        Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}

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

private Iterable<CSVRecord> getCsv(ResourceBundle resourceBundle) {
    // we parse the csv file to extract the first line and get the headers 
    LineIterator lineIterator;
    try {/* w ww . j a  v  a 2s .  c  om*/
        lineIterator = FileUtils.lineIterator(FileUtils.getFile(resourceBundle.getString("export.csvPath")));
    } catch (IOException ex) {
        Logger.getLogger(CopyFiles.class.getName()).log(Level.SEVERE, null, ex);
        lineIterator = null;
    }
    String[] csvHeaders = lineIterator.next().split(String.valueOf(delimiter));

    // from here we just add each line to a build to re-create the csv content
    // without the first line.
    StringBuilder strb = new StringBuilder();
    while (lineIterator.hasNext()) {
        strb.append(lineIterator.next());
        strb.append("\n");
    }
    Reader in;
    try {
        in = new StringReader(strb.toString());
        CSVFormat csvf = CSVFormat.newFormat(delimiter).withHeader(csvHeaders);
        return csvf.parse(in);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(CopyFiles.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    } catch (IOException ex) {
        Logger.getLogger(CopyFiles.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}

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()/*from   w w  w. ja  v  a2  s .c  o  m*/
                .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 a  v a  2 s.c om
                .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");
        }/*  www .  ja  v  a 2 s  .co m*/
        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:org.trend.hgraph.mapreduce.lib.input.CalculateInputSplitMapperTest.java

@Test
public void testRun_b4() throws IOException, Exception {
    // init test table
    String tableName = "test.vertex-02";
    String outputPath = "/run_b4";
    createTestTable(tableName, "00030", "00060");

    // start test
    Configuration conf = TEST_UTIL.getConfiguration();
    Tool driver = new CalculateInputSplitMapper(conf);
    int code = driver.run(new String[] { "-b", "4", tableName, outputPath });
    Assert.assertEquals(0, code);/*w ww.  ja  v  a  2s. c  o m*/

    // get test results
    Path path = new Path(outputPath);
    FileSystem fs = path.getFileSystem(conf);

    // FileStatus[] files = fs.listStatus(path);
    // for (int a = 0; a < files.length; a++) {
    // System.out.println(files[a].getPath());
    // }
    InputStream is = fs.open(new Path(path, "part-r-00000"));
    LineIterator it = IOUtils.lineIterator(is, "UTF-8");
    System.out.println("print out test results");
    while (it.hasNext()) {
        System.out.println(it.next());
    }
    LineIterator.closeQuietly(it);
    IOUtils.closeQuietly(is);
}

From source file:org.trend.hgraph.mapreduce.lib.input.TableInputFormat.java

@Override
public List<InputSplit> getSplits(JobContext context) throws IOException {
    LinkedList<InputSplit> newSplits = new LinkedList<InputSplit>();
    FileSystem fs = null;// ww w. j  a  va2s .  c  o  m
    InputStream is = null;
    LineIterator it = null;
    try {
        List<InputSplit> targets = super.getSplits(context);
        Map<String, List<TableSplit>> targetMap = createTargetMap(targets);

        HTable table = getHTable();
        byte[] tableName = table.getTableName();

        Path path = new Path(inputPath);
        fs = path.getFileSystem(this.getConf());
        is = fs.open(path);
        it = IOUtils.lineIterator(is, "UTF-8");
        String line = null;
        String[] lineSplits = null;
        String regionName = null;
        byte[] startKey = null;
        byte[] endKey = null;
        byte[] regionStartKey = null;
        byte[] regionEndKey = null;
        boolean nextRegion = false;

        HRegionLocation regionlocation = null;
        String location = null;
        HRegionInfo regionInfo = null;
        List<TableSplit> splits = null;
        TableSplit split = null;
        while (it.hasNext()) {
            if (!nextRegion) {
                line = it.nextLine();
            } else {
                nextRegion = false;
            }
            if (null == line || "".equals(line)) {
                String msg = "skip a invalid line";
                LOGGER.error(msg);
                throw new IllegalStateException(msg);
            }

            lineSplits = getLineSplits(line);
            regionName = lineSplits[0];
            startKey = Bytes.toBytes(lineSplits[1]);
            endKey = Bytes.toBytes(lineSplits[2]);
            LOGGER.info("start to process region:" + regionName);
            regionlocation = table.getRegionLocation(startKey, false);
            if (null == regionlocation) {
                String msg = "can not find location for region:" + regionName + " !!";
                LOGGER.error(msg);
                throw new IllegalStateException(msg);
            }

            regionInfo = regionlocation.getRegionInfo();
            location = regionlocation.getHostnamePort().split(Addressing.HOSTNAME_PORT_SEPARATOR)[0];
            if (!targetMap.containsKey(location)) {
                String msg = "regionLocation:" + regionlocation + " not found in TableSplits !!";
                LOGGER.error(msg);
                throw new IllegalStateException(msg);
            }

            regionStartKey = regionInfo.getStartKey();
            regionEndKey = regionInfo.getEndKey();

            splits = targetMap.get(location);
            LOGGER.info("splits.size is " + splits.size());
            for (int a = 0; a < splits.size(); a++) {
                split = splits.get(a);
                if (Arrays.equals(split.getStartRow(), regionStartKey)
                        && Arrays.equals(split.getEndRow(), regionEndKey)) {
                    splits.remove(a);

                    if (Arrays.equals(HConstants.EMPTY_BYTE_ARRAY, regionStartKey)) {
                        startKey = regionStartKey;
                    }

                    split = new TableSplit(tableName, startKey, endKey, location);
                    newSplits.add(split);

                    if (it.hasNext()) {
                        while (it.hasNext()) {
                            line = it.nextLine();
                            lineSplits = getLineSplits(line);
                            // keep doing due to it may be in same region
                            if (regionName.equals(lineSplits[0])) {
                                split = new TableSplit(tableName, Bytes.toBytes(lineSplits[1]),
                                        Bytes.toBytes(lineSplits[2]), location);
                                newSplits.add(split);
                            } else {
                                checkAndPatchRegionEndKey(newSplits, regionEndKey);

                                nextRegion = true;
                                break;
                            }
                        }
                    } else {
                        checkAndPatchRegionEndKey(newSplits, regionEndKey);
                    }
                    if (splits.size() == 0) {
                        // remove itself if it is empty
                        targetMap.remove(location);
                    }
                    break;
                }
            }
        }

        // check if still any split not processed yet ?
        if (targetMap.size() > 0) {
            String msg = "targetMap.size:" + targetMap.size() + " still bigger than 0,"
                    + " which means that there still has TableSplit(s) not processed yet !!";
            LOGGER.error(msg);
            LOGGER.error("" + targetMap);
            throw new IllegalStateException(msg);
        }
    } catch (IOException e) {
        LOGGER.error("unknow error occues !!", e);
        throw e;
    } finally {
        if (null != it) {
            LineIterator.closeQuietly(it);
        }
        if (null != is) {
            IOUtils.closeQuietly(is);
        }
    }

    return newSplits;
}