[jdom-interest] A whitespace proposal

Jason Hunter jhunter at acm.org
Sun Jun 4 13:05:46 PDT 2000


I had a two hour drive last night and thought about the whitespace issue
most of the way.  Here's what I've come up with.  Brett also supports
this.

getContent()
  Returns the content with the surrounding whitespace trimmed.  This is
what I believe to be the common case, and JDOM should work best for the
common case.  It also allows Brett's book to remain current, and avoids
the stream of 4,000 people a month buying the book and getting confused.

getContent(true)
  Returns the content with the whitespace preserved.  I have grown to
prefer the boolean because we need something similar for
getMixedContent().

getMixedContent()
  Returns the mixed content with surrounding whitespace trimmed.  So for

<elt>
  <!-- comment -->
  <?pi data?>
  Text
</elt>

this method will return a List containing a Comment, a PI, and a String.

getMixedContent(true)
  Returns the mixed content including surrounding whitespace.  For the
previous example it returns five objects: "\n  ", Comment, "\n  ", PI,
"\n  Text\n".  This is ugly, but some people may want this.

Note that Document.getMixedContent(true) will need to be added as well.

The normal XMLOutputter will use getMixedContent() to print as usual. 
We may want a second outputter (LiteralXMLOutputter?) that uses
getMixedContent(true) and will not do any additional indentation or \n
adds, the result being an output document as close as possible to the
input document.

When I thought about how to deal with mixed content it became clear to
me that this is the best proposal.  A default of returning whitespace
would make getMixedContent() very annoying for the common case.  

We're still fully spec compliant because the XML spec says parsers must
make the whitespace available.  We do make it available.  (In my
opinion, the spec says parsers must preserve whitespace for the same
reason the IRS says you must save your old tax returns -- because
sometimes it's important.  The information doesn't need to be "in your
face" though.)

-jh-





More information about the jdom-interest mailing list