Skip to Content.
Sympa Menu

xom-interest - [XOM-interest] preseve whitespace in serialization w/o inserting xml:space

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Adam Constabaris <adamc AT email.unc.edu>
  • To: xom-interest AT lists.ibiblio.org
  • Subject: [XOM-interest] preseve whitespace in serialization w/o inserting xml:space
  • Date: Thu, 09 Sep 2004 11:39:28 -0400

I'm writing an application which involves editing DocBook documents that includes a 'pretty print' function. The base Serializer class does a pretty good job of this, but I'd prefer tthe output preserve whitespace in some DocBook elements (such as programlisting and screen). The recommended (?) approach is to add the xml:space="true" attribute to the elements on which
I want to preserve space.

Currently, the approach I'm taking involves a subclass of Serializer that overrides write(Element) to check to see if the element's localName is on the list of whitespace preserving elements, and adds the xml:space="true" attribute to those elements. This does a great job of preserving the whitespace on the specified elements while indenting all the other ones.

However, I want the output to be validatable against the DocBook DTDs, which creates a problem for me, since the xml:space attribute is not defined in that DTD.

What would be ideal, from a programmatic standpoint, would be to be able to 'virtualize' the attribute -- pretend it's there for purposes of whitespace preservation, but don't actually output it. One way I could achieve this (I think) would be to call setPreserveSpace(true) on the TextWriter used by Serializer for the 'preserving' elements. However, the variable has private access to Serializer, so I'd have to instantiate my own in my subclass (alarm bell one!). At any rate, the TextWriter is package private, so this is definitely not possible for a Serializer subclass that doesn't live in the same package (alarm bell two). Even if it is, it looks like I might have to override more than the one method (alarm bell three).

So, I'm seeking advice (meaning that I don't have a well-formulated question
=):

I'm willing to cop that what I'm wanting to do (get the behavior without the markup) may not be the best approach in the first place. My reasons are that it is least surprising to my users -- I don't want serialization to alter the document in a radical fashion, beyond the prettification. My users are not, as a rule, interested in the gory details of DocBook to begin with. Secondly, getting the behavior without the markup requires the fewest changes elsewhere in the system. I'm all ears if anybody has any advice on this point.

I suppose I could pretty print the original document, adding the attributes, then read the result into a new XOM document, stripping the unrecognized-by-the-DTD attributes, and serialize that output without pretty printing, but I'm not sure even Rube Goldberg would be happy with that approach.

For gory technical reasons, I'm unable to use W3C DOM for pretty printing (bugs in pre-5.0 JVMs mean that the DOM building classes don't call resolveEntity() (!) and I need to use an entity resolver), and the application delivery method -- Web Start -- doesn't let me override core JAXP classes).


--
========================
Adam Constabaris
ITS Knowledge Management
========================




Archive powered by MHonArc 2.6.24.

Top of Page