author | mullan |
Mon, 26 Sep 2011 17:20:45 -0700 | |
changeset 10694 | cf59e2badd14 |
parent 1337 | e8d6cef36199 |
child 18240 | cda839ac048f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* reserved comment block |
|
3 |
* DO NOT REMOVE OR ALTER! |
|
4 |
*/ |
|
5 |
/* |
|
6 |
* Copyright 1999-2004 The Apache Software Foundation. |
|
7 |
* |
|
8 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
9 |
* you may not use this file except in compliance with the License. |
|
10 |
* You may obtain a copy of the License at |
|
11 |
* |
|
12 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
13 |
* |
|
14 |
* Unless required by applicable law or agreed to in writing, software |
|
15 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
17 |
* See the License for the specific language governing permissions and |
|
18 |
* limitations under the License. |
|
19 |
* |
|
20 |
*/ |
|
21 |
package com.sun.org.apache.xml.internal.security.utils; |
|
22 |
||
23 |
||
24 |
||
25 |
import java.io.IOException; |
|
26 |
import java.io.OutputStream; |
|
1337 | 27 |
import java.security.AccessController; |
28 |
import java.security.PrivilegedAction; |
|
29 |
import java.util.HashMap; |
|
2 | 30 |
import java.util.HashSet; |
31 |
import java.util.Iterator; |
|
1337 | 32 |
import java.util.Map; |
2 | 33 |
import java.util.Set; |
34 |
||
35 |
import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException; |
|
36 |
import com.sun.org.apache.xml.internal.security.c14n.Canonicalizer; |
|
37 |
import com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException; |
|
38 |
import org.w3c.dom.Attr; |
|
39 |
import org.w3c.dom.Document; |
|
40 |
import org.w3c.dom.Element; |
|
41 |
import org.w3c.dom.NamedNodeMap; |
|
42 |
import org.w3c.dom.Node; |
|
43 |
import org.w3c.dom.NodeList; |
|
44 |
import org.w3c.dom.Text; |
|
45 |
||
46 |
||
47 |
||
48 |
/** |
|
49 |
* DOM and XML accessibility and comfort functions. |
|
50 |
* |
|
51 |
* @author Christian Geuer-Pollmann |
|
52 |
*/ |
|
53 |
public class XMLUtils { |
|
54 |
||
1337 | 55 |
private static boolean ignoreLineBreaks = |
56 |
AccessController.doPrivileged(new PrivilegedAction<Boolean>() { |
|
57 |
public Boolean run() { |
|
58 |
return Boolean.getBoolean |
|
59 |
("com.sun.org.apache.xml.internal.security.ignoreLineBreaks"); |
|
60 |
} |
|
61 |
}); |
|
62 |
||
2 | 63 |
/** |
64 |
* Constructor XMLUtils |
|
65 |
* |
|
66 |
*/ |
|
67 |
private XMLUtils() { |
|
68 |
||
69 |
// we don't allow instantiation |
|
70 |
} |
|
1337 | 71 |
public static Element getNextElement(Node el) { |
72 |
while ((el!=null) && (el.getNodeType()!=Node.ELEMENT_NODE)) { |
|
73 |
el=el.getNextSibling(); |
|
74 |
} |
|
75 |
return (Element)el; |
|
2 | 76 |
|
1337 | 77 |
} |
2 | 78 |
|
79 |
/** |
|
80 |
* @param rootNode |
|
81 |
* @param result |
|
82 |
* @param exclude |
|
83 |
* @param com wheather comments or not |
|
84 |
*/ |
|
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
85 |
public static void getSet(Node rootNode,Set<Node> result,Node exclude ,boolean com) { |
2 | 86 |
if ((exclude!=null) && isDescendantOrSelf(exclude,rootNode)){ |
87 |
return; |
|
88 |
} |
|
89 |
getSetRec(rootNode,result,exclude,com); |
|
90 |
} |
|
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
91 |
|
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
92 |
@SuppressWarnings("fallthrough") |
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
93 |
static final void getSetRec(final Node rootNode,final Set<Node> result, |
2 | 94 |
final Node exclude ,final boolean com) { |
95 |
//Set result = new HashSet(); |
|
96 |
if (rootNode==exclude) { |
|
97 |
return; |
|
98 |
} |
|
99 |
switch (rootNode.getNodeType()) { |
|
100 |
case Node.ELEMENT_NODE: |
|
101 |
result.add(rootNode); |
|
102 |
Element el=(Element)rootNode; |
|
103 |
if (el.hasAttributes()) { |
|
104 |
NamedNodeMap nl = ((Element)rootNode).getAttributes(); |
|
105 |
for (int i=0;i<nl.getLength();i++) { |
|
106 |
result.add(nl.item(i)); |
|
107 |
} |
|
108 |
} |
|
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
109 |
//no return keep working - ignore fallthrough warning |
2 | 110 |
case Node.DOCUMENT_NODE: |
111 |
for (Node r=rootNode.getFirstChild();r!=null;r=r.getNextSibling()){ |
|
112 |
if (r.getNodeType()==Node.TEXT_NODE) { |
|
113 |
result.add(r); |
|
114 |
while ((r!=null) && (r.getNodeType()==Node.TEXT_NODE)) { |
|
115 |
r=r.getNextSibling(); |
|
116 |
} |
|
117 |
if (r==null) |
|
118 |
return; |
|
119 |
} |
|
120 |
getSetRec(r,result,exclude,com); |
|
121 |
} |
|
122 |
return; |
|
123 |
case Node.COMMENT_NODE: |
|
124 |
if (com) { |
|
125 |
result.add(rootNode); |
|
126 |
} |
|
127 |
return; |
|
128 |
case Node.DOCUMENT_TYPE_NODE: |
|
129 |
return; |
|
130 |
default: |
|
131 |
result.add(rootNode); |
|
132 |
} |
|
133 |
return; |
|
134 |
} |
|
135 |
||
136 |
||
137 |
/** |
|
138 |
* Outputs a DOM tree to an {@link OutputStream}. |
|
139 |
* |
|
140 |
* @param contextNode root node of the DOM tree |
|
141 |
* @param os the {@link OutputStream} |
|
142 |
*/ |
|
143 |
public static void outputDOM(Node contextNode, OutputStream os) { |
|
144 |
XMLUtils.outputDOM(contextNode, os, false); |
|
145 |
} |
|
146 |
||
147 |
/** |
|
148 |
* Outputs a DOM tree to an {@link OutputStream}. <I>If an Exception is |
|
149 |
* thrown during execution, it's StackTrace is output to System.out, but the |
|
150 |
* Exception is not re-thrown.</I> |
|
151 |
* |
|
152 |
* @param contextNode root node of the DOM tree |
|
153 |
* @param os the {@link OutputStream} |
|
154 |
* @param addPreamble |
|
155 |
*/ |
|
156 |
public static void outputDOM(Node contextNode, OutputStream os, |
|
157 |
boolean addPreamble) { |
|
158 |
||
159 |
try { |
|
160 |
if (addPreamble) { |
|
161 |
os.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".getBytes()); |
|
162 |
} |
|
163 |
||
164 |
os.write( |
|
165 |
Canonicalizer.getInstance( |
|
166 |
Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS).canonicalizeSubtree( |
|
167 |
contextNode)); |
|
168 |
} catch (IOException ex) {} |
|
169 |
catch (InvalidCanonicalizerException ex) { |
|
170 |
ex.printStackTrace(); |
|
171 |
} catch (CanonicalizationException ex) { |
|
172 |
ex.printStackTrace(); |
|
173 |
} |
|
174 |
} |
|
175 |
||
176 |
/** |
|
177 |
* Serializes the <CODE>contextNode</CODE> into the OutputStream, <I>but |
|
178 |
* supresses all Exceptions</I>. |
|
179 |
* <BR /> |
|
180 |
* NOTE: <I>This should only be used for debugging purposes, |
|
181 |
* NOT in a production environment; this method ignores all exceptions, |
|
182 |
* so you won't notice if something goes wrong. If you're asking what is to |
|
183 |
* be used in a production environment, simply use the code inside the |
|
184 |
* <code>try{}</code> statement, but handle the Exceptions appropriately.</I> |
|
185 |
* |
|
186 |
* @param contextNode |
|
187 |
* @param os |
|
188 |
*/ |
|
189 |
public static void outputDOMc14nWithComments(Node contextNode, |
|
190 |
OutputStream os) { |
|
191 |
||
192 |
try { |
|
193 |
os.write( |
|
194 |
Canonicalizer.getInstance( |
|
195 |
Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS).canonicalizeSubtree( |
|
196 |
contextNode)); |
|
197 |
} catch (IOException ex) { |
|
198 |
||
199 |
// throw new RuntimeException(ex.getMessage()); |
|
200 |
} catch (InvalidCanonicalizerException ex) { |
|
201 |
||
202 |
// throw new RuntimeException(ex.getMessage()); |
|
203 |
} catch (CanonicalizationException ex) { |
|
204 |
||
205 |
// throw new RuntimeException(ex.getMessage()); |
|
206 |
} |
|
207 |
} |
|
208 |
||
209 |
||
210 |
/** |
|
211 |
* Method getFullTextChildrenFromElement |
|
212 |
* |
|
213 |
* @param element |
|
214 |
* @return the string of chi;ds |
|
215 |
*/ |
|
216 |
public static String getFullTextChildrenFromElement(Element element) { |
|
217 |
||
218 |
StringBuffer sb = new StringBuffer(); |
|
219 |
NodeList children = element.getChildNodes(); |
|
220 |
int iMax = children.getLength(); |
|
221 |
||
222 |
for (int i = 0; i < iMax; i++) { |
|
223 |
Node curr = children.item(i); |
|
224 |
||
225 |
if (curr.getNodeType() == Node.TEXT_NODE) { |
|
226 |
sb.append(((Text) curr).getData()); |
|
227 |
} |
|
228 |
} |
|
229 |
||
230 |
return sb.toString(); |
|
231 |
} |
|
232 |
||
233 |
||
1337 | 234 |
static String dsPrefix=null; |
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
235 |
static Map<String, String> namePrefixes=new HashMap<String, String>(); |
2 | 236 |
/** |
237 |
* Creates an Element in the XML Signature specification namespace. |
|
238 |
* |
|
239 |
* @param doc the factory Document |
|
240 |
* @param elementName the local name of the Element |
|
241 |
* @return the Element |
|
242 |
*/ |
|
243 |
public static Element createElementInSignatureSpace(Document doc, |
|
244 |
String elementName) { |
|
245 |
||
246 |
if (doc == null) { |
|
247 |
throw new RuntimeException("Document is null"); |
|
248 |
} |
|
249 |
||
1337 | 250 |
if ((dsPrefix == null) || (dsPrefix.length() == 0)) { |
251 |
return doc.createElementNS(Constants.SignatureSpecNS, elementName); |
|
2 | 252 |
} |
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
253 |
String namePrefix= namePrefixes.get(elementName); |
1337 | 254 |
if (namePrefix==null) { |
255 |
StringBuffer tag=new StringBuffer(dsPrefix); |
|
256 |
tag.append(':'); |
|
257 |
tag.append(elementName); |
|
258 |
namePrefix=tag.toString(); |
|
259 |
namePrefixes.put(elementName,namePrefix); |
|
260 |
} |
|
261 |
return doc.createElementNS(Constants.SignatureSpecNS, namePrefix); |
|
2 | 262 |
} |
263 |
||
264 |
/** |
|
265 |
* Returns true if the element is in XML Signature namespace and the local |
|
266 |
* name equals the supplied one. |
|
267 |
* |
|
268 |
* @param element |
|
269 |
* @param localName |
|
270 |
* @return true if the element is in XML Signature namespace and the local name equals the supplied one |
|
271 |
*/ |
|
272 |
public static boolean elementIsInSignatureSpace(Element element, |
|
273 |
String localName) { |
|
1337 | 274 |
return ElementProxy.checker.isNamespaceElement(element, localName, Constants.SignatureSpecNS); |
2 | 275 |
} |
276 |
||
277 |
/** |
|
278 |
* Returns true if the element is in XML Encryption namespace and the local |
|
279 |
* name equals the supplied one. |
|
280 |
* |
|
281 |
* @param element |
|
282 |
* @param localName |
|
283 |
* @return true if the element is in XML Encryption namespace and the local name equals the supplied one |
|
284 |
*/ |
|
285 |
public static boolean elementIsInEncryptionSpace(Element element, |
|
286 |
String localName) { |
|
1337 | 287 |
return ElementProxy.checker.isNamespaceElement(element, localName, EncryptionConstants.EncryptionSpecNS); |
2 | 288 |
} |
289 |
||
290 |
/** |
|
291 |
* This method returns the owner document of a particular node. |
|
292 |
* This method is necessary because it <I>always</I> returns a |
|
293 |
* {@link Document}. {@link Node#getOwnerDocument} returns <CODE>null</CODE> |
|
294 |
* if the {@link Node} is a {@link Document}. |
|
295 |
* |
|
296 |
* @param node |
|
297 |
* @return the owner document of the node |
|
298 |
*/ |
|
299 |
public static Document getOwnerDocument(Node node) { |
|
300 |
||
301 |
if (node.getNodeType() == Node.DOCUMENT_NODE) { |
|
302 |
return (Document) node; |
|
303 |
} |
|
304 |
try { |
|
305 |
return node.getOwnerDocument(); |
|
306 |
} catch (NullPointerException npe) { |
|
307 |
throw new NullPointerException(I18n.translate("endorsed.jdk1.4.0") |
|
308 |
+ " Original message was \"" |
|
309 |
+ npe.getMessage() + "\""); |
|
310 |
} |
|
311 |
||
312 |
} |
|
313 |
||
314 |
/** |
|
315 |
* This method returns the first non-null owner document of the Node's in this Set. |
|
316 |
* This method is necessary because it <I>always</I> returns a |
|
317 |
* {@link Document}. {@link Node#getOwnerDocument} returns <CODE>null</CODE> |
|
318 |
* if the {@link Node} is a {@link Document}. |
|
319 |
* |
|
320 |
* @param xpathNodeSet |
|
321 |
* @return the owner document |
|
322 |
*/ |
|
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
323 |
public static Document getOwnerDocument(Set<Node> xpathNodeSet) { |
2 | 324 |
NullPointerException npe = null; |
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
325 |
for (Node node : xpathNodeSet) { |
2 | 326 |
int nodeType =node.getNodeType(); |
327 |
if (nodeType == Node.DOCUMENT_NODE) { |
|
328 |
return (Document) node; |
|
329 |
} |
|
330 |
try { |
|
331 |
if (nodeType==Node.ATTRIBUTE_NODE) { |
|
332 |
return ((Attr)node).getOwnerElement().getOwnerDocument(); |
|
333 |
} |
|
334 |
return node.getOwnerDocument(); |
|
335 |
} catch (NullPointerException e) { |
|
336 |
npe = e; |
|
337 |
} |
|
338 |
||
339 |
} |
|
340 |
throw new NullPointerException(I18n.translate("endorsed.jdk1.4.0") |
|
341 |
+ " Original message was \"" |
|
342 |
+ (npe == null ? "" : npe.getMessage()) + "\""); |
|
343 |
} |
|
344 |
||
1337 | 345 |
/** |
346 |
* Method createDSctx |
|
347 |
* |
|
348 |
* @param doc |
|
349 |
* @param prefix |
|
350 |
* @param namespace |
|
351 |
* @return the element. |
|
352 |
*/ |
|
353 |
public static Element createDSctx(Document doc, String prefix, |
|
354 |
String namespace) { |
|
2 | 355 |
|
1337 | 356 |
if ((prefix == null) || (prefix.trim().length() == 0)) { |
357 |
throw new IllegalArgumentException("You must supply a prefix"); |
|
358 |
} |
|
2 | 359 |
|
1337 | 360 |
Element ctx = doc.createElementNS(null, "namespaceContext"); |
2 | 361 |
|
1337 | 362 |
ctx.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:" + prefix.trim(), |
363 |
namespace); |
|
2 | 364 |
|
1337 | 365 |
return ctx; |
366 |
} |
|
2 | 367 |
|
368 |
/** |
|
369 |
* Method addReturnToElement |
|
370 |
* |
|
371 |
* @param e |
|
372 |
*/ |
|
373 |
public static void addReturnToElement(Element e) { |
|
374 |
||
1337 | 375 |
if (!ignoreLineBreaks) { |
376 |
Document doc = e.getOwnerDocument(); |
|
377 |
e.appendChild(doc.createTextNode("\n")); |
|
378 |
} |
|
379 |
} |
|
2 | 380 |
|
1337 | 381 |
public static void addReturnToElement(Document doc, HelperNodeList nl) { |
382 |
if (!ignoreLineBreaks) { |
|
383 |
nl.appendChild(doc.createTextNode("\n")); |
|
384 |
} |
|
385 |
} |
|
386 |
||
387 |
public static void addReturnBeforeChild(Element e, Node child) { |
|
388 |
if (!ignoreLineBreaks) { |
|
389 |
Document doc = e.getOwnerDocument(); |
|
390 |
e.insertBefore(doc.createTextNode("\n"), child); |
|
391 |
} |
|
2 | 392 |
} |
393 |
||
394 |
/** |
|
395 |
* Method convertNodelistToSet |
|
396 |
* |
|
397 |
* @param xpathNodeSet |
|
398 |
* @return the set with the nodelist |
|
399 |
*/ |
|
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
400 |
public static Set<Node> convertNodelistToSet(NodeList xpathNodeSet) { |
2 | 401 |
|
402 |
if (xpathNodeSet == null) { |
|
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
403 |
return new HashSet<Node>(); |
2 | 404 |
} |
405 |
||
406 |
int length = xpathNodeSet.getLength(); |
|
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
407 |
Set<Node> set = new HashSet<Node>(length); |
2 | 408 |
|
409 |
for (int i = 0; i < length; i++) { |
|
410 |
set.add(xpathNodeSet.item(i)); |
|
411 |
} |
|
412 |
||
413 |
return set; |
|
414 |
} |
|
415 |
||
416 |
||
417 |
/** |
|
418 |
* This method spreads all namespace attributes in a DOM document to their |
|
419 |
* children. This is needed because the XML Signature XPath transform |
|
420 |
* must evaluate the XPath against all nodes in the input, even against |
|
421 |
* XPath namespace nodes. Through a bug in XalanJ2, the namespace nodes are |
|
422 |
* not fully visible in the Xalan XPath model, so we have to do this by |
|
423 |
* hand in DOM spaces so that the nodes become visible in XPath space. |
|
424 |
* |
|
425 |
* @param doc |
|
426 |
* @see <A HREF="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=2650">Namespace axis resolution is not XPath compliant </A> |
|
427 |
*/ |
|
428 |
public static void circumventBug2650(Document doc) { |
|
429 |
||
430 |
Element documentElement = doc.getDocumentElement(); |
|
431 |
||
432 |
// if the document element has no xmlns definition, we add xmlns="" |
|
433 |
Attr xmlnsAttr = |
|
434 |
documentElement.getAttributeNodeNS(Constants.NamespaceSpecNS, "xmlns"); |
|
435 |
||
436 |
if (xmlnsAttr == null) { |
|
437 |
documentElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", ""); |
|
438 |
} |
|
439 |
||
440 |
XMLUtils.circumventBug2650internal(doc); |
|
441 |
} |
|
442 |
||
443 |
/** |
|
444 |
* This is the work horse for {@link #circumventBug2650}. |
|
445 |
* |
|
446 |
* @param node |
|
447 |
* @see <A HREF="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=2650">Namespace axis resolution is not XPath compliant </A> |
|
448 |
*/ |
|
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
449 |
@SuppressWarnings("fallthrough") |
2 | 450 |
private static void circumventBug2650internal(Node node) { |
451 |
Node parent=null; |
|
452 |
Node sibling=null; |
|
453 |
final String namespaceNs=Constants.NamespaceSpecNS; |
|
454 |
do { |
|
455 |
switch (node.getNodeType()) { |
|
456 |
case Node.ELEMENT_NODE : |
|
457 |
Element element = (Element) node; |
|
458 |
if (!element.hasChildNodes()) |
|
459 |
break; |
|
460 |
if (element.hasAttributes()) { |
|
461 |
NamedNodeMap attributes = element.getAttributes(); |
|
462 |
int attributesLength = attributes.getLength(); |
|
463 |
||
464 |
for (Node child = element.getFirstChild(); child!=null; |
|
465 |
child=child.getNextSibling()) { |
|
466 |
||
467 |
if (child.getNodeType() != Node.ELEMENT_NODE) { |
|
468 |
continue; |
|
469 |
} |
|
470 |
Element childElement = (Element) child; |
|
471 |
||
472 |
for (int i = 0; i < attributesLength; i++) { |
|
473 |
Attr currentAttr = (Attr) attributes.item(i); |
|
1337 | 474 |
if (namespaceNs!=currentAttr.getNamespaceURI()) |
2 | 475 |
continue; |
476 |
if (childElement.hasAttributeNS(namespaceNs, |
|
477 |
currentAttr.getLocalName())) { |
|
478 |
continue; |
|
479 |
} |
|
480 |
childElement.setAttributeNS(namespaceNs, |
|
481 |
currentAttr.getName(), |
|
482 |
currentAttr.getNodeValue()); |
|
483 |
||
484 |
||
485 |
} |
|
486 |
} |
|
487 |
} |
|
488 |
case Node.ENTITY_REFERENCE_NODE : |
|
489 |
case Node.DOCUMENT_NODE : |
|
490 |
parent=node; |
|
491 |
sibling=node.getFirstChild(); |
|
492 |
break; |
|
493 |
} |
|
494 |
while ((sibling==null) && (parent!=null)) { |
|
495 |
sibling=parent.getNextSibling(); |
|
496 |
parent=parent.getParentNode(); |
|
497 |
}; |
|
498 |
if (sibling==null) { |
|
499 |
return; |
|
500 |
} |
|
501 |
||
502 |
node=sibling; |
|
503 |
sibling=node.getNextSibling(); |
|
504 |
} while (true); |
|
505 |
} |
|
506 |
||
507 |
/** |
|
508 |
* @param sibling |
|
509 |
* @param nodeName |
|
510 |
* @param number |
|
511 |
* @return nodes with the constrain |
|
512 |
*/ |
|
513 |
public static Element selectDsNode(Node sibling, String nodeName, int number) { |
|
514 |
while (sibling!=null) { |
|
1337 | 515 |
if (ElementProxy.checker.isNamespaceElement(sibling, nodeName, Constants.SignatureSpecNS )) { |
2 | 516 |
if (number==0){ |
517 |
return (Element)sibling; |
|
518 |
} |
|
519 |
number--; |
|
520 |
} |
|
521 |
sibling=sibling.getNextSibling(); |
|
522 |
} |
|
523 |
return null; |
|
524 |
} |
|
525 |
||
526 |
/** |
|
527 |
* @param sibling |
|
528 |
* @param nodeName |
|
529 |
* @param number |
|
530 |
* @return nodes with the constrain |
|
531 |
*/ |
|
532 |
||
533 |
public static Element selectXencNode(Node sibling, String nodeName, int number) { |
|
534 |
while (sibling!=null) { |
|
1337 | 535 |
if (ElementProxy.checker.isNamespaceElement(sibling, nodeName, EncryptionConstants.EncryptionSpecNS )) { |
2 | 536 |
if (number==0){ |
537 |
return (Element)sibling; |
|
538 |
} |
|
539 |
number--; |
|
540 |
} |
|
541 |
sibling=sibling.getNextSibling(); |
|
542 |
} |
|
543 |
return null; |
|
544 |
} |
|
545 |
||
546 |
||
547 |
/** |
|
548 |
* @param sibling |
|
549 |
* @param nodeName |
|
550 |
* @param number |
|
551 |
* @return nodes with the constrain |
|
552 |
*/ |
|
553 |
public static Text selectDsNodeText(Node sibling, String nodeName, int number) { |
|
554 |
Node n=selectDsNode(sibling,nodeName,number); |
|
555 |
if (n==null) { |
|
556 |
return null; |
|
557 |
} |
|
558 |
n=n.getFirstChild(); |
|
559 |
while (n!=null && n.getNodeType()!=Node.TEXT_NODE) { |
|
560 |
n=n.getNextSibling(); |
|
561 |
} |
|
562 |
return (Text)n; |
|
563 |
} |
|
564 |
||
565 |
/** |
|
566 |
* @param sibling |
|
567 |
* @param uri |
|
568 |
* @param nodeName |
|
569 |
* @param number |
|
570 |
* @return nodes with the constrain |
|
571 |
*/ |
|
572 |
public static Text selectNodeText(Node sibling, String uri, String nodeName, int number) { |
|
573 |
Node n=selectNode(sibling,uri,nodeName,number); |
|
574 |
if (n==null) { |
|
575 |
return null; |
|
576 |
} |
|
577 |
n=n.getFirstChild(); |
|
578 |
while (n!=null && n.getNodeType()!=Node.TEXT_NODE) { |
|
579 |
n=n.getNextSibling(); |
|
580 |
} |
|
581 |
return (Text)n; |
|
582 |
} |
|
583 |
||
584 |
/** |
|
585 |
* @param sibling |
|
586 |
* @param uri |
|
587 |
* @param nodeName |
|
588 |
* @param number |
|
589 |
* @return nodes with the constrain |
|
590 |
*/ |
|
591 |
public static Element selectNode(Node sibling, String uri,String nodeName, int number) { |
|
592 |
while (sibling!=null) { |
|
1337 | 593 |
if (ElementProxy.checker.isNamespaceElement(sibling, nodeName, uri)) { |
2 | 594 |
if (number==0){ |
595 |
return (Element)sibling; |
|
596 |
} |
|
597 |
number--; |
|
598 |
} |
|
599 |
sibling=sibling.getNextSibling(); |
|
600 |
} |
|
601 |
return null; |
|
602 |
} |
|
603 |
||
604 |
/** |
|
605 |
* @param sibling |
|
606 |
* @param nodeName |
|
607 |
* @return nodes with the constrain |
|
608 |
*/ |
|
609 |
public static Element[] selectDsNodes(Node sibling,String nodeName) { |
|
610 |
return selectNodes(sibling,Constants.SignatureSpecNS,nodeName); |
|
611 |
} |
|
612 |
/** |
|
613 |
* @param sibling |
|
614 |
* @param uri |
|
615 |
* @param nodeName |
|
616 |
* @return nodes with the constrain |
|
617 |
*/ |
|
618 |
public static Element[] selectNodes(Node sibling,String uri,String nodeName) { |
|
619 |
int size=20; |
|
620 |
Element[] a= new Element[size]; |
|
621 |
int curr=0; |
|
622 |
//List list=new ArrayList(); |
|
623 |
while (sibling!=null) { |
|
1337 | 624 |
if (ElementProxy.checker.isNamespaceElement(sibling, nodeName, uri)) { |
2 | 625 |
a[curr++]=(Element)sibling; |
626 |
if (size<=curr) { |
|
627 |
int cursize= size<<2; |
|
628 |
Element []cp=new Element[cursize]; |
|
629 |
System.arraycopy(a,0,cp,0,size); |
|
630 |
a=cp; |
|
631 |
size=cursize; |
|
632 |
} |
|
633 |
} |
|
634 |
sibling=sibling.getNextSibling(); |
|
635 |
} |
|
636 |
Element []af=new Element[curr]; |
|
637 |
System.arraycopy(a,0,af,0,curr); |
|
638 |
return af; |
|
639 |
} |
|
640 |
||
641 |
/** |
|
642 |
* @param signatureElement |
|
643 |
* @param inputSet |
|
644 |
* @return nodes with the constrain |
|
645 |
*/ |
|
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
646 |
public static Set<Node> excludeNodeFromSet(Node signatureElement, Set<Node> inputSet) { |
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
647 |
Set<Node> resultSet = new HashSet<Node>(); |
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
648 |
Iterator<Node> iterator = inputSet.iterator(); |
2 | 649 |
|
650 |
while (iterator.hasNext()) { |
|
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
651 |
Node inputNode = iterator.next(); |
2 | 652 |
|
653 |
if (!XMLUtils |
|
654 |
.isDescendantOrSelf(signatureElement, inputNode)) { |
|
655 |
resultSet.add(inputNode); |
|
656 |
} |
|
657 |
} |
|
658 |
return resultSet; |
|
659 |
} |
|
660 |
||
661 |
/** |
|
662 |
* Returns true if the descendantOrSelf is on the descendant-or-self axis |
|
663 |
* of the context node. |
|
664 |
* |
|
665 |
* @param ctx |
|
666 |
* @param descendantOrSelf |
|
667 |
* @return true if the node is descendant |
|
668 |
*/ |
|
669 |
static public boolean isDescendantOrSelf(Node ctx, Node descendantOrSelf) { |
|
670 |
||
671 |
if (ctx == descendantOrSelf) { |
|
672 |
return true; |
|
673 |
} |
|
674 |
||
675 |
Node parent = descendantOrSelf; |
|
676 |
||
677 |
while (true) { |
|
678 |
if (parent == null) { |
|
679 |
return false; |
|
680 |
} |
|
681 |
||
682 |
if (parent == ctx) { |
|
683 |
return true; |
|
684 |
} |
|
685 |
||
686 |
if (parent.getNodeType() == Node.ATTRIBUTE_NODE) { |
|
687 |
parent = ((Attr) parent).getOwnerElement(); |
|
688 |
} else { |
|
689 |
parent = parent.getParentNode(); |
|
690 |
} |
|
691 |
} |
|
692 |
} |
|
1337 | 693 |
|
694 |
public static boolean ignoreLineBreaks() { |
|
695 |
return ignoreLineBreaks; |
|
696 |
} |
|
2 | 697 |
} |