<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7638.1">
<TITLE>RE: [jdom-interest] The default namespace behaviour </TITLE>
</HEAD>
<BODY>
<DIV dir=ltr><FONT face=Arial color=#000000 size=2>> <FONT
face="Times New Roman">JDOM seems to have no concept of a default namespace
other than perhaps the</FONT></FONT></DIV>
<DIV dir=ltr><FONT face=Arial color=#000000 size=2><FONT
face="Times New Roman">> document builder. This seems to very much be
erroneous.</FONT></FONT></DIV>
<DIV dir=ltr><FONT size=2>Not really. JDOM does have a concept of a default
namespace. It's a namespace without a prefix. You are correct that default
namespace inheritance is not directly supported except that in output
XMLOutputter is smart enough to not define a namespace over and over
again (for input, the SAX parser handles this detail and JDOM actually has
no way of knowing where a namespace gets set or how). I think this is
appropriate given that JDOM is an object model and the namespace is a
property of each object.</FONT></DIV>
<DIV dir=ltr><FONT size=2></FONT> </DIV>
<DIV dir=ltr><FONT size=2>> Constructing the first XML example in memory
with:<BR>> Element easy = new Element("easy", "short");<BR>> Element b =
new Element("b");<BR>> Element c = new Element("c");<BR>> ...<BR>>
easy.addContent(b);<BR>> b.addContent(c);<BR>> constructs b and c with no
namespace which is not as it should be. </FONT></DIV>
<DIV dir=ltr><FONT size=2>But that's what you asked it to do when you
called new Element("b") and new Element("c"). If you want them to have the
namespace URI "short", then just use the appropriate contructor. There might be
an argument to be made that, under certain circumstances, Elements without a
namespace should inherit the namspace of their parent, I personally think the
resulting code would be significantly more unreadable. It's obvious what's
going on when the lines are one after the other (as in your example), but JDOM
doesn't require that. If those three constructors were in different methods, it
becomes less clear what namespace you mean which elements to be in. The
difference is really a single line of code to create the Namespace and a few
extra characters as the namespace is explicitly passed to each appropriate
constructor. </FONT><FONT size=2>I would write the (full) code to generate the
first example as:</FONT></DIV>
<DIV dir=ltr><FONT size=2></FONT> </DIV>
<DIV dir=ltr><FONT size=2> Namespace
nsShort =
Namespace.getNamespace("short");<BR>
Namespace nsWeird = Namespace.getNamespace("e",
"weird");<BR>
<BR> Document doc = new
Document();<BR> Element easy = new
Element("easy", nsShort);<BR>
doc.setRootElement(easy);<BR> Element
b = new Element("b", nsShort);<BR>
Element c = new Element("c",
nsShort);<BR>
easy.addContent(b);<BR>
b.addContent(c);<BR>
<BR> Element d = new Element("d",
nsShort);<BR> Element f = new
Element("f", nsWeird);<BR>
f.addContent(new Element("g",
nsShort));<BR>
d.addContent(f);<BR>
easy.addContent(d);</FONT></DIV>
<DIV dir=ltr><FONT size=2></FONT> </DIV>
<DIV dir=ltr><FONT size=2>In general, I don't really understand what it is
you're trying to accomplish. From your examples, it would appear that you want
to move the element with the localName "easy" and namespace URI "short" to the
namespace URI "ReallyLongNamespace" and move the remaining elements in the
"short" namespace to no namespace. If so, you could do something
like:</FONT></DIV>
<DIV dir=ltr><FONT size=2></FONT> </DIV>
<DIV dir=ltr> for (Iterator it =
doc.getDescendants(); it.hasNext();)
{<BR> Object o
=
it.next();<BR>
if (o instanceof Element)
{<BR>
Element el = (Element)
o;<BR>
if ("short".equals(el.getNamespaceURI()))
{<BR>
if ("easy".equals(el.getName()))
{<BR>
el.setNamespace(Namespace.getNamespace("a",
"ReallyLongNamespace"));<BR>
} else
{<BR>
el.setNamespace(Namespace.NO_NAMESPACE);<BR>
}<BR>
}<BR>
}<BR> }</DIV>
<DIV dir=ltr><FONT size=2></FONT> </DIV>
<DIV dir=ltr><FONT size=2>That doesn't seem like too much code.</FONT></DIV>
<DIV dir=ltr><FONT size=2></FONT> </DIV>
<DIV dir=ltr><FONT size=2>I strongly disagree with your sense that:</FONT></DIV>
<DIV dir=ltr><FONT
size=2>a.setNamespace(namespace.getNamespace("ReallyLongNamespace"));</FONT></DIV>
<DIV dir=ltr><FONT size=2></FONT> </DIV>
<DIV dir=ltr><FONT size=2>should set the namespace for all child elements that
use the same default namespace as a. This would be a big leap for JDOM to make.
Since it's entirely legitimate to have different default namespaces at different
levels, having JDOM guess which ones you wanted to change would invariably be
incorrect (in other words, what if I really wanted setNamespace() to just set
that element's namespace). Writing the getDescendents()-using code above or even
better, as Mr. Huffman suggested, a custom JDOMFactory would be the best way to
go about explictingly specifying what transformation you wanted to do.
<DIV dir=ltr><FONT size=2></FONT> </DIV>
<DIV dir=ltr></FONT>
<HR tabIndex=-1>
<FONT face=Tahoma size=2><B>From:</B> jdom-interest-bounces@jdom.org on behalf
of Colin LeMahieu<BR><B>Sent:</B> Tue 6/6/2006 9:59 PM<BR><B>To:</B> 'Bradley S.
Huffman'<BR><B>Cc:</B> jdom-interest@jdom.org<BR><B>Subject:</B> RE:
[jdom-interest] The default namespace behaviour <BR></FONT><BR></DIV></DIV>
<DIV>
<P><FONT size=2>I'm thinking more along the lines of, the user types an XML
file:<BR><easy
xmlsn="short"><BR>
<b><BR>
<c/><BR>
</b><BR>
<d><BR>
<e:f
xmlns:e="weird"><BR>
<g/><BR>
</e:f><BR>
</d><BR></easy><BR><BR>And I'm looking to convert it
to:<BR><a:easy
xmlns:a="ReallyLongNamespace"><BR>
<b><BR>
<c/><BR>
</b><BR>
<d><BR>
<e:f
xmlns:e="weird"><BR>
<g/><BR>
</e:f><BR>
</d><BR></a><BR><BR>As I see it right now, with the current JDOM
model there's no way to easily<BR>just change the namespace of a and affect
elements b, c, d, and g as it<BR>should.<BR><BR>JDOM seems to have no concept of
a default namespace other than perhaps the<BR>document builder. This seems
to very much be erroneous.<BR><BR>Constructing the first XML example in memory
with:<BR>Element easy = new Element("easy", "short");<BR>Element b = new
Element("b");<BR>Element c = new
Element("c");<BR>...<BR>easy.addContent(b);<BR>b.addContent(c);<BR><BR>constructs
b and c with no namespace which is not as it should be. My
quote<BR>earlier is from someone claiming that this is the correct behaviour,
"if you<BR>don't know what namespace an element is in, you don't know what
element<BR>you're creating" I say it's incorrect, or otherwise tell me a
way to
do:<BR><BR>a.setNamespace(namespace.getNamespace("ReallyLongNamespace"));<BR><BR>and
have it affect b, c, d, and a possible g somewhere down the XML tree<BR>that
would inherit the default namespace from a.<BR><BR>----------------<BR>Colin
LeMahieu<BR>Phone: 408-499-5269<BR>Fax: 267-989-4575<BR>Email:
clemahieu@gmail.com<BR><BR><BR>-----Original Message-----<BR>From: Bradley S.
Huffman [<A
href="mailto:hip@cs.okstate.edu">mailto:hip@cs.okstate.edu</A>]<BR>Sent:
Tuesday, June 06, 2006 13:30<BR>To: Colin LeMahieu<BR>Cc:
jdom-interest@jdom.org<BR>Subject: Re: [jdom-interest] The default namespace
behaviour<BR><BR>"Colin LeMahieu" writes:<BR><BR>> Essentially my problem is
that I have a lot of longhand names and<BR>namespaces<BR>> and a user will
only pick a subset of them to use in a particular<BR>document.<BR>> I would
like to allow shorthand names in different namespaces and then<BR>> translate
them to the longhand forms. The document would then
be<BR>processed<BR>> further.<BR><BR>How about a custom JDOMFactory that
overrides the element
methods.<BR><BR>Brad<BR><BR>_______________________________________________<BR>To
control your jdom-interest membership:<BR><A
href="http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com">http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com</A><BR></FONT></P></DIV>
</BODY>
</HTML>