in batik-dom/src/main/java/org/apache/batik/dom/AbstractDocument.java [1038:1246]
public Node renameNode(Node n, String ns, String qn) {
AbstractNode an = (AbstractNode) n;
if (an == getDocumentElement()) {
throw createDOMException(DOMException.NOT_SUPPORTED_ERR,
"rename.document.element",
new Object[] {});
}
int nt = n.getNodeType();
if (nt != Node.ELEMENT_NODE && nt != Node.ATTRIBUTE_NODE) {
throw createDOMException(DOMException.NOT_SUPPORTED_ERR,
"rename.node",
new Object[] {nt,
n.getNodeName() });
}
if (xmlVersion.equals(XMLConstants.XML_VERSION_11)
&& !DOMUtilities.isValidName11(qn)
|| !DOMUtilities.isValidName(qn)) {
throw createDOMException(DOMException.NOT_SUPPORTED_ERR,
"wf.invalid.name",
new Object[] { qn });
}
if (n.getOwnerDocument() != this) {
throw createDOMException(DOMException.NOT_SUPPORTED_ERR,
"node.from.wrong.document",
new Object[] {nt,
n.getNodeName() });
}
int i = qn.indexOf(':');
if (i == 0 || i == qn.length() - 1) {
throw createDOMException(DOMException.NAMESPACE_ERR,
"qname",
new Object[] {nt,
n.getNodeName(),
qn });
}
String prefix = DOMUtilities.getPrefix(qn);
if (ns != null && ns.length() == 0) {
ns = null;
}
if (prefix != null && ns == null) {
throw createDOMException(DOMException.NAMESPACE_ERR,
"prefix",
new Object[] {nt,
n.getNodeName(),
prefix });
}
if (strictErrorChecking) {
if (XMLConstants.XML_PREFIX.equals(prefix)
&& !XMLConstants.XML_NAMESPACE_URI.equals(ns)
|| XMLConstants.XMLNS_PREFIX.equals(prefix)
&& !XMLConstants.XMLNS_NAMESPACE_URI.equals(ns)) {
throw createDOMException(DOMException.NAMESPACE_ERR,
"namespace",
new Object[] {nt,
n.getNodeName(),
ns });
}
}
String prevNamespaceURI = n.getNamespaceURI();
String prevNodeName = n.getNodeName();
if (nt == Node.ELEMENT_NODE) {
Node parent = n.getParentNode();
AbstractElement e = (AbstractElement) createElementNS(ns, qn);
// Move event handlers across
EventSupport es1 = an.getEventSupport();
if (es1 != null) {
EventSupport es2 = e.getEventSupport();
if (es2 == null) {
AbstractDOMImplementation di
= (AbstractDOMImplementation) implementation;
es2 = di.createEventSupport(e);
setEventsEnabled(true);
e.eventSupport = es2;
}
es1.moveEventListeners(e.getEventSupport());
}
// Move user data across
e.userData = e.userData == null
? null
: (HashMap) an.userData.clone();
e.userDataHandlers = e.userDataHandlers == null
? null
: (HashMap) an.userDataHandlers.clone();
// Remove from parent
Node next = null;
if (parent != null) {
n.getNextSibling();
parent.removeChild(n);
}
// Move child nodes across
while (n.getFirstChild() != null) {
e.appendChild(n.getFirstChild());
}
// Move attributes across
NamedNodeMap nnm = n.getAttributes();
for (int j = 0; j < nnm.getLength(); j++) {
Attr a = (Attr) nnm.item(j);
e.setAttributeNodeNS(a);
}
// while (nnm.getLength() > 0) {
// Attr a = (Attr) nnm.item(0);
// e.setAttributeNodeNS(a);
// }
// Reinsert into parent
if (parent != null) {
if (next == null) {
parent.appendChild(e);
} else {
parent.insertBefore(next, e);
}
}
fireUserDataHandlers(UserDataHandler.NODE_RENAMED, n, e);
if (getEventsEnabled()) {
MutationNameEvent ev =
(MutationNameEvent) createEvent("MutationNameEvent");
ev.initMutationNameEventNS(XMLConstants.XML_EVENTS_NAMESPACE_URI,
"DOMElementNameChanged",
true, // canBubbleArg
false, // cancelableArg
null, // relatedNodeArg
prevNamespaceURI,
prevNodeName);
dispatchEvent(ev);
}
return e;
} else {
if (n instanceof AbstractAttrNS) {
AbstractAttrNS a = (AbstractAttrNS) n;
Element e = a.getOwnerElement();
// Remove attribute from element
if (e != null) {
e.removeAttributeNode(a);
}
// Update name
a.namespaceURI = ns;
a.nodeName = qn;
// Reinsert attribute into element
if (e != null) {
e.setAttributeNodeNS(a);
}
fireUserDataHandlers(UserDataHandler.NODE_RENAMED, a, null);
if (getEventsEnabled()) {
MutationNameEvent ev =
(MutationNameEvent) createEvent("MutationNameEvent");
ev.initMutationNameEventNS(XMLConstants.XML_EVENTS_NAMESPACE_URI,
"DOMAttrNameChanged",
true, // canBubbleArg
false, // cancelableArg
a, // relatedNodeArg
prevNamespaceURI,
prevNodeName);
dispatchEvent(ev);
}
return a;
} else {
AbstractAttr a = (AbstractAttr) n;
Element e = a.getOwnerElement();
// Remove attribute from element and create new one
if (e != null) {
e.removeAttributeNode(a);
}
AbstractAttr a2 = (AbstractAttr) createAttributeNS(ns, qn);
// Move attribute value across
a2.setNodeValue(a.getNodeValue());
// Move user data across
a2.userData = a.userData == null
? null
: (HashMap) a.userData.clone();
a2.userDataHandlers = a.userDataHandlers == null
? null
: (HashMap) a.userDataHandlers.clone();
// Reinsert attribute into parent
if (e != null) {
e.setAttributeNodeNS(a2);
}
fireUserDataHandlers(UserDataHandler.NODE_RENAMED, a, a2);
if (getEventsEnabled()) {
MutationNameEvent ev
= (MutationNameEvent) createEvent("MutationNameEvent");
ev.initMutationNameEventNS(XMLConstants.XML_EVENTS_NAMESPACE_URI,
"DOMAttrNameChanged",
true, // canBubbleArg
false, // cancelableArg
a2, // relatedNodeArg
prevNamespaceURI,
prevNodeName);
dispatchEvent(ev);
}
return a2;
}
}
}