| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.dom; |
| 9 |
|
| 10 |
import net.sf.saxon.event.NamespaceReducer; |
| 11 |
import net.sf.saxon.event.Receiver; |
| 12 |
import net.sf.saxon.event.ReceiverOptions; |
| 13 |
import net.sf.saxon.expr.parser.ExplicitLocation; |
| 14 |
import net.sf.saxon.expr.parser.Location; |
| 15 |
import net.sf.saxon.lib.NamespaceConstant; |
| 16 |
import net.sf.saxon.om.FingerprintedQName; |
| 17 |
import net.sf.saxon.om.NamespaceBinding; |
| 18 |
import net.sf.saxon.om.NoNamespaceName; |
| 19 |
import net.sf.saxon.om.NodeName; |
| 20 |
import net.sf.saxon.trans.XPathException; |
| 21 |
import net.sf.saxon.type.BuiltInAtomicType; |
| 22 |
import net.sf.saxon.type.Type; |
| 23 |
import net.sf.saxon.type.Untyped; |
| 24 |
import org.w3c.dom.*; |
| 25 |
import org.w3c.dom.CharacterData; |
| 26 |
|
| 27 |
import java.net.URI; |
| 28 |
import java.net.URISyntaxException; |
| 29 |
import java.util.*; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class DOMSender { |
| 39 |
private Receiver receiver; |
| 40 |
protected Node root; |
| 41 |
protected String systemId; |
| 42 |
private Stack<List<NamespaceBinding>> namespaces = new Stack<>(); |
| 43 |
private Node currentNode; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public DOMSender(Node startNode, Receiver receiver) { |
| 56 |
if (startNode == null) { |
| 57 |
throw new NullPointerException("startNode"); |
| 58 |
} |
| 59 |
if (receiver == null) { |
| 60 |
throw new NullPointerException("receiver"); |
| 61 |
} |
| 62 |
this.root = startNode; |
| 63 |
this.receiver = new NamespaceReducer(receiver); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public void setSystemId(String systemId) { |
| 74 |
this.systemId = systemId; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public void send() throws XPathException { |
| 88 |
receiver.setSystemId(systemId); |
| 89 |
|
| 90 |
|
| 91 |
final Location loc = ExplicitLocation.UNKNOWN_LOCATION; |
| 92 |
switch (root.getNodeType()) { |
| 93 |
case Node.DOCUMENT_NODE: |
| 94 |
case Node.DOCUMENT_FRAGMENT_NODE: |
| 95 |
receiver.startDocument(0); |
| 96 |
sendUnparsedEntities(); |
| 97 |
walkNode(root); |
| 98 |
receiver.endDocument(); |
| 99 |
break; |
| 100 |
case Node.ELEMENT_NODE: |
| 101 |
sendElement((Element) root); |
| 102 |
break; |
| 103 |
case Node.TEXT_NODE: |
| 104 |
case Node.CDATA_SECTION_NODE: |
| 105 |
receiver.characters(((CharacterData) root).getData(), loc, 0); |
| 106 |
break; |
| 107 |
case Node.COMMENT_NODE: |
| 108 |
receiver.comment(((Comment) root).getData(), loc, 0); |
| 109 |
break; |
| 110 |
case Node.PROCESSING_INSTRUCTION_NODE: |
| 111 |
receiver.processingInstruction( |
| 112 |
((ProcessingInstruction) root).getTarget(), |
| 113 |
((ProcessingInstruction) root).getData(), loc, 0); |
| 114 |
break; |
| 115 |
default: |
| 116 |
throw new IllegalStateException("DOMSender: unsupported kind of start node (" + root.getNodeType() + ")"); |
| 117 |
} |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
private void sendUnparsedEntities() throws XPathException { |
| 122 |
if (root instanceof Document) { |
| 123 |
DocumentType docType = ((Document)root).getDoctype(); |
| 124 |
if (docType != null) { |
| 125 |
NamedNodeMap map = docType.getEntities(); |
| 126 |
if (map != null) { |
| 127 |
for (int i = 0; i < map.getLength(); i++) { |
| 128 |
Entity e = (Entity) map.item(i); |
| 129 |
if (e.getNotationName() != null) { |
| 130 |
|
| 131 |
String name = e.getNodeName(); |
| 132 |
String systemId = e.getSystemId(); |
| 133 |
try { |
| 134 |
URI systemIdURI = new URI(systemId); |
| 135 |
if (!systemIdURI.isAbsolute()) { |
| 136 |
String base = root.getBaseURI(); |
| 137 |
if (base != null) { |
| 138 |
systemIdURI = new URI(base).resolve(systemIdURI); |
| 139 |
systemId = systemIdURI.toString(); |
| 140 |
} else { |
| 141 |
|
| 142 |
} |
| 143 |
} |
| 144 |
} catch (URISyntaxException err) { |
| 145 |
|
| 146 |
} |
| 147 |
String publicId = e.getPublicId(); |
| 148 |
receiver.setUnparsedEntity(name, systemId, publicId); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
private void sendElement(Element startNode) throws XPathException { |
| 167 |
List<Element> ancestors = new ArrayList<>(); |
| 168 |
List<NamespaceBinding> bindings = new ArrayList<>(); |
| 169 |
Node parent = startNode; |
| 170 |
while (parent != null && parent.getNodeType() == Type.ELEMENT) { |
| 171 |
ancestors.add((Element) parent); |
| 172 |
parent = parent.getParentNode(); |
| 173 |
} |
| 174 |
for (int i=ancestors.size()-1; i>=0; i--) { |
| 175 |
gatherNamespaces(ancestors.get(i), bindings); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
Map<String, String> distinctNamespaces = new HashMap<>(); |
| 180 |
for (NamespaceBinding nb : bindings) { |
| 181 |
distinctNamespaces.put(nb.getPrefix(), nb.getURI()); |
| 182 |
} |
| 183 |
if (distinctNamespaces.size() != bindings.size()) { |
| 184 |
bindings = new ArrayList<>(); |
| 185 |
for (Map.Entry<String, String> entry : distinctNamespaces.entrySet()) { |
| 186 |
bindings.add(new NamespaceBinding(entry.getKey(), entry.getValue())); |
| 187 |
} |
| 188 |
} |
| 189 |
namespaces.push(bindings); |
| 190 |
outputElement(startNode, true); |
| 191 |
namespaces.pop(); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
private NodeName getNodeName(String name, boolean useDefaultNS) { |
| 202 |
int colon = name.indexOf(':'); |
| 203 |
if (colon < 0) { |
| 204 |
if (useDefaultNS) { |
| 205 |
String uri = getUriForPrefix(""); |
| 206 |
if (!uri.isEmpty()) { |
| 207 |
return new FingerprintedQName("", uri, name); |
| 208 |
} |
| 209 |
} |
| 210 |
return new NoNamespaceName(name); |
| 211 |
} else { |
| 212 |
String prefix = name.substring(0, colon); |
| 213 |
String uri = getUriForPrefix(prefix); |
| 214 |
if (uri == null) { |
| 215 |
throw new IllegalStateException("Prefix " + prefix + " is not bound to any namespace"); |
| 216 |
} |
| 217 |
return new FingerprintedQName(prefix, uri, name.substring(colon+1)); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
private void walkNode(Node node) throws XPathException { |
| 230 |
final Location loc = ExplicitLocation.UNKNOWN_LOCATION; |
| 231 |
if (node.hasChildNodes()) { |
| 232 |
NodeList nit = node.getChildNodes(); |
| 233 |
final int len = nit.getLength(); |
| 234 |
for (int i = 0; i < len; i++) { |
| 235 |
Node child = nit.item(i); |
| 236 |
currentNode = child; |
| 237 |
switch (child.getNodeType()) { |
| 238 |
case Node.DOCUMENT_NODE: |
| 239 |
case Node.DOCUMENT_FRAGMENT_NODE: |
| 240 |
break; |
| 241 |
case Node.ELEMENT_NODE: |
| 242 |
Element element = (Element) child; |
| 243 |
List<NamespaceBinding> bindings = new ArrayList<>(); |
| 244 |
namespaces.push(bindings); |
| 245 |
gatherNamespaces(element, bindings); |
| 246 |
outputElement(element, !bindings.isEmpty()); |
| 247 |
namespaces.pop(); |
| 248 |
break; |
| 249 |
case Node.ATTRIBUTE_NODE: |
| 250 |
break; |
| 251 |
case Node.PROCESSING_INSTRUCTION_NODE: |
| 252 |
receiver.processingInstruction( |
| 253 |
((ProcessingInstruction) child).getTarget(), |
| 254 |
((ProcessingInstruction) child).getData(), |
| 255 |
loc, 0); |
| 256 |
break; |
| 257 |
case Node.COMMENT_NODE: { |
| 258 |
String text = ((Comment) child).getData(); |
| 259 |
if (text != null) { |
| 260 |
receiver.comment(text, loc, 0); |
| 261 |
} |
| 262 |
break; |
| 263 |
} |
| 264 |
case Node.TEXT_NODE: |
| 265 |
case Node.CDATA_SECTION_NODE: { |
| 266 |
String text = ((CharacterData) child).getData(); |
| 267 |
if (text != null) { |
| 268 |
receiver.characters(text, loc, 0); |
| 269 |
} |
| 270 |
break; |
| 271 |
} |
| 272 |
case Node.ENTITY_REFERENCE_NODE: |
| 273 |
walkNode(child); |
| 274 |
break; |
| 275 |
default: |
| 276 |
break; |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
public Node getCurrentNode() { |
| 290 |
return currentNode; |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
private void outputElement(Element element, boolean hasNamespaceDeclarations) throws XPathException { |
| 303 |
|
| 304 |
NodeName name = getNodeName(element.getTagName(), true); |
| 305 |
final Location loc = new ExplicitLocation(systemId, -1, -1); |
| 306 |
receiver.startElement(name, Untyped.getInstance(), loc, 0); |
| 307 |
for (NamespaceBinding ns : namespaces.peek()) { |
| 308 |
receiver.namespace(ns, 0); |
| 309 |
} |
| 310 |
|
| 311 |
NamedNodeMap atts = element.getAttributes(); |
| 312 |
if (atts != null) { |
| 313 |
final int len = atts.getLength(); |
| 314 |
for (int a2 = 0; a2 < len; a2++) { |
| 315 |
Attr att = (Attr) atts.item(a2); |
| 316 |
int props = att.isId() ? ReceiverOptions.IS_ID : 0; |
| 317 |
String attname = att.getName(); |
| 318 |
if (hasNamespaceDeclarations && (attname.equals("xmlns") || attname.startsWith("xmlns:"))) { |
| 319 |
|
| 320 |
} else { |
| 321 |
|
| 322 |
NodeName attNodeName = getNodeName(attname, false); |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
receiver.attribute(attNodeName, BuiltInAtomicType.UNTYPED_ATOMIC, att.getValue(), loc, props); |
| 329 |
} |
| 330 |
} |
| 331 |
} |
| 332 |
receiver.startContent(); |
| 333 |
|
| 334 |
walkNode(element); |
| 335 |
|
| 336 |
receiver.endElement(); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
private String getUriForPrefix(String prefix) { |
| 351 |
if (prefix.equals("xml")) { |
| 352 |
return NamespaceConstant.XML; |
| 353 |
} |
| 354 |
for (int i=namespaces.size()-1; i>=0; i--) { |
| 355 |
List<NamespaceBinding> localNamespaces = namespaces.get(i); |
| 356 |
for (NamespaceBinding n : localNamespaces) { |
| 357 |
if (n.getPrefix().equals(prefix)) { |
| 358 |
return n.getURI(); |
| 359 |
} |
| 360 |
} |
| 361 |
} |
| 362 |
if (prefix.equals("")) { |
| 363 |
return ""; |
| 364 |
} else { |
| 365 |
throw new IllegalStateException("No binding for namespace prefix " + prefix); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
private void gatherNamespaces(Element element, List<NamespaceBinding> list) { |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
try { |
| 383 |
String prefix = element.getPrefix(); |
| 384 |
String uri = element.getNamespaceURI(); |
| 385 |
if (prefix == null) { |
| 386 |
prefix = ""; |
| 387 |
} |
| 388 |
if (uri == null) { |
| 389 |
uri = ""; |
| 390 |
} |
| 391 |
|
| 392 |
if (!(prefix.isEmpty() && uri.isEmpty())) { |
| 393 |
list.add(new NamespaceBinding(prefix, uri)); |
| 394 |
} |
| 395 |
} catch (Throwable err) { |
| 396 |
|
| 397 |
} |
| 398 |
|
| 399 |
NamedNodeMap atts = element.getAttributes(); |
| 400 |
|
| 401 |
|
| 402 |
if (atts == null) { |
| 403 |
return; |
| 404 |
} |
| 405 |
int alen = atts.getLength(); |
| 406 |
for (int a1 = 0; a1 < alen; a1++) { |
| 407 |
Attr att = (Attr) atts.item(a1); |
| 408 |
String attname = att.getName(); |
| 409 |
boolean possibleNamespace = attname.startsWith("xmlns"); |
| 410 |
if (possibleNamespace && attname.length() == 5) { |
| 411 |
String uri = att.getValue(); |
| 412 |
list.add(new NamespaceBinding("", uri)); |
| 413 |
} else if (possibleNamespace && attname.startsWith("xmlns:")) { |
| 414 |
String prefix = attname.substring(6); |
| 415 |
String uri = att.getValue(); |
| 416 |
list.add(new NamespaceBinding(prefix, uri)); |
| 417 |
} else if (attname.indexOf(':') >= 0) { |
| 418 |
try { |
| 419 |
String prefix = att.getPrefix(); |
| 420 |
String uri = att.getNamespaceURI(); |
| 421 |
list.add(new NamespaceBinding(prefix, uri)); |
| 422 |
} catch (Throwable err) { |
| 423 |
|
| 424 |
} |
| 425 |
} |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
} |
| 430 |
|