[jdom-interest] handling xml:space attribute
Gabor Greif
gabor at no.netopia.com
Mon Jul 3 04:42:54 PDT 2000
On Fri, Jun 23, 2000 18:53 Uhr, Jason Hunter <mailto:jhunter at collab.net>
wrote:
>> The names of the attributes should be xml:space and xml:lang.
>> This is just for the case the code contains "xml:language".
>> I hope to get the CVS tree today and test the fix.
>
>Fixed in the tree, and thanks for testing.
>
>-jh-
>
>
At last I could test the proposed fix and I am still not convinced ;-)
I still get an exception thrown.
<trace>
org.jdom.JDOMException: The name "" is not legal for JDOM/XML namespaces:
Namespace URIs must be non-null and non-empty Strings..: The name "" is not
legal for JDOM/XML namespaces: Namespace URIs must be non-null and
non-empty Strings..
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:243)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:298)
at
org.jdom.examples.io.JTreeOutputterDemo.doFile(JTreeOutputterDemo.java:364)
at
org.jdom.examples.io.JTreeOutputterDemo.<init>(JTreeOutputterDemo.java:184)
at
org.jdom.examples.io.JTreeOutputterDemo.main(JTreeOutputterDemo.java:91)
at sun.tools.debug.MainThread.run(Agent.java)
Root cause: org.jdom.IllegalNameException: The name "" is not legal for
JDOM/XML namespaces: Namespace URIs must be non-null and non-empty
Strings..
at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:1225)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:223)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:298)
at
org.jdom.examples.io.JTreeOutputterDemo.doFile(JTreeOutputterDemo.java:364)
at
org.jdom.examples.io.JTreeOutputterDemo.<init>(JTreeOutputterDemo.java:184)
at
org.jdom.examples.io.JTreeOutputterDemo.main(JTreeOutputterDemo.java:91)
at sun.tools.debug.MainThread.run(Agent.java)
</trace>
The relevant lines are from
org.jdom.input.SAXHandler.startElement:586 (around)
if (!attName.startsWith("xmlns")) {
name = attName;
int attSplit;
prefix = "";
if ((attSplit = name.indexOf(":")) != -1) {
prefix = name.substring(0, attSplit);
name = name.substring(attSplit + 1);
}
// Only put attribute in namespace if there is a prefix
if (prefix.equals("")) {
element.addAttribute(
new Attribute(name, atts.getValue(i)));
} else {
element.addAttribute(
new Attribute(name, // <<<<<<<<<<<<
prefix,
getNamespaceURI(prefix),
atts.getValue(i)));
}
}
The marked line throws. This is because getNamespaceURI(prefix) returns an
empty string for "xml" namespace.
If I read this
http://www.w3.org/TR/REC-xml-names/
correctly, the below section defines that getNamespaceURI("xml") should
return
"http://www.w3.org/XML/1998/namespace"
<quote>
Namespace Constraint: Prefix Declared
The namespace prefix, unless it is xml or xmlns, must have been declared in
a namespace declaration attribute in either the start-tag of the element
where the
prefix is used or in an an ancestor element (i.e. an element in whose
content the prefixed markup occurs). The prefix xml is by definition bound
to the namespace
name http://www.w3.org/XML/1998/namespace. The prefix xmlns is used only
for namespace bindings and is not itself bound to any namespace name.
</quote>
So probably getNamespaceURI should be fixed accordingly. The other option
that comes to my mind is to add
} else if (prefix.equals("xml")) {
element.addAttribute(
new Attribute(name,
"xml",
"http://www.w3.org/XML/1998/namespace",
atts.getValue(i)));
before the mark above.
However I believe fixing the getNamespaceURI (resp. getURI) methods is the
correct solution.
Another problem that popped up along with the above change:
XMLOutputter seems to produce the below
<dialog name="large dialog">
; <number name="okButton" xmlns:xml="http://www.w3.org/XML/1998/namespace"
xml:space="preserve">3</number>
; <boolean name="hasCloseBox">true</boolean>
</dialog>
on this SAXBuilder input:
<dialog name="large dialog">
<number name="okButton" xml:space="preserve">3</number>;
<boolean name="hasCloseBox">true</boolean>
</dialog>
Note the redundant xmlns:xml attribute and the definitely incorrect
semicolons in the output. The former could be suppressed by testing for a
special case in XMLOutputter but I have no idea where the semicolons come
from.
I am using the 30 Jun 2000 CVS sources.
Thanks for the responsiveness,
Gabor
More information about the jdom-interest
mailing list