Affects Version/s: Nightly Builds
Fix Version/s: None
Operating System: other
In an attempt to reuse a digester to parse multiple
files, I found that I was always getting the contents
of the first file I parsed.
It turns out the that what happens, is that
Digester.endDocument() handles the end-of-document event.
At the end of the method, Digester.clear() is called.
You'll notice that clear() actually clears all the rules!
So the next time you parse, no rules are fired, and the
parser returns normally (not signalling any exceptions
which would be caught in the test-case). The private
field 'root' remains unchanged from the original parse,
and the originally parsed (root) object is returned.
It was a simple fix...patch is included for both
RuleTestCase.java and Digester.java. Here are some notes
on the patch, since functionality changed slightly.
The method 'clear()' has been changed to not clear the
rules. This keeps with the stated documentation that clear()
which suggests that only clears the object stack. Additionally,
I extended clear() to set root to null. This seems to make
sense from the point of view of a user of the Digester API.
If I want to "clear" my digester, I also want the root to be
set to null, this frees a reference to my root object to allow
it to (possibly) be garbage collected.
I've refactored the functionality of the 'public clear()'
to a 'private clear(boolean)' method which allows the caller
to determine whether or not root should be cleared. This
way, endDocument() can call clear(false), and make sure that
root is not set to null.
Lastly, I've updated RuleTestCase to fail if the instances
returned for two subsequent parses reference the same object.