| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.event; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.parser.Location; |
| 11 |
import net.sf.saxon.lib.NamespaceConstant; |
| 12 |
import net.sf.saxon.om.NamespaceBinding; |
| 13 |
import net.sf.saxon.om.NamespaceBindingSet; |
| 14 |
import net.sf.saxon.om.NamespaceResolver; |
| 15 |
import net.sf.saxon.om.NodeName; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.type.SchemaType; |
| 18 |
|
| 19 |
import java.util.ArrayList; |
| 20 |
import java.util.Arrays; |
| 21 |
import java.util.Iterator; |
| 22 |
import java.util.List; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class NamespaceReducer extends ProxyReceiver implements NamespaceResolver { |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
private NamespaceBinding[] namespaces = new NamespaceBinding[50]; |
| 41 |
private int namespacesSize = 0; |
| 42 |
private int[] countStack = new int[50]; |
| 43 |
private int depth = 0; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
private boolean[] disinheritStack = new boolean[50]; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
private NamespaceBinding[] pendingUndeclarations = null; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public NamespaceReducer(Receiver next) { |
| 65 |
super(next); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public void startElement(NodeName elemName, SchemaType typeCode, Location location, int properties) throws XPathException { |
| 74 |
|
| 75 |
nextReceiver.startElement(elemName, typeCode, location, properties); |
| 76 |
|
| 77 |
|
| 78 |
if ((properties & ReceiverOptions.REFUSE_NAMESPACES) != 0) { |
| 79 |
|
| 80 |
pendingUndeclarations = Arrays.copyOf(namespaces, namespacesSize); |
| 81 |
} else if (depth > 0 && disinheritStack[depth - 1]) { |
| 82 |
|
| 83 |
|
| 84 |
List<NamespaceBinding> undeclarations = new ArrayList<>(namespacesSize); |
| 85 |
int k = namespacesSize; |
| 86 |
for (int d = depth-1; d>=0; d--) { |
| 87 |
if (!disinheritStack[d]) { |
| 88 |
break; |
| 89 |
} |
| 90 |
for (int i=0; i<countStack[d]; i++) { |
| 91 |
undeclarations.add(namespaces[--k]); |
| 92 |
} |
| 93 |
} |
| 94 |
pendingUndeclarations = undeclarations.toArray(NamespaceBinding.EMPTY_ARRAY); |
| 95 |
} else { |
| 96 |
pendingUndeclarations = null; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
countStack[depth] = 0; |
| 102 |
disinheritStack[depth] = (properties & ReceiverOptions.DISINHERIT_NAMESPACES) != 0; |
| 103 |
if (++depth >= countStack.length) { |
| 104 |
countStack = Arrays.copyOf(countStack, depth*2); |
| 105 |
disinheritStack = Arrays.copyOf(disinheritStack, depth*2); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
if ((properties & ReceiverOptions.NAMESPACE_OK) == 0) { |
| 113 |
namespace(elemName.getNamespaceBinding(), 0); |
| 114 |
} |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public void namespace(NamespaceBindingSet namespaceBindings, int properties) throws XPathException { |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
for (NamespaceBinding ns : namespaceBindings) { |
| 131 |
if (isNeeded(ns)) { |
| 132 |
addToStack(ns); |
| 133 |
countStack[depth - 1]++; |
| 134 |
nextReceiver.namespace(ns, properties); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
private boolean isNeeded(NamespaceBinding nsBinding) { |
| 148 |
if (nsBinding.isXmlNamespace()) { |
| 149 |
|
| 150 |
return false; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
String prefix = nsBinding.getPrefix(); |
| 156 |
if (pendingUndeclarations != null) { |
| 157 |
for (int p = 0; p < pendingUndeclarations.length; p++) { |
| 158 |
NamespaceBinding nb = pendingUndeclarations[p]; |
| 159 |
if (nb != null && prefix.equals(nb.getPrefix())) { |
| 160 |
pendingUndeclarations[p] = null; |
| 161 |
|
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
for (int i = namespacesSize - 1; i >= 0; i--) { |
| 167 |
if (namespaces[i].equals(nsBinding)) { |
| 168 |
|
| 169 |
return false; |
| 170 |
} |
| 171 |
if (namespaces[i].getPrefix().equals(nsBinding.getPrefix())) { |
| 172 |
|
| 173 |
return true; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
return !nsBinding.isDefaultUndeclaration(); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
private void addToStack(NamespaceBinding nsBinding) { |
| 188 |
|
| 189 |
if (namespacesSize + 1 >= namespaces.length) { |
| 190 |
namespaces = Arrays.copyOf(namespaces, namespacesSize * 2); |
| 191 |
} |
| 192 |
namespaces[namespacesSize++] = nsBinding; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public boolean isDisinheritingNamespaces() { |
| 201 |
return depth > 0 && disinheritStack[depth-1]; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
public void startContent() throws XPathException { |
| 210 |
|
| 211 |
if (pendingUndeclarations != null) { |
| 212 |
for (NamespaceBinding ns : pendingUndeclarations) { |
| 213 |
if (ns != null) { |
| 214 |
namespace(new NamespaceBinding(ns.getPrefix(), ""), 0); |
| 215 |
|
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
pendingUndeclarations = null; |
| 220 |
nextReceiver.startContent(); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public void endElement() throws XPathException { |
| 229 |
if (depth-- == 0) { |
| 230 |
throw new IllegalStateException("Attempt to output end tag with no matching start tag"); |
| 231 |
} |
| 232 |
|
| 233 |
namespacesSize -= countStack[depth]; |
| 234 |
|
| 235 |
nextReceiver.endElement(); |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
public String getURIForPrefix(String prefix, boolean useDefault) { |
| 251 |
if (prefix.isEmpty() && !useDefault) { |
| 252 |
return NamespaceConstant.NULL; |
| 253 |
} else if ("xml".equals(prefix)) { |
| 254 |
return NamespaceConstant.XML; |
| 255 |
} else { |
| 256 |
for (int i = namespacesSize - 1; i >= 0; i--) { |
| 257 |
if (namespaces[i].getPrefix().equals(prefix)) { |
| 258 |
return namespaces[i].getURI(); |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
return prefix.isEmpty() ? NamespaceConstant.NULL : null; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
public Iterator<String> iteratePrefixes() { |
| 271 |
List<String> prefixes = new ArrayList<>(namespacesSize); |
| 272 |
for (int i = namespacesSize - 1; i >= 0; i--) { |
| 273 |
String prefix = namespaces[i].getPrefix(); |
| 274 |
if (!prefixes.contains(prefix)) { |
| 275 |
prefixes.add(prefix); |
| 276 |
} |
| 277 |
} |
| 278 |
prefixes.add("xml"); |
| 279 |
return prefixes.iterator(); |
| 280 |
} |
| 281 |
} |
| 282 |
|