org.jcp.xml.dsig.internal.dom.DOMKeyInfo Maven / Gradle / Ivy
The newest version!
/* */ package org.jcp.xml.dsig.internal.dom;
/* */
/* */ import java.util.ArrayList;
/* */ import java.util.Collections;
/* */ import java.util.List;
/* */ import javax.xml.crypto.MarshalException;
/* */ import javax.xml.crypto.XMLCryptoContext;
/* */ import javax.xml.crypto.XMLStructure;
/* */ import javax.xml.crypto.dom.DOMCryptoContext;
/* */ import javax.xml.crypto.dsig.keyinfo.KeyInfo;
/* */ import org.w3c.dom.Document;
/* */ import org.w3c.dom.Element;
/* */ import org.w3c.dom.Node;
/* */ import org.w3c.dom.NodeList;
/* */
/* */ public final class DOMKeyInfo extends DOMStructure
/* */ implements KeyInfo
/* */ {
/* */ private final String id;
/* */ private final List keyInfoTypes;
/* */
/* */ public DOMKeyInfo(List content, String id)
/* */ {
/* 44 */ if (content == null) {
/* 45 */ throw new NullPointerException("content cannot be null");
/* */ }
/* 47 */ List typesCopy = new ArrayList(content);
/* 48 */ if (typesCopy.isEmpty()) {
/* 49 */ throw new IllegalArgumentException("content cannot be empty");
/* */ }
/* 51 */ int i = 0; for (int size = typesCopy.size(); i < size; i++) {
/* 52 */ if (!(typesCopy.get(i) instanceof XMLStructure)) {
/* 53 */ throw new ClassCastException("content[" + i + "] is not a valid KeyInfo type");
/* */ }
/* */ }
/* */
/* 57 */ this.keyInfoTypes = Collections.unmodifiableList(typesCopy);
/* 58 */ this.id = id;
/* */ }
/* */
/* */ public DOMKeyInfo(Element kiElem, XMLCryptoContext context)
/* */ throws MarshalException
/* */ {
/* 69 */ this.id = DOMUtils.getAttributeValue(kiElem, "Id");
/* */
/* 72 */ NodeList nl = kiElem.getChildNodes();
/* 73 */ int length = nl.getLength();
/* 74 */ if (length < 1) {
/* 75 */ throw new MarshalException("KeyInfo must contain at least one type");
/* */ }
/* */
/* 78 */ List content = new ArrayList(length);
/* 79 */ for (int i = 0; i < length; i++) {
/* 80 */ Node child = nl.item(i);
/* */
/* 82 */ if (child.getNodeType() == 1)
/* */ {
/* 85 */ Element childElem = (Element)child;
/* 86 */ String localName = childElem.getLocalName();
/* 87 */ if (localName.equals("X509Data"))
/* 88 */ content.add(new DOMX509Data(childElem));
/* 89 */ else if (localName.equals("KeyName"))
/* 90 */ content.add(new DOMKeyName(childElem));
/* 91 */ else if (localName.equals("KeyValue"))
/* 92 */ content.add(new DOMKeyValue(childElem));
/* 93 */ else if (localName.equals("RetrievalMethod"))
/* 94 */ content.add(new DOMRetrievalMethod(childElem, context));
/* */ else
/* 96 */ content.add(new javax.xml.crypto.dom.DOMStructure(childElem));
/* */ }
/* */ }
/* 99 */ this.keyInfoTypes = Collections.unmodifiableList(content);
/* */ }
/* */
/* */ public String getId() {
/* 103 */ return this.id;
/* */ }
/* */
/* */ public List getContent() {
/* 107 */ return this.keyInfoTypes;
/* */ }
/* */
/* */ public void marshal(XMLStructure parent, XMLCryptoContext context) throws MarshalException
/* */ {
/* 112 */ if (parent == null) {
/* 113 */ throw new NullPointerException("parent is null");
/* */ }
/* */
/* 116 */ DOMStructure dp = (DOMStructure)parent;
/* 117 */ marshal(((javax.xml.crypto.dom.DOMStructure)parent).getNode(), DOMUtils.getSignaturePrefix(context), (DOMCryptoContext)context);
/* */ }
/* */
/* */ public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
/* */ throws MarshalException
/* */ {
/* 123 */ marshal(parent, null, dsPrefix, context);
/* */ }
/* */
/* */ public void marshal(Node parent, Node nextSibling, String dsPrefix, DOMCryptoContext context) throws MarshalException
/* */ {
/* 128 */ Document ownerDoc = DOMUtils.getOwnerDocument(parent);
/* */
/* 130 */ Element kiElem = DOMUtils.createElement(ownerDoc, "KeyInfo", "http://www.w3.org/2000/09/xmldsig#", dsPrefix);
/* */
/* 134 */ int i = 0; for (int size = this.keyInfoTypes.size(); i < size; i++) {
/* 135 */ XMLStructure kiType = (XMLStructure)this.keyInfoTypes.get(i);
/* 136 */ if ((kiType instanceof DOMStructure))
/* 137 */ ((DOMStructure)kiType).marshal(kiElem, dsPrefix, context);
/* */ else {
/* 139 */ DOMUtils.appendChild(kiElem, ((javax.xml.crypto.dom.DOMStructure)kiType).getNode());
/* */ }
/* */
/* */ }
/* */
/* 145 */ DOMUtils.setAttributeID(kiElem, "Id", this.id);
/* */
/* 147 */ parent.insertBefore(kiElem, nextSibling);
/* */ }
/* */
/* */ public boolean equals(Object o) {
/* 151 */ if (this == o) {
/* 152 */ return true;
/* */ }
/* */
/* 155 */ if (!(o instanceof KeyInfo)) {
/* 156 */ return false;
/* */ }
/* 158 */ KeyInfo oki = (KeyInfo)o;
/* */
/* 160 */ boolean idsEqual = this.id == null ? false : oki.getId() == null ? true : this.id.equals(oki.getId());
/* */
/* 163 */ return (this.keyInfoTypes.equals(oki.getContent())) && (idsEqual);
/* */ }
/* */ }
/* Location: E:\HYN\Java\trunk\ref\lib-dep\xmldsig\xmldsig.jar
* Qualified Name: org.jcp.xml.dsig.internal.dom.DOMKeyInfo
* JD-Core Version: 0.6.2
*/