diff --git a/changelog/@unreleased/pr-1603.v2.yml b/changelog/@unreleased/pr-1603.v2.yml new file mode 100644 index 000000000..4994491f5 --- /dev/null +++ b/changelog/@unreleased/pr-1603.v2.yml @@ -0,0 +1,6 @@ +type: fix +fix: + description: Disable `WhitespaceAround` Checkstyle rule when palantir-java-format + is applied. + links: + - https://github.com/palantir/gradle-baseline/pull/1603 diff --git a/gradle-baseline-java-config/resources/checkstyle/checkstyle.xml b/gradle-baseline-java-config/resources/checkstyle/checkstyle.xml index 2be3288d7..72b51818f 100644 --- a/gradle-baseline-java-config/resources/checkstyle/checkstyle.xml +++ b/gradle-baseline-java-config/resources/checkstyle/checkstyle.xml @@ -81,14 +81,7 @@ - + @@ -138,12 +131,12 @@ - + - + @@ -397,7 +390,7 @@ - + diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineConfig.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineConfig.java index 6191d71d0..a6a66471f 100644 --- a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineConfig.java +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineConfig.java @@ -16,18 +16,30 @@ package com.palantir.baseline.plugins; -import com.google.common.base.Preconditions; import com.palantir.baseline.plugins.BaselineFormat.FormatterState; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; +import java.io.FileReader; +import java.io.FileWriter; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Optional; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; import org.gradle.api.Action; import org.gradle.api.Project; import org.gradle.api.Task; import org.gradle.api.artifacts.Configuration; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.xml.sax.InputSource; /** Extracts Baseline configuration into the configuration directory. */ class BaselineConfig extends AbstractBaselinePlugin { @@ -96,25 +108,29 @@ public void execute(Task task) { Path checkstyleXml = configDir.resolve("checkstyle/checkstyle.xml"); try { - String contents = new String(Files.readAllBytes(checkstyleXml), StandardCharsets.UTF_8); - String replaced = contents.replace( - " " - + "\n" - + " \n" - + " \n" - + " \n", - "") - .replace( - " \n", - "") - .replace( - " " - + "\n", - ""); - Preconditions.checkState(!contents.equals(replaced), "Patching checkstyle.xml must make a change"); - Files.write(checkstyleXml, replaced.getBytes(StandardCharsets.UTF_8)); - } catch (IOException e) { + DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = builderFactory.newDocumentBuilder(); + + InputSource inputSource = new InputSource(new FileReader(checkstyleXml.toFile())); + inputSource.setEncoding("UTF-8"); + Document document = builder.parse(inputSource); + + XPathFactory xPathFactory = XPathFactory.newInstance(); + XPath xPath = xPathFactory.newXPath(); + + removeNode(document, xPath, "//module[@name='Indentation']"); + removeNode(document, xPath, "//module[@name='ParenPad']"); + removeNode(document, xPath, "//module[@name='LeftCurly']"); + removeNode(document, xPath, "//module[@name='WhitespaceAround']"); + + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + Transformer transformer = transformerFactory.newTransformer(); + transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); + + DOMSource source = new DOMSource(document); + StreamResult result = new StreamResult(new FileWriter(checkstyleXml.toFile())); + transformer.transform(source, result); + } catch (Exception e) { throw new RuntimeException("Unable to patch " + checkstyleXml, e); } } @@ -133,5 +149,11 @@ public void execute(Task task) { }); } } + + private void removeNode(Document document, XPath xPath, String expression) throws XPathExpressionException { + xPath.reset(); + Node node = (Node) xPath.compile(expression).evaluate(document, XPathConstants.NODE); + node.getParentNode().removeChild(node); + } } } diff --git a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineConfigIntegrationTest.groovy b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineConfigIntegrationTest.groovy index 7dcb53b31..809d9a45a 100644 --- a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineConfigIntegrationTest.groovy +++ b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineConfigIntegrationTest.groovy @@ -152,5 +152,11 @@ class BaselineConfigIntegrationTest extends AbstractPluginTest { !new File(projectDir, '.baseline/checkstyle/checkstyle.xml').readLines().any { it.contains '' } + !new File(projectDir, '.baseline/checkstyle/checkstyle.xml').readLines().any { + it.contains '' + } + !new File(projectDir, '.baseline/checkstyle/checkstyle.xml').readLines().any { + it.contains '' + } } }