author | xuelei |
Sat, 12 Oct 2013 20:46:35 -0700 | |
changeset 22325 | f22d936a0ff5 |
parent 18780 | f47b920867e7 |
permissions | -rw-r--r-- |
1337 | 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:
10694
diff
changeset
|
5 |
/** |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
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:
10694
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:
10694
diff
changeset
|
8 |
* distributed with this work for additional information |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
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:
10694
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:
10694
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:
10694
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:
10694
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:
10694
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:
10694
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:
10694
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:
10694
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:
10694
diff
changeset
|
20 |
* specific language governing permissions and limitations |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
21 |
* under the License. |
1337 | 22 |
*/ |
23 |
package com.sun.org.apache.xml.internal.security.c14n.implementations; |
|
24 |
||
25 |
import java.io.IOException; |
|
26 |
import java.net.URI; |
|
27 |
import java.net.URISyntaxException; |
|
28 |
import java.util.ArrayList; |
|
29 |
import java.util.Collection; |
|
30 |
import java.util.HashMap; |
|
31 |
import java.util.Iterator; |
|
32 |
import java.util.List; |
|
33 |
import java.util.Map; |
|
34 |
import java.util.Set; |
|
35 |
import java.util.SortedSet; |
|
36 |
import java.util.TreeSet; |
|
37 |
import javax.xml.parsers.ParserConfigurationException; |
|
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.xml.sax.SAXException; |
|
44 |
||
45 |
import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException; |
|
46 |
import com.sun.org.apache.xml.internal.security.c14n.helper.C14nHelper; |
|
47 |
import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput; |
|
48 |
import com.sun.org.apache.xml.internal.security.utils.Constants; |
|
49 |
import com.sun.org.apache.xml.internal.security.utils.XMLUtils; |
|
50 |
||
51 |
/** |
|
52 |
* Implements <A HREF="http://www.w3.org/TR/2008/PR-xml-c14n11-20080129/"> |
|
53 |
* Canonical XML Version 1.1</A>, a W3C Proposed Recommendation from 29 |
|
54 |
* January 2008. |
|
55 |
* |
|
56 |
* @author Sean Mullan |
|
57 |
* @author Raul Benito |
|
58 |
*/ |
|
59 |
public abstract class Canonicalizer11 extends CanonicalizerBase { |
|
60 |
||
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
61 |
private static final String XMLNS_URI = Constants.NamespaceSpecNS; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
62 |
private static final String XML_LANG_URI = Constants.XML_LANG_SPACE_SpecNS; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
63 |
private static java.util.logging.Logger log = |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
64 |
java.util.logging.Logger.getLogger(Canonicalizer11.class.getName()); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
65 |
private final SortedSet<Attr> result = new TreeSet<Attr>(COMPARE); |
1337 | 66 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
67 |
private boolean firstCall = true; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
68 |
|
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
69 |
private static class XmlAttrStack { |
1337 | 70 |
static class XmlsStackElement { |
71 |
int level; |
|
72 |
boolean rendered = false; |
|
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
73 |
List<Attr> nodes = new ArrayList<Attr>(); |
1337 | 74 |
}; |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
75 |
|
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
76 |
int currentLevel = 0; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
77 |
int lastlevel = 0; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
78 |
XmlsStackElement cur; |
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
79 |
List<XmlsStackElement> levels = new ArrayList<XmlsStackElement>(); |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
80 |
|
1337 | 81 |
void push(int level) { |
82 |
currentLevel = level; |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
83 |
if (currentLevel == -1) { |
1337 | 84 |
return; |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
85 |
} |
1337 | 86 |
cur = null; |
87 |
while (lastlevel >= currentLevel) { |
|
88 |
levels.remove(levels.size() - 1); |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
89 |
int newSize = levels.size(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
90 |
if (newSize == 0) { |
1337 | 91 |
lastlevel = 0; |
92 |
return; |
|
93 |
} |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
94 |
lastlevel = (levels.get(newSize - 1)).level; |
1337 | 95 |
} |
96 |
} |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
97 |
|
1337 | 98 |
void addXmlnsAttr(Attr n) { |
99 |
if (cur == null) { |
|
100 |
cur = new XmlsStackElement(); |
|
101 |
cur.level = currentLevel; |
|
102 |
levels.add(cur); |
|
103 |
lastlevel = currentLevel; |
|
104 |
} |
|
105 |
cur.nodes.add(n); |
|
106 |
} |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
107 |
|
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
108 |
void getXmlnsAttr(Collection<Attr> col) { |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
109 |
int size = levels.size() - 1; |
1337 | 110 |
if (cur == null) { |
111 |
cur = new XmlsStackElement(); |
|
112 |
cur.level = currentLevel; |
|
113 |
lastlevel = currentLevel; |
|
114 |
levels.add(cur); |
|
115 |
} |
|
116 |
boolean parentRendered = false; |
|
117 |
XmlsStackElement e = null; |
|
118 |
if (size == -1) { |
|
119 |
parentRendered = true; |
|
120 |
} else { |
|
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
121 |
e = levels.get(size); |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
122 |
if (e.rendered && e.level + 1 == currentLevel) { |
1337 | 123 |
parentRendered = true; |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
124 |
} |
1337 | 125 |
} |
126 |
if (parentRendered) { |
|
127 |
col.addAll(cur.nodes); |
|
128 |
cur.rendered = true; |
|
129 |
return; |
|
130 |
} |
|
131 |
||
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
132 |
Map<String, Attr> loa = new HashMap<String, Attr>(); |
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
133 |
List<Attr> baseAttrs = new ArrayList<Attr>(); |
1337 | 134 |
boolean successiveOmitted = true; |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
135 |
for (; size >= 0; size--) { |
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
136 |
e = levels.get(size); |
1337 | 137 |
if (e.rendered) { |
138 |
successiveOmitted = false; |
|
139 |
} |
|
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
140 |
Iterator<Attr> it = e.nodes.iterator(); |
1337 | 141 |
while (it.hasNext() && successiveOmitted) { |
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
142 |
Attr n = it.next(); |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
143 |
if (n.getLocalName().equals("base") && !e.rendered) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
144 |
baseAttrs.add(n); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
145 |
} else if (!loa.containsKey(n.getName())) { |
1337 | 146 |
loa.put(n.getName(), n); |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
147 |
} |
1337 | 148 |
} |
149 |
} |
|
150 |
if (!baseAttrs.isEmpty()) { |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
151 |
Iterator<Attr> it = col.iterator(); |
1337 | 152 |
String base = null; |
153 |
Attr baseAttr = null; |
|
154 |
while (it.hasNext()) { |
|
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
155 |
Attr n = it.next(); |
1337 | 156 |
if (n.getLocalName().equals("base")) { |
157 |
base = n.getValue(); |
|
158 |
baseAttr = n; |
|
159 |
break; |
|
160 |
} |
|
161 |
} |
|
162 |
it = baseAttrs.iterator(); |
|
163 |
while (it.hasNext()) { |
|
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
1337
diff
changeset
|
164 |
Attr n = it.next(); |
1337 | 165 |
if (base == null) { |
166 |
base = n.getValue(); |
|
167 |
baseAttr = n; |
|
168 |
} else { |
|
169 |
try { |
|
170 |
base = joinURI(n.getValue(), base); |
|
171 |
} catch (URISyntaxException ue) { |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
172 |
if (log.isLoggable(java.util.logging.Level.FINE)) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
173 |
log.log(java.util.logging.Level.FINE, ue.getMessage(), ue); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
174 |
} |
1337 | 175 |
} |
176 |
} |
|
177 |
} |
|
178 |
if (base != null && base.length() != 0) { |
|
179 |
baseAttr.setValue(base); |
|
180 |
col.add(baseAttr); |
|
181 |
} |
|
182 |
} |
|
183 |
||
184 |
cur.rendered = true; |
|
185 |
col.addAll(loa.values()); |
|
186 |
} |
|
187 |
}; |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
188 |
|
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
189 |
private XmlAttrStack xmlattrStack = new XmlAttrStack(); |
1337 | 190 |
|
191 |
/** |
|
192 |
* Constructor Canonicalizer11 |
|
193 |
* |
|
194 |
* @param includeComments |
|
195 |
*/ |
|
196 |
public Canonicalizer11(boolean includeComments) { |
|
197 |
super(includeComments); |
|
198 |
} |
|
199 |
||
200 |
/** |
|
201 |
* Always throws a CanonicalizationException because this is inclusive c14n. |
|
202 |
* |
|
203 |
* @param xpathNodeSet |
|
204 |
* @param inclusiveNamespaces |
|
205 |
* @return none it always fails |
|
206 |
* @throws CanonicalizationException always |
|
207 |
*/ |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
208 |
public byte[] engineCanonicalizeXPathNodeSet( |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
209 |
Set<Node> xpathNodeSet, String inclusiveNamespaces |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
210 |
) throws CanonicalizationException { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
211 |
throw new CanonicalizationException("c14n.Canonicalizer.UnsupportedOperation"); |
1337 | 212 |
} |
213 |
||
214 |
/** |
|
215 |
* Always throws a CanonicalizationException because this is inclusive c14n. |
|
216 |
* |
|
217 |
* @param rootNode |
|
218 |
* @param inclusiveNamespaces |
|
219 |
* @return none it always fails |
|
220 |
* @throws CanonicalizationException |
|
221 |
*/ |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
222 |
public byte[] engineCanonicalizeSubTree( |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
223 |
Node rootNode, String inclusiveNamespaces |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
224 |
) throws CanonicalizationException { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
225 |
throw new CanonicalizationException("c14n.Canonicalizer.UnsupportedOperation"); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
226 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
227 |
|
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
228 |
/** |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
229 |
* Returns the Attr[]s to be output for the given element. |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
230 |
* <br> |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
231 |
* The code of this method is a copy of {@link #handleAttributes(Element, |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
232 |
* NameSpaceSymbTable)}, |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
233 |
* whereas it takes into account that subtree-c14n is -- well -- |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
234 |
* subtree-based. |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
235 |
* So if the element in question isRoot of c14n, it's parent is not in the |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
236 |
* node set, as well as all other ancestors. |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
237 |
* |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
238 |
* @param element |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
239 |
* @param ns |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
240 |
* @return the Attr[]s to be output |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
241 |
* @throws CanonicalizationException |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
242 |
*/ |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
243 |
@Override |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
244 |
protected Iterator<Attr> handleAttributesSubtree(Element element, NameSpaceSymbTable ns) |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
245 |
throws CanonicalizationException { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
246 |
if (!element.hasAttributes() && !firstCall) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
247 |
return null; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
248 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
249 |
// result will contain the attrs which have to be output |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
250 |
final SortedSet<Attr> result = this.result; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
251 |
result.clear(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
252 |
|
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
253 |
if (element.hasAttributes()) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
254 |
NamedNodeMap attrs = element.getAttributes(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
255 |
int attrsLength = attrs.getLength(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
256 |
|
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
257 |
for (int i = 0; i < attrsLength; i++) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
258 |
Attr attribute = (Attr) attrs.item(i); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
259 |
String NUri = attribute.getNamespaceURI(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
260 |
String NName = attribute.getLocalName(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
261 |
String NValue = attribute.getValue(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
262 |
|
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
263 |
if (!XMLNS_URI.equals(NUri)) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
264 |
// It's not a namespace attr node. Add to the result and continue. |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
265 |
result.add(attribute); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
266 |
} else if (!(XML.equals(NName) && XML_LANG_URI.equals(NValue))) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
267 |
// The default mapping for xml must not be output. |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
268 |
Node n = ns.addMappingAndRender(NName, NValue, attribute); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
269 |
|
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
270 |
if (n != null) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
271 |
// Render the ns definition |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
272 |
result.add((Attr)n); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
273 |
if (C14nHelper.namespaceIsRelative(attribute)) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
274 |
Object exArgs[] = {element.getTagName(), NName, attribute.getNodeValue()}; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
275 |
throw new CanonicalizationException( |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
276 |
"c14n.Canonicalizer.RelativeNamespace", exArgs |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
277 |
); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
278 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
279 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
280 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
281 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
282 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
283 |
|
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
284 |
if (firstCall) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
285 |
// It is the first node of the subtree |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
286 |
// Obtain all the namespaces defined in the parents, and added to the output. |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
287 |
ns.getUnrenderedNodes(result); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
288 |
// output the attributes in the xml namespace. |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
289 |
xmlattrStack.getXmlnsAttr(result); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
290 |
firstCall = false; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
291 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
292 |
|
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
293 |
return result.iterator(); |
1337 | 294 |
} |
295 |
||
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
296 |
/** |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
297 |
* Returns the Attr[]s to be output for the given element. |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
298 |
* <br> |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
299 |
* IMPORTANT: This method expects to work on a modified DOM tree, i.e. a |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
300 |
* DOM which has been prepared using |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
301 |
* {@link com.sun.org.apache.xml.internal.security.utils.XMLUtils#circumventBug2650( |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
302 |
* org.w3c.dom.Document)}. |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
303 |
* |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
304 |
* @param element |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
305 |
* @param ns |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
306 |
* @return the Attr[]s to be output |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
307 |
* @throws CanonicalizationException |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
308 |
*/ |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
309 |
@Override |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
310 |
protected Iterator<Attr> handleAttributes(Element element, NameSpaceSymbTable ns) |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
311 |
throws CanonicalizationException { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
312 |
// result will contain the attrs which have to be output |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
313 |
xmlattrStack.push(ns.getLevel()); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
314 |
boolean isRealVisible = isVisibleDO(element, ns.getLevel()) == 1; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
315 |
final SortedSet<Attr> result = this.result; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
316 |
result.clear(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
317 |
|
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
318 |
if (element.hasAttributes()) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
319 |
NamedNodeMap attrs = element.getAttributes(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
320 |
int attrsLength = attrs.getLength(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
321 |
|
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
322 |
for (int i = 0; i < attrsLength; i++) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
323 |
Attr attribute = (Attr) attrs.item(i); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
324 |
String NUri = attribute.getNamespaceURI(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
325 |
String NName = attribute.getLocalName(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
326 |
String NValue = attribute.getValue(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
327 |
|
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
328 |
if (!XMLNS_URI.equals(NUri)) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
329 |
//A non namespace definition node. |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
330 |
if (XML_LANG_URI.equals(NUri)) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
331 |
if (NName.equals("id")) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
332 |
if (isRealVisible) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
333 |
// treat xml:id like any other attribute |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
334 |
// (emit it, but don't inherit it) |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
335 |
result.add(attribute); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
336 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
337 |
} else { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
338 |
xmlattrStack.addXmlnsAttr(attribute); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
339 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
340 |
} else if (isRealVisible) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
341 |
//The node is visible add the attribute to the list of output attributes. |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
342 |
result.add(attribute); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
343 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
344 |
} else if (!XML.equals(NName) || !XML_LANG_URI.equals(NValue)) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
345 |
/* except omit namespace node with local name xml, which defines |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
346 |
* the xml prefix, if its string value is |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
347 |
* http://www.w3.org/XML/1998/namespace. |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
348 |
*/ |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
349 |
// add the prefix binding to the ns symb table. |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
350 |
if (isVisible(attribute)) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
351 |
if (isRealVisible || !ns.removeMappingIfRender(NName)) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
352 |
// The xpath select this node output it if needed. |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
353 |
Node n = ns.addMappingAndRender(NName, NValue, attribute); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
354 |
if (n != null) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
355 |
result.add((Attr)n); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
356 |
if (C14nHelper.namespaceIsRelative(attribute)) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
357 |
Object exArgs[] = { element.getTagName(), NName, attribute.getNodeValue() }; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
358 |
throw new CanonicalizationException( |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
359 |
"c14n.Canonicalizer.RelativeNamespace", exArgs |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
360 |
); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
361 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
362 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
363 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
364 |
} else { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
365 |
if (isRealVisible && !XMLNS.equals(NName)) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
366 |
ns.removeMapping(NName); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
367 |
} else { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
368 |
ns.addMapping(NName, NValue, attribute); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
369 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
370 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
371 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
372 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
373 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
374 |
|
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
375 |
if (isRealVisible) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
376 |
//The element is visible, handle the xmlns definition |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
377 |
Attr xmlns = element.getAttributeNodeNS(XMLNS_URI, XMLNS); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
378 |
Node n = null; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
379 |
if (xmlns == null) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
380 |
//No xmlns def just get the already defined. |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
381 |
n = ns.getMapping(XMLNS); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
382 |
} else if (!isVisible(xmlns)) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
383 |
//There is a definition but the xmlns is not selected by the xpath. |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
384 |
//then xmlns="" |
22325 | 385 |
n = ns.addMappingAndRender( |
386 |
XMLNS, "", getNullNode(xmlns.getOwnerDocument())); |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
387 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
388 |
//output the xmlns def if needed. |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
389 |
if (n != null) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
390 |
result.add((Attr)n); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
391 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
392 |
//Float all xml:* attributes of the unselected parent elements to this one. |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
393 |
xmlattrStack.getXmlnsAttr(result); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
394 |
ns.getUnrenderedNodes(result); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
395 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
396 |
|
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
397 |
return result.iterator(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
398 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
399 |
|
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
400 |
protected void circumventBugIfNeeded(XMLSignatureInput input) |
1337 | 401 |
throws CanonicalizationException, ParserConfigurationException, |
402 |
IOException, SAXException { |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
403 |
if (!input.isNeedsToBeExpanded()) { |
1337 | 404 |
return; |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
405 |
} |
1337 | 406 |
Document doc = null; |
407 |
if (input.getSubNode() != null) { |
|
408 |
doc = XMLUtils.getOwnerDocument(input.getSubNode()); |
|
409 |
} else { |
|
410 |
doc = XMLUtils.getOwnerDocument(input.getNodeSet()); |
|
411 |
} |
|
412 |
XMLUtils.circumventBug2650(doc); |
|
413 |
} |
|
414 |
||
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
415 |
protected void handleParent(Element e, NameSpaceSymbTable ns) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
416 |
if (!e.hasAttributes() && e.getNamespaceURI() == null) { |
1337 | 417 |
return; |
418 |
} |
|
419 |
xmlattrStack.push(-1); |
|
420 |
NamedNodeMap attrs = e.getAttributes(); |
|
421 |
int attrsLength = attrs.getLength(); |
|
422 |
for (int i = 0; i < attrsLength; i++) { |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
423 |
Attr attribute = (Attr) attrs.item(i); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
424 |
String NName = attribute.getLocalName(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
425 |
String NValue = attribute.getNodeValue(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
426 |
|
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
427 |
if (Constants.NamespaceSpecNS.equals(attribute.getNamespaceURI())) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
428 |
if (!XML.equals(NName) || !Constants.XML_LANG_SPACE_SpecNS.equals(NValue)) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
429 |
ns.addMapping(NName, NValue, attribute); |
1337 | 430 |
} |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
431 |
} else if (!"id".equals(NName) && XML_LANG_URI.equals(attribute.getNamespaceURI())) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
432 |
xmlattrStack.addXmlnsAttr(attribute); |
1337 | 433 |
} |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
434 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
435 |
if (e.getNamespaceURI() != null) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
436 |
String NName = e.getPrefix(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
437 |
String NValue = e.getNamespaceURI(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
438 |
String Name; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
439 |
if (NName == null || NName.equals("")) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
440 |
NName = "xmlns"; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
441 |
Name = "xmlns"; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
442 |
} else { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
443 |
Name = "xmlns:" + NName; |
1337 | 444 |
} |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
445 |
Attr n = e.getOwnerDocument().createAttributeNS("http://www.w3.org/2000/xmlns/", Name); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
446 |
n.setValue(NValue); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
447 |
ns.addMapping(NName, NValue, n); |
1337 | 448 |
} |
449 |
} |
|
450 |
||
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
451 |
private static String joinURI(String baseURI, String relativeURI) throws URISyntaxException { |
1337 | 452 |
String bscheme = null; |
453 |
String bauthority = null; |
|
454 |
String bpath = ""; |
|
455 |
String bquery = null; |
|
456 |
||
457 |
// pre-parse the baseURI |
|
458 |
if (baseURI != null) { |
|
459 |
if (baseURI.endsWith("..")) { |
|
460 |
baseURI = baseURI + "/"; |
|
461 |
} |
|
462 |
URI base = new URI(baseURI); |
|
463 |
bscheme = base.getScheme(); |
|
464 |
bauthority = base.getAuthority(); |
|
465 |
bpath = base.getPath(); |
|
466 |
bquery = base.getQuery(); |
|
467 |
} |
|
468 |
||
469 |
URI r = new URI(relativeURI); |
|
470 |
String rscheme = r.getScheme(); |
|
471 |
String rauthority = r.getAuthority(); |
|
472 |
String rpath = r.getPath(); |
|
473 |
String rquery = r.getQuery(); |
|
474 |
||
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
475 |
String tscheme, tauthority, tpath, tquery; |
1337 | 476 |
if (rscheme != null && rscheme.equals(bscheme)) { |
477 |
rscheme = null; |
|
478 |
} |
|
479 |
if (rscheme != null) { |
|
480 |
tscheme = rscheme; |
|
481 |
tauthority = rauthority; |
|
482 |
tpath = removeDotSegments(rpath); |
|
483 |
tquery = rquery; |
|
484 |
} else { |
|
485 |
if (rauthority != null) { |
|
486 |
tauthority = rauthority; |
|
487 |
tpath = removeDotSegments(rpath); |
|
488 |
tquery = rquery; |
|
489 |
} else { |
|
490 |
if (rpath.length() == 0) { |
|
491 |
tpath = bpath; |
|
492 |
if (rquery != null) { |
|
493 |
tquery = rquery; |
|
494 |
} else { |
|
495 |
tquery = bquery; |
|
496 |
} |
|
497 |
} else { |
|
498 |
if (rpath.startsWith("/")) { |
|
499 |
tpath = removeDotSegments(rpath); |
|
500 |
} else { |
|
501 |
if (bauthority != null && bpath.length() == 0) { |
|
502 |
tpath = "/" + rpath; |
|
503 |
} else { |
|
504 |
int last = bpath.lastIndexOf('/'); |
|
505 |
if (last == -1) { |
|
506 |
tpath = rpath; |
|
507 |
} else { |
|
508 |
tpath = bpath.substring(0, last+1) + rpath; |
|
509 |
} |
|
510 |
} |
|
511 |
tpath = removeDotSegments(tpath); |
|
512 |
} |
|
513 |
tquery = rquery; |
|
514 |
} |
|
515 |
tauthority = bauthority; |
|
516 |
} |
|
517 |
tscheme = bscheme; |
|
518 |
} |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
519 |
return new URI(tscheme, tauthority, tpath, tquery, null).toString(); |
1337 | 520 |
} |
521 |
||
522 |
private static String removeDotSegments(String path) { |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
523 |
if (log.isLoggable(java.util.logging.Level.FINE)) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
524 |
log.log(java.util.logging.Level.FINE, "STEP OUTPUT BUFFER\t\tINPUT BUFFER"); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
525 |
} |
1337 | 526 |
|
527 |
// 1. The input buffer is initialized with the now-appended path |
|
528 |
// components then replace occurrences of "//" in the input buffer |
|
529 |
// with "/" until no more occurrences of "//" are in the input buffer. |
|
530 |
String input = path; |
|
531 |
while (input.indexOf("//") > -1) { |
|
532 |
input = input.replaceAll("//", "/"); |
|
533 |
} |
|
534 |
||
535 |
// Initialize the output buffer with the empty string. |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
536 |
StringBuilder output = new StringBuilder(); |
1337 | 537 |
|
538 |
// If the input buffer starts with a root slash "/" then move this |
|
539 |
// character to the output buffer. |
|
540 |
if (input.charAt(0) == '/') { |
|
541 |
output.append("/"); |
|
542 |
input = input.substring(1); |
|
543 |
} |
|
544 |
||
545 |
printStep("1 ", output.toString(), input); |
|
546 |
||
547 |
// While the input buffer is not empty, loop as follows |
|
548 |
while (input.length() != 0) { |
|
549 |
// 2A. If the input buffer begins with a prefix of "./", |
|
550 |
// then remove that prefix from the input buffer |
|
551 |
// else if the input buffer begins with a prefix of "../", then |
|
552 |
// if also the output does not contain the root slash "/" only, |
|
553 |
// then move this prefix to the end of the output buffer else |
|
554 |
// remove that prefix |
|
555 |
if (input.startsWith("./")) { |
|
556 |
input = input.substring(2); |
|
557 |
printStep("2A", output.toString(), input); |
|
558 |
} else if (input.startsWith("../")) { |
|
559 |
input = input.substring(3); |
|
560 |
if (!output.toString().equals("/")) { |
|
561 |
output.append("../"); |
|
562 |
} |
|
563 |
printStep("2A", output.toString(), input); |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
564 |
// 2B. if the input buffer begins with a prefix of "/./" or "/.", |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
565 |
// where "." is a complete path segment, then replace that prefix |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
566 |
// with "/" in the input buffer; otherwise, |
1337 | 567 |
} else if (input.startsWith("/./")) { |
568 |
input = input.substring(2); |
|
569 |
printStep("2B", output.toString(), input); |
|
570 |
} else if (input.equals("/.")) { |
|
571 |
// FIXME: what is complete path segment? |
|
572 |
input = input.replaceFirst("/.", "/"); |
|
573 |
printStep("2B", output.toString(), input); |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
574 |
// 2C. if the input buffer begins with a prefix of "/../" or "/..", |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
575 |
// where ".." is a complete path segment, then replace that prefix |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
576 |
// with "/" in the input buffer and if also the output buffer is |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
577 |
// empty, last segment in the output buffer equals "../" or "..", |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
578 |
// where ".." is a complete path segment, then append ".." or "/.." |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
579 |
// for the latter case respectively to the output buffer else |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
580 |
// remove the last segment and its preceding "/" (if any) from the |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
581 |
// output buffer and if hereby the first character in the output |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
582 |
// buffer was removed and it was not the root slash then delete a |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
583 |
// leading slash from the input buffer; otherwise, |
1337 | 584 |
} else if (input.startsWith("/../")) { |
585 |
input = input.substring(3); |
|
586 |
if (output.length() == 0) { |
|
587 |
output.append("/"); |
|
588 |
} else if (output.toString().endsWith("../")) { |
|
589 |
output.append(".."); |
|
590 |
} else if (output.toString().endsWith("..")) { |
|
591 |
output.append("/.."); |
|
592 |
} else { |
|
593 |
int index = output.lastIndexOf("/"); |
|
594 |
if (index == -1) { |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
595 |
output = new StringBuilder(); |
1337 | 596 |
if (input.charAt(0) == '/') { |
597 |
input = input.substring(1); |
|
598 |
} |
|
599 |
} else { |
|
600 |
output = output.delete(index, output.length()); |
|
601 |
} |
|
602 |
} |
|
603 |
printStep("2C", output.toString(), input); |
|
604 |
} else if (input.equals("/..")) { |
|
605 |
// FIXME: what is complete path segment? |
|
606 |
input = input.replaceFirst("/..", "/"); |
|
607 |
if (output.length() == 0) { |
|
608 |
output.append("/"); |
|
609 |
} else if (output.toString().endsWith("../")) { |
|
610 |
output.append(".."); |
|
611 |
} else if (output.toString().endsWith("..")) { |
|
612 |
output.append("/.."); |
|
613 |
} else { |
|
614 |
int index = output.lastIndexOf("/"); |
|
615 |
if (index == -1) { |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
616 |
output = new StringBuilder(); |
1337 | 617 |
if (input.charAt(0) == '/') { |
618 |
input = input.substring(1); |
|
619 |
} |
|
620 |
} else { |
|
621 |
output = output.delete(index, output.length()); |
|
622 |
} |
|
623 |
} |
|
624 |
printStep("2C", output.toString(), input); |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
625 |
// 2D. if the input buffer consists only of ".", then remove |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
626 |
// that from the input buffer else if the input buffer consists |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
627 |
// only of ".." and if the output buffer does not contain only |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
628 |
// the root slash "/", then move the ".." to the output buffer |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
629 |
// else delte it.; otherwise, |
1337 | 630 |
} else if (input.equals(".")) { |
631 |
input = ""; |
|
632 |
printStep("2D", output.toString(), input); |
|
633 |
} else if (input.equals("..")) { |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
634 |
if (!output.toString().equals("/")) { |
1337 | 635 |
output.append(".."); |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
636 |
} |
1337 | 637 |
input = ""; |
638 |
printStep("2D", output.toString(), input); |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
639 |
// 2E. move the first path segment (if any) in the input buffer |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
640 |
// to the end of the output buffer, including the initial "/" |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
641 |
// character (if any) and any subsequent characters up to, but not |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
10694
diff
changeset
|
642 |
// including, the next "/" character or the end of the input buffer. |
1337 | 643 |
} else { |
644 |
int end = -1; |
|
645 |
int begin = input.indexOf('/'); |
|
646 |
if (begin == 0) { |
|
647 |
end = input.indexOf('/', 1); |
|
648 |
} else { |
|
649 |
end = begin; |
|
650 |
begin = 0; |
|
651 |
} |
|
652 |
String segment; |
|
653 |
if (end == -1) { |
|
654 |
segment = input.substring(begin); |
|
655 |
input = ""; |
|
656 |
} else { |
|
657 |
segment = input.substring(begin, end); |
|
658 |
input = input.substring(end); |
|
659 |
} |
|
660 |
output.append(segment); |
|
661 |
printStep("2E", output.toString(), input); |
|
662 |
} |
|
663 |
} |
|
664 |
||
665 |
// 3. Finally, if the only or last segment of the output buffer is |
|
666 |
// "..", where ".." is a complete path segment not followed by a slash |
|
667 |
// then append a slash "/". The output buffer is returned as the result |
|
668 |
// of remove_dot_segments |
|
669 |
if (output.toString().endsWith("..")) { |
|
670 |
output.append("/"); |
|
671 |
printStep("3 ", output.toString(), input); |
|
672 |
} |
|
673 |
||
674 |
return output.toString(); |
|
675 |
} |
|
676 |
||
677 |
private static void printStep(String step, String output, String input) { |
|
678 |
if (log.isLoggable(java.util.logging.Level.FINE)) { |
|
679 |
log.log(java.util.logging.Level.FINE, " " + step + ": " + output); |
|
680 |
if (output.length() == 0) { |
|
681 |
log.log(java.util.logging.Level.FINE, "\t\t\t\t" + input); |
|
682 |
} else { |
|
683 |
log.log(java.util.logging.Level.FINE, "\t\t\t" + input); |
|
684 |
} |
|
685 |
} |
|
686 |
} |
|
687 |
} |