Skip to content

XML entity encoding for carriage return and line feed #2399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ private String escapeXmlEntities(String s) {
s = s.replace("'", "'");
s = s.replace("&lt;", "<");
s = s.replace("&gt;", ">");
s = s.replace("&#x0D;", "\r");
s = s.replace("&#x0A;", "\n");
Comment on lines +196 to +197
Copy link
Contributor

Choose a reason for hiding this comment

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

This feels super inefficient, but I guess it's just 1.x. We should do better in 2.x to avoid all of the string copying.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1 The micro optimization articles I shared a while ago come handy now! 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks Matt and Zoe , will add this optimization in another PR
https://medium.com/javarevisited/micro-optimizations-in-java-string-replaceall-c6d0edf2ef6

// Ampersands should always be the last to unescape
s = s.replace("&amp;", "&");
}
Expand All @@ -202,6 +204,8 @@ private String escapeXmlEntities(String s) {
s = s.replace("'", "&apos;");
s = s.replace("<", "&lt;");
s = s.replace(">", "&gt;");
s = s.replace("\r", "&#x0D;");
s = s.replace("\n", "&#x0A;");
return s;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@
}
}
},
{
"description": "Escape Characters are marshalled correctly as XML in the payload",
"given": {

"input": {
"stringMember": "\"'<\r\nNormalCharacters\"'>\r\n"
}
},
"when": {
"action": "marshall",
"operation": "AllTypes"
},
"then": {
"serializedAs": {
"body": {
"xmlEquals": "<AllTypesRequest xmlns=\"https://restxml/\"><stringMember>&quot;&apos;&lt;&#x0D;&#x0A;NormalCharacters&quot;&apos;&gt;&#x0D;&#x0A;</stringMember></AllTypesRequest>"
}
}
}
},
{
"description": "Boolean member with value true is marshalled correctly as XML",
"given": {
Expand Down