2
|
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: DOMXMLObject.java,v 1.16 2005/05/12 19:28:35 mullan Exp $
|
|
27 |
*/
|
|
28 |
package org.jcp.xml.dsig.internal.dom;
|
|
29 |
|
|
30 |
import javax.xml.crypto.*;
|
|
31 |
import javax.xml.crypto.dom.DOMCryptoContext;
|
|
32 |
import javax.xml.crypto.dsig.*;
|
|
33 |
|
|
34 |
import java.util.*;
|
|
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 |
/**
|
|
41 |
* DOM-based implementation of XMLObject.
|
|
42 |
*
|
|
43 |
* @author Sean Mullan
|
|
44 |
*/
|
|
45 |
public final class DOMXMLObject extends DOMStructure implements XMLObject {
|
|
46 |
|
|
47 |
private final String id;
|
|
48 |
private final String mimeType;
|
|
49 |
private final String encoding;
|
|
50 |
private final List content;
|
|
51 |
|
|
52 |
/**
|
|
53 |
* Creates an <code>XMLObject</code> from the specified parameters.
|
|
54 |
*
|
|
55 |
* @param content a list of {@link XMLStructure}s. The list
|
|
56 |
* is defensively copied to protect against subsequent modification.
|
|
57 |
* May be <code>null</code> or empty.
|
|
58 |
* @param id the Id (may be <code>null</code>)
|
|
59 |
* @param mimeType the mime type (may be <code>null</code>)
|
|
60 |
* @param encoding the encoding (may be <code>null</code>)
|
|
61 |
* @return an <code>XMLObject</code>
|
|
62 |
* @throws ClassCastException if <code>content</code> contains any
|
|
63 |
* entries that are not of type {@link XMLStructure}
|
|
64 |
*/
|
|
65 |
public DOMXMLObject(List content, String id, String mimeType,
|
|
66 |
String encoding) {
|
|
67 |
if (content == null || content.isEmpty()) {
|
|
68 |
this.content = Collections.EMPTY_LIST;
|
|
69 |
} else {
|
|
70 |
List contentCopy = new ArrayList(content);
|
|
71 |
for (int i = 0, size = contentCopy.size(); i < size; i++) {
|
|
72 |
if (!(contentCopy.get(i) instanceof XMLStructure)) {
|
|
73 |
throw new ClassCastException
|
|
74 |
("content["+i+"] is not a valid type");
|
|
75 |
}
|
|
76 |
}
|
|
77 |
this.content = Collections.unmodifiableList(contentCopy);
|
|
78 |
}
|
|
79 |
this.id = id;
|
|
80 |
this.mimeType = mimeType;
|
|
81 |
this.encoding = encoding;
|
|
82 |
}
|
|
83 |
|
|
84 |
/**
|
|
85 |
* Creates an <code>XMLObject</code> from an element.
|
|
86 |
*
|
|
87 |
* @param objElem an Object element
|
|
88 |
* @throws MarshalException if there is an error when unmarshalling
|
|
89 |
*/
|
|
90 |
public DOMXMLObject(Element objElem, XMLCryptoContext context)
|
|
91 |
throws MarshalException {
|
|
92 |
// unmarshal attributes
|
|
93 |
this.encoding = DOMUtils.getAttributeValue(objElem, "Encoding");
|
|
94 |
this.id = DOMUtils.getAttributeValue(objElem, "Id");
|
|
95 |
this.mimeType = DOMUtils.getAttributeValue(objElem, "MimeType");
|
|
96 |
|
|
97 |
NodeList nodes = objElem.getChildNodes();
|
|
98 |
int length = nodes.getLength();
|
|
99 |
List content = new ArrayList(length);
|
|
100 |
for (int i = 0; i < length; i++) {
|
|
101 |
Node child = nodes.item(i);
|
|
102 |
if (child.getNodeType() == Node.ELEMENT_NODE) {
|
|
103 |
Element childElem = (Element) child;
|
|
104 |
String tag = childElem.getLocalName();
|
|
105 |
if (tag.equals("Manifest")) {
|
|
106 |
content.add(new DOMManifest(childElem, context));
|
|
107 |
continue;
|
|
108 |
} else if (tag.equals("SignatureProperties")) {
|
|
109 |
content.add(new DOMSignatureProperties(childElem));
|
|
110 |
continue;
|
|
111 |
} else if (tag.equals("X509Data")) {
|
|
112 |
content.add(new DOMX509Data(childElem));
|
|
113 |
continue;
|
|
114 |
}
|
|
115 |
//@@@FIXME: check for other dsig structures
|
|
116 |
}
|
|
117 |
content.add(new javax.xml.crypto.dom.DOMStructure(child));
|
|
118 |
}
|
|
119 |
if (content.isEmpty()) {
|
|
120 |
this.content = Collections.EMPTY_LIST;
|
|
121 |
} else {
|
|
122 |
this.content = Collections.unmodifiableList(content);
|
|
123 |
}
|
|
124 |
}
|
|
125 |
|
|
126 |
public List getContent() {
|
|
127 |
return content;
|
|
128 |
}
|
|
129 |
|
|
130 |
public String getId() {
|
|
131 |
return id;
|
|
132 |
}
|
|
133 |
|
|
134 |
public String getMimeType() {
|
|
135 |
return mimeType;
|
|
136 |
}
|
|
137 |
|
|
138 |
public String getEncoding() {
|
|
139 |
return encoding;
|
|
140 |
}
|
|
141 |
|
|
142 |
public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
|
|
143 |
throws MarshalException {
|
|
144 |
Document ownerDoc = DOMUtils.getOwnerDocument(parent);
|
|
145 |
|
|
146 |
Element objElem = DOMUtils.createElement
|
|
147 |
(ownerDoc, "Object", XMLSignature.XMLNS, dsPrefix);
|
|
148 |
|
|
149 |
// set attributes
|
|
150 |
DOMUtils.setAttributeID(objElem, "Id", id);
|
|
151 |
DOMUtils.setAttribute(objElem, "MimeType", mimeType);
|
|
152 |
DOMUtils.setAttribute(objElem, "Encoding", encoding);
|
|
153 |
|
|
154 |
// create and append any elements and mixed content, if necessary
|
|
155 |
for (int i = 0, size = content.size(); i < size; i++) {
|
|
156 |
XMLStructure object = (XMLStructure) content.get(i);
|
|
157 |
if (object instanceof DOMStructure) {
|
|
158 |
((DOMStructure) object).marshal(objElem, dsPrefix, context);
|
|
159 |
} else {
|
|
160 |
javax.xml.crypto.dom.DOMStructure domObject =
|
|
161 |
(javax.xml.crypto.dom.DOMStructure) object;
|
|
162 |
DOMUtils.appendChild(objElem, domObject.getNode());
|
|
163 |
}
|
|
164 |
}
|
|
165 |
|
|
166 |
parent.appendChild(objElem);
|
|
167 |
}
|
|
168 |
|
|
169 |
public boolean equals(Object o) {
|
|
170 |
if (this == o) {
|
|
171 |
return true;
|
|
172 |
}
|
|
173 |
|
|
174 |
if (!(o instanceof XMLObject)) {
|
|
175 |
return false;
|
|
176 |
}
|
|
177 |
XMLObject oxo = (XMLObject) o;
|
|
178 |
|
|
179 |
boolean idsEqual = (id == null ? oxo.getId() == null :
|
|
180 |
id.equals(oxo.getId()));
|
|
181 |
boolean encodingsEqual = (encoding == null ? oxo.getEncoding() == null :
|
|
182 |
encoding.equals(oxo.getEncoding()));
|
|
183 |
boolean mimeTypesEqual = (mimeType == null ? oxo.getMimeType() == null :
|
|
184 |
mimeType.equals(oxo.getMimeType()));
|
|
185 |
|
|
186 |
return (idsEqual && encodingsEqual && mimeTypesEqual &&
|
|
187 |
equalsContent(oxo.getContent()));
|
|
188 |
}
|
|
189 |
|
|
190 |
private boolean equalsContent(List otherContent) {
|
|
191 |
if (content.size() != otherContent.size()) {
|
|
192 |
return false;
|
|
193 |
}
|
|
194 |
for (int i = 0, osize = otherContent.size(); i < osize; i++) {
|
|
195 |
XMLStructure oxs = (XMLStructure) otherContent.get(i);
|
|
196 |
XMLStructure xs = (XMLStructure) content.get(i);
|
|
197 |
if (oxs instanceof javax.xml.crypto.dom.DOMStructure) {
|
|
198 |
if (!(xs instanceof javax.xml.crypto.dom.DOMStructure)) {
|
|
199 |
return false;
|
|
200 |
}
|
|
201 |
Node onode =
|
|
202 |
((javax.xml.crypto.dom.DOMStructure) oxs).getNode();
|
|
203 |
Node node =
|
|
204 |
((javax.xml.crypto.dom.DOMStructure) xs).getNode();
|
|
205 |
if (!DOMUtils.nodesEqual(node, onode)) {
|
|
206 |
return false;
|
|
207 |
}
|
|
208 |
} else {
|
|
209 |
if (!(xs.equals(oxs))) {
|
|
210 |
return false;
|
|
211 |
}
|
|
212 |
}
|
|
213 |
}
|
|
214 |
|
|
215 |
return true;
|
|
216 |
}
|
|
217 |
}
|