1 17 package com.sun.org.apache.xml.internal.security.transforms; 18 19 20 21 import java.io.IOException ; 22 import java.io.OutputStream ; 23 import java.util.HashMap ; 24 import java.security.AccessController ; 25 import java.security.PrivilegedAction ; 26 27 import javax.xml.parsers.ParserConfigurationException ; 28 29 import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException; 30 import com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException; 31 import com.sun.org.apache.xml.internal.security.exceptions.AlgorithmAlreadyRegisteredException; 32 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException; 33 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput; 34 import com.sun.org.apache.xml.internal.security.utils.Constants; 35 import com.sun.org.apache.xml.internal.security.utils.HelperNodeList; 36 import com.sun.org.apache.xml.internal.security.utils.SignatureElementProxy; 37 import org.w3c.dom.Document ; 38 import org.w3c.dom.Element ; 39 import org.w3c.dom.NodeList ; 40 import org.xml.sax.SAXException ; 41 42 43 57 public final class Transform extends SignatureElementProxy { 58 59 60 static java.util.logging.Logger log = 61 java.util.logging.Logger.getLogger(Transform.class.getName()); 62 63 64 static boolean _alreadyInitialized = false; 65 66 67 static HashMap _transformHash = null; 68 69 70 protected TransformSpi transformSpi = null; 71 72 82 public Transform(Document doc, String algorithmURI, NodeList contextNodes) 83 throws InvalidTransformException { 84 85 super(doc); 86 87 try { 88 this._constructionElement.setAttributeNS(null, Constants._ATT_ALGORITHM, 89 algorithmURI); 90 91 Class implementingClass = 92 Transform.getImplementingClass(algorithmURI); 93 94 if(implementingClass == null) { 95 Object exArgs[] = { algorithmURI }; 96 97 throw new InvalidTransformException( 98 "signature.Transform.UnknownTransform", exArgs); 99 } 100 if (true) { 101 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Create URI \"" + algorithmURI + "\" class \"" 102 + implementingClass + "\""); 103 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "The NodeList is " + contextNodes); 104 } 105 106 this.transformSpi = 108 (TransformSpi) implementingClass.newInstance(); 109 110 this.transformSpi.setTransform(this); 111 112 if (contextNodes != null) { 114 119 120 for (int i = 0; i < contextNodes.getLength(); i++) { 121 this._constructionElement.appendChild(contextNodes.item(i).cloneNode(true)); 122 } 123 124 } 125 } catch (IllegalAccessException ex) { 126 Object exArgs[] = { algorithmURI }; 127 128 throw new InvalidTransformException( 129 "signature.Transform.UnknownTransform", exArgs, ex); 130 } catch (InstantiationException ex) { 131 Object exArgs[] = { algorithmURI }; 132 133 throw new InvalidTransformException( 134 "signature.Transform.UnknownTransform", exArgs, ex); 135 } 136 } 137 138 148 public Transform(Element element, String BaseURI) 149 throws InvalidTransformException, TransformationException, 150 XMLSecurityException { 151 152 super(element, BaseURI); 153 154 String AlgorithmURI = element.getAttributeNS(null, Constants._ATT_ALGORITHM); 156 157 if ((AlgorithmURI == null) || (AlgorithmURI.length() == 0)) { 158 Object exArgs[] = { Constants._ATT_ALGORITHM, 159 Constants._TAG_TRANSFORM }; 160 161 throw new TransformationException("xml.WrongContent", exArgs); 162 } 163 164 try { 165 Class implementingClass = (Class ) _transformHash.get(AlgorithmURI); 166 this.transformSpi = 167 (TransformSpi) implementingClass.newInstance(); 168 169 this.transformSpi.setTransform(this); 170 } catch (IllegalAccessException e) { 171 Object exArgs[] = { AlgorithmURI }; 172 173 throw new InvalidTransformException( 174 "signature.Transform.UnknownTransform", exArgs); 175 } catch (InstantiationException e) { 176 Object exArgs[] = { AlgorithmURI }; 177 178 throw new InvalidTransformException( 179 "signature.Transform.UnknownTransform", exArgs); 180 } catch (NullPointerException e) { 181 Object exArgs[] = { AlgorithmURI }; 182 183 throw new InvalidTransformException( 184 "signature.Transform.UnknownTransform", exArgs); 185 } 186 } 187 188 196 public static final Transform getInstance( 197 Document doc, String algorithmURI) throws InvalidTransformException { 198 return Transform.getInstance(doc, algorithmURI, (NodeList) null); 199 } 200 201 210 public static final Transform getInstance( 211 Document doc, String algorithmURI, Element contextChild) 212 throws InvalidTransformException { 213 214 HelperNodeList contextNodes = new HelperNodeList(); 215 216 contextNodes.appendChild(doc.createTextNode("\n")); 217 contextNodes.appendChild(contextChild); 218 contextNodes.appendChild(doc.createTextNode("\n")); 219 220 return Transform.getInstance(doc, algorithmURI, contextNodes); 221 } 222 223 232 public static final Transform getInstance( 233 Document doc, String algorithmURI, NodeList contextNodes) 234 throws InvalidTransformException { 235 return new Transform(doc, algorithmURI, contextNodes); 236 } 237 238 242 public static void init() { 243 244 if (!_alreadyInitialized) { 245 _transformHash = new HashMap (10); 246 _alreadyInitialized = true; 247 } 248 } 249 250 258 public static void register(String algorithmURI, String implementingClass) 259 throws AlgorithmAlreadyRegisteredException { 260 261 { 262 263 Class registeredClass = Transform.getImplementingClass(algorithmURI); 265 266 if ((registeredClass != null) ) { 267 Object exArgs[] = { algorithmURI, registeredClass }; 268 269 throw new AlgorithmAlreadyRegisteredException( 270 "algorithm.alreadyRegistered", exArgs); 271 } 272 273 ClassLoader cl = (ClassLoader ) AccessController.doPrivileged( 274 new PrivilegedAction () { 275 public Object run() { 276 return Thread.currentThread().getContextClassLoader(); 277 } 278 }); 279 280 try { 281 Transform._transformHash.put 282 (algorithmURI, Class.forName(implementingClass, true, cl)); 283 } catch (ClassNotFoundException e) { 284 e.printStackTrace(); 286 } 287 } 288 } 289 290 295 public final String getURI() { 296 return this._constructionElement.getAttributeNS(null, Constants._ATT_ALGORITHM); 297 } 298 299 309 public XMLSignatureInput performTransform(XMLSignatureInput input) 310 throws IOException , CanonicalizationException, 311 InvalidCanonicalizerException, TransformationException { 312 313 XMLSignatureInput result = null; 314 315 try { 316 result = transformSpi.enginePerformTransform(input); 317 } catch (ParserConfigurationException ex) { 318 Object exArgs[] = { this.getURI(), "ParserConfigurationException" }; 319 320 throw new CanonicalizationException( 321 "signature.Transform.ErrorDuringTransform", exArgs, ex); 322 } catch (SAXException ex) { 323 Object exArgs[] = { this.getURI(), "SAXException" }; 324 325 throw new CanonicalizationException( 326 "signature.Transform.ErrorDuringTransform", exArgs, ex); 327 } 328 329 return result; 330 } 331 332 343 public XMLSignatureInput performTransform(XMLSignatureInput input, OutputStream os) 344 throws IOException , CanonicalizationException, 345 InvalidCanonicalizerException, TransformationException { 346 347 XMLSignatureInput result = null; 348 349 try { 350 result = transformSpi.enginePerformTransform(input,os); 351 } catch (ParserConfigurationException ex) { 352 Object exArgs[] = { this.getURI(), "ParserConfigurationException" }; 353 354 throw new CanonicalizationException( 355 "signature.Transform.ErrorDuringTransform", exArgs, ex); 356 } catch (SAXException ex) { 357 Object exArgs[] = { this.getURI(), "SAXException" }; 358 359 throw new CanonicalizationException( 360 "signature.Transform.ErrorDuringTransform", exArgs, ex); 361 } 362 363 return result; 364 } 365 366 372 private static Class getImplementingClass(String URI) { 373 return (Class )Transform._transformHash.get(URI); 374 } 375 376 377 378 public String getBaseLocalName() { 379 return Constants._TAG_TRANSFORM; 380 } 381 } 382 | Popular Tags |