[jdom-interest] Re: Factories for Element creation

David Churchville dmc at clearlight.com
Tue Oct 31 18:01:49 PST 2000


I know this has been discussed a couple of times, and I see the rationale for directly creating Elements, etc., rather than the DOM style "Document.createElement(), Document.createAttribute(), etc."

However, I have found a compelling use for "custom" subclasses for Elements. We are trying to add application specific metadata to our elements, and without Factory capability, need to wrap each element with an ElementDecorator style class. This works, but adds some memory overhead, and generally isn't much fun.

It is clear that if you are creating documents from scratch, its kind of easy to just make a subclass of Element, and say "new CustomElement()". However, the main reason we want this stuff is to read existing documents, then as we process the documents, to annotate the elements/attributes with app-specific data.

What I propose to get this kind of functionality is to enhace the SAXBuilder/DOMBuilder classes to accept either a custom class name or a JDOMFactory interface and use these to create the "right" kind of Element on demand.



Consider an app-specific element I want (so that, for example, I can navigate my elements and assign some :

public class CustomElement extends Element {

String extraData;

public void setExtraData( String data) {...}

}

1) Custom class approach:

SAXBuilder myBuilder = new SAXBuilder();

myBuilder.setElementClass("com.bar.foo.CustomElement");


2) Factory approach:

SAXBuilder myBuilder = new SAXBuilder();

myBuilder.setElementFactory( new MyElementFactory());


public class MyElementFactory implements JDOMElementFactory {

public Element createElement() { return new CustomElement() ...}

public Element createElement( arg1, arg2.) { ...}

...

}



Has anyone thought about this approach? I think its a nice compromise between imposing a Factory style on JDOM (plus its a bit late now, anyway), and forcing users to "deal with it" by writing custom code for this case.

--Dave

David Churchville

dmc at clearlight.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20001031/fc6a3b2f/attachment.htm


More information about the jdom-interest mailing list