1 2 18 package com.sun.org.apache.xml.internal.security.c14n.implementations; 19 20 21 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import java.util.Set ; 26 import java.util.SortedSet ; 27 import java.util.TreeSet ; 28 29 import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException; 30 import com.sun.org.apache.xml.internal.security.c14n.helper.C14nHelper; 31 import com.sun.org.apache.xml.internal.security.utils.Constants; 32 import org.w3c.dom.Attr ; 33 import org.w3c.dom.Element ; 34 import org.w3c.dom.NamedNodeMap ; 35 import org.w3c.dom.Node ; 36 37 38 45 public abstract class Canonicalizer20010315 extends CanonicalizerBase { 46 boolean firstCall=true; 47 final SortedSet result= new TreeSet (COMPARE); 48 static final String XMLNS_URI=Constants.NamespaceSpecNS; 49 static final String XML_LANG_URI=Constants.XML_LANG_SPACE_SpecNS; 50 55 public Canonicalizer20010315(boolean includeComments) { 56 super(includeComments); 57 } 58 59 73 Iterator handleAttributesSubtree(Element E, NameSpaceSymbTable ns ) 74 throws CanonicalizationException { 75 if (!E.hasAttributes() && !firstCall) { 76 return null; 77 } 78 final SortedSet result = this.result; 80 result.clear(); 81 NamedNodeMap attrs = E.getAttributes(); 82 int attrsLength = attrs.getLength(); 83 84 for (int i = 0; i < attrsLength; i++) { 85 Attr N = (Attr ) attrs.item(i); 86 String NName=N.getLocalName(); 87 String NValue=N.getValue(); 88 String NUri =N.getNamespaceURI(); 89 90 if (!XMLNS_URI.equals(NUri)) { 91 result.add(N); 93 continue; 94 } 95 96 if (XML.equals(NName) 97 && XML_LANG_URI.equals(NValue)) { 98 continue; 100 } 101 102 Node n=ns.addMappingAndRender(NName,NValue,N); 103 104 if (n!=null) { 105 result.add(n); 107 if (C14nHelper.namespaceIsRelative(N)) { 108 Object exArgs[] = { E.getTagName(), NName, N.getNodeValue() }; 109 throw new CanonicalizationException( 110 "c14n.Canonicalizer.RelativeNamespace", exArgs); 111 } 112 } 113 } 114 115 if (firstCall) { 116 ns.getUnrenderedNodes(result); 119 addXmlAttributesSubtree(E, result); 121 firstCall=false; 122 } 123 124 return result.iterator(); 125 } 126 127 132 private void addXmlAttributesSubtree(Element E, SortedSet result) { 133 Node parent = E.getParentNode(); 135 Map loa = new HashMap (); 136 137 if ((parent != null) && (parent.getNodeType() == Node.ELEMENT_NODE)) { 138 139 for (Node ancestor = parent; 141 (ancestor != null) 142 && (ancestor.getNodeType() == Node.ELEMENT_NODE); 143 ancestor = ancestor.getParentNode()) { 144 Element el=((Element ) ancestor); 145 if (!el.hasAttributes()) { 146 continue; 147 } 148 NamedNodeMap ancestorAttrs = el.getAttributes(); 150 151 for (int i = 0; i < ancestorAttrs.getLength(); i++) { 152 Attr currentAncestorAttr = (Attr ) ancestorAttrs.item(i); 154 155 if (XML_LANG_URI.equals( 156 currentAncestorAttr.getNamespaceURI())) { 157 158 if (!E.hasAttributeNS( 160 XML_LANG_URI, 161 currentAncestorAttr.getLocalName())) { 162 163 if (!loa.containsKey(currentAncestorAttr.getName())) { 165 loa.put(currentAncestorAttr.getName(), 166 currentAncestorAttr); 167 } 168 } 169 } 170 } 171 } 172 } 173 174 result.addAll( loa.values()); 175 176 } 177 178 190 Iterator handleAttributes(Element E, NameSpaceSymbTable ns ) throws CanonicalizationException { 191 boolean isRealVisible=isVisible(E); 193 NamedNodeMap attrs = null; 194 int attrsLength = 0; 195 if (E.hasAttributes()) { 196 attrs=E.getAttributes(); 197 attrsLength= attrs.getLength(); 198 } 199 200 201 SortedSet result = this.result; 202 result.clear(); 203 204 205 for (int i = 0; i < attrsLength; i++) { 206 Attr N = (Attr ) attrs.item(i); 207 String NName=N.getLocalName(); 208 String NValue=N.getValue(); 209 String NUri =N.getNamespaceURI(); 210 211 if (!XMLNS_URI.equals(NUri)) { 212 if (isRealVisible){ 214 result.add(N); 216 } 217 continue; 219 } 220 221 222 if ("xml".equals(NName) 223 && XML_LANG_URI.equals(NValue)) { 224 227 continue; 228 } 229 if (isVisible(N)) { 232 Node n=ns.addMappingAndRenderXNodeSet(NName,NValue,N,isRealVisible); 234 if (n!=null) { 235 result.add(n); 236 if (C14nHelper.namespaceIsRelative(N)) { 237 Object exArgs[] = { E.getTagName(), NName, N.getNodeValue() }; 238 throw new CanonicalizationException( 239 "c14n.Canonicalizer.RelativeNamespace", exArgs); 240 } 241 } 242 } 243 } 244 if (isRealVisible) { 245 Attr xmlns = E.getAttributeNodeNS(XMLNS_URI, XMLNS); 247 Node n=null; 248 if (xmlns == null) { 249 n=ns.getMapping(XMLNS); 251 } else if ( !isVisible(xmlns)) { 252 n=ns.addMappingAndRenderXNodeSet(XMLNS,"",nullNode,true); 255 } 256 if (n!=null) { 258 result.add(n); 259 } 260 addXmlAttributes(E,result); 262 } 263 264 return result.iterator(); 265 } 266 271 private void addXmlAttributes(Element E, SortedSet result) { 272 285 286 Node parent = E.getParentNode(); 288 Map loa = new HashMap (); 289 290 if ((parent != null) && (parent.getNodeType() == Node.ELEMENT_NODE) 291 &&!isVisible(parent)) { 292 293 for (Node ancestor = parent; 295 (ancestor != null) 296 && (ancestor.getNodeType() == Node.ELEMENT_NODE); 297 ancestor = ancestor.getParentNode()) { 298 Element el=((Element ) ancestor); 299 if (!el.hasAttributes()) { 300 continue; 301 } 302 NamedNodeMap ancestorAttrs =el.getAttributes(); 304 305 for (int i = 0; i < ancestorAttrs.getLength(); i++) { 306 307 Attr currentAncestorAttr = (Attr ) ancestorAttrs.item(i); 309 310 if (XML_LANG_URI.equals( 311 currentAncestorAttr.getNamespaceURI())) { 312 313 if (!E.hasAttributeNS( 315 XML_LANG_URI, 316 currentAncestorAttr.getLocalName())) { 317 318 if (!loa.containsKey(currentAncestorAttr.getName())) { 320 loa.put(currentAncestorAttr.getName(), 321 currentAncestorAttr); 322 } 323 } 324 } 325 } 326 } 327 } 328 result.addAll(loa.values()); 329 330 } 331 332 340 public byte[] engineCanonicalizeXPathNodeSet(Set xpathNodeSet, String inclusiveNamespaces) 341 throws CanonicalizationException { 342 343 344 throw new CanonicalizationException( 345 "c14n.Canonicalizer.UnsupportedOperation"); 346 } 347 348 356 public byte[] engineCanonicalizeSubTree(Node rootNode, String inclusiveNamespaces) 357 throws CanonicalizationException { 358 359 360 throw new CanonicalizationException( 361 "c14n.Canonicalizer.UnsupportedOperation"); 362 } 363 } 364 | Popular Tags |