From a0a7d6212299a1723cf6c0666f9838136934cc07 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Sun, 28 Aug 2011 20:18:55 +0200 Subject: Avoid pollution by redundant xmlns attributes. --- org/madore/damlengine/DamlEngine.java | 1 + org/madore/damlengine/TodoAttr.java | 6 ++++++ org/madore/damlengine/TodoWrefAttr.java | 2 +- org/madore/damlengine/TodoXemptyAttr.java | 2 +- org/madore/damlengine/TodoXmlnsAttr.java | 28 ++++++++++++++++++++++++++++ 5 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 org/madore/damlengine/TodoXmlnsAttr.java diff --git a/org/madore/damlengine/DamlEngine.java b/org/madore/damlengine/DamlEngine.java index da49aeb..6581f37 100644 --- a/org/madore/damlengine/DamlEngine.java +++ b/org/madore/damlengine/DamlEngine.java @@ -13,6 +13,7 @@ import org.apache.xerces.jaxp.DocumentBuilderFactoryImpl; public final class DamlEngine { + public static final String XMLNS_NS = XMLConstants.XMLNS_ATTRIBUTE_NS_URI; public static final String XML_NS = XMLConstants.XML_NS_URI; public static final String XHTML_NS = "http://www.w3.org/1999/xhtml"; public static final String DAML_NS = "http://www.madore.org/~david/NS/daml/"; diff --git a/org/madore/damlengine/TodoAttr.java b/org/madore/damlengine/TodoAttr.java index b88dc53..8e672d0 100644 --- a/org/madore/damlengine/TodoAttr.java +++ b/org/madore/damlengine/TodoAttr.java @@ -21,6 +21,9 @@ public abstract class TodoAttr extends TodoItem { damlAttrFactories.put("wref", new TodoWrefAttr.Factory()); } + protected final static Factory xmlnsAttrFactory + = new TodoXmlnsAttr.Factory(); + protected final Attr attr; protected final Element owner; @@ -38,6 +41,9 @@ public abstract class TodoAttr extends TodoItem { String nsuri = attr.getNamespaceURI(); if ( nsuri != null && nsuri.equals(DamlEngine.DAML_NS) ) factory = damlAttrFactories.get(attr.getLocalName()); + else if ( nsuri != null && nsuri.equals(DamlEngine.XMLNS_NS) ) + // Note to self: even the plain "xmlns" attribute has this namespace + factory = xmlnsAttrFactory; if ( factory == null ) return null; return factory.newItem(attr, owner, ctx, caller); diff --git a/org/madore/damlengine/TodoWrefAttr.java b/org/madore/damlengine/TodoWrefAttr.java index df30d99..d9b8950 100644 --- a/org/madore/damlengine/TodoWrefAttr.java +++ b/org/madore/damlengine/TodoWrefAttr.java @@ -42,7 +42,7 @@ public class TodoWrefAttr extends TodoAttr { String targetFragment = "d." + wrefYearStr + "-" + wrefMonthStr + "-" + wrefDayStr + "." + wrefNumberStr + wrefSupplementStr; String target = targetFile + "#" + targetFragment; - this.owner.removeAttribute(this.attr.getName()); + this.owner.removeAttributeNode(this.attr); this.owner.setAttributeNS(null, "href", target); if ( ! this.owner.hasAttributeNS(null, "class") ) this.owner.setAttributeNS(null, "class", "weblog-internal-link"); diff --git a/org/madore/damlengine/TodoXemptyAttr.java b/org/madore/damlengine/TodoXemptyAttr.java index a9f7c94..332a63c 100644 --- a/org/madore/damlengine/TodoXemptyAttr.java +++ b/org/madore/damlengine/TodoXemptyAttr.java @@ -21,7 +21,7 @@ public class TodoXemptyAttr extends TodoAttr { @Override public void handle() { - this.owner.removeAttribute(this.attr.getName()); + this.owner.removeAttributeNode(this.attr); this.owner.appendChild(ctx.doc.createComment(" EMPTY ")); } diff --git a/org/madore/damlengine/TodoXmlnsAttr.java b/org/madore/damlengine/TodoXmlnsAttr.java new file mode 100644 index 0000000..81dafb5 --- /dev/null +++ b/org/madore/damlengine/TodoXmlnsAttr.java @@ -0,0 +1,28 @@ +package org.madore.damlengine; + +import org.w3c.dom.*; + +public class TodoXmlnsAttr extends TodoAttr { + + public static class Factory extends TodoAttr.Factory { + @Override + public TodoXmlnsAttr newItem(Attr attr, Element owner, + Context ctx, + TodoItem caller) { + return new TodoXmlnsAttr(attr, owner, ctx, caller); + } + } + + public TodoXmlnsAttr(Attr attr, Element owner, + Context ctx, + TodoItem caller) { + super(attr, owner, ctx, caller); + } + + @Override + public void handle() { + if ( this.attr.getName().equals("xmlns") ) + this.owner.removeAttributeNode(this.attr); + } + +} -- cgit v1.2.3