[jdom-interest] getElementById

Hallvard Tr?tteberg hal at idi.ntnu.no
Wed Jan 30 14:20:12 PST 2002


Hi,

> To support this feature, JDOM needs to know that a given
> attribute is an ID. The latest JDOM (in CVS) supports attribute types. So
if you have
> a DTD, you can declare some attributes to be IDs and JDOM will save this
information.

To make this useful, an IDREF attribute should be able to include the
reference to the pointed to element, to make it possible to follow IDREFs.
Using a lookup table in the document is an alternative, although it would be
a lot slower.

> Now accessing an element by its ID is just a matter of building a look-up
> table when parsing the document. This feature is not included in
> the default JDOM Document and Element objects because it would consume
extra
> CPU and memory every time a document is parsed or a new Element is added
> to a Document.
>
> So, the best way to support this feature is to subclass Document
> and Element and create a new JDOMFactory implementation to return these
> subclasses.

I have made such a factory and found what I consider a problem with JDOM:
The factory cannot decide which Attribute (sub)class to instantiate based on
the Element it will be attached to. Consider a factory that knows that the
"x" attribute of "y" elements is an IDREF and hence wants to create an
IdrefAttribute (extends Attribute) instance, which has an Element idRef
field. However, when the Attribute needs to be created, it doesn't know
which element it should be added to, because 1) the element to add to isn't
part of the factory method signature/call or 2) there is no guarantee that
the Attribute will be added to the last Element created. In my factory code
I ended up with extending the Element.setAttribute(Attribute) method to test
for ID and IDREF attributes and make a new Attribute instance of the
appropriate class.

BTW, a similar problem exists with documents, doc types and root elements:
You cannot instantiate a special Document class for specific root elements,
or create special Elements for a specific Document class, since the order of
creation is not defined. Given that the factory has a document creation
method with both docType and rootElement attributes, I would have expected
the builder to delay the Document creation until these had been parsed, but
it didn't, this specific factory method was never called.

To handle these problems I suggest:
- changing the factory methods for creating attributes to also include the
Element it will be added to
- adding a field in the builder classes that defines the creation order of
documents and root elements and the root's content, e.g. DOC_ROOT_CONTENT,
ROOT_DOC_CONTENT or even CONTENT_ROOT_DOC. Alternatively, we could have
different builders that implement each.

> A example of such a factory will shortly be available in the contrib
package.
> If you are interested in this sample code, just drop me a mail and I'll
send
> you the code.

If you're interested in my experiments, I can send the code to you.

Hallvard




More information about the jdom-interest mailing list