Skip to content

Commit

Permalink
Merge pull request pmd#4592 from Monits:xml-declaration
Browse files Browse the repository at this point in the history
[xml] Add MissingEncoding rule pmd#4592
  • Loading branch information
adangel committed Oct 19, 2023
2 parents 0c53b69 + 42c7fb0 commit 14b9029
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 4 deletions.
8 changes: 8 additions & 0 deletions docs/pages/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ The remaining section describes the complete release notes for 7.0.0.
**New Rules**

* {% rule java/codestyle/UseExplicitTypes %} reports usages of `var` keyword, which was introduced with Java 10.
* {% rule xml/bestpractices/MissingEncoding %} finds XML files without explicit encoding.

#### Fixed issues

Expand All @@ -55,6 +56,8 @@ The remaining section describes the complete release notes for 7.0.0.
* java-codestyle
* [#2847](https://github.com/pmd/pmd/issues/2847): \[java] New Rule: Use Explicit Types
* [#4578](https://github.com/pmd/pmd/issues/4578): \[java] CommentDefaultAccessModifier comment needs to be before annotation if present
* xml-bestpractices
* [#4592](https://github.com/pmd/pmd/pull/4592): \[xml] Add MissingEncoding rule

#### API Changes

Expand Down Expand Up @@ -240,6 +243,9 @@ can be parsed now. PMD should now be able to parse Apex code up to version 59.0
* {% rule swift/errorprone/ForceCast %}
* {% rule swift/errorprone/ForceTry %}

**XML**
* {% rule xml/bestpractices/MissingEncoding %} finds XML files without explicit encoding.

#### Changed Rules

**General changes**
Expand Down Expand Up @@ -649,6 +655,8 @@ Language specific fixes:
* [#1882](https://github.com/pmd/pmd/pull/1882): \[swift] UnavailableFunction Swift rule
* xml
* [#1800](https://github.com/pmd/pmd/pull/1800): \[xml] Unimplement org.w3c.dom.Node from the XmlNodeWrapper
* xml-bestpractices
* [#4592](https://github.com/pmd/pmd/pull/4592): \[xml] Add MissingEncoding rule

### ✨ External Contributions

Expand Down
3 changes: 3 additions & 0 deletions docs/pages/release_notes_pmd7.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ can be parsed now. PMD should now be able to parse Apex code up to version 59.0
* {% rule swift/errorprone/ForceCast %}
* {% rule swift/errorprone/ForceTry %}

**XML**
* {% rule xml/bestpractices/MissingEncoding %} finds XML files without explicit encoding.

### Changed Rules

**General changes**
Expand Down
1 change: 1 addition & 0 deletions pmd-core/src/main/resources/rulesets/releases/700.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ This ruleset contains links to rules that are new in PMD v7.0.0
<rule ref="category/swift/bestpractices.xml/ProhibitedInterfaceBuilder"/>
<rule ref="category/swift/bestpractices.xml/UnavailableFunction"/>

<rule ref="category/xml/bestpractices.xml/MissingEncoding"/>
</ruleset>

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Expand All @@ -23,6 +24,9 @@
import net.sourceforge.pmd.lang.ast.ParseException;
import net.sourceforge.pmd.lang.ast.Parser.ParserTask;
import net.sourceforge.pmd.lang.ast.RootNode;
import net.sourceforge.pmd.lang.rule.xpath.Attribute;
import net.sourceforge.pmd.lang.rule.xpath.NoAttribute;
import net.sourceforge.pmd.lang.rule.xpath.impl.AttributeAxisIterator;
import net.sourceforge.pmd.lang.xml.ast.XmlNode;

public final class XmlParserImpl {
Expand Down Expand Up @@ -108,6 +112,31 @@ public XmlNode wrap(Node domNode) {
public Document getNode() {
return (Document) super.getNode();
}

public String getXmlEncoding() {
return getNode().getXmlEncoding();
}

public boolean isXmlStandalone() {
return getNode().getXmlStandalone();
}

public String getXmlVersion() {
return getNode().getXmlVersion();
}

// Hide the image attribute
@NoAttribute
@Override
public String getImage() {
return super.getImage();
}

@Override
public Iterator<Attribute> getXPathAttributesIterator() {
// Expose this node's attributes through reflection
return new AttributeAxisIterator(this);
}
}

}
29 changes: 29 additions & 0 deletions pmd-xml/src/main/resources/category/xml/bestpractices.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,33 @@
<description>
Rules which enforce generally accepted best practices.
</description>

<rule name="MissingEncoding"
language="xml"
since="7.0.0"
message="Set an explicit XML encoding in the XML declaration to ensure proper parsing"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_xml_bestpractices.html#missingencoding">
<description>
When the character encoding is missing from the XML declaration,
the parser may produce garbled text.

This is completely dependent on how the parser is set up
and the content of the XML file, so it may be hard to reproduce.

Providing an explicit encoding ensures accurate and consistent
parsing.
</description>
<priority>3</priority>
<properties>
<property name="version" value="3.1"/>
<property name="xpath">
<value>
<![CDATA[
/document[@XmlEncoding = ""]
]]>
</value>
</property>
</properties>
</rule>
</ruleset>
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
#

rulesets.filenames=\
category/xml/bestpractices.xml,\
category/xml/errorprone.xml

#
# categories without rules
#
# category/xml/bestpractices.xml
# category/xml/codestyle.xml
# category/xml/design.xml
# category/xml/documentation.xml
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/

package net.sourceforge.pmd.lang.xml.rule.bestpractices;

import net.sourceforge.pmd.testframework.PmdRuleTst;

class MissingEncodingTest extends PmdRuleTst {
// no additional unit tests
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
+- document[]
+- document[@XmlEncoding = "UTF-8", @XmlStandalone = false, @XmlVersion = "1.0"]
+- deployment-plan[@global-variables = "false", @xmlns = "http://xmlns.oracle.com/weblogic/deployment-plan", @xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance", @xsi:schemaLocation = "http://xmlns.oracle.com/weblogic/deployment-plan http://xmlns.oracle.com/weblogic/deployment-plan/1.0/deployment-plan.xsd"]
+- text[@Image = "\n "]
+- application-name[]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
+- document[]
+- document[@XmlEncoding = null, @XmlStandalone = false, @XmlVersion = "1.0"]
+- comment[]
+- rootElement[]
+- rootElement[]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
+- document[]
+- document[@XmlEncoding = null, @XmlStandalone = false, @XmlVersion = "1.0"]
+- pmd:rootElement[@xmlns:pmd = "http://pmd.sf.net"]
+- text[@Image = "\n "]
+- comment[]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data
xmlns="http://pmd.sourceforge.net/rule-tests"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd">

<test-code>
<description>No XML Declaration</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
<root>
<child/>
</root>
]]></code>
</test-code>

<test-code>
<description>XML Declaration without encoding</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
<?xml version="1.0" ?>
<root>
<child/>
</root>
]]></code>
</test-code>

<test-code>
<description>XML Declaration with UTF-8 encoding</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<child/>
</root>
]]></code>
</test-code>

<test-code>
<description>XML Declaration with ISO-8859-1 encoding</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
<?xml version="1.0" encoding="ISO-8859-1" ?>
<root>
<child/>
</root>
]]></code>
</test-code>

<test-code>
<description>XML Declaration with UTF-8 encoding and standalone</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<root>
<child/>
</root>
]]></code>
</test-code>
</test-data>

0 comments on commit 14b9029

Please sign in to comment.