[jdom-interest] Element Reference from Attribute

James Strachan james at metastuff.com
Thu Nov 23 02:33:14 PST 2000


----- Original Message -----
From: "Jason Hunter" <jhunter at collab.net>
> > It also means that instances of
> > Attribute and other leaf nodes cannot be shared throughout a
> > tree which can have a big effect on performance and
> > memory consumption.
>
> They can't be shared as is, because a setValue() on one would wrongly
> affect the other.

Sure. For a singly linked tree it would make sense to use immutable
Attributes so they can easily be shared. For doubly linked trees,  where
sharing cannot ever be done, then it would make sense to use make these
mutable.

> I like the idea of adding a getParent() method to the major components
> because that also solves the problem of duplicate adds.

Sure - this is both useful and essential for doubly linked trees. For a
singly linked tree of immutable Attribute objects, sharing is a good thing
from a performance / memory perspective.

> That's where you add an attribute to another element but do not remove
> the original.  This causes problems when the new or old attribute
> changes -- because they're the same attribute.  This bit Andy while he
> was creating JATO, and he talked my ear off about it.

So we could do both solutions so everyone is happy ;-)

1. immutable Attribute class which can be shared across Elements and
Documents for singly linked trees to promote attribute instance reuse,
memory & speed efficency.

2. mutable Attribute class which maintains a reference to its parent Element
for use in doubly linked trees which is useful for people who mutate trees
in place (calling setValue() and the like) where performance is less of a
priority - easy navigation and in place mutation is more imperitive.

e.g. through 2 possible attribute implementations we can easily implement
both solutions...

/** Immutable attribute for use in singly linked trees to allow instance
reuse
   * and enumerations to be supported efficiently.
   * The setValue() method has been removed.
   */
public class Attribute extends Attribute {
    private String value;

    public String getValue() {
        return value;
    }
    ...
}

/** Mutable attribute, allows editing in place when in a doubly linked tree.
   * Also stores a reference to its parent Element so can be used in upward
XPath expressions
   */
public class AttributeNode extends Attribute implements Node {
    private Element parent;

    // mutable interface...
    public void setValue( String value ) {
        this.value = value;
    }

    // Node interface...
    public Element getParent() {
        return parent;
    }
}

Incidentally, rather than using the previously suggested naming
convention...

// singly linked tree..
org.jdom.Element
org.jdom.Attribute
...

and

// doubly linked tree...
org.jdom.NavElement
org.jdom.NavAttribute

I'm now favouring the use of Elliot's Node interface in the naming of the
implementations such that

org.jdom.ElementNode
org.jdom.AttributeNode
org.jdom.CommentNode
...
represent the doubly linked tree implementations where each class implements
the Node interface which has both getParent(), getValue() and XPath
selectNodes() and selectSingleNodes() methods.


J.

James Strachan
=============
email: james at metastuff.com
web: http://www.metastuff.com




If you are not the addressee of this confidential e-mail and any
attachments, please delete it and inform the sender; unauthorised
redistribution or publication is prohibited. Views expressed are those of
the author and do not necessarily represent those of Citria Limited.



More information about the jdom-interest mailing list