author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 27747 | jdk/src/java.xml.crypto/share/classes/org/jcp/xml/dsig/internal/dom/DOMUtils.java@3a271dc8b758 |
child 50614 | 3810c9a2efa1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
1337 | 2 |
* reserved comment block |
3 |
* DO NOT REMOVE OR ALTER! |
|
2 | 4 |
*/ |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
5 |
/** |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
6 |
* Licensed to the Apache Software Foundation (ASF) under one |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
7 |
* or more contributor license agreements. See the NOTICE file |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
8 |
* distributed with this work for additional information |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
9 |
* regarding copyright ownership. The ASF licenses this file |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
10 |
* to you under the Apache License, Version 2.0 (the |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
11 |
* "License"); you may not use this file except in compliance |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
12 |
* with the License. You may obtain a copy of the License at |
1337 | 13 |
* |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
14 |
* http://www.apache.org/licenses/LICENSE-2.0 |
1337 | 15 |
* |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
16 |
* Unless required by applicable law or agreed to in writing, |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
17 |
* software distributed under the License is distributed on an |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
18 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
19 |
* KIND, either express or implied. See the License for the |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
20 |
* specific language governing permissions and limitations |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
21 |
* under the License. |
1337 | 22 |
*/ |
23 |
/* |
|
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
19051
diff
changeset
|
24 |
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. |
1337 | 25 |
*/ |
26 |
/* |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
27 |
* $Id: DOMUtils.java 1333415 2012-05-03 12:03:51Z coheigea $ |
2 | 28 |
*/ |
29 |
package org.jcp.xml.dsig.internal.dom; |
|
30 |
||
31 |
import java.util.*; |
|
32 |
import java.security.spec.AlgorithmParameterSpec; |
|
33 |
import org.w3c.dom.Attr; |
|
34 |
import org.w3c.dom.Document; |
|
35 |
import org.w3c.dom.Element; |
|
36 |
import org.w3c.dom.Node; |
|
37 |
import org.w3c.dom.NodeList; |
|
38 |
import javax.xml.crypto.*; |
|
39 |
import javax.xml.crypto.dsig.*; |
|
40 |
import javax.xml.crypto.dsig.spec.*; |
|
41 |
||
42 |
/** |
|
43 |
* Useful static DOM utility methods. |
|
44 |
* |
|
45 |
* @author Sean Mullan |
|
46 |
*/ |
|
47 |
public class DOMUtils { |
|
48 |
||
49 |
// class cannot be instantiated |
|
50 |
private DOMUtils() {} |
|
51 |
||
52 |
/** |
|
53 |
* Returns the owner document of the specified node. |
|
54 |
* |
|
55 |
* @param node the node |
|
56 |
* @return the owner document |
|
57 |
*/ |
|
58 |
public static Document getOwnerDocument(Node node) { |
|
59 |
if (node.getNodeType() == Node.DOCUMENT_NODE) { |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
60 |
return (Document)node; |
2 | 61 |
} else { |
62 |
return node.getOwnerDocument(); |
|
63 |
} |
|
64 |
} |
|
65 |
||
66 |
/** |
|
67 |
* Creates an element in the specified namespace, with the specified tag |
|
68 |
* and namespace prefix. |
|
69 |
* |
|
70 |
* @param doc the owner document |
|
71 |
* @param tag the tag |
|
72 |
* @param nsURI the namespace URI |
|
73 |
* @param prefix the namespace prefix |
|
74 |
* @return the newly created element |
|
75 |
*/ |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
76 |
public static Element createElement(Document doc, String tag, |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
77 |
String nsURI, String prefix) |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
78 |
{ |
1337 | 79 |
String qName = (prefix == null || prefix.length() == 0) |
80 |
? tag : prefix + ":" + tag; |
|
2 | 81 |
return doc.createElementNS(nsURI, qName); |
82 |
} |
|
83 |
||
84 |
/** |
|
85 |
* Sets an element's attribute (using DOM level 2) with the |
|
86 |
* specified value and namespace prefix. |
|
87 |
* |
|
88 |
* @param elem the element to set the attribute on |
|
89 |
* @param name the name of the attribute |
|
90 |
* @param value the attribute value. If null, no attribute is set. |
|
91 |
*/ |
|
92 |
public static void setAttribute(Element elem, String name, String value) { |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
93 |
if (value == null) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
94 |
return; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
95 |
} |
2 | 96 |
elem.setAttributeNS(null, name, value); |
97 |
} |
|
98 |
||
99 |
/** |
|
100 |
* Sets an element's attribute (using DOM level 2) with the |
|
101 |
* specified value and namespace prefix AND registers the ID value with |
|
102 |
* the specified element. This is for resolving same-document |
|
103 |
* ID references. |
|
104 |
* |
|
105 |
* @param elem the element to set the attribute on |
|
106 |
* @param name the name of the attribute |
|
107 |
* @param value the attribute value. If null, no attribute is set. |
|
108 |
*/ |
|
109 |
public static void setAttributeID(Element elem, String name, String value) { |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
110 |
if (value == null) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
111 |
return; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
112 |
} |
2 | 113 |
elem.setAttributeNS(null, name, value); |
18240 | 114 |
elem.setIdAttributeNS(null, name, true); |
2 | 115 |
} |
116 |
||
117 |
/** |
|
118 |
* Returns the first child element of the specified node, or null if there |
|
119 |
* is no such element. |
|
120 |
* |
|
121 |
* @param node the node |
|
122 |
* @return the first child element of the specified node, or null if there |
|
123 |
* is no such element |
|
124 |
* @throws NullPointerException if <code>node == null</code> |
|
125 |
*/ |
|
126 |
public static Element getFirstChildElement(Node node) { |
|
127 |
Node child = node.getFirstChild(); |
|
128 |
while (child != null && child.getNodeType() != Node.ELEMENT_NODE) { |
|
129 |
child = child.getNextSibling(); |
|
130 |
} |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
131 |
return (Element)child; |
2 | 132 |
} |
133 |
||
134 |
/** |
|
19051
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
135 |
* Returns the first child element of the specified node and checks that |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
136 |
* the local name is equal to {@code localName}. |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
137 |
* |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
138 |
* @param node the node |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
139 |
* @return the first child element of the specified node |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
140 |
* @throws NullPointerException if {@code node == null} |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
141 |
* @throws MarshalException if no such element or the local name is not |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
142 |
* equal to {@code localName} |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
143 |
*/ |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
144 |
public static Element getFirstChildElement(Node node, String localName) |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
145 |
throws MarshalException |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
146 |
{ |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
147 |
return verifyElement(getFirstChildElement(node), localName); |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
148 |
} |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
149 |
|
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
150 |
private static Element verifyElement(Element elem, String localName) |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
151 |
throws MarshalException |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
152 |
{ |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
153 |
if (elem == null) { |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
154 |
throw new MarshalException("Missing " + localName + " element"); |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
155 |
} |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
156 |
String name = elem.getLocalName(); |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
157 |
if (!name.equals(localName)) { |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
158 |
throw new MarshalException("Invalid element name: " + |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
159 |
name + ", expected " + localName); |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
160 |
} |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
161 |
return elem; |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
162 |
} |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
163 |
|
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
164 |
/** |
2 | 165 |
* Returns the last child element of the specified node, or null if there |
166 |
* is no such element. |
|
167 |
* |
|
168 |
* @param node the node |
|
169 |
* @return the last child element of the specified node, or null if there |
|
170 |
* is no such element |
|
171 |
* @throws NullPointerException if <code>node == null</code> |
|
172 |
*/ |
|
173 |
public static Element getLastChildElement(Node node) { |
|
174 |
Node child = node.getLastChild(); |
|
175 |
while (child != null && child.getNodeType() != Node.ELEMENT_NODE) { |
|
176 |
child = child.getPreviousSibling(); |
|
177 |
} |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
178 |
return (Element)child; |
2 | 179 |
} |
180 |
||
181 |
/** |
|
182 |
* Returns the next sibling element of the specified node, or null if there |
|
183 |
* is no such element. |
|
184 |
* |
|
185 |
* @param node the node |
|
186 |
* @return the next sibling element of the specified node, or null if there |
|
187 |
* is no such element |
|
188 |
* @throws NullPointerException if <code>node == null</code> |
|
189 |
*/ |
|
190 |
public static Element getNextSiblingElement(Node node) { |
|
191 |
Node sibling = node.getNextSibling(); |
|
192 |
while (sibling != null && sibling.getNodeType() != Node.ELEMENT_NODE) { |
|
193 |
sibling = sibling.getNextSibling(); |
|
194 |
} |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
195 |
return (Element)sibling; |
2 | 196 |
} |
197 |
||
198 |
/** |
|
19051
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
199 |
* Returns the next sibling element of the specified node and checks that |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
200 |
* the local name is equal to {@code localName}. |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
201 |
* |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
202 |
* @param node the node |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
203 |
* @return the next sibling element of the specified node |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
204 |
* @throws NullPointerException if {@code node == null} |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
205 |
* @throws MarshalException if no such element or the local name is not |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
206 |
* equal to {@code localName} |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
207 |
*/ |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
208 |
public static Element getNextSiblingElement(Node node, String localName) |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
209 |
throws MarshalException |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
210 |
{ |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
211 |
return verifyElement(getNextSiblingElement(node), localName); |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
212 |
} |
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
213 |
|
6c0cfc00b3ed
8012288: XML DSig API allows wrong tag names and extra elements in SignedInfo
mullan
parents:
18780
diff
changeset
|
214 |
/** |
2 | 215 |
* Returns the attribute value for the attribute with the specified name. |
216 |
* Returns null if there is no such attribute, or |
|
217 |
* the empty string if the attribute value is empty. |
|
218 |
* |
|
219 |
* <p>This works around a limitation of the DOM |
|
220 |
* <code>Element.getAttributeNode</code> method, which does not distinguish |
|
221 |
* between an unspecified attribute and an attribute with a value of |
|
222 |
* "" (it returns "" for both cases). |
|
223 |
* |
|
224 |
* @param elem the element containing the attribute |
|
225 |
* @param name the name of the attribute |
|
226 |
* @return the attribute value (may be null if unspecified) |
|
227 |
*/ |
|
228 |
public static String getAttributeValue(Element elem, String name) { |
|
229 |
Attr attr = elem.getAttributeNodeNS(null, name); |
|
230 |
return (attr == null) ? null : attr.getValue(); |
|
231 |
} |
|
232 |
||
233 |
/** |
|
234 |
* Returns a Set of <code>Node</code>s, backed by the specified |
|
235 |
* <code>NodeList</code>. |
|
236 |
* |
|
237 |
* @param nl the NodeList |
|
238 |
* @return a Set of Nodes |
|
239 |
*/ |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
240 |
public static Set<Node> nodeSet(NodeList nl) { |
2 | 241 |
return new NodeSet(nl); |
242 |
} |
|
243 |
||
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
244 |
static class NodeSet extends AbstractSet<Node> { |
2 | 245 |
private NodeList nl; |
246 |
public NodeSet(NodeList nl) { |
|
247 |
this.nl = nl; |
|
248 |
} |
|
249 |
||
250 |
public int size() { return nl.getLength(); } |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
251 |
public Iterator<Node> iterator() { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
252 |
return new Iterator<Node>() { |
2 | 253 |
int index = 0; |
254 |
||
255 |
public void remove() { |
|
256 |
throw new UnsupportedOperationException(); |
|
257 |
} |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
258 |
public Node next() { |
2 | 259 |
if (!hasNext()) { |
260 |
throw new NoSuchElementException(); |
|
261 |
} |
|
262 |
return nl.item(index++); |
|
263 |
} |
|
264 |
public boolean hasNext() { |
|
265 |
return index < nl.getLength() ? true : false; |
|
266 |
} |
|
267 |
}; |
|
268 |
} |
|
269 |
} |
|
270 |
||
271 |
/** |
|
272 |
* Returns the prefix associated with the specified namespace URI |
|
273 |
* |
|
274 |
* @param context contains the namespace map |
|
275 |
* @param nsURI the namespace URI |
|
276 |
* @return the prefix associated with the specified namespace URI, or |
|
277 |
* null if not set |
|
278 |
*/ |
|
279 |
public static String getNSPrefix(XMLCryptoContext context, String nsURI) { |
|
280 |
if (context != null) { |
|
281 |
return context.getNamespacePrefix |
|
282 |
(nsURI, context.getDefaultNamespacePrefix()); |
|
283 |
} else { |
|
284 |
return null; |
|
285 |
} |
|
286 |
} |
|
287 |
||
288 |
/** |
|
289 |
* Returns the prefix associated with the XML Signature namespace URI |
|
290 |
* |
|
291 |
* @param context contains the namespace map |
|
292 |
* @return the prefix associated with the specified namespace URI, or |
|
293 |
* null if not set |
|
294 |
*/ |
|
295 |
public static String getSignaturePrefix(XMLCryptoContext context) { |
|
296 |
return getNSPrefix(context, XMLSignature.XMLNS); |
|
297 |
} |
|
298 |
||
299 |
/** |
|
300 |
* Removes all children nodes from the specified node. |
|
301 |
* |
|
302 |
* @param node the parent node whose children are to be removed |
|
303 |
*/ |
|
304 |
public static void removeAllChildren(Node node) { |
|
305 |
NodeList children = node.getChildNodes(); |
|
306 |
for (int i = 0, length = children.getLength(); i < length; i++) { |
|
307 |
node.removeChild(children.item(i)); |
|
308 |
} |
|
309 |
} |
|
310 |
||
311 |
/** |
|
312 |
* Compares 2 nodes for equality. Implementation is not complete. |
|
313 |
*/ |
|
314 |
public static boolean nodesEqual(Node thisNode, Node otherNode) { |
|
315 |
if (thisNode == otherNode) { |
|
316 |
return true; |
|
317 |
} |
|
318 |
if (thisNode.getNodeType() != otherNode.getNodeType()) { |
|
319 |
return false; |
|
320 |
} |
|
321 |
// FIXME - test content, etc |
|
322 |
return true; |
|
323 |
} |
|
324 |
||
325 |
/** |
|
326 |
* Checks if child element has same owner document before |
|
327 |
* appending to the parent, and imports it to the parent's document |
|
328 |
* if necessary. |
|
329 |
*/ |
|
330 |
public static void appendChild(Node parent, Node child) { |
|
331 |
Document ownerDoc = getOwnerDocument(parent); |
|
332 |
if (child.getOwnerDocument() != ownerDoc) { |
|
333 |
parent.appendChild(ownerDoc.importNode(child, true)); |
|
334 |
} else { |
|
335 |
parent.appendChild(child); |
|
336 |
} |
|
337 |
} |
|
338 |
||
339 |
public static boolean paramsEqual(AlgorithmParameterSpec spec1, |
|
340 |
AlgorithmParameterSpec spec2) { |
|
341 |
if (spec1 == spec2) { |
|
342 |
return true; |
|
343 |
} |
|
344 |
if (spec1 instanceof XPathFilter2ParameterSpec && |
|
345 |
spec2 instanceof XPathFilter2ParameterSpec) { |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
346 |
return paramsEqual((XPathFilter2ParameterSpec)spec1, |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
347 |
(XPathFilter2ParameterSpec)spec2); |
2 | 348 |
} |
349 |
if (spec1 instanceof ExcC14NParameterSpec && |
|
350 |
spec2 instanceof ExcC14NParameterSpec) { |
|
351 |
return paramsEqual((ExcC14NParameterSpec) spec1, |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
352 |
(ExcC14NParameterSpec)spec2); |
2 | 353 |
} |
354 |
if (spec1 instanceof XPathFilterParameterSpec && |
|
355 |
spec2 instanceof XPathFilterParameterSpec) { |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
356 |
return paramsEqual((XPathFilterParameterSpec)spec1, |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
357 |
(XPathFilterParameterSpec)spec2); |
2 | 358 |
} |
359 |
if (spec1 instanceof XSLTTransformParameterSpec && |
|
360 |
spec2 instanceof XSLTTransformParameterSpec) { |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
361 |
return paramsEqual((XSLTTransformParameterSpec)spec1, |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
362 |
(XSLTTransformParameterSpec)spec2); |
2 | 363 |
} |
364 |
return false; |
|
365 |
} |
|
366 |
||
367 |
private static boolean paramsEqual(XPathFilter2ParameterSpec spec1, |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
368 |
XPathFilter2ParameterSpec spec2) |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
369 |
{ |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
370 |
List<XPathType> types = spec1.getXPathList(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
371 |
List<XPathType> otypes = spec2.getXPathList(); |
2 | 372 |
int size = types.size(); |
373 |
if (size != otypes.size()) { |
|
374 |
return false; |
|
375 |
} |
|
376 |
for (int i = 0; i < size; i++) { |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
377 |
XPathType type = types.get(i); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
378 |
XPathType otype = otypes.get(i); |
2 | 379 |
if (!type.getExpression().equals(otype.getExpression()) || |
1337 | 380 |
!type.getNamespaceMap().equals(otype.getNamespaceMap()) || |
2 | 381 |
type.getFilter() != otype.getFilter()) { |
382 |
return false; |
|
383 |
} |
|
384 |
} |
|
385 |
return true; |
|
386 |
} |
|
387 |
||
388 |
private static boolean paramsEqual(ExcC14NParameterSpec spec1, |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
389 |
ExcC14NParameterSpec spec2) |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
390 |
{ |
2 | 391 |
return spec1.getPrefixList().equals(spec2.getPrefixList()); |
392 |
} |
|
393 |
||
394 |
private static boolean paramsEqual(XPathFilterParameterSpec spec1, |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
395 |
XPathFilterParameterSpec spec2) |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
396 |
{ |
1337 | 397 |
return (spec1.getXPath().equals(spec2.getXPath()) && |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
398 |
spec1.getNamespaceMap().equals(spec2.getNamespaceMap())); |
2 | 399 |
} |
400 |
||
401 |
private static boolean paramsEqual(XSLTTransformParameterSpec spec1, |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
402 |
XSLTTransformParameterSpec spec2) |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
403 |
{ |
2 | 404 |
|
405 |
XMLStructure ostylesheet = spec2.getStylesheet(); |
|
406 |
if (!(ostylesheet instanceof javax.xml.crypto.dom.DOMStructure)) { |
|
407 |
return false; |
|
408 |
} |
|
409 |
Node ostylesheetElem = |
|
410 |
((javax.xml.crypto.dom.DOMStructure) ostylesheet).getNode(); |
|
411 |
XMLStructure stylesheet = spec1.getStylesheet(); |
|
412 |
Node stylesheetElem = |
|
413 |
((javax.xml.crypto.dom.DOMStructure) stylesheet).getNode(); |
|
414 |
return nodesEqual(stylesheetElem, ostylesheetElem); |
|
415 |
} |
|
416 |
} |