23 import java.io.IOException; |
23 import java.io.IOException; |
24 import java.net.URI; |
24 import java.net.URI; |
25 import java.net.URISyntaxException; |
25 import java.net.URISyntaxException; |
26 import java.util.ArrayList; |
26 import java.util.ArrayList; |
27 import java.util.Collection; |
27 import java.util.Collection; |
|
28 import java.util.Comparator; |
28 import java.util.HashMap; |
29 import java.util.HashMap; |
29 import java.util.Iterator; |
30 import java.util.Iterator; |
30 import java.util.List; |
31 import java.util.List; |
31 import java.util.Map; |
32 import java.util.Map; |
32 import java.util.Set; |
33 import java.util.Set; |
33 import java.util.SortedSet; |
34 import java.util.SortedSet; |
34 import java.util.TreeSet; |
35 import java.util.TreeSet; |
35 import javax.xml.parsers.ParserConfigurationException; |
36 import javax.xml.parsers.ParserConfigurationException; |
|
37 import javax.xml.xpath.XPath; |
36 import org.w3c.dom.Attr; |
38 import org.w3c.dom.Attr; |
37 import org.w3c.dom.Document; |
39 import org.w3c.dom.Document; |
38 import org.w3c.dom.Element; |
40 import org.w3c.dom.Element; |
39 import org.w3c.dom.NamedNodeMap; |
41 import org.w3c.dom.NamedNodeMap; |
40 import org.w3c.dom.Node; |
42 import org.w3c.dom.Node; |
57 * @author Raul Benito |
59 * @author Raul Benito |
58 * @version $Revision: 1.2 $ |
60 * @version $Revision: 1.2 $ |
59 */ |
61 */ |
60 public abstract class Canonicalizer11 extends CanonicalizerBase { |
62 public abstract class Canonicalizer11 extends CanonicalizerBase { |
61 boolean firstCall = true; |
63 boolean firstCall = true; |
62 final SortedSet result = new TreeSet(COMPARE); |
64 final SortedSet<Attr> result = new TreeSet<Attr>(COMPARE); |
63 static final String XMLNS_URI = Constants.NamespaceSpecNS; |
65 static final String XMLNS_URI = Constants.NamespaceSpecNS; |
64 static final String XML_LANG_URI = Constants.XML_LANG_SPACE_SpecNS; |
66 static final String XML_LANG_URI = Constants.XML_LANG_SPACE_SpecNS; |
65 |
67 |
66 static Logger log = Logger.getLogger(Canonicalizer11.class.getName()); |
68 static Logger log = Logger.getLogger(Canonicalizer11.class.getName()); |
67 |
69 |
70 int lastlevel = 0; |
72 int lastlevel = 0; |
71 XmlsStackElement cur; |
73 XmlsStackElement cur; |
72 static class XmlsStackElement { |
74 static class XmlsStackElement { |
73 int level; |
75 int level; |
74 boolean rendered = false; |
76 boolean rendered = false; |
75 List nodes = new ArrayList(); |
77 List<Attr> nodes = new ArrayList<Attr>(); |
76 }; |
78 }; |
77 List levels = new ArrayList(); |
79 List<XmlsStackElement> levels = new ArrayList<XmlsStackElement>(); |
78 void push(int level) { |
80 void push(int level) { |
79 currentLevel = level; |
81 currentLevel = level; |
80 if (currentLevel == -1) |
82 if (currentLevel == -1) |
81 return; |
83 return; |
82 cur = null; |
84 cur = null; |
109 boolean parentRendered = false; |
111 boolean parentRendered = false; |
110 XmlsStackElement e = null; |
112 XmlsStackElement e = null; |
111 if (size == -1) { |
113 if (size == -1) { |
112 parentRendered = true; |
114 parentRendered = true; |
113 } else { |
115 } else { |
114 e = (XmlsStackElement) levels.get(size); |
116 e = levels.get(size); |
115 if (e.rendered && e.level+1 == currentLevel) |
117 if (e.rendered && e.level+1 == currentLevel) |
116 parentRendered = true; |
118 parentRendered = true; |
117 } |
119 } |
118 if (parentRendered) { |
120 if (parentRendered) { |
119 col.addAll(cur.nodes); |
121 col.addAll(cur.nodes); |
120 cur.rendered = true; |
122 cur.rendered = true; |
121 return; |
123 return; |
122 } |
124 } |
123 |
125 |
124 Map loa = new HashMap(); |
126 Map<String, Attr> loa = new HashMap<String, Attr>(); |
125 List baseAttrs = new ArrayList(); |
127 List<Attr> baseAttrs = new ArrayList<Attr>(); |
126 boolean successiveOmitted = true; |
128 boolean successiveOmitted = true; |
127 for (;size>=0;size--) { |
129 for (;size>=0;size--) { |
128 e = (XmlsStackElement) levels.get(size); |
130 e = levels.get(size); |
129 if (e.rendered) { |
131 if (e.rendered) { |
130 successiveOmitted = false; |
132 successiveOmitted = false; |
131 } |
133 } |
132 Iterator it = e.nodes.iterator(); |
134 Iterator<Attr> it = e.nodes.iterator(); |
133 while (it.hasNext() && successiveOmitted) { |
135 while (it.hasNext() && successiveOmitted) { |
134 Attr n = (Attr) it.next(); |
136 Attr n = it.next(); |
135 if (n.getLocalName().equals("base")) { |
137 if (n.getLocalName().equals("base")) { |
136 if (!e.rendered) { |
138 if (!e.rendered) { |
137 baseAttrs.add(n); |
139 baseAttrs.add(n); |
138 } |
140 } |
139 } else if (!loa.containsKey(n.getName())) |
141 } else if (!loa.containsKey(n.getName())) |
140 loa.put(n.getName(), n); |
142 loa.put(n.getName(), n); |
141 } |
143 } |
142 } |
144 } |
143 if (!baseAttrs.isEmpty()) { |
145 if (!baseAttrs.isEmpty()) { |
144 Iterator it = cur.nodes.iterator(); |
146 Iterator<Attr> it = cur.nodes.iterator(); |
145 String base = null; |
147 String base = null; |
146 Attr baseAttr = null; |
148 Attr baseAttr = null; |
147 while (it.hasNext()) { |
149 while (it.hasNext()) { |
148 Attr n = (Attr) it.next(); |
150 Attr n = it.next(); |
149 if (n.getLocalName().equals("base")) { |
151 if (n.getLocalName().equals("base")) { |
150 base = n.getValue(); |
152 base = n.getValue(); |
151 baseAttr = n; |
153 baseAttr = n; |
152 break; |
154 break; |
153 } |
155 } |
154 } |
156 } |
155 it = baseAttrs.iterator(); |
157 it = baseAttrs.iterator(); |
156 while (it.hasNext()) { |
158 while (it.hasNext()) { |
157 Attr n = (Attr) it.next(); |
159 Attr n = it.next(); |
158 if (base == null) { |
160 if (base == null) { |
159 base = n.getValue(); |
161 base = n.getValue(); |
160 baseAttr = n; |
162 baseAttr = n; |
161 } else { |
163 } else { |
162 try { |
164 try { |
200 * @param E |
202 * @param E |
201 * @param ns |
203 * @param ns |
202 * @return the Attr[]s to be outputted |
204 * @return the Attr[]s to be outputted |
203 * @throws CanonicalizationException |
205 * @throws CanonicalizationException |
204 */ |
206 */ |
205 Iterator handleAttributesSubtree(Element E, NameSpaceSymbTable ns) |
207 Iterator<Attr> handleAttributesSubtree(Element E, NameSpaceSymbTable ns) |
206 throws CanonicalizationException { |
208 throws CanonicalizationException { |
207 if (!E.hasAttributes() && !firstCall) { |
209 if (!E.hasAttributes() && !firstCall) { |
208 return null; |
210 return null; |
209 } |
211 } |
210 // result will contain the attrs which have to be outputted |
212 // result will contain the attrs which have to be outputted |
211 final SortedSet result = this.result; |
213 final SortedSet<Attr> result = this.result; |
212 result.clear(); |
214 result.clear(); |
213 NamedNodeMap attrs = E.getAttributes(); |
215 NamedNodeMap attrs = E.getAttributes(); |
214 int attrsLength = attrs.getLength(); |
216 int attrsLength = attrs.getLength(); |
215 |
217 |
216 for (int i = 0; i < attrsLength; i++) { |
218 for (int i = 0; i < attrsLength; i++) { |
234 |
236 |
235 Node n = ns.addMappingAndRender(NName, NValue, N); |
237 Node n = ns.addMappingAndRender(NName, NValue, N); |
236 |
238 |
237 if (n != null) { |
239 if (n != null) { |
238 // Render the ns definition |
240 // Render the ns definition |
239 result.add(n); |
241 result.add((Attr)n); |
240 if (C14nHelper.namespaceIsRelative(N)) { |
242 if (C14nHelper.namespaceIsRelative(N)) { |
241 Object exArgs[] = {E.getTagName(), NName, N.getNodeValue()}; |
243 Object exArgs[] = {E.getTagName(), NName, N.getNodeValue()}; |
242 throw new CanonicalizationException( |
244 throw new CanonicalizationException( |
243 "c14n.Canonicalizer.RelativeNamespace", exArgs); |
245 "c14n.Canonicalizer.RelativeNamespace", exArgs); |
244 } |
246 } |
249 // It is the first node of the subtree |
251 // It is the first node of the subtree |
250 // Obtain all the namespaces defined in the parents, and added |
252 // Obtain all the namespaces defined in the parents, and added |
251 // to the output. |
253 // to the output. |
252 ns.getUnrenderedNodes(result); |
254 ns.getUnrenderedNodes(result); |
253 // output the attributes in the xml namespace. |
255 // output the attributes in the xml namespace. |
254 xmlattrStack.getXmlnsAttr(result); |
256 xmlattrStack.getXmlnsAttr(getSortedSetAsCollection(result)); |
255 firstCall = false; |
257 firstCall = false; |
256 } |
258 } |
257 |
259 |
258 return result.iterator(); |
260 return result.iterator(); |
259 } |
261 } |
|
262 |
|
263 |
260 |
264 |
261 /** |
265 /** |
262 * Returns the Attr[]s to be outputted for the given element. |
266 * Returns the Attr[]s to be outputted for the given element. |
263 * <br> |
267 * <br> |
264 * IMPORTANT: This method expects to work on a modified DOM tree, i.e. a |
268 * IMPORTANT: This method expects to work on a modified DOM tree, i.e. a |
269 * @param E |
273 * @param E |
270 * @param ns |
274 * @param ns |
271 * @return the Attr[]s to be outputted |
275 * @return the Attr[]s to be outputted |
272 * @throws CanonicalizationException |
276 * @throws CanonicalizationException |
273 */ |
277 */ |
274 Iterator handleAttributes(Element E, NameSpaceSymbTable ns) |
278 Iterator<Attr> handleAttributes(Element E, NameSpaceSymbTable ns) |
275 throws CanonicalizationException { |
279 throws CanonicalizationException { |
276 // result will contain the attrs which have to be output |
280 // result will contain the attrs which have to be output |
277 xmlattrStack.push(ns.getLevel()); |
281 xmlattrStack.push(ns.getLevel()); |
278 boolean isRealVisible = isVisibleDO(E, ns.getLevel()) == 1; |
282 boolean isRealVisible = isVisibleDO(E, ns.getLevel()) == 1; |
279 NamedNodeMap attrs = null; |
283 NamedNodeMap attrs = null; |
281 if (E.hasAttributes()) { |
285 if (E.hasAttributes()) { |
282 attrs = E.getAttributes(); |
286 attrs = E.getAttributes(); |
283 attrsLength = attrs.getLength(); |
287 attrsLength = attrs.getLength(); |
284 } |
288 } |
285 |
289 |
286 SortedSet result = this.result; |
290 SortedSet<Attr> result = this.result; |
287 result.clear(); |
291 result.clear(); |
288 |
292 |
289 for (int i = 0; i < attrsLength; i++) { |
293 for (int i = 0; i < attrsLength; i++) { |
290 Attr N = (Attr) attrs.item(i); |
294 Attr N = (Attr)attrs.item(i); |
291 String NUri = N.getNamespaceURI(); |
295 String NUri = N.getNamespaceURI(); |
292 |
296 |
293 if (XMLNS_URI != NUri) { |
297 if (XMLNS_URI != NUri) { |
294 // A non namespace definition node. |
298 // A non namespace definition node. |
295 if (XML_LANG_URI == NUri) { |
299 if (XML_LANG_URI == NUri) { |
330 // The xpath select this node output it if needed. |
334 // The xpath select this node output it if needed. |
331 // Node n = ns.addMappingAndRenderXNodeSet |
335 // Node n = ns.addMappingAndRenderXNodeSet |
332 // (NName, NValue, N, isRealVisible); |
336 // (NName, NValue, N, isRealVisible); |
333 Node n = ns.addMappingAndRender(NName, NValue, N); |
337 Node n = ns.addMappingAndRender(NName, NValue, N); |
334 if (n != null) { |
338 if (n != null) { |
335 result.add(n); |
339 result.add((Attr)n); |
336 if (C14nHelper.namespaceIsRelative(N)) { |
340 if (C14nHelper.namespaceIsRelative(N)) { |
337 Object exArgs[] = |
341 Object exArgs[] = |
338 { E.getTagName(), NName, N.getNodeValue() }; |
342 { E.getTagName(), NName, N.getNodeValue() }; |
339 throw new CanonicalizationException( |
343 throw new CanonicalizationException( |
340 "c14n.Canonicalizer.RelativeNamespace", exArgs); |
344 "c14n.Canonicalizer.RelativeNamespace", exArgs); |
360 // then xmlns="" |
364 // then xmlns="" |
361 n = ns.addMappingAndRender(XMLNS, "", nullNode); |
365 n = ns.addMappingAndRender(XMLNS, "", nullNode); |
362 } |
366 } |
363 // output the xmlns def if needed. |
367 // output the xmlns def if needed. |
364 if (n != null) { |
368 if (n != null) { |
365 result.add(n); |
369 result.add((Attr)n); |
366 } |
370 } |
367 // Float all xml:* attributes of the unselected parent elements to |
371 // Float all xml:* attributes of the unselected parent elements to |
368 // this one. addXmlAttributes(E,result); |
372 // this one. addXmlAttributes(E,result); |
369 xmlattrStack.getXmlnsAttr(result); |
373 xmlattrStack.getXmlnsAttr(result); |
370 ns.getUnrenderedNodes(result); |
374 ns.getUnrenderedNodes(result); |
379 * @param xpathNodeSet |
383 * @param xpathNodeSet |
380 * @param inclusiveNamespaces |
384 * @param inclusiveNamespaces |
381 * @return none it always fails |
385 * @return none it always fails |
382 * @throws CanonicalizationException always |
386 * @throws CanonicalizationException always |
383 */ |
387 */ |
384 public byte[] engineCanonicalizeXPathNodeSet(Set xpathNodeSet, |
388 public byte[] engineCanonicalizeXPathNodeSet(Set<Node> xpathNodeSet, |
385 String inclusiveNamespaces) throws CanonicalizationException { |
389 String inclusiveNamespaces) throws CanonicalizationException { |
386 throw new CanonicalizationException( |
390 throw new CanonicalizationException( |
387 "c14n.Canonicalizer.UnsupportedOperation"); |
391 "c14n.Canonicalizer.UnsupportedOperation"); |
388 } |
392 } |
389 |
393 |