Clarify namespace, version, pmdVersion

This commit is contained in:
Andreas Dangel
2024-06-23 19:21:49 +02:00
parent 29983a91a2
commit a8ab215010
3 changed files with 23 additions and 11 deletions

View File

@ -36,8 +36,9 @@ import net.sourceforge.pmd.util.StringUtil;
* *
*/ */
public final class XMLRenderer implements CPDReportRenderer { public final class XMLRenderer implements CPDReportRenderer {
private static final String NAMESPACE_URI = "https://pmd-code.org/ns/cpd-report/1.0.0"; private static final String NAMESPACE_URI = "https://pmd-code.org/schema/cpd-report";
private static final String NAMESPACE_LOCATION = "https://pmd-code.org/ns/cpd-report_1_0_0.xsd"; private static final String NAMESPACE_LOCATION = "https://pmd.github.io/schema/cpd-report_1_0_0.xsd";
private static final String SCHEMA_VERSION = "1.0.0";
private String encoding; private String encoding;
@ -103,7 +104,8 @@ public final class XMLRenderer implements CPDReportRenderer {
final Element root = doc.createElementNS(NAMESPACE_URI, "pmd-cpd"); final Element root = doc.createElementNS(NAMESPACE_URI, "pmd-cpd");
root.setAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "xsi:schemaLocation", NAMESPACE_URI + " " + NAMESPACE_LOCATION); root.setAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "xsi:schemaLocation", NAMESPACE_URI + " " + NAMESPACE_LOCATION);
root.setAttributeNS(NAMESPACE_URI, "version", PMDVersion.VERSION); root.setAttributeNS(NAMESPACE_URI, "version", SCHEMA_VERSION);
root.setAttributeNS(NAMESPACE_URI, "pmdVersion", PMDVersion.VERSION);
root.setAttributeNS(NAMESPACE_URI, "timestamp", OffsetDateTime.now().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)); root.setAttributeNS(NAMESPACE_URI, "timestamp", OffsetDateTime.now().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
final Map<FileId, Integer> numberOfTokensPerFile = report.getNumberOfTokensPerFile(); final Map<FileId, Integer> numberOfTokensPerFile = report.getNumberOfTokensPerFile();
doc.appendChild(root); doc.appendChild(root);

View File

@ -1,10 +1,18 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<xs:schema <xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="https://pmd-code.org/ns/cpd-report/1.0.0" xmlns="https://pmd-code.org/schema/cpd-report" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://pmd-code.org/ns/cpd-report/1.0.0" targetNamespace="https://pmd-code.org/schema/cpd-report"
elementFormDefault="qualified"> elementFormDefault="qualified">
<xsd:annotation>
<xs:documentation><![CDATA[
PMD CPD Report Schema, version 1.0.0
This XML format is produced by CPD's XMLRenderer "xml".
]]></xs:documentation>
</xsd:annotation>
<xs:element name="pmd-cpd"> <xs:element name="pmd-cpd">
<xs:complexType> <xs:complexType>
<xs:sequence> <xs:sequence>
@ -13,6 +21,7 @@
<xs:element name="error" type="error" minOccurs="0" maxOccurs="unbounded"/> <xs:element name="error" type="error" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence> </xs:sequence>
<xs:attribute name="version" type="xs:string" use="required"/> <xs:attribute name="version" type="xs:string" use="required"/>
<xs:attribute name="pmdVersion" type="xs:string" use="required"/>
<xs:attribute name="timestamp" type="xs:string" use="required"/> <xs:attribute name="timestamp" type="xs:string" use="required"/>
</xs:complexType> </xs:complexType>
</xs:element> </xs:element>

View File

@ -57,14 +57,15 @@ class XMLRendererTest {
assertReportIsValidSchema(report); assertReportIsValidSchema(report);
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<pmd-cpd xmlns=\"https://pmd-code.org/ns/cpd-report/1.0.0\"\n" + "<pmd-cpd xmlns=\"https://pmd-code.org/schema/cpd-report\"\n"
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
+ " pmdVersion=\"XXX\"\n"
+ " timestamp=\"XXX\"\n" + " timestamp=\"XXX\"\n"
+ " version=\"XXX\"\n" + " version=\"1.0.0\"\n"
+ " xsi:schemaLocation=\"https://pmd-code.org/ns/cpd-report/1.0.0 https://pmd-code.org/ns/cpd-report_1_0_0.xsd\"/>\n", + " xsi:schemaLocation=\"https://pmd-code.org/schema/cpd-report https://pmd.github.io/schema/cpd-report_1_0_0.xsd\"/>\n",
report.replaceAll(" {4}timestamp=\".+?\"", " timestamp=\"XXX\"") report.replaceAll("timestamp=\".+?\"", "timestamp=\"XXX\"")
.replaceAll(" {4}version=\".+?\"", " version=\"XXX\""), .replaceAll("pmdVersion=\".+?\"", "pmdVersion=\"XXX\""),
"namespace is missing"); "namespace is missing or wrong");
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder() Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
.parse(new ByteArrayInputStream(report.getBytes(ENCODING))); .parse(new ByteArrayInputStream(report.getBytes(ENCODING)));