1 17 package com.sun.org.apache.xml.internal.security.c14n; 18 19 20 21 import java.io.ByteArrayInputStream ; 22 import java.io.OutputStream ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 import java.util.Set ; 26 27 import javax.xml.parsers.DocumentBuilder ; 28 import javax.xml.parsers.DocumentBuilderFactory ; 29 30 import com.sun.org.apache.xml.internal.security.exceptions.AlgorithmAlreadyRegisteredException; 31 import org.w3c.dom.Document ; 32 import org.w3c.dom.Node ; 33 import org.w3c.dom.NodeList ; 34 import org.xml.sax.InputSource ; 35 36 37 42 public class Canonicalizer { 43 44 46 public static final String ENCODING = "UTF8"; 47 48 49 52 public static final String XPATH_C14N_WITH_COMMENTS_SINGLE_NODE = "(.//. | .//@* | .//namespace::*)"; 53 54 55 58 public static final String ALGO_ID_C14N_OMIT_COMMENTS = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"; 59 62 public static final String ALGO_ID_C14N_WITH_COMMENTS = ALGO_ID_C14N_OMIT_COMMENTS + "#WithComments"; 63 66 public static final String ALGO_ID_C14N_EXCL_OMIT_COMMENTS = "http://www.w3.org/2001/10/xml-exc-c14n#"; 67 70 public static final String ALGO_ID_C14N_EXCL_WITH_COMMENTS = ALGO_ID_C14N_EXCL_OMIT_COMMENTS + "WithComments"; 71 72 static boolean _alreadyInitialized = false; 73 static Map _canonicalizerHash = null; 74 75 protected CanonicalizerSpi canonicalizerSpi = null; 76 78 82 public static void init() { 83 84 if (!Canonicalizer._alreadyInitialized) { 85 Canonicalizer._canonicalizerHash = new HashMap (10); 86 Canonicalizer._alreadyInitialized = true; 87 } 88 } 89 90 96 private Canonicalizer(String algorithmURI) 97 throws InvalidCanonicalizerException { 98 99 try { 100 Class implementingClass = getImplementingClass(algorithmURI); 101 102 this.canonicalizerSpi = 103 (CanonicalizerSpi) implementingClass.newInstance(); 104 this.canonicalizerSpi.reset=true; 105 } catch (Exception e) { 106 Object exArgs[] = { algorithmURI }; 107 108 throw new InvalidCanonicalizerException( 109 "signature.Canonicalizer.UnknownCanonicalizer", exArgs); 110 } 111 } 112 113 120 public static final Canonicalizer getInstance(String algorithmURI) 121 throws InvalidCanonicalizerException { 122 123 Canonicalizer c14nizer = new Canonicalizer(algorithmURI); 124 125 return c14nizer; 126 } 127 128 135 public static void register(String algorithmURI, String implementingClass) 136 throws AlgorithmAlreadyRegisteredException { 137 138 Class registeredClass = getImplementingClass(algorithmURI); 140 141 if (registeredClass != null) { 142 Object exArgs[] = { algorithmURI, registeredClass }; 143 144 throw new AlgorithmAlreadyRegisteredException( 145 "algorithm.alreadyRegistered", exArgs); 146 } 147 148 try { 149 _canonicalizerHash.put(algorithmURI, Class.forName(implementingClass)); 150 } catch (ClassNotFoundException e) { 151 throw new RuntimeException ("c14n class not found"); 152 } 153 } 154 155 160 public final String getURI() { 161 return this.canonicalizerSpi.engineGetURI(); 162 } 163 164 169 public boolean getIncludeComments() { 170 return this.canonicalizerSpi.engineGetIncludeComments(); 171 } 172 173 185 public byte[] canonicalize(byte[] inputBytes) 186 throws javax.xml.parsers.ParserConfigurationException , 187 java.io.IOException , org.xml.sax.SAXException , 188 CanonicalizationException { 189 190 ByteArrayInputStream bais = new ByteArrayInputStream (inputBytes); 191 InputSource in = new InputSource (bais); 192 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 193 194 dfactory.setNamespaceAware(true); 195 196 dfactory.setValidating(true); 198 199 DocumentBuilder db = dfactory.newDocumentBuilder(); 200 201 223 db.setErrorHandler(new com.sun.org.apache.xml.internal.security.utils 224 .IgnoreAllErrorHandler()); 225 226 Document document = db.parse(in); 227 byte result[] = this.canonicalizeSubtree(document); 228 229 return result; 230 } 231 232 240 public byte[] canonicalizeSubtree(Node node) 241 throws CanonicalizationException { 242 return this.canonicalizerSpi.engineCanonicalizeSubTree(node); 243 } 244 245 253 public byte[] canonicalizeSubtree(Node node, String inclusiveNamespaces) 254 throws CanonicalizationException { 255 return this.canonicalizerSpi.engineCanonicalizeSubTree(node, 256 inclusiveNamespaces); 257 } 258 259 267 public byte[] canonicalizeXPathNodeSet(NodeList xpathNodeSet) 268 throws CanonicalizationException { 269 return this.canonicalizerSpi.engineCanonicalizeXPathNodeSet(xpathNodeSet); 270 } 271 272 281 public byte[] canonicalizeXPathNodeSet( 282 NodeList xpathNodeSet, String inclusiveNamespaces) 283 throws CanonicalizationException { 284 return this.canonicalizerSpi.engineCanonicalizeXPathNodeSet(xpathNodeSet, 285 inclusiveNamespaces); 286 } 287 288 295 public byte[] canonicalizeXPathNodeSet(Set xpathNodeSet) 296 throws CanonicalizationException { 297 return this.canonicalizerSpi.engineCanonicalizeXPathNodeSet(xpathNodeSet); 298 } 299 300 308 public byte[] canonicalizeXPathNodeSet( 309 Set xpathNodeSet, String inclusiveNamespaces) 310 throws CanonicalizationException { 311 return this.canonicalizerSpi.engineCanonicalizeXPathNodeSet(xpathNodeSet, 312 inclusiveNamespaces); 313 } 314 315 320 public void setWriter(OutputStream os) { 321 this.canonicalizerSpi.setWriter(os); 322 } 323 324 329 public String getImplementingCanonicalizerClass() { 330 return this.canonicalizerSpi.getClass().getName(); 331 } 332 333 339 private static Class getImplementingClass(String URI) { 340 return (Class ) _canonicalizerHash.get(URI); 341 } 342 343 347 public void notReset() { 348 this.canonicalizerSpi.reset=false; 349 } 350 } 351 | Popular Tags |