1 2 18 package com.sun.org.apache.xml.internal.security.c14n.implementations; 19 20 import java.util.Iterator ; 21 import java.util.Set ; 22 import java.util.SortedSet ; 23 import java.util.TreeSet ; 24 25 import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException; 26 import com.sun.org.apache.xml.internal.security.c14n.helper.C14nHelper; 27 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput; 28 import com.sun.org.apache.xml.internal.security.transforms.params.InclusiveNamespaces; 29 import com.sun.org.apache.xml.internal.security.utils.Constants; 30 import org.w3c.dom.Attr ; 31 import org.w3c.dom.Element ; 32 import org.w3c.dom.NamedNodeMap ; 33 import org.w3c.dom.Node ; 34 50 public abstract class Canonicalizer20010315Excl extends CanonicalizerBase { 51 55 TreeSet _inclusiveNSSet = null; 56 static final String XMLNS_URI=Constants.NamespaceSpecNS; 57 final SortedSet result = new TreeSet (COMPARE); 58 63 public Canonicalizer20010315Excl(boolean includeComments) { 64 super(includeComments); 65 } 66 67 74 public byte[] engineCanonicalizeSubTree(Node rootNode) 75 throws CanonicalizationException { 76 return this.engineCanonicalizeSubTree(rootNode, "",null); 77 } 78 86 public byte[] engineCanonicalizeSubTree(Node rootNode, 87 String inclusiveNamespaces) throws CanonicalizationException { 88 return this.engineCanonicalizeSubTree(rootNode, inclusiveNamespaces,null); 89 } 90 98 public byte[] engineCanonicalizeSubTree(Node rootNode, 99 String inclusiveNamespaces,Node excl) throws CanonicalizationException { 100 this._inclusiveNSSet = (TreeSet )InclusiveNamespaces 101 .prefixStr2Set(inclusiveNamespaces); 102 return super.engineCanonicalizeSubTree(rootNode,excl); 103 } 104 111 public byte[] engineCanonicalize(XMLSignatureInput rootNode, 112 String inclusiveNamespaces) throws CanonicalizationException { 113 this._inclusiveNSSet = (TreeSet )InclusiveNamespaces 114 .prefixStr2Set(inclusiveNamespaces); 115 return super.engineCanonicalize(rootNode); 116 } 117 118 124 Iterator handleAttributesSubtree(Element E,NameSpaceSymbTable ns) 125 throws CanonicalizationException { 126 SortedSet result = this.result; 130 result.clear(); 131 NamedNodeMap attrs=null; 132 133 int attrsLength = 0; 134 if (E.hasAttributes()) { 135 attrs = E.getAttributes(); 136 attrsLength = attrs.getLength(); 137 } 138 SortedSet visiblyUtilized =(SortedSet ) _inclusiveNSSet.clone(); 140 141 for (int i = 0; i < attrsLength; i++) { 142 Attr N = (Attr ) attrs.item(i); 143 String NName=N.getLocalName(); 144 String NNodeValue=N.getNodeValue(); 145 146 if (!XMLNS_URI.equals(N.getNamespaceURI())) { 147 String prefix = N.getPrefix(); 150 if ( (prefix != null) && (!prefix.equals(XML) && !prefix.equals(XMLNS)) ) { 151 visiblyUtilized.add(prefix); 152 } 153 result.add(N); 155 continue; 156 } 157 158 if (ns.addMapping(NName, NNodeValue,N)) { 159 if (C14nHelper.namespaceIsRelative(NNodeValue)) { 161 Object exArgs[] = {E.getTagName(), NName, 162 N.getNodeValue()}; 163 throw new CanonicalizationException( 164 "c14n.Canonicalizer.RelativeNamespace", exArgs); 165 } 166 } 167 } 168 169 if (E.getNamespaceURI() != null) { 170 String prefix = E.getPrefix(); 171 if ((prefix == null) || (prefix.length() == 0)) { 172 visiblyUtilized.add(XMLNS); 173 } else { 174 visiblyUtilized.add(prefix); 175 } 176 } else { 177 visiblyUtilized.add(XMLNS); 178 } 179 180 Iterator it=visiblyUtilized.iterator(); 182 while (it.hasNext()) { 183 String s=(String )it.next(); 184 Attr key=ns.getMapping(s); 185 if (key==null) { 186 continue; 187 } 188 result.add(key); 189 } 190 191 return result.iterator(); 192 } 193 194 201 public byte[] engineCanonicalizeXPathNodeSet(Set xpathNodeSet, 202 String inclusiveNamespaces) throws CanonicalizationException { 203 204 205 this._inclusiveNSSet = (TreeSet )InclusiveNamespaces 206 .prefixStr2Set(inclusiveNamespaces); 207 return super.engineCanonicalizeXPathNodeSet(xpathNodeSet); 208 209 } 210 211 212 public byte[] engineCanonicalizeXPathNodeSet(Set xpathNodeSet 213 ) throws CanonicalizationException { 214 return engineCanonicalizeXPathNodeSet(xpathNodeSet,""); 215 } 216 217 222 final Iterator handleAttributes(Element E, NameSpaceSymbTable ns) 223 throws CanonicalizationException { 224 SortedSet result = this.result; 226 result.clear(); 227 NamedNodeMap attrs = null; 228 int attrsLength = 0; 229 if (E.hasAttributes()) { 230 attrs = E.getAttributes(); 231 attrsLength = attrs.getLength(); 232 } 233 Set visiblyUtilized =null; 235 boolean isOutputElement = isVisible(E); 237 if (isOutputElement) { 238 visiblyUtilized = (Set ) this._inclusiveNSSet.clone(); 239 } 240 241 for (int i = 0; i < attrsLength; i++) { 242 Attr N = (Attr ) attrs.item(i); 243 String NName=N.getLocalName(); 244 String NNodeValue=N.getNodeValue(); 245 if ( !isVisible(N) ) { 246 continue; 248 } 249 250 if (!XMLNS_URI.equals(N.getNamespaceURI())) { 251 if (isOutputElement) { 253 String prefix = N.getPrefix(); 255 if ((prefix != null) && (!prefix.equals(XML) && !prefix.equals(XMLNS)) ){ 256 visiblyUtilized.add(prefix); 257 } 258 result.add(N); 260 } 261 continue; 262 } 263 264 265 if (ns.addMapping(NName, NNodeValue,N)) { 266 if (C14nHelper.namespaceIsRelative(NNodeValue)) { 268 Object exArgs[] = {E.getTagName(), NName, 269 N.getNodeValue()}; 270 throw new CanonicalizationException( 271 "c14n.Canonicalizer.RelativeNamespace", exArgs); 272 } 273 } 274 } 275 276 if (isOutputElement) { 277 Attr xmlns = E.getAttributeNodeNS(XMLNS_URI, XMLNS); 279 if ((xmlns!=null) && (!isVisible(xmlns))) { 280 ns.addMapping(XMLNS,"",nullNode); 283 } 284 285 if (E.getNamespaceURI() != null) { 286 String prefix = E.getPrefix(); 287 if ((prefix == null) || (prefix.length() == 0)) { 288 visiblyUtilized.add(XMLNS); 289 } else { 290 visiblyUtilized.add( prefix); 291 } 292 } else { 293 visiblyUtilized.add(XMLNS); 294 } 295 Iterator it=visiblyUtilized.iterator(); 298 while (it.hasNext()) { 299 String s=(String )it.next(); 300 Attr key=ns.getMapping(s); 301 if (key==null) { 302 continue; 303 } 304 result.add(key); 305 } 306 } else { 307 Iterator it=this._inclusiveNSSet.iterator(); 308 while (it.hasNext()) { 309 String s=(String )it.next(); 310 Attr key=ns.getMappingWithoutRendered(s); 311 if (key==null) { 312 continue; 313 } 314 result.add(key); 315 } 316 } 317 318 return result.iterator(); 319 } 320 } 321 322 | Popular Tags |