[jdom-interest] CDATA and Text

Jason Hunter jhunter at acm.org
Sun Apr 7 01:44:02 PST 2002

> > Made it so "CDATA extends Text" so now you can look just for Text nodes
> > in content and don't need to differentiate CDATA sections if you don't
> > want to.  This does require "instanceof CDATA" to come before "instanceof
> > Text" now.  Watch out for that subtle bug.
> I really think this is the wrong way to go. There has been a proposal to
> cancel the CDATA class, and add a flag to Text that determines if it's a
> CDATA (i.e. text.isCDATA() and text.setCDATA(boolean)). IMHO it is a better
> solution, because many programmer don't care about the type of the text.

When iterating over content, you can say "instanceof Text" if you don't
care.  I'm not sure how combining the two into one class helps that.

My own thoughts for keeping CDATA separate is that that's how people
think of it.  You create a new CDATA section.  You don't create a new
Text section with the flag CDATA set to true.

new CDATA("a > b");


new Text("a > b").setCDATA(true);

Especially when few people constructing documents will even say "new
Text()" ever since there are String-based shortcuts.

> That bug you mentioned can be really hard to find. One will have to search
> for all the times he used "instanceof CDATA" and "instanceof Text", and
> possibly other thing. I might be tricky.
> Cancelling the CDATA class, OTOH, will make the compiler find all those cases
> for the programmer.

I'm more worried about getting the API right than in making sure
existing code is easy to upgrade.  However, there's a good point that
someone doing instanceof checks even starting with the new API might
check for Text and then check for CDATA and be surprised the CDATA check
never goes true.  That's a gotcha that never goes away!

Other things to consider?


More information about the jdom-interest mailing list