From: Julien HENRY Date: Tue, 1 Jun 2021 16:02:02 +0000 (+0200) Subject: SONAR-14882 Restore support for BOM in RulesDefinitionXmlLoader X-Git-Tag: 9.0.0.45539~150 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2c6664c45074f5a76d20160b41ddeb56f8122334;p=sonarqube.git SONAR-14882 Restore support for BOM in RulesDefinitionXmlLoader --- diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinitionXmlLoader.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinitionXmlLoader.java index fb5e0494766..034ee858dfb 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinitionXmlLoader.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinitionXmlLoader.java @@ -34,6 +34,8 @@ import javax.xml.stream.XMLStreamException; import javax.xml.stream.events.Attribute; import javax.xml.stream.events.StartElement; import javax.xml.stream.events.XMLEvent; +import org.apache.commons.io.ByteOrderMark; +import org.apache.commons.io.input.BOMInputStream; import org.apache.commons.lang.StringUtils; import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.rule.RuleStatus; @@ -208,7 +210,9 @@ public class RulesDefinitionXmlLoader { * @since 5.1 */ public void load(RulesDefinition.NewRepository repo, InputStream input, Charset charset) { - try (Reader reader = new InputStreamReader(input, charset)) { + try (Reader reader = new InputStreamReader(new BOMInputStream(input, + ByteOrderMark.UTF_8, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE, + ByteOrderMark.UTF_32LE, ByteOrderMark.UTF_32BE), charset)) { load(repo, reader); } catch (IOException e) { throw new IllegalStateException("Error while reading XML rules definition for repository " + repo.key(), e); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionXmlLoaderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionXmlLoaderTest.java index 5dc8172d149..c45475af3a9 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionXmlLoaderTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionXmlLoaderTest.java @@ -115,6 +115,20 @@ public class RulesDefinitionXmlLoaderTest { assertThat(rule.htmlDescription().charAt(2)).isEqualTo('\u0026'); } + @Test + public void test_utf8_encoding_with_bom() { + InputStream input = getClass().getResourceAsStream("RulesDefinitionXmlLoaderTest/utf8-with-bom.xml"); + RulesDefinition.Repository repository = load(input, StandardCharsets.UTF_8.name()); + + assertThat(repository.rules()).hasSize(1); + RulesDefinition.Rule rule = repository.rules().get(0); + assertThat(rule.key()).isEqualTo("com.puppycrawl.tools.checkstyle.checks.naming.LocalVariableNameCheck"); + assertThat(rule.name()).isEqualTo("M & M"); + assertThat(rule.htmlDescription().charAt(0)).isEqualTo('\u00E9'); + assertThat(rule.htmlDescription().charAt(1)).isEqualTo('\u00E0'); + assertThat(rule.htmlDescription().charAt(2)).isEqualTo('\u0026'); + } + @Test public void support_deprecated_format() { // the deprecated format uses some attributes instead of nodes diff --git a/sonar-plugin-api/src/test/resources/org/sonar/api/server/rule/RulesDefinitionXmlLoaderTest/utf8-with-bom.xml b/sonar-plugin-api/src/test/resources/org/sonar/api/server/rule/RulesDefinitionXmlLoaderTest/utf8-with-bom.xml new file mode 100644 index 00000000000..0c0b5c88c33 --- /dev/null +++ b/sonar-plugin-api/src/test/resources/org/sonar/api/server/rule/RulesDefinitionXmlLoaderTest/utf8-with-bom.xml @@ -0,0 +1,11 @@ + + + com.puppycrawl.tools.checkstyle.checks.naming.LocalVariableNameCheck + BLOCKER + Checker/TreeWalker/LocalVariableName + M & M + + + + +