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: DOMManifest.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.dom.DOMCryptoContext;
0N/Aimport javax.xml.crypto.dsig.*;
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/A
0N/A/**
0N/A * DOM-based implementation of Manifest.
0N/A *
0N/A * @author Sean Mullan
0N/A */
0N/Apublic final class DOMManifest extends DOMStructure implements Manifest {
0N/A
0N/A private final List references;
0N/A private final String id;
0N/A
0N/A /**
0N/A * Creates a <code>DOMManifest</code> containing the specified
0N/A * list of {@link Reference}s and optional id.
0N/A *
0N/A * @param references a list of one or more <code>Reference</code>s. The list
0N/A * is defensively copied to protect against subsequent modification.
0N/A * @param id the id (may be <code>null</code>
0N/A * @throws NullPointerException if <code>references</code> is
0N/A * <code>null</code>
0N/A * @throws IllegalArgumentException if <code>references</code> is empty
0N/A * @throws ClassCastException if <code>references</code> contains any
0N/A * entries that are not of type {@link Reference}
0N/A */
0N/A public DOMManifest(List references, String id) {
0N/A if (references == null) {
0N/A throw new NullPointerException("references cannot be null");
0N/A }
0N/A List refCopy = new ArrayList(references);
0N/A if (refCopy.isEmpty()) {
0N/A throw new IllegalArgumentException("list of references must " +
0N/A "contain at least one entry");
0N/A }
0N/A for (int i = 0, size = refCopy.size(); i < size; i++) {
0N/A if (!(refCopy.get(i) instanceof Reference)) {
0N/A throw new ClassCastException
0N/A ("references["+i+"] is not a valid type");
0N/A }
0N/A }
0N/A this.references = Collections.unmodifiableList(refCopy);
0N/A this.id = id;
0N/A }
0N/A
0N/A /**
0N/A * Creates a <code>DOMManifest</code> from an element.
0N/A *
0N/A * @param manElem a Manifest element
0N/A */
661N/A public DOMManifest(Element manElem, XMLCryptoContext context,
661N/A Provider provider) throws MarshalException {
6159N/A Attr attr = manElem.getAttributeNodeNS(null, "Id");
6159N/A if (attr != null) {
6159N/A this.id = attr.getValue();
6159N/A manElem.setIdAttributeNode(attr, true);
6159N/A } else {
6159N/A this.id = null;
6159N/A }
6159N/A
6159N/A boolean secVal = Utils.secureValidation(context);
0N/A Element refElem = DOMUtils.getFirstChildElement(manElem);
0N/A List refs = new ArrayList();
6159N/A int refCount = 0;
0N/A while (refElem != null) {
661N/A refs.add(new DOMReference(refElem, context, provider));
0N/A refElem = DOMUtils.getNextSiblingElement(refElem);
6159N/A
6159N/A refCount++;
6159N/A if (secVal && (refCount > DOMSignedInfo.MAXIMUM_REFERENCE_COUNT)) {
6159N/A String error = "A maxiumum of " +
6159N/A DOMSignedInfo.MAXIMUM_REFERENCE_COUNT +
6159N/A " references per Manifest are allowed with" +
6159N/A " secure validation";
6159N/A throw new MarshalException(error);
6159N/A }
0N/A }
0N/A this.references = Collections.unmodifiableList(refs);
0N/A }
0N/A
0N/A public String getId() {
0N/A return id;
0N/A }
0N/A
0N/A public List getReferences() {
0N/A return references;
0N/A }
0N/A
0N/A public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
0N/A throws MarshalException {
0N/A Document ownerDoc = DOMUtils.getOwnerDocument(parent);
0N/A
0N/A Element manElem = DOMUtils.createElement
0N/A (ownerDoc, "Manifest", XMLSignature.XMLNS, dsPrefix);
0N/A
0N/A DOMUtils.setAttributeID(manElem, "Id", id);
0N/A
0N/A // add references
0N/A for (int i = 0, size = references.size(); i < size; i++) {
0N/A DOMReference ref = (DOMReference) references.get(i);
0N/A ref.marshal(manElem, dsPrefix, context);
0N/A }
0N/A parent.appendChild(manElem);
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 Manifest)) {
0N/A return false;
0N/A }
0N/A Manifest oman = (Manifest) o;
0N/A
0N/A boolean idsEqual = (id == null ? oman.getId() == null :
0N/A id.equals(oman.getId()));
0N/A
0N/A return (idsEqual && references.equals(oman.getReferences()));
0N/A }
0N/A}