XStream

Allow use of custom friendly naming with SaxWriter and TraxSource

Details

  • Type: Improvement Improvement
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 1.2.1, 1.2.2
  • Fix Version/s: 1.3
  • Component/s: IO
  • Labels:
    None
  • JDK version and platform:
    Sun 1.5 Windows

Description

When you are stuck with a pre-existing schema which contains underscores, it is currently impossible to transform objects based on this schema using existing XSLT sheets, as they expect the names with single underscores, and the SaxWriter class only uses the default XmlReader constructor which does not allow for custom escaping to be defined.

Enclosed patch rectifies this

  • SaxWriter gains a constructor which enables you to pass a custom name replacer
  • TraxSource is able to use SaxWriter instance passed to setXMLReader method.

Apologies for the lack of test code, but it does flawlessly transform about 3000 files here

Usage example -
{{{ TraxSource t = new TraxSource(model, xs); t.setXMLReader(new SaxWriter(new XmlFriendlyReplacer("$", "_"))); }}}

Activity

Hide
Joerg Schaible added a comment -

I've applied a slightly different version for the TraxSource and fixed the patch for the SaxWriter to keep our unit tests running

You may give the current SNAPSHOT a try.

Thanks!

Show
Joerg Schaible added a comment - I've applied a slightly different version for the TraxSource and fixed the patch for the SaxWriter to keep our unit tests running You may give the current SNAPSHOT a try. Thanks!
Hide
Adrian Wilkins added a comment -

Thanks, this increases my convenience greatly as I no longer have to maintain a separate branch (although my svk mirror will be sticking around a while until I wrap up.... never know when you'll need to patch again)

It threw me to start with, but once you wrap the SaxWriter in an instance of org.xml.sax.helpers.XMLFilterImpl you're good to go again.

Show
Adrian Wilkins added a comment - Thanks, this increases my convenience greatly as I no longer have to maintain a separate branch (although my svk mirror will be sticking around a while until I wrap up.... never know when you'll need to patch again) It threw me to start with, but once you wrap the SaxWriter in an instance of org.xml.sax.helpers.XMLFilterImpl you're good to go again.
Hide
Adrian Wilkins added a comment -

The code doesn't ever use the custom SaxWriter instance that you pass in ; filterChain will never be an instance of SaxWriter because you can't pass a SaxWriter through to the method because of the API restrictions (which allow SaxWriter to enter but throw an exception because it's not an XMLFilter.

Patch below fixes it.

Index: java/com/thoughtworks/xstream/io/xml/TraxSource.java
===================================================================
— java/com/thoughtworks/xstream/io/xml/TraxSource.java (revision 3)
+++ java/com/thoughtworks/xstream/io/xml/TraxSource.java (revision 4)
@@ -265,7 +265,7 @@
while (filter.getParent() instanceof XMLFilter) { filter = (XMLFilter)(filter.getParent()); }

  • if (!(filterChain instanceof SaxWriter)) {
    + if (!(filter.getParent() instanceof SaxWriter)) { filter.setParent(new SaxWriter()); }
Show
Adrian Wilkins added a comment - The code doesn't ever use the custom SaxWriter instance that you pass in ; filterChain will never be an instance of SaxWriter because you can't pass a SaxWriter through to the method because of the API restrictions (which allow SaxWriter to enter but throw an exception because it's not an XMLFilter. Patch below fixes it. Index: java/com/thoughtworks/xstream/io/xml/TraxSource.java =================================================================== — java/com/thoughtworks/xstream/io/xml/TraxSource.java (revision 3) +++ java/com/thoughtworks/xstream/io/xml/TraxSource.java (revision 4) @@ -265,7 +265,7 @@ while (filter.getParent() instanceof XMLFilter) { filter = (XMLFilter)(filter.getParent()); }
  • if (!(filterChain instanceof SaxWriter)) { + if (!(filter.getParent() instanceof SaxWriter)) { filter.setParent(new SaxWriter()); }
Hide
Joerg Schaible added a comment -

Hi Adrian,

thanks a lot, I've applied your patch.

Regards,
Jörg

Show
Joerg Schaible added a comment - Hi Adrian, thanks a lot, I've applied your patch. Regards, Jörg
Hide
Joerg Schaible added a comment -

Closing issues before next release.

Show
Joerg Schaible added a comment - Closing issues before next release.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: