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

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

Introduction

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

Prototype

public static FileInputStream openInputStream(File file) throws IOException 

Source Link

Document

Opens a FileInputStream for the specified file, providing better error messages than simply calling new FileInputStream(file).

Usage

From source file:org.sonar.plugins.android.lint.AndroidLintRuleParser.java

public List<Rule> parse(File file) {
    BufferedReader reader = null;
    try {//ww  w .ja  v  a  2 s. c  om
        reader = new BufferedReader(new InputStreamReader(FileUtils.openInputStream(file), CharEncoding.UTF_8));
        return parse(reader);

    } catch (IOException e) {
        throw new SonarException("Fail to load the file: " + file, e);

    } finally {
        Closeables.closeQuietly(reader);
    }
}

From source file:org.sonar.plugins.javascript.jslint.JsLintXmlRuleParser.java

public List<JsLintRule> parse(File file) {
    Reader reader = null;//from www  . j a  v a2 s. c  om
    try {
        reader = new InputStreamReader(FileUtils.openInputStream(file), CharEncoding.UTF_8);
        return parse(reader);

    } catch (IOException e) {
        throw new SonarException("Fail to load the file: " + file, e);

    } finally {
        IOUtils.closeQuietly(reader);
    }
}

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;//ww w  .  ja va2  s.  c  o m
    int numLines = 0;
    int numEmptyLines = 0;

    try {
        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.checks.XmlSourceCode.java

InputStream createInputStream() {
    if (file != null) {
        try {//  w w  w.  j  av  a 2s . co m
            return FileUtils.openInputStream(file);
        } catch (IOException e) {
            throw new SonarException(e);
        }
    } else {
        return new ByteArrayInputStream(code.getBytes());
    }
}

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;//  w  w w.  j  a  v  a2 s.co  m
    int numLines = 0;
    int numEmptyLines = 0;

    try {
        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.sonar.plugins.xml.LineCountSensorTest.java

@Test
public void testLineCountParser() throws IOException {
    LineCountParser parser = new LineCountParser();
    int numCommentLines = parser.countLinesOfComment(
            FileUtils.openInputStream(new File("src/test/resources/checks/generic/catalog.xml")));
    assertEquals(1, numCommentLines);//  w w  w  .j  a v a  2s .c  o  m
}

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

@Test
public void failingLineCountParser() throws IOException {
    LineCountParser parser = new LineCountParser();
    String filename = "src/test/resources/checks/generic/header.html";

    int numCommentLines = parser.countLinesOfComment(FileUtils.openInputStream(new File(filename)));
    assertEquals(0, numCommentLines);/*from  w  w w.ja  v a  2 s.c o m*/
}

From source file:org.sonar.plugins.xml.parsers.LineCountParser.java

private void processCommentLines(File file) throws SAXException, IOException {
    SAXParser parser = newSaxParser(false);
    XMLReader xmlReader = parser.getXMLReader();
    commentHandler = new CommentHandler();
    xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", commentHandler);
    parser.parse(FileUtils.openInputStream(file), commentHandler);
}

From source file:org.sonar.plugins.xml.parsers.LineCountParserTest.java

@Test
public void testSimpleLineCountParser() throws IOException {
    LineCountParser parser = new LineCountParser();
    int numCommentLine = parser.countLinesOfComment(
            FileUtils.openInputStream(new File("src/test/resources/parsers/linecount/simple.xml")));

    assertThat(numCommentLine).isEqualTo(1);
}

From source file:org.sonar.plugins.xml.parsers.LineCountParserTest.java

@Test
public void testComplexLineCountParser() throws IOException {
    LineCountParser parser = new LineCountParser();
    int numCommentLine = parser.countLinesOfComment(
            FileUtils.openInputStream(new File("src/test/resources/parsers/linecount/complex.xml")));

    assertThat(numCommentLine).isEqualTo(4);
}