All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jcp.xml.dsig.internal.dom.ApacheTransform Maven / Gradle / Ivy

The newest version!
/*     */ package org.jcp.xml.dsig.internal.dom;
/*     */ 
/*     */ import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
/*     */ import com.sun.org.apache.xml.internal.security.transforms.Transform;
/*     */ import java.io.OutputStream;
/*     */ import java.security.InvalidAlgorithmParameterException;
/*     */ import java.security.spec.AlgorithmParameterSpec;
/*     */ import java.util.Set;
/*     */ import java.util.logging.Level;
/*     */ import java.util.logging.Logger;
/*     */ import javax.xml.crypto.Data;
/*     */ import javax.xml.crypto.MarshalException;
/*     */ import javax.xml.crypto.NodeSetData;
/*     */ import javax.xml.crypto.OctetStreamData;
/*     */ import javax.xml.crypto.XMLCryptoContext;
/*     */ import javax.xml.crypto.XMLStructure;
/*     */ import javax.xml.crypto.dom.DOMCryptoContext;
/*     */ import javax.xml.crypto.dom.DOMStructure;
/*     */ import javax.xml.crypto.dsig.TransformException;
/*     */ import javax.xml.crypto.dsig.TransformService;
/*     */ import javax.xml.crypto.dsig.spec.TransformParameterSpec;
/*     */ import org.w3c.dom.Document;
/*     */ import org.w3c.dom.Element;
/*     */ 
/*     */ public abstract class ApacheTransform extends TransformService
/*     */ {
/*  36 */   private static Logger log = Logger.getLogger("org.jcp.xml.dsig.internal.dom");
/*     */   private Transform apacheTransform;
/*     */   protected Document ownerDoc;
/*     */   protected Element transformElem;
/*     */   protected TransformParameterSpec params;
/*     */ 
/*     */   public final AlgorithmParameterSpec getParameterSpec()
/*     */   {
/*  43 */     return this.params;
/*     */   }
/*     */ 
/*     */   public void init(XMLStructure parent, XMLCryptoContext context) throws InvalidAlgorithmParameterException
/*     */   {
/*  48 */     if ((context != null) && (!(context instanceof DOMCryptoContext))) {
/*  49 */       throw new ClassCastException("context must be of type DOMCryptoContext");
/*     */     }
/*     */ 
/*  52 */     this.transformElem = ((Element)((DOMStructure)parent).getNode());
/*     */ 
/*  54 */     this.ownerDoc = DOMUtils.getOwnerDocument(this.transformElem);
/*     */   }
/*     */ 
/*     */   public void marshalParams(XMLStructure parent, XMLCryptoContext context) throws MarshalException
/*     */   {
/*  59 */     if ((context != null) && (!(context instanceof DOMCryptoContext))) {
/*  60 */       throw new ClassCastException("context must be of type DOMCryptoContext");
/*     */     }
/*     */ 
/*  63 */     this.transformElem = ((Element)((DOMStructure)parent).getNode());
/*     */ 
/*  65 */     this.ownerDoc = DOMUtils.getOwnerDocument(this.transformElem);
/*     */   }
/*     */ 
/*     */   public Data transform(Data data, XMLCryptoContext xc) throws TransformException
/*     */   {
/*  70 */     if (data == null) {
/*  71 */       throw new NullPointerException("data must not be null");
/*     */     }
/*  73 */     return transformIt(data, xc, (OutputStream)null);
/*     */   }
/*     */ 
/*     */   public Data transform(Data data, XMLCryptoContext xc, OutputStream os) throws TransformException
/*     */   {
/*  78 */     if (data == null) {
/*  79 */       throw new NullPointerException("data must not be null");
/*     */     }
/*  81 */     if (os == null) {
/*  82 */       throw new NullPointerException("output stream must not be null");
/*     */     }
/*  84 */     return transformIt(data, xc, os);
/*     */   }
/*     */ 
/*     */   private Data transformIt(Data data, XMLCryptoContext xc, OutputStream os)
/*     */     throws TransformException
/*     */   {
/*  90 */     if (this.ownerDoc == null) {
/*  91 */       throw new TransformException("transform must be marshalled");
/*     */     }
/*     */ 
/*  94 */     if (this.apacheTransform == null)
/*     */       try {
/*  96 */         this.apacheTransform = Transform.getInstance(this.ownerDoc, getAlgorithm(), this.transformElem.getChildNodes());
/*     */ 
/*  98 */         this.apacheTransform.setElement(this.transformElem, xc.getBaseURI());
/*  99 */         if (log.isLoggable(Level.FINE))
/* 100 */           log.log(Level.FINE, "Created transform for algorithm: " + getAlgorithm());
/*     */       }
/*     */       catch (Exception ex)
/*     */       {
/* 104 */         throw new TransformException("Couldn't find Transform for: " + getAlgorithm(), ex);
/*     */       }
/*     */     XMLSignatureInput in;
/*     */     XMLSignatureInput in;
/* 110 */     if ((data instanceof ApacheData)) {
/* 111 */       if (log.isLoggable(Level.FINE)) {
/* 112 */         log.log(Level.FINE, "ApacheData = true");
/*     */       }
/* 114 */       in = ((ApacheData)data).getXMLSignatureInput();
/*     */     }
/*     */     else
/*     */     {
/*     */       XMLSignatureInput in;
/* 115 */       if ((data instanceof NodeSetData)) {
/* 116 */         if (log.isLoggable(Level.FINE)) {
/* 117 */           log.log(Level.FINE, "isNodeSet() = true");
/*     */         }
/* 119 */         if ((data instanceof DOMSubTreeData)) {
/* 120 */           if (log.isLoggable(Level.FINE)) {
/* 121 */             log.log(Level.FINE, "DOMSubTreeData = true");
/*     */           }
/* 123 */           DOMSubTreeData subTree = (DOMSubTreeData)data;
/* 124 */           XMLSignatureInput in = new XMLSignatureInput(subTree.getRoot());
/* 125 */           in.setExcludeComments(subTree.excludeComments());
/*     */         } else {
/* 127 */           Set nodeSet = Utils.toNodeSet(((NodeSetData)data).iterator());
/*     */ 
/* 129 */           in = new XMLSignatureInput(nodeSet);
/*     */         }
/*     */       } else {
/* 132 */         if (log.isLoggable(Level.FINE))
/* 133 */           log.log(Level.FINE, "isNodeSet() = false");
/*     */         try
/*     */         {
/* 136 */           in = new XMLSignatureInput(((OctetStreamData)data).getOctetStream());
/*     */         }
/*     */         catch (Exception ex) {
/* 139 */           throw new TransformException(ex);
/*     */         }
/*     */       }
/*     */     }
/*     */     try {
/* 144 */       if (os != null) {
/* 145 */         in = this.apacheTransform.performTransform(in, os);
/* 146 */         if ((!in.isNodeSet()) && (!in.isElement()))
/* 147 */           return null;
/*     */       }
/*     */       else {
/* 150 */         in = this.apacheTransform.performTransform(in);
/*     */       }
/* 152 */       if (in.isOctetStream()) {
/* 153 */         return new ApacheOctetStreamData(in);
/*     */       }
/* 155 */       return new ApacheNodeSetData(in);
/*     */     }
/*     */     catch (Exception ex) {
/* 158 */       throw new TransformException(ex);
/*     */     }
/*     */   }
/*     */ 
/*     */   public final boolean isFeatureSupported(String feature) {
/* 163 */     if (feature == null) {
/* 164 */       throw new NullPointerException();
/*     */     }
/* 166 */     return false;
/*     */   }
/*     */ }

/* Location:           E:\HYN\Java\trunk\ref\lib-dep\xmldsig\xmldsig.jar
 * Qualified Name:     org.jcp.xml.dsig.internal.dom.ApacheTransform
 * JD-Core Version:    0.6.2
 */




© 2015 - 2024 Weber Informatics LLC | Privacy Policy