GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [core] Fix XMLRenderer encoding issues by adangel · Pull Request #2633 · pmd/pmd

Conversation

@adangel

@adangel adangel commented Jul 5, 2020

Copy link
Copy Markdown
Member

Describe the PR

This PR tries to fix #2615 . In order to use the correct encoding through the rendering process, the new API Renderer::setReportFile is added.

I'm not entirely sure about CPD renderer, whether we have a similar issue there. But as far as I can see, only the default platform encoding is used, there is no way to change the encoding. There is also no way to specify a output file as a command line option, so CPD renderers always render to Sysout and hence should use the default platform encoding.

Related issues

  • Fixes [core] PMD/CPD produces invalid XML (insufficient escaping/wrong encoding) #2615

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by travis)
  • Added (in-code) documentation (if needed).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
adangel added 4 commits July 3, 2020 21:11
@adangel
[core] CPD: Add correct XML 1.0 escaping for code snippets
3004e76
@adangel
[core] CPD: Also escape filename, explicitly set XML 1.0
3bb091e
@adangel
[core] PMD XMLRenderer - correctly support encoding
d927973
The XMLRenderer uses by default UTF-8 encoding, but the
writer uses the system default encoding, which doesn't work
well together.

Provide new experimental API Renderer::setReportFile, so that
renderer implementations can create their own writers.
The default implementation in AbstractRenderer is backwards compatible.
@adangel
[doc] Update release notes, fixes pmd#2615
60f905b
@adangel adangel added this to the 6.26.0 milestone Jul 5, 2020
adangel
adangel commented Jul 5, 2020
View reviewed changes
Comment thread pmd-core/src/main/java/net/sourceforge/pmd/cpd/XMLRenderer.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

adangel
adangel commented Jul 5, 2020
View reviewed changes
Comment thread pmd-core/src/main/java/net/sourceforge/pmd/renderers/XMLRenderer.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@ghost

ghost commented Jul 5, 2020 •
edited by ghost
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
1 Message
📖 This changeset introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Full report
✅ This changeset introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Full report
✅ This changeset introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Full report
✅ This changeset introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Full report
✅ This changeset introduces 106260 new violations, 0 new errors and 0 new configuration errors,
removes 122673 violations, 0 errors and 0 configuration errors.
Full report
✅ This changeset introduces 106260 new violations, 0 new errors and 0 new configuration errors,
removes 122673 violations, 0 errors and 0 configuration errors.
Full report

Generated by 🚫 Danger

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@adangel
[core] XMLRenderer - avoid unmappable characters for non UTF-8 encodings
0bd9b1d
@adangel adangel added the is:WIP For PRs that are not fully ready, or issues that are actively being tackled label Jul 5, 2020
@adangel

adangel commented Jul 5, 2020

Copy link
Copy Markdown
Member Author

This PR showed actually a bug in pmd-regression-tester: With this PR, we are now escaping certain characters in XML, which we previously didn't:

before:

<?xml version="1.0" encoding="UTF-8"?>
<pmd xmlns="http://pmd.sourceforge.net/report/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sourceforge.net/report/2.0.0 http://pmd.sourceforge.net/report_2_0_0.xsd"
    version="6.24.0" timestamp="2020-07-05T21:26:00.982">
<file name="/home/andreas/Downloads/chunk-diff-issue-2615/src/TestFile.java">
<violation beginline="2" endline="2" begincolumn="21" endcolumn="35" rule="MethodArgumentCouldBeFinal" ruleset="Code Style" class="TestFile" method="bar" variable="fileName" externalInfoUrl="https://pmd.github.io/pmd-6.24.0/pmd_rules_java_codestyle.html#methodargumentcouldbefinal" priority="3">
Parameter 'fileName' is not assigned and could be declared final
</violation>
</file>
</pmd>

after:

<?xml version="1.0" encoding="UTF-8"?>
<pmd xmlns="http://pmd.sourceforge.net/report/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sourceforge.net/report/2.0.0 http://pmd.sourceforge.net/report_2_0_0.xsd"
    version="6.26.0-SNAPSHOT" timestamp="2020-07-05T21:26:32.079">
<file name="/home/andreas/Downloads/chunk-diff-issue-2615/src/TestFile.java">
<violation beginline="2" endline="2" begincolumn="21" endcolumn="35" rule="MethodArgumentCouldBeFinal" ruleset="Code Style" class="TestFile" method="bar" variable="fileName" externalInfoUrl="https://pmd.github.io/pmd/pmd_rules_java_codestyle.html#methodargumentcouldbefinal" priority="3">
Parameter &apos;fileName&apos; is not assigned and could be declared final
</violation>
</file>
</pmd>

pmd-regression-tester doesn't parse the character data correctly - with entities, the character data between tags (where the description is), is now split into multiple chunks of character data, which needs to be accumulated. The parser currently only takes the last character data. Hence the difference in the violations, e.g. before: Parameter 'fileName' is not assigned and could be declared final, after: is not assigned and could be declared final.

I'll fix that tomorrow.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

adangel added 4 commits July 6, 2020 21:44
@adangel
Update pmd-regression-tester
53c581b
@adangel
Update pmd-regression-tester 1.0.1
bb97b69
@adangel
[core] Refactor XMLRenderer to use XMLStreamWriter
2e00669
In order to properly support different encodings, a OutputStream
is needed. Then Java will take care of unmappaple characters
and encode them as entities for XML.

For backwards compatibility, a writer is still created and exposed.
@adangel
[core] Provide StringUtil.removeInvalidXml10Characters
e6600ec
And use it in both CPD/PMD XMLRenderers.
@adangel adangel removed the is:WIP For PRs that are not fully ready, or issues that are actively being tackled label Jul 9, 2020
@adangel

adangel commented Jul 9, 2020

Copy link
Copy Markdown
Member Author

I think, this is ready now.
I refactored the XMLRenderer for PMD to use XMLStreamWriter instead of manually creating XML via StringBuilder...
I learned that the standard XML libraries mostly work correctly, at least in terms of encoding - the only thing to keep in mind is: it works only, if you provide a OutputStream - if you provide a Writer, then now charset specific encoding is happening (like dealing with unmappable characters). Invalid characters are still kept unfortunately, hence I created a new StringUtil method....

I've added the experimental API Renderer::setReportFile which is good for now. But for PMD 7, we should rethink the API: Maybe we can say, that each renderer works with a OutputStream and has to know the encoding (falling back to platform default). It would be nice, to have the Renderer API decoupled from File - at least in unit tests, you often just want to render into memory.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@adangel

adangel commented Jul 16, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

Refs #1962
See also https://github.com/pmd/pmd/wiki/PMD-7.0.0-API#reporting-api

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@oowekyala oowekyala self-assigned this Jul 19, 2020
@oowekyala
Merge branch 'master' into issue-2615
2e9f5ad
oowekyala
oowekyala approved these changes Jul 20, 2020
View reviewed changes

@oowekyala oowekyala left a comment •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine for now. As we said last time, the added API is experimental. We can iron it out in PMD 7.

I'll merge once the travis build goes green

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pmd-core/src/main/java/net/sourceforge/pmd/renderers/Renderer.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@oowekyala
oowekyala merged commit d239987 into pmd:master Jul 23, 2020
@adangel
adangel deleted the issue-2615 branch July 24, 2020 07:22
@adangel adangel mentioned this pull request Oct 15, 2020
[core] Fix XMLRenderer newlines when running under IBM Java #2831
Merged
2 of 4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@oowekyala oowekyala oowekyala approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@oowekyala oowekyala

Labels

None yet

Projects

None yet

Milestone

6.26.0

Development

Successfully merging this pull request may close these issues.

[core] PMD/CPD produces invalid XML (insufficient escaping/wrong encoding)

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@adangel @oowekyala
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.