Menu

#21 Support serializing escaped content directly to a Writer/Appendable

2.8
open
nobody
None
5
2018-07-26
2018-07-25
Dave
No

I'm sure that in some cases, there is no way around creating a StringBuilder, adding each character or escaped version of a character, and then using the resulting String. But since the methods of interest for serialization all involve a Writer, it is almost certainly more efficient for larger content strings (and probably most other cases) to have the escaped characters be written directly to a Writer or Appendable.

I suggest the following refactoring to support this:

  1. add an overloaded Utils.escapeXml declared as void but accepting an Appendable;
  2. move the body of the existing method, with references to the StringBuilder becoming references to the Appendable (just make the parameter name "result" to match the name of the StringBuilder, and remove the StringBuilder declaration/assignment); and finally
  3. have the existing method create a StringBuilder to delegate to the new version, and then return its toString() method.

It would also then be useful to add to HtmlSerializer a version of escapeText that accepts the Writer being used for serialization, which could delegate to a new version of Utils.escapeHtml, and so forth.

Discussion

  • Dave

    Dave - 2018-07-25

    Let me know if you're interested in my help implementing such a change; and if so, if there are any details you would like to specify about how I do so.

     
  • Scott Wilson

    Scott Wilson - 2018-07-26

    Hi Dave,

    I'm always very interested in any help offered!

    For this refactoring, the best approach is to create a single patch that I can apply to the trunk; I'll then run the unit tests, and if they're OK, I'll commit the patch and it'll be in the next release.

     

Log in to post a comment.

MongoDB Logo MongoDB