History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: XSTR-429
Type: Improvement Improvement
Status: Closed Closed
Resolution: Fixed
Priority: Minor Minor
Assignee: Joerg Schaible
Reporter: Adrian Wilkins
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
XStream

Allow use of custom friendly naming with SaxWriter and TraxSource

Created: 13/Sep/07 08:54 AM   Updated: 25/Feb/08 05:01 PM
Component/s: IO
Affects Version/s: 1.2.1, 1.2.2
Fix Version/s: 1.3

File Attachments: 1. File xstream-patch.diff (3 kb)


JDK version and platform: Sun 1.5 Windows


 Description  « Hide
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("$", "_")));
}}}

 All   Comments   Change History      Sort Order: Ascending order - Click to sort in descending order
Joerg Schaible - 15/Sep/07 08:33 AM
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!

Adrian Wilkins - 08/Oct/07 06:48 AM
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.

Adrian Wilkins - 20/Dec/07 09:14 PM
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());
                 }
 

Joerg Schaible - 22/Dec/07 02:42 PM
Hi Adrian,

thanks a lot, I've applied your patch.

Regards,
Jörg

Joerg Schaible - 25/Feb/08 05:01 PM
Closing issues before next release.