Search in sources :

Example 26 with FilePredicates

use of org.sonar.api.batch.fs.FilePredicates in project sonar-java by SonarSource.

the class PmdXmlReportReader method onXmlEvent.

private void onXmlEvent(XMLEvent event) {
    if (event.isStartElement()) {
        StartElement element = event.asStartElement();
        String elementName = element.getName().getLocalPart();
        if ("file".equals(elementName)) {
            String filePath = getAttributeValue(element, "name");
            FilePredicates predicates = context.fileSystem().predicates();
            inputFile = context.fileSystem().inputFile(predicates.hasPath(filePath));
            if (inputFile == null) {
                LOG.warn("No input file found for {}. No PMD issue will be imported on this file.", filePath);
            }
        } else if ("violation".equals(elementName) && inputFile != null) {
            onViolationStartElement(element);
        }
    } else if (event.isCharacters()) {
        issueMessage.append(event.asCharacters().getData());
    } else if (event.isEndElement() && "violation".equals(event.asEndElement().getName().getLocalPart()) && inputFile != null && issue != null) {
        issueLocation.message(issueMessage.toString());
        issue.at(issueLocation).save();
    }
}
Also used : StartElement(javax.xml.stream.events.StartElement) FilePredicates(org.sonar.api.batch.fs.FilePredicates)

Example 27 with FilePredicates

use of org.sonar.api.batch.fs.FilePredicates in project sonar-java by SonarSource.

the class SpotBugsXmlReportReader method findInputFile.

private static InputFile findInputFile(SensorContext context, List<String> sourceDirs, String relativeLinuxPath) {
    FilePredicates predicates = context.fileSystem().predicates();
    InputFile inputFile = null;
    for (String sourceDir : sourceDirs) {
        File sourceFile = new File(sourceDir, relativeLinuxPath);
        inputFile = context.fileSystem().inputFile(predicates.hasPath(sourceFile.toString()));
        if (inputFile != null) {
            break;
        }
    }
    return inputFile;
}
Also used : FilePredicates(org.sonar.api.batch.fs.FilePredicates) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) InputFile(org.sonar.api.batch.fs.InputFile)

Example 28 with FilePredicates

use of org.sonar.api.batch.fs.FilePredicates in project sonar-java by SonarSource.

the class CheckstyleXmlReportReader method onFileElement.

private void onFileElement(StartElement element) {
    String filePath = getAttributeValue(element, NAME);
    if (filePath.isEmpty()) {
        inputFile = null;
        return;
    }
    FilePredicates predicates = context.fileSystem().predicates();
    inputFile = context.fileSystem().inputFile(predicates.hasPath(filePath));
    if (inputFile == null) {
        LOG.warn("No input file found for '{}'. No checkstyle issues will be imported on this file.", filePath);
    }
}
Also used : FilePredicates(org.sonar.api.batch.fs.FilePredicates)

Example 29 with FilePredicates

use of org.sonar.api.batch.fs.FilePredicates in project sonar-checkstyle by checkstyle.

the class CheckstyleAuditListenerTest method before.

@Before
public void before() {
    fileSystem = mock(FileSystem.class);
    context = mock(SensorContext.class);
    inputFile = mock(InputFile.class);
    ruleFinder = mock(ActiveRules.class);
    final FilePredicates predicates = mock(FilePredicates.class);
    final FilePredicate filePredicate = mock(FilePredicate.class);
    when(fileSystem.inputFile(any(FilePredicate.class))).thenReturn(inputFile);
    when(fileSystem.predicates()).thenReturn(predicates);
    when(predicates.hasAbsolutePath(anyString())).thenReturn(filePredicate);
}
Also used : SensorContext(org.sonar.api.batch.sensor.SensorContext) FilePredicate(org.sonar.api.batch.fs.FilePredicate) FileSystem(org.sonar.api.batch.fs.FileSystem) FilePredicates(org.sonar.api.batch.fs.FilePredicates) ActiveRules(org.sonar.api.batch.rule.ActiveRules) InputFile(org.sonar.api.batch.fs.InputFile) Before(org.junit.Before)

Example 30 with FilePredicates

use of org.sonar.api.batch.fs.FilePredicates in project sonar-checkstyle by checkstyle.

the class CheckstyleConfiguration method getSourceFiles.

public List<InputFile> getSourceFiles() {
    final FilePredicates predicates = fileSystem.predicates();
    final Iterable<InputFile> files = fileSystem.inputFiles(predicates.and(predicates.hasLanguage(CheckstyleConstants.JAVA_KEY), predicates.hasType(InputFile.Type.MAIN)));
    final List<InputFile> fileList = new ArrayList<>();
    for (InputFile file : files) {
        fileList.add(file);
    }
    return fileList;
}
Also used : FilePredicates(org.sonar.api.batch.fs.FilePredicates) ArrayList(java.util.ArrayList) InputFile(org.sonar.api.batch.fs.InputFile)

Aggregations

FilePredicates (org.sonar.api.batch.fs.FilePredicates)43 InputFile (org.sonar.api.batch.fs.InputFile)38 FileSystem (org.sonar.api.batch.fs.FileSystem)22 File (java.io.File)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 Path (java.nio.file.Path)4 FilePredicate (org.sonar.api.batch.fs.FilePredicate)4 SensorContext (org.sonar.api.batch.sensor.SensorContext)4 RuleKey (org.sonar.api.rule.RuleKey)4 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)3 NewIssue (org.sonar.api.batch.sensor.issue.NewIssue)3 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 StreamSupport (java.util.stream.StreamSupport)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 ActiveRule (org.sonar.api.batch.rule.ActiveRule)2