/
SOAPDocumentImpl.java
683 lines (580 loc) · 20.7 KB
/
SOAPDocumentImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
/*
* Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
/**
*
* @author SAAJ RI Development Team
*/
package com.sun.xml.messaging.saaj.soap;
import com.sun.xml.messaging.saaj.soap.impl.CDATAImpl;
import com.sun.xml.messaging.saaj.soap.impl.ElementFactory;
import com.sun.xml.messaging.saaj.soap.impl.ElementImpl;
import com.sun.xml.messaging.saaj.soap.impl.NamedNodeMapImpl;
import com.sun.xml.messaging.saaj.soap.impl.NodeListImpl;
import com.sun.xml.messaging.saaj.soap.impl.SOAPCommentImpl;
import com.sun.xml.messaging.saaj.soap.impl.SOAPTextImpl;
import com.sun.xml.messaging.saaj.soap.name.NameImpl;
import com.sun.xml.messaging.saaj.util.LogDomainConstants;
import com.sun.xml.messaging.saaj.util.SAAJUtil;
import org.w3c.dom.Attr;
import org.w3c.dom.CDATASection;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Comment;
import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.DOMException;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import org.w3c.dom.EntityReference;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.ProcessingInstruction;
import org.w3c.dom.Text;
import org.w3c.dom.UserDataHandler;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import jakarta.xml.soap.SOAPElement;
import jakarta.xml.soap.SOAPException;
import java.lang.reflect.Constructor;
import java.text.MessageFormat;
import java.util.logging.Logger;
public class SOAPDocumentImpl implements SOAPDocument, jakarta.xml.soap.Node, Document {
public static final String SAAJ_NODE = "jakarta.xml.soap.Node";
private static final String XMLNS = "xmlns".intern();
protected static final Logger log =
Logger.getLogger(LogDomainConstants.SOAP_DOMAIN,
"com.sun.xml.messaging.saaj.soap.LocalStrings");
SOAPPartImpl enclosingSOAPPart;
private Document document;
public SOAPDocumentImpl(SOAPPartImpl enclosingDocument) {
this(enclosingDocument, createDocument());
}
SOAPDocumentImpl(SOAPPartImpl enclosingDocument, Document document) {
this.document = document;
this.enclosingSOAPPart = enclosingDocument;
register(this);
}
private static Document createDocument() {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance("com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl", SAAJUtil.getSystemClassLoader());
try {
final DocumentBuilder documentBuilder = docFactory.newDocumentBuilder();
return documentBuilder.newDocument();
} catch (ParserConfigurationException e) {
throw new RuntimeException("Error creating xml document", e);
}
}
// public SOAPDocumentImpl(boolean grammarAccess) {
// super(grammarAccess);
// }
//
// public SOAPDocumentImpl(DocumentType doctype) {
// super(doctype);
// }
//
// public SOAPDocumentImpl(DocumentType doctype, boolean grammarAccess) {
// super(doctype, grammarAccess);
// }
@Override
public SOAPPartImpl getSOAPPart() {
if (enclosingSOAPPart == null) {
log.severe("SAAJ0541.soap.fragment.not.bound.to.part");
throw new RuntimeException("Could not complete operation. Fragment not bound to SOAP part.");
}
return enclosingSOAPPart;
}
@Override
public SOAPDocumentImpl getDocument() {
return this;
}
@Override
public DocumentType getDoctype() {
// SOAP means no DTD, No DTD means no doctype (SOAP 1.2 only?)
return null;
}
@Override
public DOMImplementation getImplementation() {
return document.getImplementation();
}
@Override
public Element getDocumentElement() {
// This had better be an Envelope!
getSOAPPart().doGetDocumentElement();
return doGetDocumentElement();
}
protected Element doGetDocumentElement() {
return document.getDocumentElement();
}
@Override
public Element createElement(String tagName) throws DOMException {
return ElementFactory.createElement(
this,
NameImpl.getLocalNameFromTagName(tagName),
NameImpl.getPrefixFromTagName(tagName),
null);
}
@Override
public DocumentFragment createDocumentFragment() {
return new SOAPDocumentFragment(this);
}
@Override
public org.w3c.dom.Text createTextNode(String data) {
return new SOAPTextImpl(this, data);
}
@Override
public Comment createComment(String data) {
return new SOAPCommentImpl(this, data);
}
@Override
public CDATASection createCDATASection(String data) throws DOMException {
return new CDATAImpl(this, data);
}
@Override
public ProcessingInstruction createProcessingInstruction(
String target,
String data)
throws DOMException {
log.severe("SAAJ0542.soap.proc.instructions.not.allowed.in.docs");
throw new UnsupportedOperationException("Processing Instructions are not allowed in SOAP documents");
}
@Override
public Attr createAttribute(String name) throws DOMException {
boolean isQualifiedName = (name.indexOf(":") > 0);
if (isQualifiedName) {
String nsUri = null;
String prefix = name.substring(0, name.indexOf(":"));
//cannot do anything to resolve the URI if prefix is not
//XMLNS.
if (XMLNS.equals(prefix)) {
nsUri = ElementImpl.XMLNS_URI;
return createAttributeNS(nsUri, name);
}
}
return document.createAttribute(name);
}
@Override
public EntityReference createEntityReference(String name)
throws DOMException {
log.severe("SAAJ0543.soap.entity.refs.not.allowed.in.docs");
throw new UnsupportedOperationException("Entity References are not allowed in SOAP documents");
}
@Override
public NodeList getElementsByTagName(String tagname) {
return new NodeListImpl(this, document.getElementsByTagName(tagname));
}
@Override
public org.w3c.dom.Node importNode(Node importedNode, boolean deep)
throws DOMException {
Node domNode = getDomNode(importedNode);
final Node newNode = document.importNode(domNode, deep);
if (importedNode instanceof jakarta.xml.soap.Node) {
Node newSoapNode = createSoapNode(importedNode.getClass(), newNode);
newNode.setUserData(SAAJ_NODE, newSoapNode, null);
if (deep && newSoapNode.hasChildNodes()) {
NodeList childNodes = newSoapNode.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
registerChildNodes(childNodes.item(i), deep);
}
}
return newSoapNode;
}
registerChildNodes(newNode, deep);
return findIfPresent(newNode);
}
/**
* If the parentNode is not registered to domToSoap, create soap wapper for parentNode and register it to domToSoap
* If deep = true, also register all children transitively of parentNode to domToSoap map.
* @param parentNode node to wrap
* @param deep wrap child nodes transitively
*/
public void registerChildNodes(Node parentNode, boolean deep) {
if (parentNode.getUserData(SAAJ_NODE) == null) {
if (parentNode instanceof Element) {
ElementFactory.createElement(this, (Element) parentNode);
} else if (parentNode instanceof CharacterData) {
switch (parentNode.getNodeType()) {
case CDATA_SECTION_NODE:
new CDATAImpl(this, (CharacterData) parentNode);
break;
case COMMENT_NODE:
new SOAPCommentImpl(this, (CharacterData) parentNode);
break;
case TEXT_NODE:
new SOAPTextImpl(this, (CharacterData) parentNode);
break;
}
} else if (parentNode instanceof DocumentFragment) {
new SOAPDocumentFragment(this, (DocumentFragment) parentNode);
}
}
if (deep) {
NodeList nodeList = parentNode.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node nextChild = nodeList.item(i);
registerChildNodes(nextChild, true);
}
}
}
@Override
public Element createElementNS(String namespaceURI, String qualifiedName)
throws DOMException {
return ElementFactory.createElement(
this,
NameImpl.getLocalNameFromTagName(qualifiedName),
NameImpl.getPrefixFromTagName(qualifiedName),
namespaceURI);
}
@Override
public Attr createAttributeNS(String namespaceURI, String qualifiedName)
throws DOMException {
return document.createAttributeNS(namespaceURI, qualifiedName);
}
@Override
public NodeList getElementsByTagNameNS(
String namespaceURI,
String localName) {
return new NodeListImpl(this, document.getElementsByTagNameNS(namespaceURI, localName));
}
@Override
public Element getElementById(String elementId) {
return (Element) findIfPresent(document.getElementById(elementId));
}
@Override
public String getInputEncoding() {
return document.getInputEncoding();
}
@Override
public String getXmlEncoding() {
return document.getXmlEncoding();
}
@Override
public boolean getXmlStandalone() {
return document.getXmlStandalone();
}
@Override
public void setXmlStandalone(boolean xmlStandalone) throws DOMException {
document.setXmlStandalone(xmlStandalone);
}
@Override
public String getXmlVersion() {
return document.getXmlVersion();
}
@Override
public void setXmlVersion(String xmlVersion) throws DOMException {
document.setXmlVersion(xmlVersion);
}
@Override
public boolean getStrictErrorChecking() {
return document.getStrictErrorChecking();
}
@Override
public void setStrictErrorChecking(boolean strictErrorChecking) {
document.setStrictErrorChecking(strictErrorChecking);
}
@Override
public String getDocumentURI() {
return document.getDocumentURI();
}
@Override
public void setDocumentURI(String documentURI) {
document.setDocumentURI(documentURI);
}
@Override
public Node adoptNode(Node source) throws DOMException {
return document.adoptNode(source);
}
@Override
public DOMConfiguration getDomConfig() {
return document.getDomConfig();
}
@Override
public void normalizeDocument() {
document.normalizeDocument();
}
@Override
public Node renameNode(Node n, String namespaceURI, String qualifiedName) throws DOMException {
return findIfPresent(document.renameNode(n, namespaceURI, qualifiedName));
}
@Override
public String getNodeName() {
return document.getNodeName();
}
@Override
public String getNodeValue() throws DOMException {
return document.getNodeValue();
}
@Override
public void setNodeValue(String nodeValue) throws DOMException {
document.setNodeValue(nodeValue);
}
@Override
public short getNodeType() {
return document.getNodeType();
}
@Override
public Node getParentNode() {
return findIfPresent(document.getParentNode());
}
@Override
public NodeList getChildNodes() {
return new NodeListImpl(this, document.getChildNodes());
}
@Override
public Node getFirstChild() {
return findIfPresent(document.getFirstChild());
}
@Override
public Node getLastChild() {
return findIfPresent(document.getLastChild());
}
@Override
public Node getPreviousSibling() {
return findIfPresent(document.getPreviousSibling());
}
@Override
public Node getNextSibling() {
return findIfPresent(document.getNextSibling());
}
@Override
public NamedNodeMap getAttributes() {
NamedNodeMap attributes = document.getAttributes();
if (attributes == null) {
return null;
}
return new NamedNodeMapImpl(attributes, this);
}
@Override
public Document getOwnerDocument() {
return document.getOwnerDocument();
}
@Override
public Node insertBefore(Node newChild, Node refChild) throws DOMException {
return document.insertBefore(getDomNode(newChild), getDomNode(refChild));
}
@Override
public Node replaceChild(Node newChild, Node oldChild) throws DOMException {
return document.replaceChild(getDomNode(newChild), getDomNode(oldChild));
}
@Override
public Node removeChild(Node oldChild) throws DOMException {
return document.removeChild(getDomNode(oldChild));
}
@Override
public Node appendChild(Node newChild) throws DOMException {
return document.appendChild(getDomNode(newChild));
}
@Override
public boolean hasChildNodes() {
return document.hasChildNodes();
}
@Override
public Node cloneNode(boolean deep) {
SOAPPartImpl enclosingPartClone = (SOAPPartImpl) enclosingSOAPPart.cloneNode(deep);
registerChildNodes(enclosingPartClone.getDocument().getDomDocument(), deep);
return enclosingPartClone.getDocument();
}
@Override
public void normalize() {
document.normalize();
}
@Override
public boolean isSupported(String feature, String version) {
return document.isSupported(feature, version);
}
@Override
public String getNamespaceURI() {
return document.getNamespaceURI();
}
@Override
public String getPrefix() {
return document.getPrefix();
}
@Override
public void setPrefix(String prefix) throws DOMException {
document.setPrefix(prefix);
}
@Override
public String getLocalName() {
return document.getLocalName();
}
@Override
public boolean hasAttributes() {
return document.hasAttributes();
}
@Override
public String getBaseURI() {
return document.getBaseURI();
}
@Override
public short compareDocumentPosition(Node other) throws DOMException {
return document.compareDocumentPosition(getDomNode(other));
}
@Override
public String getTextContent() throws DOMException {
return document.getTextContent();
}
@Override
public void setTextContent(String textContent) throws DOMException {
document.setTextContent(textContent);
}
@Override
public boolean isSameNode(Node other) {
return document.isSameNode(getDomNode(other));
}
@Override
public String lookupPrefix(String namespaceURI) {
return document.lookupPrefix(namespaceURI);
}
@Override
public boolean isDefaultNamespace(String namespaceURI) {
return document.isDefaultNamespace(namespaceURI);
}
@Override
public String lookupNamespaceURI(String prefix) {
return document.lookupNamespaceURI(prefix);
}
@Override
public boolean isEqualNode(Node arg) {
return document.isEqualNode(getDomNode(arg));
}
@Override
public Object getFeature(String feature, String version) {
return document.getFeature(feature, version);
}
@Override
public Object setUserData(String key, Object data, UserDataHandler handler) {
return document.setUserData(key, data, handler);
}
@Override
public Object getUserData(String key) {
return document.getUserData(key);
}
public Document getDomDocument() {
return document;
}
/**
* Insert a mapping information for {@link org.w3c.dom.Node} - {@link jakarta.xml.soap.Node}.
*
* In SAAJ, elements in DOM are expected to be interfaces of SAAJ, on the other hand in JDKs Xerces,
* they are casted to internal impl classes. After removal of SAAJ dependency
* to JDKs internal classes elements in DOM can never be both of them.
*
* @param node SAAJ wrapper node for w3c DOM node
*/
public void register(jakarta.xml.soap.Node node) {
final Node domElement = getDomNode(node);
if (domElement.getUserData(SAAJ_NODE) != null) {
throw new IllegalStateException("Element " + domElement.getNodeName()
+ " is already registered");
}
domElement.setUserData(SAAJ_NODE, node, null);
}
/**
* Find a soap wrapper for w3c dom node.
*
* @param node w3c dom node nullable
* @return soap wrapper for w3c dom node
*/
public jakarta.xml.soap.Node find(Node node) {
return find(node, true);
}
private jakarta.xml.soap.Node find(Node node, boolean required) {
if (node == null) {
return null;
}
if (node instanceof jakarta.xml.soap.Node) {
return (jakarta.xml.soap.Node) node;
}
final jakarta.xml.soap.Node found = (jakarta.xml.soap.Node) node.getUserData(SAAJ_NODE);
if (found == null && required) {
throw new IllegalArgumentException(MessageFormat.format("Cannot find SOAP wrapper for element {0}", node));
}
return found;
}
/**
* If corresponding soap wrapper exists for w3c dom node it is returned,
* if not passed dom element is returned.
*
* @param node w3c dom node
* @return soap wrapper or passed w3c dom node if not found
*/
public Node findIfPresent(Node node) {
final jakarta.xml.soap.Node found = find(node, false);
return found != null ? found : node;
}
/**
* Extracts w3c dom node from corresponding soap wrapper.
*
* @param node soap or dom nullable
* @return dom node
*/
public Node getDomNode(Node node) {
if (node instanceof SOAPDocumentImpl) {
return ((SOAPDocumentImpl)node).getDomElement();
} else if (node instanceof ElementImpl) {
return ((ElementImpl) node).getDomElement();
} else if (node instanceof SOAPTextImpl) {
return ((SOAPTextImpl)node).getDomElement();
} else if (node instanceof SOAPCommentImpl) {
return ((SOAPCommentImpl)node).getDomElement();
} else if (node instanceof CDATAImpl) {
return ((CDATAImpl) node).getDomElement();
} else if (node instanceof SOAPDocumentFragment) {
return ((SOAPDocumentFragment)node).getDomNode();
}
return node;
}
private Node createSoapNode(Class nodeType, Node node) {
if (SOAPTextImpl.class.isAssignableFrom(nodeType)) {
return new SOAPTextImpl(this, (Text) node);
} else if (SOAPCommentImpl.class.isAssignableFrom(nodeType)) {
return new SOAPCommentImpl(this, (Comment) node);
} else if (CDATAImpl.class.isAssignableFrom(nodeType)) {
return new CDATAImpl(this, (CDATASection) node);
} else if (SOAPDocumentFragment.class.isAssignableFrom(nodeType)) {
return new SOAPDocumentFragment(this, (DocumentFragment) node);
}
try {
Constructor<Node> constructor = nodeType.getConstructor(SOAPDocumentImpl.class, Element.class);
return constructor.newInstance(this, node);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
public Document getDomElement() {
return document;
}
@Override
public String getValue() {
throw new UnsupportedOperationException();
}
@Override
public void setValue(String value) {
throw new UnsupportedOperationException();
}
@Override
public void setParentElement(SOAPElement parent) throws SOAPException {
throw new UnsupportedOperationException();
}
@Override
public SOAPElement getParentElement() {
throw new UnsupportedOperationException();
}
@Override
public void detachNode() {
throw new UnsupportedOperationException();
}
@Override
public void recycleNode() {
throw new UnsupportedOperationException();
}
}