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