jdk/src/share/classes/org/jcp/xml/dsig/internal/dom/DOMTransform.java
changeset 2 90ce3da70b43
child 1337 e8d6cef36199
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 /*
       
    26  * $Id: DOMTransform.java,v 1.25 2005/05/10 18:15:34 mullan Exp $
       
    27  */
       
    28 package org.jcp.xml.dsig.internal.dom;
       
    29 
       
    30 import java.io.OutputStream;
       
    31 import java.security.InvalidAlgorithmParameterException;
       
    32 import java.security.NoSuchAlgorithmException;
       
    33 import java.security.spec.AlgorithmParameterSpec;
       
    34 
       
    35 import org.w3c.dom.Document;
       
    36 import org.w3c.dom.Element;
       
    37 import org.w3c.dom.Node;
       
    38 import org.w3c.dom.NodeList;
       
    39 
       
    40 import javax.xml.crypto.*;
       
    41 import javax.xml.crypto.dsig.*;
       
    42 import javax.xml.crypto.dom.DOMCryptoContext;
       
    43 import javax.xml.crypto.dsig.dom.DOMSignContext;
       
    44 import javax.xml.crypto.dsig.spec.TransformParameterSpec;
       
    45 
       
    46 /**
       
    47  * DOM-based abstract implementation of Transform.
       
    48  *
       
    49  * @author Sean Mullan
       
    50  */
       
    51 public class DOMTransform extends DOMStructure implements Transform {
       
    52 
       
    53     protected TransformService spi;
       
    54 
       
    55     /**
       
    56      * Creates a <code>DOMTransform</code>.
       
    57      *
       
    58      * @param spi the TransformService
       
    59      */
       
    60     public DOMTransform(TransformService spi) {
       
    61         this.spi = spi;
       
    62     }
       
    63 
       
    64     /**
       
    65      * Creates a <code>DOMTransform</code> from an element. This constructor
       
    66      * invokes the abstract {@link #unmarshalParams unmarshalParams} method to
       
    67      * unmarshal any algorithm-specific input parameters.
       
    68      *
       
    69      * @param transElem a Transform element
       
    70      */
       
    71     public DOMTransform(Element transElem, XMLCryptoContext context)
       
    72         throws MarshalException {
       
    73         Document ownerDoc = transElem.getOwnerDocument();
       
    74         String algorithm = DOMUtils.getAttributeValue(transElem, "Algorithm");
       
    75         try {
       
    76             spi = TransformService.getInstance(algorithm, "DOM");
       
    77         } catch (NoSuchAlgorithmException e) {
       
    78             throw new MarshalException(e);
       
    79         }
       
    80         try {
       
    81             spi.init(new javax.xml.crypto.dom.DOMStructure(transElem), context);
       
    82         } catch (InvalidAlgorithmParameterException iape) {
       
    83             throw new MarshalException(iape);
       
    84         }
       
    85     }
       
    86 
       
    87     public final AlgorithmParameterSpec getParameterSpec() {
       
    88         return spi.getParameterSpec();
       
    89     }
       
    90 
       
    91     public final String getAlgorithm() {
       
    92         return spi.getAlgorithm();
       
    93     }
       
    94 
       
    95     /**
       
    96      * This method invokes the abstract {@link #marshalParams marshalParams}
       
    97      * method to marshal any algorithm-specific parameters.
       
    98      */
       
    99     public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
       
   100         throws MarshalException {
       
   101         Document ownerDoc = DOMUtils.getOwnerDocument(parent);
       
   102 
       
   103         Element transformElem = null;
       
   104         if (parent.getLocalName().equals("Transforms")) {
       
   105             transformElem = DOMUtils.createElement
       
   106                 (ownerDoc, "Transform", XMLSignature.XMLNS, dsPrefix);
       
   107         } else {
       
   108             transformElem = DOMUtils.createElement
       
   109             (ownerDoc, "CanonicalizationMethod", XMLSignature.XMLNS, dsPrefix);
       
   110         }
       
   111         DOMUtils.setAttribute(transformElem, "Algorithm", getAlgorithm());
       
   112 
       
   113         spi.marshalParams
       
   114             (new javax.xml.crypto.dom.DOMStructure(transformElem), context);
       
   115 
       
   116         parent.appendChild(transformElem);
       
   117     }
       
   118 
       
   119     /**
       
   120      * Transforms the specified data using the underlying transform algorithm.
       
   121      *
       
   122      * @param data the data to be transformed
       
   123      * @param sc the <code>XMLCryptoContext</code> containing
       
   124      *    additional context (may be <code>null</code> if not applicable)
       
   125      * @return the transformed data
       
   126      * @throws NullPointerException if <code>data</code> is <code>null</code>
       
   127      * @throws XMLSignatureException if an unexpected error occurs while
       
   128      *    executing the transform
       
   129      */
       
   130     public Data transform(Data data, XMLCryptoContext xc)
       
   131         throws TransformException {
       
   132         return spi.transform(data, xc);
       
   133     }
       
   134 
       
   135     /**
       
   136      * Transforms the specified data using the underlying transform algorithm.
       
   137      *
       
   138      * @param data the data to be transformed
       
   139      * @param sc the <code>XMLCryptoContext</code> containing
       
   140      *    additional context (may be <code>null</code> if not applicable)
       
   141      * @param os the <code>OutputStream</code> that should be used to write
       
   142      *    the transformed data to
       
   143      * @return the transformed data
       
   144      * @throws NullPointerException if <code>data</code> is <code>null</code>
       
   145      * @throws XMLSignatureException if an unexpected error occurs while
       
   146      *    executing the transform
       
   147      */
       
   148     public Data transform(Data data, XMLCryptoContext xc, OutputStream os)
       
   149         throws TransformException {
       
   150         return spi.transform(data, xc, os);
       
   151     }
       
   152 
       
   153     public boolean equals(Object o) {
       
   154         if (this == o) {
       
   155             return true;
       
   156         }
       
   157 
       
   158         if (!(o instanceof Transform)) {
       
   159             return false;
       
   160         }
       
   161         Transform otransform = (Transform) o;
       
   162 
       
   163         return (getAlgorithm().equals(otransform.getAlgorithm()) &&
       
   164             DOMUtils.paramsEqual
       
   165                 (getParameterSpec(), otransform.getParameterSpec()));
       
   166     }
       
   167 
       
   168     /**
       
   169      * Transforms the specified data using the underlying transform algorithm.
       
   170      * This method invokes the {@link #marshal marshal} method and passes it
       
   171      * the specified <code>DOMSignContext</code> before transforming the data.
       
   172      *
       
   173      * @param data the data to be transformed
       
   174      * @param sc the <code>XMLCryptoContext</code> containing
       
   175      *    additional context (may be <code>null</code> if not applicable)
       
   176      * @param context the marshalling context
       
   177      * @return the transformed data
       
   178      * @throws MarshalException if an exception occurs while marshalling
       
   179      * @throws NullPointerException if <code>data</code> or <code>context</code>
       
   180      *    is <code>null</code>
       
   181      * @throws XMLSignatureException if an unexpected error occurs while
       
   182      *    executing the transform
       
   183      */
       
   184     Data transform(Data data, XMLCryptoContext xc, DOMSignContext context)
       
   185         throws MarshalException, TransformException {
       
   186         marshal(context.getParent(),
       
   187             DOMUtils.getSignaturePrefix(context), context);
       
   188         return transform(data, xc);
       
   189     }
       
   190 }