in jelly-tags/xml/src/main/java/org/apache/commons/jelly/tags/xml/ReplaceNamespaceTag.java [43:103]
public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
final String fromURI = (fromNamespace != null) ? fromNamespace : "";
final String toURI = (toNamespace != null) ? toNamespace : "";
XMLOutput newOutput = output;
if (!toURI.equals(fromURI)) {
newOutput = new XMLOutput(output) {
@Override
public void startElement(String uri, String localName, String qName, Attributes atts)
throws SAXException {
super.startElement(replaceURI(uri), localName, qName, replaceURI(atts));
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
super.endElement(replaceURI(uri), localName, qName);
}
@Override
public void startPrefixMapping(String prefix, String uri)
throws SAXException {
super.startPrefixMapping(prefix, replaceURI(uri));
}
private String replaceURI(String uri) {
String newUri = uri;
if (fromURI.equals((uri != null) ? uri : "")) {
newUri = toURI;
}
return newUri;
}
private Attributes replaceURI(Attributes atts) {
AttributesImpl newAttsImpl = new AttributesImpl();
for (int i = 0; i < atts.getLength(); i++) {
// Normally attributes don't have namespaces
// But may have (only if on form prefix:attr) ?
// So, we'll only replace if needed
String QName = atts.getQName(i);
String newUri = atts.getURI(i);
int idx = QName.indexOf(':');
if (idx >= 0) {
newUri = replaceURI(newUri);
}
newAttsImpl.addAttribute(newUri, atts.getLocalName(i), atts.getQName(i),
atts.getType(i), atts.getValue(i));
}
return newAttsImpl;
}
};
}
invokeBody(newOutput);
}