Skip to content

Implemented Report and Config Parsner #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/import-control-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
<allow pkg="org.openrewrite"/>
<allow pkg="org.checkstyle"/>
<allow pkg="java.io"/>
<allow pkg="java.util"/>
<allow pkg="java.nio"/>
<allow pkg="java.lang"/>
</import-control>
3 changes: 2 additions & 1 deletion config/import-control.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<allow pkg="org.openrewrite.java"/>
<allow pkg="org.openrewrite.java.tree"/>
<allow pkg="java"/>
<allow pkg="javax.xml.stream"/>
<allow pkg="org.checkstyle"/>
<allow pkg="java.util"/>
</import-control>
</import-control>
6 changes: 5 additions & 1 deletion config/suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
"-//Checkstyle//DTD SuppressionFilter Configuration 1.1//EN"
"https://checkstyle.org/dtds/suppressions_1_1.dtd">

<suppressions/>
<suppressions>
<suppress checks="MissingJavadocType" files=".*"/>
<suppress checks="JavadocVariable" files=".*"/>
<suppress checks="MissingJavadocMethod" files=".*"/>
</suppressions>
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@
</dependencyManagement>

<dependencies>

<!-- Checkstyle dependency for parsing Checkstyle configs -->
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>${checkstyle.version}</version>
</dependency>

<!-- OpenRewrite core dependencies - using consistent versions -->
<dependency>
<groupId>org.openrewrite</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
///////////////////////////////////////////////////////////////////////////////////////////////
// checkstyle-openrewrite-recipes: Automatically fix Checkstyle violations with OpenRewrite.
// Copyright (C) 2025 The Checkstyle OpenRewrite Recipes Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
///////////////////////////////////////////////////////////////////////////////////////////////

package org.checkstyle.autofix.parser;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

public final class CheckstyleReportsParser {

private static final String FILE_TAG = "file";

private static final String ERROR_TAG = "error";

private static final String FILENAME_ATTR = "name";

private static final String LINE_ATTR = "line";

private static final String COLUMN_ATTR = "column";

private static final String SEVERITY_ATTR = "severity";

private static final String MESSAGE_ATTR = "message";

private static final String SOURCE_ATTR = "source";

private CheckstyleReportsParser() {

}

public static List<CheckstyleViolation> parse(Path xmlPath)
throws IOException, XMLStreamException {

final List<CheckstyleViolation> result = new ArrayList<>();

try (InputStream inputStream = new FileInputStream(xmlPath.toFile())) {

final XMLInputFactory inputFactory = XMLInputFactory.newInstance();
final XMLEventReader reader = inputFactory.createXMLEventReader(inputStream);

try {
String filename = null;

while (reader.hasNext()) {
final XMLEvent event = reader.nextEvent();
if (event.isStartElement()) {
final StartElement startElement = event.asStartElement();
final String startElementName = startElement.getName().getLocalPart();

if (FILE_TAG.equals(startElementName)) {
filename = parseFileTag(startElement);
}
else if (ERROR_TAG.equals(startElementName)) {
Objects.requireNonNull(filename, "File name can not be null");
result.add(parseErrorTag(startElement, filename));
}
}
}
}
finally {
if (reader != null) {
reader.close();
}
}
}

return result;
}

private static String parseFileTag(StartElement startElement) {
String fileName = null;
final Iterator<Attribute> attributes = startElement.getAttributes();
while (attributes.hasNext()) {
final Attribute attribute = attributes.next();
if (FILENAME_ATTR.equals(attribute.getName().getLocalPart())) {
fileName = attribute.getValue();
break;
}
}
return fileName;
}

private static CheckstyleViolation parseErrorTag(StartElement startElement, String filename) {
Integer line = null;
Integer column = null;
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().getLocalPart();
switch (attrName) {
case LINE_ATTR:
line = Integer.valueOf(attribute.getValue());
break;
case COLUMN_ATTR:
column = Integer.parseInt(attribute.getValue());
break;
case SEVERITY_ATTR:
severity = attribute.getValue();
break;
case MESSAGE_ATTR:
message = attribute.getValue();
break;
case SOURCE_ATTR:
source = attribute.getValue();
break;
default:
break;
}
}
return new CheckstyleViolation(
line, column, severity, source, message, filename);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
///////////////////////////////////////////////////////////////////////////////////////////////
// checkstyle-openrewrite-recipes: Automatically fix Checkstyle violations with OpenRewrite.
// Copyright (C) 2025 The Checkstyle OpenRewrite Recipes Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
///////////////////////////////////////////////////////////////////////////////////////////////

package org.checkstyle.autofix.parser;

public final class CheckstyleViolation {

private final Integer line;

private final Integer column;

private final String severity;

private final String source;

private final String message;

private final String fileName;

public CheckstyleViolation(Integer line, Integer column,
String severity, String source, String message, String fileName) {
this.line = line;
this.column = column;
this.severity = severity;
this.source = source;
this.message = message;
this.fileName = fileName;
}

public int getLine() {
return line;
}

public int getColumn() {
return column;
}

public String getSource() {
return source;
}

public String getMessage() {
return message;
}

public String getFileName() {
return fileName;
}

public String getSeverity() {
return severity;
}

}
21 changes: 21 additions & 0 deletions src/main/java/org/checkstyle/autofix/parser/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
///////////////////////////////////////////////////////////////////////////////////////////////
// checkstyle-openrewrite-recipes: Automatically fix Checkstyle violations with OpenRewrite.
// Copyright (C) 2025 The Checkstyle OpenRewrite Recipes Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
///////////////////////////////////////////////////////////////////////////////////////////////

/**
* Provides classes to parse Checkstyle XML report files.
*/
package org.checkstyle.autofix.parser;
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
///////////////////////////////////////////////////////////////////////////////////////////////
// checkstyle-openrewrite-recipes: Automatically fix Checkstyle violations with OpenRewrite.
// Copyright (C) 2025 The Checkstyle OpenRewrite Recipes Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
///////////////////////////////////////////////////////////////////////////////////////////////

package org.checkstyle.autofix.parser;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Test;

public class CheckstyleReportsParserTest {

private static String getPath(String path) {
return "src/test/resources/org/checkstyle/autofix/parser/" + path;
}

@Test
public void testParseFromResource() throws Exception {
final Path xmlPath = Path.of(getPath("checkstyle-report.xml"));
final List<CheckstyleViolation> records = CheckstyleReportsParser.parse(xmlPath);

assertNotNull(records);
assertEquals(1, records.size());

final CheckstyleViolation record = records.get(0);
assertEquals(42, record.getLine());
assertEquals(13, record.getColumn());
assertEquals("error", record.getSeverity());
assertEquals("Example message", record.getMessage());
assertEquals("com.puppycrawl.example.Check", record.getSource());
assertEquals("Example.java", record.getFileName());
}

@Test
public void testParseMultipleFilesReport() throws Exception {
final Path xmlPath = Path.of(getPath("checkstyle-multiple-files.xml"));
final List<CheckstyleViolation> records = CheckstyleReportsParser.parse(xmlPath);

assertNotNull(records);
assertEquals(3, records.size());

final Map<String, List<CheckstyleViolation>> grouped = records.stream()
.collect(Collectors.groupingBy(CheckstyleViolation::getFileName));

assertEquals(2, grouped.size());

assertEquals(2, grouped.get("Main.java").size());
assertEquals(1, grouped.get("Utils.java").size());

CheckstyleViolation record = grouped.get("Main.java").get(0);
assertEquals("error", record.getSeverity());

record = grouped.get("Utils.java").get(0);
assertEquals("warning", record.getSeverity());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<checkstyle>
<file name="Main.java">
<error line="10" column="5" severity="error" message="Missing javadoc" source="com.puppycrawl.Check"/>
<error line="20" column="2" severity="error" message="Indentation" source="com.puppycrawl.Indent"/>
</file>
<file name="Utils.java">
<error line="15" column="1" severity="warning" message="Unused import" source="com.puppycrawl.Import"/>
</file>
</checkstyle>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle version="8.0">
<file name="Example.java">
<error line="42" column="13" severity="error"
message="Example message"
source="com.puppycrawl.example.Check"/>
</file>
</checkstyle>