19 * KIND, either express or implied. See the License for the |
19 * KIND, either express or implied. See the License for the |
20 * specific language governing permissions and limitations |
20 * specific language governing permissions and limitations |
21 * under the License. |
21 * under the License. |
22 */ |
22 */ |
23 /* |
23 /* |
24 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. |
24 * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved. |
25 */ |
25 */ |
26 /* |
26 /* |
27 * $Id: DOMXPathTransform.java 1203789 2011-11-18 18:46:07Z mullan $ |
27 * $Id: DOMXPathTransform.java 1788465 2017-03-24 15:10:51Z coheigea $ |
28 */ |
28 */ |
29 package org.jcp.xml.dsig.internal.dom; |
29 package org.jcp.xml.dsig.internal.dom; |
30 |
30 |
31 import javax.xml.crypto.*; |
31 import javax.xml.crypto.*; |
32 import javax.xml.crypto.dsig.*; |
32 import javax.xml.crypto.dsig.*; |
42 |
42 |
43 /** |
43 /** |
44 * DOM-based implementation of XPath Filtering Transform. |
44 * DOM-based implementation of XPath Filtering Transform. |
45 * (Uses Apache XML-Sec Transform implementation) |
45 * (Uses Apache XML-Sec Transform implementation) |
46 * |
46 * |
47 * @author Sean Mullan |
|
48 */ |
47 */ |
49 public final class DOMXPathTransform extends ApacheTransform { |
48 public final class DOMXPathTransform extends ApacheTransform { |
50 |
49 |
|
50 @Override |
51 public void init(TransformParameterSpec params) |
51 public void init(TransformParameterSpec params) |
52 throws InvalidAlgorithmParameterException |
52 throws InvalidAlgorithmParameterException |
53 { |
53 { |
54 if (params == null) { |
54 if (params == null) { |
55 throw new InvalidAlgorithmParameterException("params are required"); |
55 throw new InvalidAlgorithmParameterException("params are required"); |
58 ("params must be of type XPathFilterParameterSpec"); |
58 ("params must be of type XPathFilterParameterSpec"); |
59 } |
59 } |
60 this.params = params; |
60 this.params = params; |
61 } |
61 } |
62 |
62 |
|
63 @Override |
63 public void init(XMLStructure parent, XMLCryptoContext context) |
64 public void init(XMLStructure parent, XMLCryptoContext context) |
64 throws InvalidAlgorithmParameterException |
65 throws InvalidAlgorithmParameterException |
65 { |
66 { |
66 super.init(parent, context); |
67 super.init(parent, context); |
67 unmarshalParams(DOMUtils.getFirstChildElement(transformElem)); |
68 unmarshalParams(DOMUtils.getFirstChildElement(transformElem)); |
72 // create a Map of namespace prefixes |
73 // create a Map of namespace prefixes |
73 NamedNodeMap attributes = paramsElem.getAttributes(); |
74 NamedNodeMap attributes = paramsElem.getAttributes(); |
74 if (attributes != null) { |
75 if (attributes != null) { |
75 int length = attributes.getLength(); |
76 int length = attributes.getLength(); |
76 Map<String, String> namespaceMap = |
77 Map<String, String> namespaceMap = |
77 new HashMap<String, String>(length); |
78 new HashMap<>(length); |
78 for (int i = 0; i < length; i++) { |
79 for (int i = 0; i < length; i++) { |
79 Attr attr = (Attr)attributes.item(i); |
80 Attr attr = (Attr)attributes.item(i); |
80 String prefix = attr.getPrefix(); |
81 String prefix = attr.getPrefix(); |
81 if (prefix != null && prefix.equals("xmlns")) { |
82 if (prefix != null && "xmlns".equals(prefix)) { |
82 namespaceMap.put(attr.getLocalName(), attr.getValue()); |
83 namespaceMap.put(attr.getLocalName(), attr.getValue()); |
83 } |
84 } |
84 } |
85 } |
85 this.params = new XPathFilterParameterSpec(xPath, namespaceMap); |
86 this.params = new XPathFilterParameterSpec(xPath, namespaceMap); |
86 } else { |
87 } else { |
87 this.params = new XPathFilterParameterSpec(xPath); |
88 this.params = new XPathFilterParameterSpec(xPath); |
88 } |
89 } |
89 } |
90 } |
90 |
91 |
|
92 @Override |
91 public void marshalParams(XMLStructure parent, XMLCryptoContext context) |
93 public void marshalParams(XMLStructure parent, XMLCryptoContext context) |
92 throws MarshalException |
94 throws MarshalException |
93 { |
95 { |
94 super.marshalParams(parent, context); |
96 super.marshalParams(parent, context); |
95 XPathFilterParameterSpec xp = |
97 XPathFilterParameterSpec xp = |
97 Element xpathElem = DOMUtils.createElement(ownerDoc, "XPath", |
99 Element xpathElem = DOMUtils.createElement(ownerDoc, "XPath", |
98 XMLSignature.XMLNS, DOMUtils.getSignaturePrefix(context)); |
100 XMLSignature.XMLNS, DOMUtils.getSignaturePrefix(context)); |
99 xpathElem.appendChild(ownerDoc.createTextNode(xp.getXPath())); |
101 xpathElem.appendChild(ownerDoc.createTextNode(xp.getXPath())); |
100 |
102 |
101 // add namespace attributes, if necessary |
103 // add namespace attributes, if necessary |
|
104 @SuppressWarnings("unchecked") |
102 Set<Map.Entry<String, String>> entries = |
105 Set<Map.Entry<String, String>> entries = |
103 xp.getNamespaceMap().entrySet(); |
106 xp.getNamespaceMap().entrySet(); |
104 for (Map.Entry<String, String> entry : entries) { |
107 for (Map.Entry<String, String> entry : entries) { |
105 xpathElem.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" + |
108 xpathElem.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" + |
106 entry.getKey(), |
109 entry.getKey(), |