jdk/src/share/classes/org/jcp/xml/dsig/internal/dom/DOMManifest.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 1639 a97859015238
child 18240 cda839ac048f
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     2
 * reserved comment block
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
/*
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     6
 * Copyright 2005 The Apache Software Foundation.
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     7
 *
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     8
 *  Licensed under the Apache License, Version 2.0 (the "License");
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     9
 *  you may not use this file except in compliance with the License.
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    10
 *  You may obtain a copy of the License at
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    11
 *
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    12
 *      http://www.apache.org/licenses/LICENSE-2.0
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    13
 *
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    14
 *  Unless required by applicable law or agreed to in writing, software
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    15
 *  distributed under the License is distributed on an "AS IS" BASIS,
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    16
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    17
 *  See the License for the specific language governing permissions and
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    18
 *  limitations under the License.
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    19
 *
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    20
 */
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    21
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    23
 */
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    24
/*
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    25
 * $Id: DOMManifest.java,v 1.2 2008/07/24 15:20:32 mullan Exp $
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package org.jcp.xml.dsig.internal.dom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.xml.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.xml.crypto.dom.DOMCryptoContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.xml.crypto.dsig.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    33
import java.security.Provider;
2
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * DOM-based implementation of Manifest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public final class DOMManifest extends DOMStructure implements Manifest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private final List references;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private final String id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * Creates a <code>DOMManifest</code> containing the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * list of {@link Reference}s and optional id.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * @param references a list of one or more <code>Reference</code>s. The list
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *    is defensively copied to protect against subsequent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @param id the id (may be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * @throws NullPointerException if <code>references</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     *    <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @throws IllegalArgumentException if <code>references</code> is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @throws ClassCastException if <code>references</code> contains any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     *    entries that are not of type {@link Reference}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public DOMManifest(List references, String id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (references == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            throw new NullPointerException("references cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        List refCopy = new ArrayList(references);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (refCopy.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            throw new IllegalArgumentException("list of references must " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                "contain at least one entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        for (int i = 0, size = refCopy.size(); i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            if (!(refCopy.get(i) instanceof Reference)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                throw new ClassCastException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    ("references["+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.references = Collections.unmodifiableList(refCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        this.id = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Creates a <code>DOMManifest</code> from an element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param manElem a Manifest element
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    86
    public DOMManifest(Element manElem, XMLCryptoContext context,
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    87
        Provider provider) throws MarshalException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        this.id = DOMUtils.getAttributeValue(manElem, "Id");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        Element refElem = DOMUtils.getFirstChildElement(manElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        List refs = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        while (refElem != null) {
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    92
            refs.add(new DOMReference(refElem, context, provider));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            refElem = DOMUtils.getNextSiblingElement(refElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        this.references = Collections.unmodifiableList(refs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public String getId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public List getReferences() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return references;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        throws MarshalException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        Document ownerDoc = DOMUtils.getOwnerDocument(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        Element manElem = DOMUtils.createElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            (ownerDoc, "Manifest", XMLSignature.XMLNS, dsPrefix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        DOMUtils.setAttributeID(manElem, "Id", id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // add references
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        for (int i = 0, size = references.size(); i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            DOMReference ref = (DOMReference) references.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            ref.marshal(manElem, dsPrefix, context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        parent.appendChild(manElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (this == o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (!(o instanceof Manifest)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        Manifest oman = (Manifest) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        boolean idsEqual = (id == null ? oman.getId() == null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            id.equals(oman.getId()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return (idsEqual && references.equals(oman.getReferences()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
}