[jdom-interest] Some thoughts on namespaces

Elliotte Rusty Harold elharo at metalab.unc.edu
Sat Mar 31 12:07:09 PST 2001

I've been looking over the namespace discussion and the code in 
Namespace.java, and that's jogged my memory about some design 
decisions we made almost a year ago. Initially JDOM had almost no 
namespace support. Now it has pretty much complete namespace support. 
Some of the decision that were made in the early days of moving from 
no or very limited namespace support to today may need to be 
revisited. Specifically,

1. We decided to use the flyweight pattern for namespaces to avoid 
adding an extra two strings to every element with a namespace. The 
namespaces were stored in a Hashtable.

2. This is why the equals() method determines equality based on 
URI+prefix, because otherwise the Hashmap won't work.

3. Namespace.NO_NAMESPACE was thrown into the pot as a convenience. 
It was never really necessary, but doesn't really hurt anybody 
either. It is consistent with the terminology of the namespaces 
specification. And it gives us the simplifying case of every Element 
and Attribute having a non-null associated namespace object.

I still want to make namespace equality depend only on URI, so I 
propose two possible solutions:

1. We forget about flyweights. Instead, when a URI or prefix is 
passed to one of the methods in the Namespace class, we simply call 
intern() before storing the string in the relevant fields. This would 
save most of the memory we currently save and be a lot simpler. It 
would even allow direct construction of Namespace objects instead of 
using factory methods with private constructors.

2. If we want to keep the flyweights, we wrap a special 
NamespaceReference object, used purely internally in the Namespace 
class, and not exposed in the client API, whose equality would depend 
on prefix as well as URI. We'd store namespace references in our 
Hashmap rather then storing the namespaces directly, but there'd be a 
straight-forward 1-1 mapping between NamespaceReference objects and 
Namespace objects. This allows us to retain the semantics we want for 
equals() (URI only) in the Namespace class).


| Elliotte Rusty Harold | elharo at metalab.unc.edu | Writer/Programmer |
|                  The XML Bible (IDG Books, 1999)                   |
|              http://metalab.unc.edu/xml/books/bible/               |
|   http://www.amazon.com/exec/obidos/ISBN=0764532367/cafeaulaitA/   |
|  Read Cafe au Lait for Java News:  http://metalab.unc.edu/javafaq/ |
|  Read Cafe con Leche for XML News: http://metalab.unc.edu/xml/     |

More information about the jdom-interest mailing list