API Overview API Index Package Overview Direct link to this page
JDK 1.6
  org.jcp.xml.dsig.internal.dom. DOMXMLSignatureFactory View Javadoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243

/*
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 */
/*
 * $Id: DOMXMLSignatureFactory.java,v 1.21 2005/09/23 19:59:11 mullan Exp $
 */
package org.jcp.xml.dsig.internal.dom;

import javax.xml.crypto.*;
import javax.xml.crypto.dsig.*;
import javax.xml.crypto.dsig.dom.DOMValidateContext;
import javax.xml.crypto.dsig.keyinfo.*;
import javax.xml.crypto.dsig.spec.*;

import java.security.*;
import java.security.spec.AlgorithmParameterSpec;
import java.util.List;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * DOM-based implementation of XMLSignatureFactory.
 *
 * @author Sean Mullan
 */
public final class DOMXMLSignatureFactory extends XMLSignatureFactory {

    /**
     * Initializes a new instance of this class.
     */
    public DOMXMLSignatureFactory() {}

    public XMLSignature newXMLSignature(SignedInfo si, KeyInfo ki) {
	return new DOMXMLSignature(si, ki, null, null, null);
    }

    public XMLSignature newXMLSignature(SignedInfo si, KeyInfo ki,
        List objects, String id, String signatureValueId) {
	return new DOMXMLSignature(si, ki, objects, id, signatureValueId);
    }

    public Reference newReference(String uri, DigestMethod dm) {
	return newReference(uri, dm, null, null, null);
    }

    public Reference newReference(String uri, DigestMethod dm, List transforms,
        String type, String id) {
	return new DOMReference(uri, type, dm, transforms, id);
    }

    public Reference newReference(String uri, DigestMethod dm, 
	List appliedTransforms, Data result, List transforms, String type, 
	String id) {
	if (appliedTransforms == null) {
	    throw new NullPointerException("appliedTransforms cannot be null");
	}
	if (appliedTransforms.isEmpty()) {
	    throw new NullPointerException("appliedTransforms cannot be empty");
	}
	if (result == null) {
	    throw new NullPointerException("result cannot be null");
	}
	return new DOMReference
	    (uri, type, dm, appliedTransforms, result, transforms, id);
    }

    public Reference newReference(String uri, DigestMethod dm, List transforms,
        String type, String id, byte[] digestValue) {
	if (digestValue == null) {
	    throw new NullPointerException("digestValue cannot be null");
	}
	return new DOMReference
	    (uri, type, dm, null, null, transforms, id, digestValue);
    }

    public SignedInfo newSignedInfo(CanonicalizationMethod cm,
	SignatureMethod sm, List references) {
	return newSignedInfo(cm, sm, references, null);
    }

    public SignedInfo newSignedInfo(CanonicalizationMethod cm,
	SignatureMethod sm, List references, String id) {
	return new DOMSignedInfo(cm, sm, references, id);
    }

    // Object factory methods
    public XMLObject newXMLObject(List content, String id, String mimeType,
	String encoding) {
	return new DOMXMLObject(content, id, mimeType, encoding);
    }

    public Manifest newManifest(List references) {
	return newManifest(references, null);
    }

    public Manifest newManifest(List references, String id) {
	return new DOMManifest(references, id);
    }

    public SignatureProperties newSignatureProperties(List props, String id) {
	return new DOMSignatureProperties(props, id);
    }

    public SignatureProperty newSignatureProperty
	(List info, String target, String id) {
	return new DOMSignatureProperty(info, target, id);
    }

    public XMLSignature unmarshalXMLSignature(XMLValidateContext context)
        throws MarshalException {

        if (context == null) {
            throw new NullPointerException("context cannot be null");
        }
	return unmarshal(((DOMValidateContext) context).getNode(), context);
    }

    public XMLSignature unmarshalXMLSignature(XMLStructure xmlStructure)
        throws MarshalException {

        if (xmlStructure == null) {
            throw new NullPointerException("xmlStructure cannot be null");
        }
	return unmarshal
	    (((javax.xml.crypto.dom.DOMStructure) xmlStructure).getNode(), 
	     null);
    }

    private XMLSignature unmarshal(Node node, XMLValidateContext context) 
	throws MarshalException {

        node.normalize();
        
        Element element = null;
        if (node.getNodeType() == Node.DOCUMENT_NODE) {
            element = ((Document) node).getDocumentElement();
        } else if (node.getNodeType() == Node.ELEMENT_NODE) {
            element = (Element) node;
        } else {
            throw new MarshalException
		("Signature element is not a proper Node");
        }

        // check tag
        String tag = element.getLocalName();
	if (tag == null) {
	    throw new MarshalException("Document implementation must " +
		"support DOM Level 2 and be namespace aware");
	}
        if (tag.equals("Signature")) {
            return new DOMXMLSignature(element, context);
        } else {
            throw new MarshalException("invalid Signature tag: " + tag);
        }
    }

    public boolean isFeatureSupported(String feature) {
        if (feature == null) {
            throw new NullPointerException();
        } else {
            return false;
        }
    }

    public DigestMethod newDigestMethod(String algorithm,
        DigestMethodParameterSpec params) throws NoSuchAlgorithmException,
        InvalidAlgorithmParameterException {
	if (algorithm == null) {
	    throw new NullPointerException();
	}
        if (algorithm.equals(DigestMethod.SHA1)) {
	    return DOMSHADigestMethod.SHA1(params);
	} else if (algorithm.equals(DigestMethod.SHA256)) {
	    return DOMSHADigestMethod.SHA256(params);
	} else if (algorithm.equals(DigestMethod.SHA512)) {
            return DOMSHADigestMethod.SHA512(params);
	} else {
	    throw new NoSuchAlgorithmException("unsupported algorithm");
	}
    }

    public SignatureMethod newSignatureMethod(String algorithm,
        SignatureMethodParameterSpec params) throws NoSuchAlgorithmException,
        InvalidAlgorithmParameterException {
	if (algorithm == null) {
	    throw new NullPointerException();
	}
	if (algorithm.equals(SignatureMethod.HMAC_SHA1)) {
            return new DOMHMACSignatureMethod(params);
        } else if (algorithm.equals(SignatureMethod.RSA_SHA1)) {
            return new DOMRSASignatureMethod(params);
        } else if (algorithm.equals(SignatureMethod.DSA_SHA1)) {
            return new DOMDSASignatureMethod(params);
	} else {
	    throw new NoSuchAlgorithmException("unsupported algorithm");
	}
    }

    public Transform newTransform(String algorithm,
        TransformParameterSpec params) throws NoSuchAlgorithmException,
        InvalidAlgorithmParameterException {
	TransformService spi = TransformService.getInstance(algorithm, "DOM");
	spi.init(params);
	return new DOMTransform(spi);
    }

    public Transform newTransform(String algorithm,
        XMLStructure params) throws NoSuchAlgorithmException,
        InvalidAlgorithmParameterException {
	TransformService spi = TransformService.getInstance(algorithm, "DOM");
	if (params == null) {
	    spi.init(null);
	} else {
	    spi.init(params, null);
	}
	return new DOMTransform(spi);
    }

    public CanonicalizationMethod newCanonicalizationMethod(String algorithm,
        C14NMethodParameterSpec params) throws NoSuchAlgorithmException,
        InvalidAlgorithmParameterException {
	TransformService spi = TransformService.getInstance(algorithm, "DOM");
	spi.init(params);
	return new DOMCanonicalizationMethod(spi);
    }

    public CanonicalizationMethod newCanonicalizationMethod(String algorithm,
        XMLStructure params) throws NoSuchAlgorithmException,
        InvalidAlgorithmParameterException {
	TransformService spi = TransformService.getInstance(algorithm, "DOM");
	if (params == null) {
	    spi.init(null);
	} else {
	    spi.init(params, null);
	}
	return new DOMCanonicalizationMethod(spi);
    }

    public URIDereferencer getURIDereferencer() {
	return DOMURIDereferencer.INSTANCE;
    }
}

Generated By: JavaOnTracks Doclet 0.1.4     ©Thibaut Colar