/* * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Sun designates this * particular file as subject to the "Classpath" exception as provided * by Sun in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, * CA 95054 USA or visit www.sun.com if you need additional information or * have any questions. */ /* * $Id: DOMSignatureProperties.java,v 1.12 2005/05/12 19:28:32 mullan Exp $ */ package org.jcp.xml.dsig.internal.dom; import javax.xml.crypto.*; import javax.xml.crypto.dom.DOMCryptoContext; import javax.xml.crypto.dsig.*; import java.util.*; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; /** * DOM-based implementation of SignatureProperties. * * @author Sean Mullan */ public final class DOMSignatureProperties extends DOMStructure implements SignatureProperties { private final String id; private final List properties; /** * Creates a <code>DOMSignatureProperties</code> from the specified * parameters. * * @param properties a list of one or more {@link SignatureProperty}s. The * list is defensively copied to protect against subsequent modification. * @param id the Id (may be <code>null</code>) * @return a <code>DOMSignatureProperties</code> * @throws ClassCastException if <code>properties</code> contains any * entries that are not of type {@link SignatureProperty} * @throws IllegalArgumentException if <code>properties</code> is empty * @throws NullPointerException if <code>properties</code> */ public DOMSignatureProperties(List properties, String id) { if (properties == null) { throw new NullPointerException("properties cannot be null"); } else if (properties.isEmpty()) { throw new IllegalArgumentException("properties cannot be empty"); } else { List propsCopy = new ArrayList(properties); for (int i = 0, size = propsCopy.size(); i < size; i++) { if (!(propsCopy.get(i) instanceof SignatureProperty)) { throw new ClassCastException ("properties["+i+"] is not a valid type"); } } this.properties = Collections.unmodifiableList(propsCopy); } this.id = id; } /** * Creates a <code>DOMSignatureProperties</code> from an element. * * @param propsElem a SignatureProperties element * @throws MarshalException if a marshalling error occurs */ public DOMSignatureProperties(Element propsElem) throws MarshalException{ // unmarshal attributes id = DOMUtils.getAttributeValue(propsElem, "Id"); NodeList nodes = propsElem.getChildNodes(); int length = nodes.getLength(); List properties = new ArrayList(length); for (int i = 0; i < length; i++) { Node child = nodes.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { properties.add(new DOMSignatureProperty((Element) child)); } } if (properties.isEmpty()) { throw new MarshalException("properties cannot be empty"); } else { this.properties = Collections.unmodifiableList(properties); } } public List getProperties() { return properties; } public String getId() { return id; } public void marshal(Node parent, String dsPrefix, DOMCryptoContext context) throws MarshalException { Document ownerDoc = DOMUtils.getOwnerDocument(parent); Element propsElem = DOMUtils.createElement (ownerDoc, "SignatureProperties", XMLSignature.XMLNS, dsPrefix); // set attributes DOMUtils.setAttributeID(propsElem, "Id", id); // create and append any properties for (int i = 0, size = properties.size(); i < size; i++) { DOMSignatureProperty property = (DOMSignatureProperty) properties.get(i); property.marshal(propsElem, dsPrefix, context); } parent.appendChild(propsElem); } public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof SignatureProperties)) { return false; } SignatureProperties osp = (SignatureProperties) o; boolean idsEqual = (id == null ? osp.getId() == null : id.equals(osp.getId())); return (properties.equals(osp.getProperties()) && idsEqual); } }