Permalink
Browse files

Reworked namespace delete to support deleting namespaces in use for c…

…x:namespace-delete
  • Loading branch information...
ndw committed Jan 28, 2012
1 parent 1d2d918 commit 687e5afecb23dd3855ab1cc59f85e4216bece606
@@ -914,7 +914,7 @@ public XdmNode read() throws SaxonApiException {
S9apiUtils.writeXdmValue(cfgProcessor, nodes, dest, node.getBaseURI());
doc = dest.getXdmNode();
if (excludeUris.size() != 0) {
- doc = S9apiUtils.removeNamespaces(cfgProcessor, doc, excludeUris);
+ doc = S9apiUtils.removeNamespaces(cfgProcessor, doc, excludeUris, true);
}
} catch (SaxonApiException sae) {
throw new XProcException(sae);
@@ -909,7 +909,7 @@ public void setDescription(XdmNode desc) {
}
}
}
- node = S9apiUtils.removeNamespaces(runtime, node, prefixes);
+ node = S9apiUtils.removeNamespaces(runtime, node, prefixes, true);
description.add(node);
}
}
@@ -58,7 +58,7 @@ public void run() throws SaxonApiException {
while (source.moreDocuments()) {
XdmNode doc = source.read();
runtime.finest(this, step.getNode(), "Namespace-delete step " + step.getName() + " read " + doc.getDocumentURI());
- doc = S9apiUtils.removeNamespaces(runtime, doc, excludeUris);
+ doc = S9apiUtils.removeNamespaces(runtime, doc, excludeUris, false);
result.write(doc);
}
}
@@ -80,7 +80,7 @@ public ReadableInline(XProcRuntime runtime, Vector<XdmValue> nodes, HashSet<Stri
S9apiUtils.writeXdmValue(runtime, nodes, dest, baseURI);
XdmNode doc = dest.getXdmNode();
- doc = S9apiUtils.removeNamespaces(runtime, doc, excludeNS);
+ doc = S9apiUtils.removeNamespaces(runtime, doc, excludeNS, true);
runtime.finest(null, null, "Instantiate a ReadableInline");
documents.add(doc);
} catch (SaxonApiException sae) {
@@ -204,29 +204,27 @@ public static InputSource xdmToInputSource(XProcRuntime runtime, XdmNode node) t
StringReader sr = new StringReader(serxml);
InputSource isource = new InputSource(sr);
isource.setSystemId(node.getBaseURI().toASCIIString());
- //SAXSource source = new SAXSource(isource);
- //return source;
return isource;
}
- public static XdmNode removeNamespaces(XProcRuntime runtime, XdmNode node, HashSet<String> excludeNS) {
- return removeNamespaces(runtime.getProcessor(), node, excludeNS);
+ public static XdmNode removeNamespaces(XProcRuntime runtime, XdmNode node, HashSet<String> excludeNS, boolean preserveUsed) {
+ return removeNamespaces(runtime.getProcessor(), node, excludeNS, preserveUsed);
}
- public static XdmNode removeNamespaces(Processor proc, XdmNode node, HashSet<String> excludeNS) {
+ public static XdmNode removeNamespaces(Processor proc, XdmNode node, HashSet<String> excludeNS, boolean preserveUsed) {
TreeWriter tree = new TreeWriter(proc);
tree.startDocument(node.getBaseURI());
- removeNamespacesWriter(tree, node, excludeNS);
+ removeNamespacesWriter(tree, node, excludeNS, preserveUsed);
tree.endDocument();
return tree.getResult();
}
- private static void removeNamespacesWriter(TreeWriter tree, XdmNode node, HashSet<String> excludeNS) {
+ private static void removeNamespacesWriter(TreeWriter tree, XdmNode node, HashSet<String> excludeNS, boolean preserveUsed) {
if (node.getNodeKind() == XdmNodeKind.DOCUMENT) {
XdmSequenceIterator iter = node.axisIterator(Axis.CHILD);
while (iter.hasNext()) {
XdmNode cnode = (XdmNode) iter.next();
- removeNamespacesWriter(tree, cnode, excludeNS);
+ removeNamespacesWriter(tree, cnode, excludeNS, preserveUsed);
}
} else if (node.getNodeKind() == XdmNodeKind.ELEMENT) {
boolean usesDefaultNS = ("".equals(node.getNodeName().getPrefix())
@@ -251,7 +249,7 @@ private static void removeNamespacesWriter(TreeWriter tree, XdmNode node, HashSe
excludeDefault = excludeDefault || ("".equals(pfx) && delete);
// You can't exclude the default namespace if it's in use
- if ("".equals(pfx) && usesDefaultNS) {
+ if ("".equals(pfx) && usesDefaultNS && preserveUsed) {
delete = false;
}
@@ -275,17 +273,38 @@ private static void removeNamespacesWriter(TreeWriter tree, XdmNode node, HashSe
String pfx = pool.getPrefix(nameCode);
String uri = pool.getURI(nameCode);
- if (excludeDefault && "".equals(pfx) && !usesDefaultNS) {
- nameCode = pool.allocate("", "", pool.getLocalName(nameCode));
+ if (preserveUsed) {
+ if (excludeDefault && "".equals(pfx) && !usesDefaultNS) {
+ nameCode = pool.allocate("", "", pool.getLocalName(nameCode));
+ }
+ } else {
+ if (excludeNS.contains(uri)) {
+ nameCode = pool.allocate("", "", pool.getLocalName(nameCode));
+ }
}
tree.addStartElement(nameCode, typeCode, newNS);
- tree.addAttributes(node);
+
+ if (!preserveUsed) {
+ // In this case, we may need to change some attributes too
+ XdmSequenceIterator attriter = node.axisIterator(Axis.ATTRIBUTE);
+ while (attriter.hasNext()) {
+ XdmNode attr = (XdmNode) attriter.next();
+ String attrns = attr.getNodeName().getNamespaceURI();
+ if (excludeNS.contains(attrns)) {
+ tree.addAttribute(new QName(attr.getNodeName().getLocalName()), attr.getStringValue());
+ } else {
+ tree.addAttribute(attr);
+ }
+ }
+ } else {
+ tree.addAttributes(node);
+ }
XdmSequenceIterator iter = node.axisIterator(Axis.CHILD);
while (iter.hasNext()) {
XdmNode cnode = (XdmNode) iter.next();
- removeNamespacesWriter(tree, cnode, excludeNS);
+ removeNamespacesWriter(tree, cnode, excludeNS, preserveUsed);
}
tree.addEndElement();
} else {
@@ -7,7 +7,7 @@
<document xmlns="http://example.com/ns1">
<title>Some Title</title>
<para xmlns="http://example.com/ns2">Some paragraph.</para>
-<x:para xmlns:x="http://example1.com/ns1">Some other paragraph.</x:para>
+<x:para xmlns:x="http://example.com/ns1">Some other paragraph.</x:para>
</document>
</t:input>
@@ -31,7 +31,11 @@
</t:pipeline>
<t:output port='result'>
-<fred/>
+<document>
+<title>Some Title</title>
+<para xmlns="http://example.com/ns2">Some paragraph.</para>
+<para>Some other paragraph.</para>
+</document>
</t:output>
</t:test>

0 comments on commit 687e5af

Please sign in to comment.