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