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: DOMSignatureProperty.java,v 1.14 2005/05/12 19:28:32 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 SignatureProperty. |
| 42 | * |
| 43 | * @author Sean Mullan |
| 44 | */ |
| 45 | public final class DOMSignatureProperty extends DOMStructure |
| 46 | implements SignatureProperty { |
| 47 | |
| 48 | private final String id; |
| 49 | private final String target; |
| 50 | private final List content; |
| 51 | |
| 52 | /** |
| 53 | * Creates a <code>SignatureProperty</code> from the specified parameters. |
| 54 | * |
| 55 | * @param content a list of one or more {@link XMLStructure}s. The list |
| 56 | * is defensively copied to protect against subsequent modification. |
| 57 | * @param target the target URI |
| 58 | * @param id the Id (may be <code>null</code>) |
| 59 | * @return a <code>SignatureProperty</code> |
| 60 | * @throws ClassCastException if <code>content</code> contains any |
| 61 | * entries that are not of type {@link XMLStructure} |
| 62 | * @throws IllegalArgumentException if <code>content</code> is empty |
| 63 | * @throws NullPointerException if <code>content</code> or |
| 64 | * <code>target</code> is <code>null</code> |
| 65 | */ |
| 66 | public DOMSignatureProperty(List content, String target, String id) { |
| 67 | if (target == null) { |
| 68 | throw new NullPointerException("target cannot be null"); |
| 69 | } else if (content == null) { |
| 70 | throw new NullPointerException("content cannot be null"); |
| 71 | } else if (content.isEmpty()) { |
| 72 | throw new IllegalArgumentException("content cannot be empty"); |
| 73 | } else { |
| 74 | List contentCopy = new ArrayList(content); |
| 75 | for (int i = 0, size = contentCopy.size(); i < size; i++) { |
| 76 | if (!(contentCopy.get(i) instanceof XMLStructure)) { |
| 77 | throw new ClassCastException |
| 78 | ("content["+i+"] is not a valid type"); |
| 79 | } |
| 80 | } |
| 81 | this.content = Collections.unmodifiableList(contentCopy); |
| 82 | } |
| 83 | this.target = target; |
| 84 | this.id = id; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Creates a <code>DOMSignatureProperty</code> from an element. |
| 89 | * |
| 90 | * @param propElem a SignatureProperty element |
| 91 | */ |
| 92 | public DOMSignatureProperty(Element propElem) throws MarshalException { |
| 93 | // unmarshal attributes |
| 94 | target = DOMUtils.getAttributeValue(propElem, "Target"); |
| 95 | if (target == null) { |
| 96 | throw new MarshalException("target cannot be null"); |
| 97 | } |
| 98 | id = DOMUtils.getAttributeValue(propElem, "Id"); |
| 99 | |
| 100 | NodeList nodes = propElem.getChildNodes(); |
| 101 | int length = nodes.getLength(); |
| 102 | List content = new ArrayList(length); |
| 103 | for (int i = 0; i < length; i++) { |
| 104 | content.add(new javax.xml.crypto.dom.DOMStructure(nodes.item(i))); |
| 105 | } |
| 106 | if (content.isEmpty()) { |
| 107 | throw new MarshalException("content cannot be empty"); |
| 108 | } else { |
| 109 | this.content = Collections.unmodifiableList(content); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | public List getContent() { |
| 114 | return content; |
| 115 | } |
| 116 | |
| 117 | public String getId() { |
| 118 | return id; |
| 119 | } |
| 120 | |
| 121 | public String getTarget() { |
| 122 | return target; |
| 123 | } |
| 124 | |
| 125 | public void marshal(Node parent, String dsPrefix, DOMCryptoContext context) |
| 126 | throws MarshalException { |
| 127 | Document ownerDoc = DOMUtils.getOwnerDocument(parent); |
| 128 | |
| 129 | Element propElem = DOMUtils.createElement |
| 130 | (ownerDoc, "SignatureProperty", XMLSignature.XMLNS, dsPrefix); |
| 131 | |
| 132 | // set attributes |
| 133 | DOMUtils.setAttributeID(propElem, "Id", id); |
| 134 | DOMUtils.setAttribute(propElem, "Target", target); |
| 135 | |
| 136 | // create and append any elements and mixed content |
| 137 | for (int i = 0, size = content.size(); i < size; i++) { |
| 138 | javax.xml.crypto.dom.DOMStructure property = |
| 139 | (javax.xml.crypto.dom.DOMStructure) content.get(i); |
| 140 | DOMUtils.appendChild(propElem, property.getNode()); |
| 141 | } |
| 142 | |
| 143 | parent.appendChild(propElem); |
| 144 | } |
| 145 | |
| 146 | public boolean equals(Object o) { |
| 147 | if (this == o) { |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | if (!(o instanceof SignatureProperty)) { |
| 152 | return false; |
| 153 | } |
| 154 | SignatureProperty osp = (SignatureProperty) o; |
| 155 | |
| 156 | boolean idsEqual = (id == null ? osp.getId() == null : |
| 157 | id.equals(osp.getId())); |
| 158 | |
| 159 | return (equalsContent(osp.getContent()) && |
| 160 | target.equals(osp.getTarget()) && idsEqual); |
| 161 | } |
| 162 | |
| 163 | private boolean equalsContent(List otherContent) { |
| 164 | int osize = otherContent.size(); |
| 165 | if (content.size() != osize) { |
| 166 | return false; |
| 167 | } |
| 168 | for (int i = 0; i < osize; i++) { |
| 169 | XMLStructure oxs = (XMLStructure) otherContent.get(i); |
| 170 | XMLStructure xs = (XMLStructure) content.get(i); |
| 171 | if (oxs instanceof javax.xml.crypto.dom.DOMStructure) { |
| 172 | if (!(xs instanceof javax.xml.crypto.dom.DOMStructure)) { |
| 173 | return false; |
| 174 | } |
| 175 | Node onode = |
| 176 | ((javax.xml.crypto.dom.DOMStructure) oxs).getNode(); |
| 177 | Node node = |
| 178 | ((javax.xml.crypto.dom.DOMStructure) xs).getNode(); |
| 179 | if (!DOMUtils.nodesEqual(node, onode)) { |
| 180 | return false; |
| 181 | } |
| 182 | } else { |
| 183 | if (!(xs.equals(oxs))) { |
| 184 | return false; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | return true; |
| 190 | } |
| 191 | } |