author | weijun |
Tue, 19 Jun 2018 08:06:35 +0800 | |
changeset 50614 | 3810c9a2efa1 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* reserved comment block |
|
3 |
* DO NOT REMOVE OR ALTER! |
|
4 |
*/ |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
5 |
/** |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
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:
11674
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:
11674
diff
changeset
|
8 |
* distributed with this work for additional information |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
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:
11674
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:
11674
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:
11674
diff
changeset
|
12 |
* with the License. You may obtain a copy of the License at |
2 | 13 |
* |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
14 |
* http://www.apache.org/licenses/LICENSE-2.0 |
2 | 15 |
* |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
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:
11674
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:
11674
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:
11674
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:
11674
diff
changeset
|
20 |
* specific language governing permissions and limitations |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
21 |
* under the License. |
2 | 22 |
*/ |
23 |
package com.sun.org.apache.xml.internal.security.transforms.implementations; |
|
24 |
||
25 |
import java.io.IOException; |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
26 |
import java.io.OutputStream; |
2 | 27 |
import java.util.ArrayList; |
1337 | 28 |
import java.util.HashSet; |
2 | 29 |
import java.util.List; |
1337 | 30 |
import java.util.Set; |
2 | 31 |
|
32 |
import javax.xml.parsers.ParserConfigurationException; |
|
33 |
import javax.xml.transform.TransformerException; |
|
34 |
||
35 |
import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException; |
|
36 |
import com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException; |
|
37 |
import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException; |
|
38 |
import com.sun.org.apache.xml.internal.security.signature.NodeFilter; |
|
39 |
import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput; |
|
1337 | 40 |
import com.sun.org.apache.xml.internal.security.transforms.Transform; |
2 | 41 |
import com.sun.org.apache.xml.internal.security.transforms.TransformSpi; |
42 |
import com.sun.org.apache.xml.internal.security.transforms.TransformationException; |
|
43 |
import com.sun.org.apache.xml.internal.security.transforms.Transforms; |
|
44 |
import com.sun.org.apache.xml.internal.security.transforms.params.XPath2FilterContainer; |
|
45 |
import com.sun.org.apache.xml.internal.security.utils.XMLUtils; |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
46 |
import com.sun.org.apache.xml.internal.security.utils.XPathAPI; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
47 |
import com.sun.org.apache.xml.internal.security.utils.XPathFactory; |
2 | 48 |
import org.w3c.dom.DOMException; |
49 |
import org.w3c.dom.Document; |
|
50 |
import org.w3c.dom.Element; |
|
51 |
import org.w3c.dom.Node; |
|
52 |
import org.w3c.dom.NodeList; |
|
53 |
import org.xml.sax.SAXException; |
|
54 |
||
55 |
/** |
|
56 |
* Implements the <I>XML Signature XPath Filter v2.0</I> |
|
57 |
* |
|
58 |
* @see <A HREF="http://www.w3.org/TR/xmldsig-filter2/">XPath Filter v2.0 (TR)</A> |
|
59 |
*/ |
|
60 |
public class TransformXPath2Filter extends TransformSpi { |
|
61 |
||
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
62 |
/** Field implementedTransformURI */ |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
63 |
public static final String implementedTransformURI = |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
64 |
Transforms.TRANSFORM_XPATH2FILTER; |
2 | 65 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
66 |
/** |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
67 |
* Method engineGetURI |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
68 |
* |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
69 |
* {@inheritDoc} |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
70 |
*/ |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
71 |
protected String engineGetURI() { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
72 |
return implementedTransformURI; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
73 |
} |
2 | 74 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
75 |
/** |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
76 |
* Method enginePerformTransform |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
77 |
* {@inheritDoc} |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
78 |
* @param input |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
79 |
* |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
80 |
* @throws TransformationException |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
81 |
*/ |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
82 |
protected XMLSignatureInput enginePerformTransform( |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
83 |
XMLSignatureInput input, OutputStream os, Transform transformObject |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
84 |
) throws TransformationException { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
85 |
try { |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
86 |
List<NodeList> unionNodes = new ArrayList<>(); |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
87 |
List<NodeList> subtractNodes = new ArrayList<>(); |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
88 |
List<NodeList> intersectNodes = new ArrayList<>(); |
2 | 89 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
90 |
Element[] xpathElements = |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
91 |
XMLUtils.selectNodes( |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
92 |
transformObject.getElement().getFirstChild(), |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
93 |
XPath2FilterContainer.XPathFilter2NS, |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
94 |
XPath2FilterContainer._TAG_XPATH2 |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
95 |
); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
96 |
if (xpathElements.length == 0) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
97 |
Object exArgs[] = { Transforms.TRANSFORM_XPATH2FILTER, "XPath" }; |
2 | 98 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
99 |
throw new TransformationException("xml.WrongContent", exArgs); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
100 |
} |
2 | 101 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
102 |
Document inputDoc = null; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
103 |
if (input.getSubNode() != null) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
104 |
inputDoc = XMLUtils.getOwnerDocument(input.getSubNode()); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
105 |
} else { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
106 |
inputDoc = XMLUtils.getOwnerDocument(input.getNodeSet()); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
107 |
} |
2 | 108 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
109 |
for (int i = 0; i < xpathElements.length; i++) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
110 |
Element xpathElement = xpathElements[i]; |
2 | 111 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
112 |
XPath2FilterContainer xpathContainer = |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
113 |
XPath2FilterContainer.newInstance(xpathElement, input.getSourceURI()); |
2 | 114 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
115 |
String str = |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
116 |
XMLUtils.getStrFromNode(xpathContainer.getXPathFilterTextNode()); |
2 | 117 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
118 |
XPathFactory xpathFactory = XPathFactory.newInstance(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
119 |
XPathAPI xpathAPIInstance = xpathFactory.newXPathAPI(); |
2 | 120 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
121 |
NodeList subtreeRoots = |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
122 |
xpathAPIInstance.selectNodeList( |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
123 |
inputDoc, |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
124 |
xpathContainer.getXPathFilterTextNode(), |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
125 |
str, |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
126 |
xpathContainer.getElement()); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
127 |
if (xpathContainer.isIntersect()) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
128 |
intersectNodes.add(subtreeRoots); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
129 |
} else if (xpathContainer.isSubtract()) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
130 |
subtractNodes.add(subtreeRoots); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
131 |
} else if (xpathContainer.isUnion()) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
132 |
unionNodes.add(subtreeRoots); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
133 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
134 |
} |
2 | 135 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
136 |
input.addNodeFilter( |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
137 |
new XPath2NodeFilter(unionNodes, subtractNodes, intersectNodes) |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
138 |
); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
139 |
input.setNodeSet(true); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
140 |
return input; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
141 |
} catch (TransformerException ex) { |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
142 |
throw new TransformationException(ex); |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
143 |
} catch (DOMException ex) { |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
144 |
throw new TransformationException(ex); |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
145 |
} catch (CanonicalizationException ex) { |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
146 |
throw new TransformationException(ex); |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
147 |
} catch (InvalidCanonicalizerException ex) { |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
148 |
throw new TransformationException(ex); |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
149 |
} catch (XMLSecurityException ex) { |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
150 |
throw new TransformationException(ex); |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
151 |
} catch (SAXException ex) { |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
152 |
throw new TransformationException(ex); |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
153 |
} catch (IOException ex) { |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
154 |
throw new TransformationException(ex); |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
155 |
} catch (ParserConfigurationException ex) { |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
156 |
throw new TransformationException(ex); |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
157 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
158 |
} |
2 | 159 |
} |
160 |
||
161 |
class XPath2NodeFilter implements NodeFilter { |
|
162 |
||
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
163 |
boolean hasUnionFilter; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
164 |
boolean hasSubtractFilter; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
165 |
boolean hasIntersectFilter; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
166 |
Set<Node> unionNodes; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
167 |
Set<Node> subtractNodes; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
168 |
Set<Node> intersectNodes; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
169 |
int inSubtract = -1; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
170 |
int inIntersect = -1; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
171 |
int inUnion = -1; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
172 |
|
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
173 |
XPath2NodeFilter(List<NodeList> unionNodes, List<NodeList> subtractNodes, |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
174 |
List<NodeList> intersectNodes) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
175 |
hasUnionFilter = !unionNodes.isEmpty(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
176 |
this.unionNodes = convertNodeListToSet(unionNodes); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
177 |
hasSubtractFilter = !subtractNodes.isEmpty(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
178 |
this.subtractNodes = convertNodeListToSet(subtractNodes); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
179 |
hasIntersectFilter = !intersectNodes.isEmpty(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
180 |
this.intersectNodes = convertNodeListToSet(intersectNodes); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
181 |
} |
2 | 182 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
183 |
/** |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
184 |
* @see com.sun.org.apache.xml.internal.security.signature.NodeFilter#isNodeInclude(org.w3c.dom.Node) |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
185 |
*/ |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
186 |
public int isNodeInclude(Node currentNode) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
187 |
int result = 1; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
188 |
|
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
189 |
if (hasSubtractFilter && rooted(currentNode, subtractNodes)) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
190 |
result = -1; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
191 |
} else if (hasIntersectFilter && !rooted(currentNode, intersectNodes)) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
192 |
result = 0; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
193 |
} |
1337 | 194 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
195 |
//TODO OPTIMIZE |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
196 |
if (result == 1) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
197 |
return 1; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
198 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
199 |
if (hasUnionFilter) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
200 |
if (rooted(currentNode, unionNodes)) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
201 |
return 1; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
202 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
203 |
result = 0; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
204 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
205 |
return result; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
206 |
} |
2 | 207 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
208 |
public int isNodeIncludeDO(Node n, int level) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
209 |
int result = 1; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
210 |
if (hasSubtractFilter) { |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
211 |
if (inSubtract == -1 || level <= inSubtract) { |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
212 |
if (inList(n, subtractNodes)) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
213 |
inSubtract = level; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
214 |
} else { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
215 |
inSubtract = -1; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
216 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
217 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
218 |
if (inSubtract != -1){ |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
219 |
result = -1; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
220 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
221 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
222 |
if (result != -1 && hasIntersectFilter |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
223 |
&& (inIntersect == -1 || level <= inIntersect)) { |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
224 |
if (!inList(n, intersectNodes)) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
225 |
inIntersect = -1; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
226 |
result = 0; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
227 |
} else { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
228 |
inIntersect = level; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
229 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
230 |
} |
1337 | 231 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
232 |
if (level <= inUnion) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
233 |
inUnion = -1; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
234 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
235 |
if (result == 1) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
236 |
return 1; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
237 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
238 |
if (hasUnionFilter) { |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
239 |
if (inUnion == -1 && inList(n, unionNodes)) { |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
240 |
inUnion = level; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
241 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
242 |
if (inUnion != -1) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
243 |
return 1; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
244 |
} |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
245 |
result = 0; |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
246 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
247 |
|
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
248 |
return result; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
249 |
} |
1337 | 250 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
251 |
/** |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
252 |
* Method rooted |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
253 |
* @param currentNode |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
254 |
* @param nodeList |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
255 |
* |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
256 |
* @return if rooted bye the rootnodes |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
257 |
*/ |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
258 |
static boolean rooted(Node currentNode, Set<Node> nodeList) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
259 |
if (nodeList.isEmpty()) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
260 |
return false; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
261 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
262 |
if (nodeList.contains(currentNode)) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
263 |
return true; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
264 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
265 |
for (Node rootNode : nodeList) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
266 |
if (XMLUtils.isDescendantOrSelf(rootNode, currentNode)) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
267 |
return true; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
268 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
269 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
270 |
return false; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
271 |
} |
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
272 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
273 |
/** |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
274 |
* Method rooted |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
275 |
* @param currentNode |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
276 |
* @param nodeList |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
277 |
* |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
278 |
* @return if rooted bye the rootnodes |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
279 |
*/ |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
280 |
static boolean inList(Node currentNode, Set<Node> nodeList) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
281 |
return nodeList.contains(currentNode); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
282 |
} |
1337 | 283 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
284 |
private static Set<Node> convertNodeListToSet(List<NodeList> l) { |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
285 |
Set<Node> result = new HashSet<>(); |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
286 |
for (NodeList rootNodes : l) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
287 |
int length = rootNodes.getLength(); |
11674
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
10694
diff
changeset
|
288 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
289 |
for (int i = 0; i < length; i++) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
290 |
Node rootNode = rootNodes.item(i); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
291 |
result.add(rootNode); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
11674
diff
changeset
|
292 |
} |
11674
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
10694
diff
changeset
|
293 |
} |
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
10694
diff
changeset
|
294 |
return result; |
a657f8ba55fc
7131084: XMLDSig XPathFilter2Transform regression involving intersect filter
mullan
parents:
10694
diff
changeset
|
295 |
} |
2 | 296 |
} |