Search in sources :

Example 1 with CheckstyleRecord

use of com.github.checkstyle.data.CheckstyleRecord in project contribution by checkstyle.

the class CheckstyleTextParser method parseDifferenceFile.

/**
 * Compares the contents of the files located at {@code fielPath} at {@code baseReport} and
 * {@code patchReport}.
 *
 * @param diffReport
 *            container for parsed data.
 * @param filePath
 *            path for files.
 * @param baseReport
 *            path for base files.
 * @param patchReport
 *            path for patch files.
 * @throws IOException
 *             if there is a problem accessing a file.
 */
private static void parseDifferenceFile(DiffReport diffReport, String filePath, Path baseReport, Path patchReport) throws IOException {
    final File baseFile = new File(baseReport.toFile(), filePath);
    final File patchFile = new File(patchReport.toFile(), filePath);
    final Statistics statistics = diffReport.getStatistics();
    final Iterator<JgitDifference> iterator = JgitUtils.getDifferences(baseFile, patchFile);
    final List<CheckstyleRecord> records = new ArrayList<>();
    while (iterator.hasNext()) {
        final JgitDifference diff = iterator.next();
        final String xref;
        if (diff.getIndex() == BASE_REPORT_INDEX) {
            xref = baseFile.getPath();
        } else {
            xref = patchFile.getPath();
        }
        final CheckstyleRecord record = new CheckstyleRecord(diff.getIndex(), diff.getLineNo() + 1, 1, DEFAULT_SEVERITY, DEFAULT_SOURCE, diff.getLine(), xref);
        statistics.addSeverityRecord(DEFAULT_SEVERITY, diff.getIndex());
        statistics.addModuleRecord(DEFAULT_SOURCE, diff.getIndex());
        records.add(record);
    }
    diffReport.addRecords(records, filePath);
    statistics.incrementFileCount(BASE_REPORT_INDEX);
    statistics.incrementFileCount(PATCH_REPORT_INDEX);
}
Also used : CheckstyleRecord(com.github.checkstyle.data.CheckstyleRecord) ArrayList(java.util.ArrayList) File(java.io.File) Statistics(com.github.checkstyle.data.Statistics) JgitDifference(com.github.checkstyle.parser.JgitUtils.JgitDifference)

Example 2 with CheckstyleRecord

use of com.github.checkstyle.data.CheckstyleRecord in project contribution by checkstyle.

the class CheckstyleTextParser method parseDifferenceSingle.

/**
 * Adds the next file in the {@code reader} as a difference as there is no matching file in the
 * other side.
 *
 * @param diffReport
 *            container for parsed data.
 * @param reader
 *            reader for file list.
 * @param path
 *            path for files.
 * @param index
 *            internal index of the parsed file.
 */
private static void parseDifferenceSingle(DiffReport diffReport, StringListIterator reader, Path path, int index) {
    final int otherIndex;
    if (index == BASE_REPORT_INDEX) {
        otherIndex = PATCH_REPORT_INDEX;
    } else {
        otherIndex = BASE_REPORT_INDEX;
    }
    final String filePath = reader.next();
    final String xref = new File(path.toFile(), filePath).getPath();
    final Statistics statistics = diffReport.getStatistics();
    final List<CheckstyleRecord> records = new ArrayList<>();
    final CheckstyleRecord record = new CheckstyleRecord(otherIndex, 1, 1, DEFAULT_SEVERITY, DEFAULT_SOURCE, "File not found.", xref);
    statistics.addSeverityRecord(DEFAULT_SEVERITY, otherIndex);
    statistics.addModuleRecord(DEFAULT_SOURCE, otherIndex);
    records.add(record);
    diffReport.addRecords(records, filePath);
    statistics.incrementFileCount(index);
}
Also used : CheckstyleRecord(com.github.checkstyle.data.CheckstyleRecord) ArrayList(java.util.ArrayList) File(java.io.File) Statistics(com.github.checkstyle.data.Statistics)

Example 3 with CheckstyleRecord

use of com.github.checkstyle.data.CheckstyleRecord in project contribution by checkstyle.

the class SiteGenerator method generateBody.

/**
 * Creates main part of resulting site.
 *
 * @param tplEngine
 *        thymeleaf template engine.
 * @param writer
 *        file writer.
 * @param diffReport
 *        difference between two checkstyle reports.
 * @param paths
 *        CLI paths.
 * @param xrefGenerator
 *        xReference generator.
 */
private static void generateBody(TemplateEngine tplEngine, FileWriter writer, DiffReport diffReport, CliPaths paths, XrefGenerator xrefGenerator) {
    final AnchorCounter anchorCounter = new AnchorCounter();
    final Path refFilesPath = paths.getRefFilesPath();
    for (Map.Entry<String, List<CheckstyleRecord>> entry : diffReport.getRecords().entrySet()) {
        final List<CheckstyleRecord> records = entry.getValue();
        String filename = entry.getKey();
        xrefGenerator.reset();
        for (CheckstyleRecord record : records) {
            final String xreference = xrefGenerator.generateXref(record.getXref(), paths.isShortFilePaths());
            record.setXref(xreference);
        }
        if (refFilesPath != null) {
            try {
                filename = refFilesPath.relativize(Paths.get(filename)).toString();
            } catch (IllegalArgumentException ignore) {
            // use original file name
            }
        }
        generateContent(tplEngine, writer, records, shortenFilename(filename), anchorCounter);
    }
}
Also used : Path(java.nio.file.Path) CheckstyleRecord(com.github.checkstyle.data.CheckstyleRecord) List(java.util.List) Map(java.util.Map)

Example 4 with CheckstyleRecord

use of com.github.checkstyle.data.CheckstyleRecord in project contribution by checkstyle.

the class CheckstyleReportsParser method parseErrorTag.

/**
 * Parses "error" XML tag.
 *
 * @param startElement
 *        cursor of StAX parser pointed on the tag.
 * @param statistics
 *        container accumulating statistics.
 * @param index
 *        internal index of the parsed file.
 * @param filename
 *        file name.
 * @return parsed data as CheckstyleRecord instance.
 */
private static CheckstyleRecord parseErrorTag(StartElement startElement, Statistics statistics, int index, String filename) {
    int line = -1;
    int column = -1;
    String source = null;
    String message = null;
    String severity = null;
    final Iterator<Attribute> attributes = startElement.getAttributes();
    while (attributes.hasNext()) {
        final Attribute attribute = attributes.next();
        final String attrName = attribute.getName().toString();
        switch(attrName) {
            case LINE_ATTR:
                line = Integer.parseInt(attribute.getValue());
                break;
            case COLUMN_ATTR:
                column = Integer.parseInt(attribute.getValue());
                break;
            case SEVERITY_ATTR:
                severity = attribute.getValue();
                statistics.addSeverityRecord(severity, index);
                break;
            case MESSAGE_ATTR:
                message = attribute.getValue();
                break;
            case SOURCE_ATTR:
                source = attribute.getValue();
                statistics.addModuleRecord(source, index);
                break;
            default:
                break;
        }
    }
    return new CheckstyleRecord(index, line, column, severity, source, message, filename);
}
Also used : CheckstyleRecord(com.github.checkstyle.data.CheckstyleRecord) Attribute(javax.xml.stream.events.Attribute)

Example 5 with CheckstyleRecord

use of com.github.checkstyle.data.CheckstyleRecord in project contribution by checkstyle.

the class CheckstyleReportsParser method parseXmlPortion.

/**
 * Parses portion of the XML report.
 *
 * @param diffReport
 *        container for parsed data.
 * @param reader
 *        StAX parser interface.
 * @param numOfFilenames
 *        number of "file" tags to parse.
 * @param index
 *        internal index of the parsed file.
 * @throws XMLStreamException
 *         on internal parser error.
 */
private static void parseXmlPortion(DiffReport diffReport, XMLEventReader reader, int numOfFilenames, int index) throws XMLStreamException {
    int counter = numOfFilenames;
    String filename = null;
    List<CheckstyleRecord> records = null;
    while (reader.hasNext()) {
        final XMLEvent event = reader.nextEvent();
        if (event.isStartElement()) {
            final StartElement startElement = event.asStartElement();
            final String startElementName = startElement.getName().getLocalPart();
            // file tag encounter
            if (startElementName.equals(FILE_TAG)) {
                counter--;
                diffReport.getStatistics().incrementFileCount(index);
                final Iterator<Attribute> attributes = startElement.getAttributes();
                while (attributes.hasNext()) {
                    final Attribute attribute = attributes.next();
                    if (attribute.getName().toString().equals(FILENAME_ATTR)) {
                        filename = attribute.getValue();
                    }
                }
                records = new ArrayList<>();
            } else // error tag encounter
            if (startElementName.equals(ERROR_TAG)) {
                records.add(parseErrorTag(startElement, diffReport.getStatistics(), index, filename));
            }
        }
        if (event.isEndElement()) {
            final EndElement endElement = event.asEndElement();
            if (endElement.getName().getLocalPart().equals(FILE_TAG)) {
                diffReport.addRecords(records, filename);
                if (counter == 0) {
                    break;
                }
            }
        }
    }
}
Also used : StartElement(javax.xml.stream.events.StartElement) CheckstyleRecord(com.github.checkstyle.data.CheckstyleRecord) Attribute(javax.xml.stream.events.Attribute) EndElement(javax.xml.stream.events.EndElement) XMLEvent(javax.xml.stream.events.XMLEvent)

Aggregations

CheckstyleRecord (com.github.checkstyle.data.CheckstyleRecord)5 Statistics (com.github.checkstyle.data.Statistics)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Attribute (javax.xml.stream.events.Attribute)2 JgitDifference (com.github.checkstyle.parser.JgitUtils.JgitDifference)1 Path (java.nio.file.Path)1 List (java.util.List)1 Map (java.util.Map)1 EndElement (javax.xml.stream.events.EndElement)1 StartElement (javax.xml.stream.events.StartElement)1 XMLEvent (javax.xml.stream.events.XMLEvent)1