[jdom-interest] Reference comparisons in SAXHandler.startElement

Jason Hunter jhunter at collab.net
Mon Mar 12 16:05:12 PST 2001


Keith_Hamasaki.DATAHOUSE at datahouse.com wrote:
> 
> In the startElement method of SAXHandler (SAXBuilder.java) there are
> reference comparisons on local name vs. qualified name, both in the passed
> parameters and in the values returned by the Attributes object.  In at
> least one implementation (Resin XML) this results in a
> StringIndexOutOfBoundsException when the strings returned from the
> Attributes object are .equal() but not ==, and control passes to the
> following line:
> 
> String attPrefix = attQName.substring(0, attQName.indexOf(":"));
> 
> which is line 730 of the current CVS file.  It seems that since the
> implementation of the Strings in question is dependent on the parser being
> used, the comparison should be .equals(), or another way should be found to
> formulate this condition.  Changing the comparison to .equals() fixes the
> StringIndexOutOfBoundsException that we are seeing.  Unless, of course,
> those strings are required to be reference equal, and if this is the case
> then could someone please point me to where this is documented (I looked at
> the SAX Javadoc but couldn't see anything) so that I can report this to the
> parser implementers?

Try changing SAXBuilder to turn on the string interning feature in the
SAX parser.  See http://www.megginson.com/SAX/Java/features.html for
details.  The feature name is
http://xml.org/sax/features/string-interning.  I bet that makes things
work.

A possibly better long-term solution is to turn *off* the interning
feature and do an intern() call in the builder (before the comparison
check).  That way we are sure to intern all names even when building
from scratch.  It may not have a big effect since most names are given
as constants and thus already intern'd, but it's no worse and could be
better.  The reason I'm not 100% fond of this is that Xerces has been
optimized to do a faster custom intern than String.intern(), so relying
on String.intern() will slow down a Xerces build.  I had this on my list
of things to investigate before making a change.  If someone else wants
to conduct the test, that'd be fine.  Comments also welcome.  I'll add a
note in the TODO about this.

-jh-



More information about the jdom-interest mailing list