jdk/src/java.xml.crypto/share/classes/javax/xml/crypto/dsig/TransformService.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 22100 jdk/src/share/classes/javax/xml/crypto/dsig/TransformService.java@b5238ea86488
child 33241 27eb2d6abda9
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22100
b5238ea86488 8031148: Fix doclint issues in javax.xml.crypto.dsig
darcy
parents: 14342
diff changeset
     2
 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * $Id: TransformService.java,v 1.6.4.1 2005/09/15 12:42:11 mullan Exp $
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
package javax.xml.crypto.dsig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.InvalidAlgorithmParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.NoSuchProviderException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.Provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.Provider.Service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.Security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.xml.crypto.MarshalException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.xml.crypto.XMLStructure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.xml.crypto.XMLCryptoContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.xml.crypto.dsig.spec.TransformParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.security.jca.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.security.jca.GetInstance.Instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * A Service Provider Interface for transform and canonicalization algorithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>Each instance of <code>TransformService</code> supports a specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * transform or canonicalization algorithm and XML mechanism type. To create a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <code>TransformService</code>, call one of the static
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * {@link #getInstance getInstance} methods, passing in the algorithm URI and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * XML mechanism type desired, for example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <blockquote><code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * TransformService ts = TransformService.getInstance(Transform.XPATH2, "DOM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * </code></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p><code>TransformService</code> implementations are registered and loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * using the {@link java.security.Provider} mechanism.  Each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <code>TransformService</code> service provider implementation should include
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * a <code>MechanismType</code> service attribute that identifies the XML
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * mechanism type that it supports. If the attribute is not specified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * "DOM" is assumed. For example, a service provider that supports the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * XPath Filter 2 Transform and DOM mechanism would be specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <code>Provider</code> subclass as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *     put("TransformService." + Transform.XPATH2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *         "org.example.XPath2TransformService");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *     put("TransformService." + Transform.XPATH2 + " MechanismType", "DOM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <code>TransformService</code> implementations that support the DOM
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * mechanism type must abide by the DOM interoperability requirements defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * in the
22100
b5238ea86488 8031148: Fix doclint issues in javax.xml.crypto.dsig
darcy
parents: 14342
diff changeset
    74
 * <a href="../../../../../technotes/guides/security/xmldsig/overview.html#DOM%20Mechanism%20Requirements">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * DOM Mechanism Requirements</a> section of the API overview. See the
22100
b5238ea86488 8031148: Fix doclint issues in javax.xml.crypto.dsig
darcy
parents: 14342
diff changeset
    76
 * <a href="../../../../../technotes/guides/security/xmldsig/overview.html#Service%20Provider">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * Service Providers</a> section of the API overview for a list of standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * mechanism types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * Once a <code>TransformService</code> has been created, it can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * to process <code>Transform</code> or <code>CanonicalizationMethod</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * objects. If the <code>Transform</code> or <code>CanonicalizationMethod</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * exists in XML form (for example, when validating an existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <code>XMLSignature</code>), the {@link #init(XMLStructure, XMLCryptoContext)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * method must be first called to initialize the transform and provide document
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * context (even if there are no parameters). Alternatively, if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <code>Transform</code> or <code>CanonicalizationMethod</code> is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * created from scratch, the {@link #init(TransformParameterSpec)} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * is called to initialize the transform with parameters and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * {@link #marshalParams marshalParams} method is called to marshal the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * parameters to XML and provide the transform with document context. Finally,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * the {@link #transform transform} method is called to perform the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * transformation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <b>Concurrent Access</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * <p>The static methods of this class are guaranteed to be thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * Multiple threads may concurrently invoke the static methods defined in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * class with no ill effects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * <p>However, this is not true for the non-static methods defined by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * class. Unless otherwise documented by a specific provider, threads that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * need to access a single <code>TransformService</code> instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * concurrently should synchronize amongst themselves and provide the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * necessary locking. Multiple threads each manipulating a different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * <code>TransformService</code> instance need not synchronize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * @author Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * @author JSR 105 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
public abstract class TransformService implements Transform {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private String mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Default constructor, for invocation by subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    protected TransformService() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Returns a <code>TransformService</code> that supports the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * algorithm URI (ex: {@link Transform#XPATH2}) and mechanism type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * (ex: DOM).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <p>This method uses the standard JCA provider lookup mechanism to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * locate and instantiate a <code>TransformService</code> implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * of the desired algorithm and <code>MechanismType</code> service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * attribute. It traverses the list of registered security
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * <code>Provider</code>s, starting with the most preferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * <code>Provider</code>. A new <code>TransformService</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * from the first <code>Provider</code> that supports the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * algorithm and mechanism type is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param algorithm the URI of the algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param mechanismType the type of the XML processing mechanism and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *   representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @return a new <code>TransformService</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @throws NullPointerException if <code>algorithm</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *   <code>mechanismType</code> is  <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @throws NoSuchAlgorithmException if no <code>Provider</code> supports a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *   <code>TransformService</code> implementation for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *   algorithm and mechanism type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public static TransformService getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        (String algorithm, String mechanismType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (mechanismType == null || algorithm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        boolean dom = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (mechanismType.equals("DOM")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            dom = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
11276
6a7de6dddc18 7118546: fix warnings in javax.xml.crypto, javax.script
smarks
parents: 5506
diff changeset
   160
        List<Service> services = GetInstance.getServices("TransformService", algorithm);
6a7de6dddc18 7118546: fix warnings in javax.xml.crypto, javax.script
smarks
parents: 5506
diff changeset
   161
        for (Iterator<Service> t = services.iterator(); t.hasNext(); ) {
6a7de6dddc18 7118546: fix warnings in javax.xml.crypto, javax.script
smarks
parents: 5506
diff changeset
   162
            Service s = t.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            String value = s.getAttribute("MechanismType");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            if ((value == null && dom) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                (value != null && value.equals(mechanismType))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                Instance instance = GetInstance.getInstance(s, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                TransformService ts = (TransformService) instance.impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                ts.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                ts.mechanism = mechanismType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                ts.provider = instance.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                return ts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            (algorithm + " algorithm and " + mechanismType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                 + " mechanism not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Returns a <code>TransformService</code> that supports the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * algorithm URI (ex: {@link Transform#XPATH2}) and mechanism type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * (ex: DOM) as supplied by the specified provider. Note that the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * <code>Provider</code> object does not have to be registered in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @param algorithm the URI of the algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @param mechanismType the type of the XML processing mechanism and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *   representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param provider the <code>Provider</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @return a new <code>TransformService</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @throws NullPointerException if <code>provider</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *   <code>algorithm</code>, or <code>mechanismType</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *   <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @throws NoSuchAlgorithmException if a <code>TransformService</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *   implementation for the specified algorithm and mechanism type is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *   available from the specified <code>Provider</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public static TransformService getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        (String algorithm, String mechanismType, Provider provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (mechanismType == null || algorithm == null || provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        boolean dom = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (mechanismType.equals("DOM")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            dom = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        Service s = GetInstance.getService
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            ("TransformService", algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        String value = s.getAttribute("MechanismType");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if ((value == null && dom) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            (value != null && value.equals(mechanismType))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            Instance instance = GetInstance.getInstance(s, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            TransformService ts = (TransformService) instance.impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            ts.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            ts.mechanism = mechanismType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            ts.provider = instance.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            return ts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            (algorithm + " algorithm and " + mechanismType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                 + " mechanism not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * Returns a <code>TransformService</code> that supports the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * algorithm URI (ex: {@link Transform#XPATH2}) and mechanism type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * (ex: DOM) as supplied by the specified provider. The specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * must be registered in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * <p>Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @param algorithm the URI of the algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @param mechanismType the type of the XML processing mechanism and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *   representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @param provider the string name of the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @return a new <code>TransformService</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @throws NoSuchProviderException if the specified provider is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *   registered in the security provider list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @throws NullPointerException if <code>provider</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *   <code>mechanismType</code>, or <code>algorithm</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *   <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @throws NoSuchAlgorithmException if a <code>TransformService</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *   implementation for the specified algorithm and mechanism type is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *   available from the specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    public static TransformService getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        (String algorithm, String mechanismType, String provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        throws NoSuchAlgorithmException, NoSuchProviderException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (mechanismType == null || algorithm == null || provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        } else if (provider.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            throw new NoSuchProviderException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        boolean dom = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (mechanismType.equals("DOM")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            dom = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        Service s = GetInstance.getService
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            ("TransformService", algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        String value = s.getAttribute("MechanismType");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if ((value == null && dom) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            (value != null && value.equals(mechanismType))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            Instance instance = GetInstance.getInstance(s, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            TransformService ts = (TransformService) instance.impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            ts.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            ts.mechanism = mechanismType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            ts.provider = instance.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            return ts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            (algorithm + " algorithm and " + mechanismType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                 + " mechanism not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
11276
6a7de6dddc18 7118546: fix warnings in javax.xml.crypto, javax.script
smarks
parents: 5506
diff changeset
   280
    private static class MechanismMapEntry implements Map.Entry<String,String> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        private final String mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        private final String key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        MechanismMapEntry(String algorithm, String mechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            this.mechanism = mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            this.key = "TransformService." + algorithm + " MechanismType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            if (!(o instanceof Map.Entry)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
11276
6a7de6dddc18 7118546: fix warnings in javax.xml.crypto, javax.script
smarks
parents: 5506
diff changeset
   293
            Map.Entry<?,?> e = (Map.Entry<?,?>) o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            return (getKey()==null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    e.getKey()==null : getKey().equals(e.getKey())) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                   (getValue()==null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    e.getValue()==null : getValue().equals(e.getValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
11276
6a7de6dddc18 7118546: fix warnings in javax.xml.crypto, javax.script
smarks
parents: 5506
diff changeset
   299
        public String getKey() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
11276
6a7de6dddc18 7118546: fix warnings in javax.xml.crypto, javax.script
smarks
parents: 5506
diff changeset
   302
        public String getValue() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            return mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
11276
6a7de6dddc18 7118546: fix warnings in javax.xml.crypto, javax.script
smarks
parents: 5506
diff changeset
   305
        public String setValue(String value) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            return (getKey()==null ? 0 : getKey().hashCode()) ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                   (getValue()==null ? 0 : getValue().hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * Returns the mechanism type supported by this <code>TransformService</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @return the mechanism type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public final String getMechanismType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        return mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Returns the URI of the algorithm supported by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * <code>TransformService</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @return the algorithm URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    public final String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        return algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Returns the provider of this <code>TransformService</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @return the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    public final Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * Initializes this <code>TransformService</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * <p>If the parameters exist in XML form, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * {@link #init(XMLStructure, XMLCryptoContext)} method should be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * initialize the <code>TransformService</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @param params the algorithm parameters (may be <code>null</code> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *   not required or optional)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @throws InvalidAlgorithmParameterException if the specified parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *   are invalid for this algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public abstract void init(TransformParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        throws InvalidAlgorithmParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Marshals the algorithm-specific parameters. If there are no parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * to be marshalled, this method returns without throwing an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @param parent a mechanism-specific structure containing the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *    node that the marshalled parameters should be appended to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @param context the <code>XMLCryptoContext</code> containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *    additional context (may be <code>null</code> if not applicable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @throws ClassCastException if the type of <code>parent</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *    <code>context</code> is not compatible with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *    <code>TransformService</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @throws NullPointerException if <code>parent</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @throws MarshalException if the parameters cannot be marshalled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    public abstract void marshalParams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        (XMLStructure parent, XMLCryptoContext context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        throws MarshalException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * Initializes this <code>TransformService</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * parameters and document context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @param parent a mechanism-specific structure containing the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *    structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @param context the <code>XMLCryptoContext</code> containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *    additional context (may be <code>null</code> if not applicable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @throws ClassCastException if the type of <code>parent</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *    <code>context</code> is not compatible with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *    <code>TransformService</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @throws NullPointerException if <code>parent</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * @throws InvalidAlgorithmParameterException if the specified parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *   are invalid for this algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    public abstract void init(XMLStructure parent, XMLCryptoContext context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        throws InvalidAlgorithmParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
}