[jdom-interest] Element Reference from Attribute

James Strachan james at metastuff.com
Fri Nov 17 10:59:09 PST 2000

----- Original Message -----
From: "Jason Hunter" <jhunter at collab.net>
> James Strachan wrote:
> As I've said earlier, I think SAXBuilder should be designed to be easily
> subclassed with protected createElement() style methods which can be
> intricately tied to the information SAXBuilder has, rather than try to
> make a generic plug-in factory model a la
> setElementFactory(ElementFactory).

Its the SAXHandler that needs these methods, as creates the Element /
Attribute objects. So you actually need to subclass SAXHandler and also
SAXBuilder, to create your new SAXHandler derivation.

> I think the subclass route has more power, and keeps us from needing six
> new *Factory classes.

I hear you and agree. I've posted to this list twice today a refactored
SAXBuilder / SAXHandler that does exactly this. I'm still waiting for it to
show up on the list though :-(

Deriving from SAXBuilder and SAXHandler to implement this kinda
functionality is OK.

However I'm warming to the idea of having a single SAXFactory class with all
the factory methods in it that SAXHandler requires together in one class
together with default implementations (createElement(), createAttribute,
etc.). Then just a single factory class needs to be subclassed to get the
behaviour people need. It is likely that if a user wants to do some custom
building, they may want to create more than just custom Elements, they may
want something else too (Attribute, Entity, CDATA, Namespace or whatever) so
it would make things simpler and easier to keep the factory methods in one

So the only API change would be something like the following... (I've
included a default Singleton pattern into the implementation, to keep the
perfomance obsessives folk happy ;-)

public class SAXBuilder {
    private SAXFactory factory;

    /** Default singleton implementation */
    private static final SAXFactory defaultFactory = new SAXFactory();

    public void setFactory(SAXFactory factory) {
        this.factory = factory;

    public SAXFactory getFactory() {
        if ( factory == null ) {
            return defaultFactory;
        return factory;


/** Default factory used by SAXBuilder / SAXHandler to
  * create JDOM objects from a SAX event stream
public class SAXFactory {

    public Element createElement(String localName, Namespace
elementNamespace) {
        return new Element( localName, elementNamespace );
    public Element createElement(String localName) {
        return new Element(localName);

    public Attribute createAttribute(Element element, String attLocalName,
String value, Namespace attNamespace) {
        return new Attribute(attLocalName, value, attNamespace);

    public Attribute createAttribute(Element element, String attLocalName,
String value) {
        return new Attribute(attLocalName, value);

    public ProcessingInstruction createProcessingInstruction(String target,
String data) {
        return new ProcessingInstruction(target, data);

    public CDATA createCDATA(String data) {
        return new CDATA(data);

    public DocType createDocType(String name, String publicId, String
systemId) {
        return new DocType(name, publicId, systemId);

    public Entity createEntity(String name) {
        return new Entity(name);

    public Comment createComment(String commentText) {
        return new Comment(commentText);


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