0N/A/*
661N/A * reserved comment block
661N/A * DO NOT REMOVE OR ALTER!
0N/A */
0N/A/*
661N/A * Copyright 2005 The Apache Software Foundation.
661N/A *
661N/A * Licensed under the Apache License, Version 2.0 (the "License");
661N/A * you may not use this file except in compliance with the License.
661N/A * You may obtain a copy of the License at
661N/A *
661N/A * http://www.apache.org/licenses/LICENSE-2.0
661N/A *
661N/A * Unless required by applicable law or agreed to in writing, software
661N/A * distributed under the License is distributed on an "AS IS" BASIS,
661N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
661N/A * See the License for the specific language governing permissions and
661N/A * limitations under the License.
661N/A *
661N/A */
661N/A/*
2362N/A * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
661N/A */
661N/A/*
661N/A * $Id: DOMKeyInfo.java,v 1.2 2008/07/24 15:20:32 mullan Exp $
0N/A */
0N/Apackage org.jcp.xml.dsig.internal.dom;
0N/A
0N/Aimport javax.xml.crypto.*;
0N/Aimport javax.xml.crypto.dsig.*;
0N/Aimport javax.xml.crypto.dsig.dom.DOMSignContext;
0N/Aimport javax.xml.crypto.dsig.keyinfo.KeyInfo;
0N/Aimport javax.xml.crypto.dom.*;
0N/A
661N/Aimport java.security.Provider;
0N/Aimport java.util.*;
6159N/Aimport org.w3c.dom.Attr;
0N/Aimport org.w3c.dom.Document;
0N/Aimport org.w3c.dom.Element;
0N/Aimport org.w3c.dom.Node;
0N/Aimport org.w3c.dom.NodeList;
0N/A
0N/A/**
0N/A * DOM-based implementation of KeyInfo.
0N/A *
0N/A * @author Sean Mullan
0N/A */
0N/Apublic final class DOMKeyInfo extends DOMStructure implements KeyInfo {
0N/A
0N/A private final String id;
0N/A private final List keyInfoTypes;
0N/A
0N/A /**
0N/A * Creates a <code>DOMKeyInfo</code>.
0N/A *
0N/A * @param content a list of one or more {@link XMLStructure}s representing
0N/A * key information types. The list is defensively copied to protect
0N/A * against subsequent modification.
0N/A * @param id an ID attribute
0N/A * @throws NullPointerException if <code>content</code> is <code>null</code>
0N/A * @throws IllegalArgumentException if <code>content</code> is empty
0N/A * @throws ClassCastException if <code>content</code> contains any entries
0N/A * that are not of type {@link XMLStructure}
0N/A */
0N/A public DOMKeyInfo(List content, String id) {
0N/A if (content == null) {
0N/A throw new NullPointerException("content cannot be null");
0N/A }
0N/A List typesCopy = new ArrayList(content);
0N/A if (typesCopy.isEmpty()) {
0N/A throw new IllegalArgumentException("content cannot be empty");
0N/A }
0N/A for (int i = 0, size = typesCopy.size(); i < size; i++) {
0N/A if (!(typesCopy.get(i) instanceof XMLStructure)) {
0N/A throw new ClassCastException
0N/A ("content["+i+"] is not a valid KeyInfo type");
0N/A }
0N/A }
0N/A this.keyInfoTypes = Collections.unmodifiableList(typesCopy);
0N/A this.id = id;
0N/A }
0N/A
0N/A /**
0N/A * Creates a <code>DOMKeyInfo</code> from XML.
0N/A *
661N/A * @param kiElem KeyInfo element
0N/A */
661N/A public DOMKeyInfo(Element kiElem, XMLCryptoContext context,
661N/A Provider provider) throws MarshalException {
0N/A // get Id attribute, if specified
6159N/A Attr attr = kiElem.getAttributeNodeNS(null, "Id");
6159N/A if (attr != null) {
6159N/A id = attr.getValue();
6159N/A kiElem.setIdAttributeNode(attr, true);
6159N/A } else {
6159N/A id = null;
6159N/A }
0N/A
0N/A // get all children nodes
0N/A NodeList nl = kiElem.getChildNodes();
0N/A int length = nl.getLength();
0N/A if (length < 1) {
0N/A throw new MarshalException
0N/A ("KeyInfo must contain at least one type");
0N/A }
0N/A List content = new ArrayList(length);
0N/A for (int i = 0; i < length; i++) {
0N/A Node child = nl.item(i);
0N/A // ignore all non-Element nodes
0N/A if (child.getNodeType() != Node.ELEMENT_NODE) {
0N/A continue;
0N/A }
0N/A Element childElem = (Element) child;
0N/A String localName = childElem.getLocalName();
0N/A if (localName.equals("X509Data")) {
0N/A content.add(new DOMX509Data(childElem));
0N/A } else if (localName.equals("KeyName")) {
0N/A content.add(new DOMKeyName(childElem));
0N/A } else if (localName.equals("KeyValue")) {
0N/A content.add(new DOMKeyValue(childElem));
0N/A } else if (localName.equals("RetrievalMethod")) {
661N/A content.add
661N/A (new DOMRetrievalMethod(childElem, context, provider));
661N/A } else if (localName.equals("PGPData")) {
661N/A content.add(new DOMPGPData(childElem));
0N/A } else { //may be MgmtData, SPKIData or element from other namespace
0N/A content.add(new javax.xml.crypto.dom.DOMStructure((childElem)));
0N/A }
0N/A }
0N/A keyInfoTypes = Collections.unmodifiableList(content);
0N/A }
0N/A
0N/A public String getId() {
0N/A return id;
0N/A }
0N/A
0N/A public List getContent() {
0N/A return keyInfoTypes;
0N/A }
0N/A
0N/A public void marshal(XMLStructure parent, XMLCryptoContext context)
0N/A throws MarshalException {
0N/A if (parent == null) {
0N/A throw new NullPointerException("parent is null");
0N/A }
0N/A
0N/A Node pNode = ((javax.xml.crypto.dom.DOMStructure) parent).getNode();
0N/A String dsPrefix = DOMUtils.getSignaturePrefix(context);
0N/A Element kiElem = DOMUtils.createElement
0N/A (DOMUtils.getOwnerDocument(pNode), "KeyInfo",
0N/A XMLSignature.XMLNS, dsPrefix);
661N/A if (dsPrefix == null || dsPrefix.length() == 0) {
0N/A kiElem.setAttributeNS
0N/A ("http://www.w3.org/2000/xmlns/", "xmlns", XMLSignature.XMLNS);
0N/A } else {
0N/A kiElem.setAttributeNS
0N/A ("http://www.w3.org/2000/xmlns/", "xmlns:" + dsPrefix,
0N/A XMLSignature.XMLNS);
0N/A }
0N/A marshal(pNode, kiElem, null, dsPrefix, (DOMCryptoContext) context);
0N/A }
0N/A
0N/A public void marshal(Node parent, String dsPrefix,
0N/A DOMCryptoContext context) throws MarshalException {
0N/A marshal(parent, null, dsPrefix, context);
0N/A }
0N/A
0N/A public void marshal(Node parent, Node nextSibling, String dsPrefix,
0N/A DOMCryptoContext context) throws MarshalException {
0N/A Document ownerDoc = DOMUtils.getOwnerDocument(parent);
0N/A
0N/A Element kiElem = DOMUtils.createElement
0N/A (ownerDoc, "KeyInfo", XMLSignature.XMLNS, dsPrefix);
0N/A marshal(parent, kiElem, nextSibling, dsPrefix, context);
0N/A }
0N/A
0N/A private void marshal(Node parent, Element kiElem, Node nextSibling,
0N/A String dsPrefix, DOMCryptoContext context) throws MarshalException {
0N/A // create and append KeyInfoType elements
0N/A for (int i = 0, size = keyInfoTypes.size(); i < size; i++) {
0N/A XMLStructure kiType = (XMLStructure) keyInfoTypes.get(i);
0N/A if (kiType instanceof DOMStructure) {
0N/A ((DOMStructure) kiType).marshal(kiElem, dsPrefix, context);
0N/A } else {
0N/A DOMUtils.appendChild(kiElem,
0N/A ((javax.xml.crypto.dom.DOMStructure) kiType).getNode());
0N/A }
0N/A }
0N/A
0N/A // append id attribute
0N/A DOMUtils.setAttributeID(kiElem, "Id", id);
0N/A
0N/A parent.insertBefore(kiElem, nextSibling);
0N/A }
0N/A
0N/A public boolean equals(Object o) {
0N/A if (this == o) {
0N/A return true;
0N/A }
0N/A
0N/A if (!(o instanceof KeyInfo)) {
0N/A return false;
0N/A }
0N/A KeyInfo oki = (KeyInfo) o;
0N/A
0N/A boolean idsEqual = (id == null ? oki.getId() == null :
0N/A id.equals(oki.getId()));
0N/A
0N/A return (keyInfoTypes.equals(oki.getContent()) && idsEqual);
0N/A }
0N/A}