středa 11. listopadu 2009

... sleepless night full of programming and debugging

I didn't sleep last night. There is a nasty bug in the Java XML parser. Under certain circumstances (inherited default namespace with together with attributes present) the parser presents a rich imagination. In this case an element namespace properties are wrong. No namespace URI is assigned to the element and the null namespace is attached.
As a result when serialized extra namespace definition appears xmlns="" and as a result the element is not bound to any namespace. A colleague of mine performed an excellent analysis of the problem.

Here is a result of the sleepless night (traverseDom method walk the tree recursively calling processNode on each node:

traverseDom(doc.getDocumentElement(),
new NodeProcessor() {
public void processNode(Node n) {
//TODO watch this in case the java xml parser will get better in future versions
//TODO watch this for general purpose XML signature processing where such elements may be legal
//fix parser error - we can afford this as no element is unqualified
// in case there is a unqualified element with some attributes with default null namespace
// it is concidered as an error produced by buggy parser
if (n.getNodeType()==n.ELEMENT_NODE &&
n.getAttributes().getLength()!=0 &&
n.isDefaultNamespace(null) &&
n.getNamespaceURI()==null){
String prfx=n.lookupPrefix(n.getParentNode().getNamespaceURI())
n.getOwnerDocument().renameNode(n,
n.getParentNode().getNamespaceURI(), prfx+":"+n.getLocalName());
}
}
}, true);