[jdom-interest] Element Reference from Attribute

Patrick Dowler Patrick.Dowler at nrc.ca
Fri Nov 17 15:05:16 PST 2000


On Fri, 17 Nov 2000, Jason Hunter wrote:
> > > No, XPath doesn't require that a user can find the Element parent of
> > > a returned Attribute.  But it sure does sound useful to me,
> > > personally.
> > 
> > I agree that it sounds useful. Jason can decide if it is the 
> > killer use case :-)
> 
> I've been thinking about this, and it raises an interesting issue.  What
> should the process be to move attributes from one element to another? 
> Right now one could have the same Attribute object added to two
> different elements, because there's no way for the API to know if an
> Attribute is "owned" already or not.  
> 
> The problem with this is that setting the value on the new attribute
> would also set it on the old attribute at the same time, since they're
> the same object.
> 
> Solutions are to:
> 
> 1) Leave it alone with this behavior.
> 
> 2) Clone any attribute added to an Element.  (No good, because
> addAttribute(new Attribute(...)) would do the clone unnecessarily.)
> 
> 3) Make Attribute immutable.  Remove all set methods.  Then the
> Attribute may be added twice, but no one will care.
> 
> 4) Make Attribute have a parent and use the parent variable to enforce
> the same rules for moving attribs as we have for Element.

More often than not, java objects are mutable and one may keep several
references to them. As such, programmers have to knwo about this possibility
and handle it themselves, IMO. There is alot of efficiency in sharing references
to objects and people should be able to use that when they know it is safe.

Immutable objects are quite a pain to use. I tried to do something mathematical
with objects (interval arithmetic) and immutable is very costly once you do start
changing values.

So, I'd say (1) leave it alone. If people use el.addAttribute(a) then they have to know
it is shallow. Most of the time that is what you want. 

For a "transformer" implementation (say XSLT kind of thing) does the Document
get changed in place or is a new one created? The former would obviously
be hazardous with shared references to objects that could be transformed...

I assume Builders don't figure out how to share (ie. intern) objects... although
maybe a "read-only" document could make use of such a thing?

-- 

Patrick Dowler
Canadian Astronomy Data Centre




More information about the jdom-interest mailing list