API Overview API Index Package Overview Direct link to this page
JDK 1.6
  org.jcp.xml.dsig.internal.dom. ApacheCanonicalizer 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

/*
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 */
/*
 * $Id: ApacheCanonicalizer.java,v 1.17 2005/09/19 18:20:04 mullan Exp $
 */
package org.jcp.xml.dsig.internal.dom;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.spec.AlgorithmParameterSpec;
import java.security.InvalidAlgorithmParameterException;
import java.util.Set;
import java.util.logging.Logger;
import java.util.logging.Level;
import javax.xml.crypto.*;
import javax.xml.crypto.dom.DOMCryptoContext;
import javax.xml.crypto.dsig.TransformException;
import javax.xml.crypto.dsig.TransformService;
import javax.xml.crypto.dsig.XMLSignatureException;
import javax.xml.crypto.dsig.spec.C14NMethodParameterSpec;

import com.sun.org.apache.xml.internal.security.c14n.Canonicalizer;
import com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException;
import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
import com.sun.org.apache.xml.internal.security.transforms.Transform;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public abstract class ApacheCanonicalizer extends TransformService {

    private static Logger log = Logger.getLogger("org.jcp.xml.dsig.internal.dom");
    protected Canonicalizer apacheCanonicalizer;
    private Transform apacheTransform;
    protected String inclusiveNamespaces;
    protected C14NMethodParameterSpec params;
    protected Document ownerDoc;
    protected Element transformElem;
    
    public final AlgorithmParameterSpec getParameterSpec() {
	return params;
    }

    public void init(XMLStructure parent, XMLCryptoContext context)
	throws InvalidAlgorithmParameterException {
	if (context != null && !(context instanceof DOMCryptoContext)) {
	    throw new ClassCastException
		("context must be of type DOMCryptoContext");
	}
        transformElem = (Element)
            ((javax.xml.crypto.dom.DOMStructure) parent).getNode();
        ownerDoc = DOMUtils.getOwnerDocument(transformElem);
    }

    public void marshalParams(XMLStructure parent, XMLCryptoContext context)
	throws MarshalException {
	if (context != null && !(context instanceof DOMCryptoContext)) {
	    throw new ClassCastException
		("context must be of type DOMCryptoContext");
	}
        transformElem = (Element)
            ((javax.xml.crypto.dom.DOMStructure) parent).getNode();
        ownerDoc = DOMUtils.getOwnerDocument(transformElem);
    }
    
    public Data canonicalize(Data data, XMLCryptoContext xc) 
	throws TransformException {
	return canonicalize(data, xc, null);
    }

    public Data canonicalize(Data data, XMLCryptoContext xc, OutputStream os) 
	throws TransformException {

	if (apacheCanonicalizer == null) {
	    try {
                apacheCanonicalizer = Canonicalizer.getInstance(getAlgorithm());
		if (log.isLoggable(Level.FINE)) {
                    log.log(Level.FINE, "Created canonicalizer for algorithm: " 
		        + getAlgorithm());
		}
	    } catch (InvalidCanonicalizerException ice) {
                throw new TransformException
		    ("Couldn't find Canonicalizer for: " + getAlgorithm() +
			": " + ice.getMessage(), ice);
	    }
	}

	if (os != null) {
	    apacheCanonicalizer.setWriter(os);
	} else {
	    apacheCanonicalizer.setWriter(new ByteArrayOutputStream());
	}

	try {
	    Set nodeSet = null;
	    if (data instanceof ApacheData) {
		XMLSignatureInput in = 
		    ((ApacheData) data).getXMLSignatureInput();
		if (in.isElement()) {
	            if (inclusiveNamespaces != null) {
	                return new OctetStreamData(new ByteArrayInputStream
		            (apacheCanonicalizer.canonicalizeSubtree
		                (in.getSubNode(), inclusiveNamespaces)));
	            } else {
	                return new OctetStreamData(new ByteArrayInputStream
		            (apacheCanonicalizer.canonicalizeSubtree
		                (in.getSubNode())));
		    }
	        } else if (in.isNodeSet()) {
                    nodeSet = in.getNodeSet();
		} else {
		    return new OctetStreamData(new ByteArrayInputStream(
		        apacheCanonicalizer.canonicalize(
        		    Utils.readBytesFromStream(in.getOctetStream()))));
		}
	    } else if (data instanceof DOMSubTreeData) {
	        DOMSubTreeData subTree = (DOMSubTreeData) data;
	        if (inclusiveNamespaces != null) {
	            return new OctetStreamData(new ByteArrayInputStream
		        (apacheCanonicalizer.canonicalizeSubtree
		         (subTree.getRoot(), inclusiveNamespaces)));
	        } else {
	            return new OctetStreamData(new ByteArrayInputStream
		        (apacheCanonicalizer.canonicalizeSubtree
		         (subTree.getRoot())));
	        }
	    } else if (data instanceof NodeSetData) {
	        NodeSetData nsd = (NodeSetData) data;
	        // convert Iterator to Set
	        nodeSet = Utils.toNodeSet(nsd.iterator());
		if (log.isLoggable(Level.FINE)) {
	            log.log(Level.FINE, "Canonicalizing " + nodeSet.size() 
		        + " nodes");
		}
            } else {
		return new OctetStreamData(new ByteArrayInputStream(
		    apacheCanonicalizer.canonicalize(
        		Utils.readBytesFromStream(
        		((OctetStreamData)data).getOctetStream()))));
	    }
	    if (inclusiveNamespaces != null) {
	        return new OctetStreamData(new ByteArrayInputStream(
                    apacheCanonicalizer.canonicalizeXPathNodeSet
		        (nodeSet, inclusiveNamespaces)));
	    } else {
	        return new OctetStreamData(new ByteArrayInputStream(
                    apacheCanonicalizer.canonicalizeXPathNodeSet(nodeSet)));
	    }
	} catch (Exception e) {
            throw new TransformException(e);
	}
    }

    public Data transform(Data data, XMLCryptoContext xc, OutputStream os)
        throws TransformException {
	if (data == null) {
	    throw new NullPointerException("data must not be null");
	}
	if (os == null) {
	    throw new NullPointerException("output stream must not be null");
	}

        if (ownerDoc == null) {
            throw new TransformException("transform must be marshalled");
        }

        if (apacheTransform == null) {
            try {
                apacheTransform = Transform.getInstance
                    (ownerDoc, getAlgorithm(), transformElem.getChildNodes());
                apacheTransform.setElement(transformElem, xc.getBaseURI());
		if (log.isLoggable(Level.FINE)) {
                    log.log(Level.FINE, "Created transform for algorithm: " 
		        + getAlgorithm());            
		}
	    } catch (Exception ex) {
                throw new TransformException
                    ("Couldn't find Transform for: " + getAlgorithm(), ex);
            }
        }

        XMLSignatureInput in;
        if (data instanceof ApacheData) {
	    if (log.isLoggable(Level.FINE)) {
                log.log(Level.FINE, "ApacheData = true");
	    }
            in = ((ApacheData) data).getXMLSignatureInput();
        } else if (data instanceof NodeSetData) {
	    if (log.isLoggable(Level.FINE)) {
                log.log(Level.FINE, "isNodeSet() = true");
	    }
            if (data instanceof DOMSubTreeData) {
                DOMSubTreeData subTree = (DOMSubTreeData) data;
                in = new XMLSignatureInput(subTree.getRoot());
		in.setExcludeComments(subTree.excludeComments());
            } else {
                Set nodeSet =
                    Utils.toNodeSet(((NodeSetData) data).iterator());
                in = new XMLSignatureInput(nodeSet);
            }
        } else {
	    if (log.isLoggable(Level.FINE)) {
                log.log(Level.FINE, "isNodeSet() = false");
	    }
            try {
                in = new XMLSignatureInput
                    (((OctetStreamData)data).getOctetStream());
            } catch (Exception ex) {
                throw new TransformException(ex);
            }
        }

        try {
            if (os != null) {
                in = apacheTransform.performTransform(in, os);
		if (!in.isNodeSet() && !in.isElement()) {
		    return null;
		}
            } else {
                in = apacheTransform.performTransform(in);
            }
	    if (in.isOctetStream()) {
                return new ApacheOctetStreamData(in);
	    } else {
                return new ApacheNodeSetData(in);
	    }
        } catch (Exception ex) {
            throw new TransformException(ex);
        }
    }

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

Generated By: JavaOnTracks Doclet 0.1.4     ©Thibaut Colar