aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2021-06-01 18:02:02 +0200
committersonartech <sonartech@sonarsource.com>2021-06-01 20:51:24 +0000
commit2c6664c45074f5a76d20160b41ddeb56f8122334 (patch)
tree8a906cad949308c8c6ff2d8443929fbd7931d22f /sonar-plugin-api
parent0b460064180d6abc6b837124996708b25539b17a (diff)
downloadsonarqube-2c6664c45074f5a76d20160b41ddeb56f8122334.tar.gz
sonarqube-2c6664c45074f5a76d20160b41ddeb56f8122334.zip
SONAR-14882 Restore support for BOM in RulesDefinitionXmlLoader
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinitionXmlLoader.java6
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionXmlLoaderTest.java14
-rw-r--r--sonar-plugin-api/src/test/resources/org/sonar/api/server/rule/RulesDefinitionXmlLoaderTest/utf8-with-bom.xml11
3 files changed, 30 insertions, 1 deletions
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
@@ -116,6 +116,20 @@ public class RulesDefinitionXmlLoaderTest {
}
@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
InputStream input = getClass().getResourceAsStream("RulesDefinitionXmlLoaderTest/deprecated.xml");
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 @@
+<rules>
+ <rule>
+ <key>com.puppycrawl.tools.checkstyle.checks.naming.LocalVariableNameCheck</key>
+ <priority>BLOCKER</priority>
+ <configKey>Checker/TreeWalker/LocalVariableName</configKey>
+ <name>M &amp; M</name>
+ <description>
+ <![CDATA[éà&]]>
+ </description>
+ </rule>
+</rules>