J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 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: DOMManifest.java,v 1.16 2005/05/12 19:28:31 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 | |
| 39 | /** |
| 40 | * DOM-based implementation of Manifest. |
| 41 | * |
| 42 | * @author Sean Mullan |
| 43 | */ |
| 44 | public final class DOMManifest extends DOMStructure implements Manifest { |
| 45 | |
| 46 | private final List references; |
| 47 | private final String id; |
| 48 | |
| 49 | /** |
| 50 | * Creates a <code>DOMManifest</code> containing the specified |
| 51 | * list of {@link Reference}s and optional id. |
| 52 | * |
| 53 | * @param references a list of one or more <code>Reference</code>s. The list |
| 54 | * is defensively copied to protect against subsequent modification. |
| 55 | * @param id the id (may be <code>null</code> |
| 56 | * @throws NullPointerException if <code>references</code> is |
| 57 | * <code>null</code> |
| 58 | * @throws IllegalArgumentException if <code>references</code> is empty |
| 59 | * @throws ClassCastException if <code>references</code> contains any |
| 60 | * entries that are not of type {@link Reference} |
| 61 | */ |
| 62 | public DOMManifest(List references, String id) { |
| 63 | if (references == null) { |
| 64 | throw new NullPointerException("references cannot be null"); |
| 65 | } |
| 66 | List refCopy = new ArrayList(references); |
| 67 | if (refCopy.isEmpty()) { |
| 68 | throw new IllegalArgumentException("list of references must " + |
| 69 | "contain at least one entry"); |
| 70 | } |
| 71 | for (int i = 0, size = refCopy.size(); i < size; i++) { |
| 72 | if (!(refCopy.get(i) instanceof Reference)) { |
| 73 | throw new ClassCastException |
| 74 | ("references["+i+"] is not a valid type"); |
| 75 | } |
| 76 | } |
| 77 | this.references = Collections.unmodifiableList(refCopy); |
| 78 | this.id = id; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Creates a <code>DOMManifest</code> from an element. |
| 83 | * |
| 84 | * @param manElem a Manifest element |
| 85 | */ |
| 86 | public DOMManifest(Element manElem, XMLCryptoContext context) |
| 87 | throws MarshalException { |
| 88 | this.id = DOMUtils.getAttributeValue(manElem, "Id"); |
| 89 | Element refElem = DOMUtils.getFirstChildElement(manElem); |
| 90 | List refs = new ArrayList(); |
| 91 | while (refElem != null) { |
| 92 | refs.add(new DOMReference(refElem, context)); |
| 93 | refElem = DOMUtils.getNextSiblingElement(refElem); |
| 94 | } |
| 95 | this.references = Collections.unmodifiableList(refs); |
| 96 | } |
| 97 | |
| 98 | public String getId() { |
| 99 | return id; |
| 100 | } |
| 101 | |
| 102 | public List getReferences() { |
| 103 | return references; |
| 104 | } |
| 105 | |
| 106 | public void marshal(Node parent, String dsPrefix, DOMCryptoContext context) |
| 107 | throws MarshalException { |
| 108 | Document ownerDoc = DOMUtils.getOwnerDocument(parent); |
| 109 | |
| 110 | Element manElem = DOMUtils.createElement |
| 111 | (ownerDoc, "Manifest", XMLSignature.XMLNS, dsPrefix); |
| 112 | |
| 113 | DOMUtils.setAttributeID(manElem, "Id", id); |
| 114 | |
| 115 | // add references |
| 116 | for (int i = 0, size = references.size(); i < size; i++) { |
| 117 | DOMReference ref = (DOMReference) references.get(i); |
| 118 | ref.marshal(manElem, dsPrefix, context); |
| 119 | } |
| 120 | parent.appendChild(manElem); |
| 121 | } |
| 122 | |
| 123 | public boolean equals(Object o) { |
| 124 | if (this == o) { |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | if (!(o instanceof Manifest)) { |
| 129 | return false; |
| 130 | } |
| 131 | Manifest oman = (Manifest) o; |
| 132 | |
| 133 | boolean idsEqual = (id == null ? oman.getId() == null : |
| 134 | id.equals(oman.getId())); |
| 135 | |
| 136 | return (idsEqual && references.equals(oman.getReferences())); |
| 137 | } |
| 138 | } |