Hi Vivek

----- Original Message -----
From: "Vivek Sahasranaman" <[EMAIL PROTECTED]>
> Hi :
>   I'm trying to use a Dom4J DOM adorned with User defined objects
> (UserDataElement)
>   I tried to add an attribute to a UserDataElement (org.dom4j.util)
> after the DOM was created, and found that it added a "DefaultAttribute"
> rather than a "UserDataAttribute". Upon looking a bit at the source code,
> seems like the "getDocumentFactory() method that UserDataElement inherits
> from DefaultElement should be overriden so that it can provide it's own
> documentFactory. Can someone verify this is correct ? (Or do I have a
> version that is old ?)

I've investigated this issue quite a bit. I've tried to reproduce your
problem in the test case in src/test/org/dom4j/TestUserData in particular
cases testNewAddtions() and testNewDocument()

In both cases, without patching the code, I always get UserDataElement and
UserDataAttributes being created. So I don't think a patch is needed (more
on this later).

I'd appreciate seeing the code you use to create the new Attributes to try
reproduce the problem. Even better, if you could submit a patch to the
TestUserData JUnit test case that actually fails, then we'll be able to fix
it pretty quick.

The (slightly complex) reason I think this is all working fine is that when
adding new nodes to an existing Branch (Document or Element) then it uses
its own getDocumentFactory() method.

So

Element foo = ...;
Element bar = foo.addElement( "bar" );

So foo's internal getDocumentFactory() will be used to create the new nodes.
A Document has its own  DocumentFactory property it uses that can be changed
explicitly via setDocumentFactory(). Element objects by default will use the
DocumentFactory associated with their QNames if one is available or use the
singleton. This allows different DocumentFactory objects to be used for
different named elements. e.g.

<pizza>
    <cheese/>
    <mushroom/>
</pizza>

It may be that we want to use UserDataDocumentFactory for <pizza> elements
but use the CheeseDocumentFactory for <cheese> elements.

So if you create a Document from a custom DocumentFactory, then all nodes
added to it will use the new factory, provided that the DocumentFactory
associates itself with each QName it creates. This is what
UserDataDocumentFactory actually does. So the following... (extracting some
code from the TestUserData test case)

        DocumentFactory factory = UserDataDocumentFactory.getInstance();
        Document document = factory.createDocument();

        Element root = document.addElement( "root" );
        Element newElement = root.addElement( "foo1234" );
        root.addAttribute( "bar456", "123" );
        Attribute newAttribute = root.attribute( "bar456" );

Will make UserDataElement and UserDataAttribute objects.

Does that help clear this up?

James



_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


_______________________________________________
dom4j-dev mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-dev

Reply via email to