-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
src/main/java/org/checkstyle/autofix/parser/CheckstyleReportsParser.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/org/checkstyle/autofix/parser/CheckstyleViolation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
src/main/java/org/checkstyle/autofix/parser/package-info.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
75 changes: 75 additions & 0 deletions
75
src/test/java/org/checkstyle/autofix/parser/CheckstyleReportsParserTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/test/resources/org/checkstyle/autofix/parser/checkstyle-multiple-files.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
8 changes: 8 additions & 0 deletions
8
src/test/resources/org/checkstyle/autofix/parser/checkstyle-report.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.