package org.jcp.xml.dsig.internal.dom;
import javax.xml.crypto.*;
import javax.xml.crypto.dom.DOMCryptoContext;
import javax.xml.crypto.dsig.*;
import java.security.Provider;
import java.util.*;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
public final class DOMManifest extends DOMStructure implements Manifest {
private final List<Reference> references;
private final String id;
public DOMManifest(List<? extends Reference> references, String id) {
if (references == null) {
throw new NullPointerException("references cannot be null");
}
this.references =
Collections.unmodifiableList(new ArrayList<Reference>(references));
if (this.references.isEmpty()) {
throw new IllegalArgumentException("list of references must " +
"contain at least one entry");
}
for (int i = 0, size = this.references.size(); i < size; i++) {
if (!(this.references.get(i) instanceof Reference)) {
throw new ClassCastException
("references["+i+"] is not a valid type");
}
}
this.id = id;
}
public DOMManifest(Element manElem, XMLCryptoContext context,
Provider provider)
throws MarshalException
{
Attr attr = manElem.getAttributeNodeNS(null, "Id");
if (attr != null) {
this.id = attr.getValue();
manElem.setIdAttributeNode(attr, true);
} else {
this.id = null;
}
boolean secVal = Utils.secureValidation(context);
Element refElem = DOMUtils.getFirstChildElement(manElem, "Reference");
List<Reference> refs = new ArrayList<Reference>();
refs.add(new DOMReference(refElem, context, provider));
refElem = DOMUtils.getNextSiblingElement(refElem);
while (refElem != null) {
String localName = refElem.getLocalName();
if (!localName.equals("Reference")) {
throw new MarshalException("Invalid element name: " +
localName + ", expected Reference");
}
refs.add(new DOMReference(refElem, context, provider));
if (secVal && (refs.size() > DOMSignedInfo.MAXIMUM_REFERENCE_COUNT)) {
String error = "A maxiumum of " + DOMSignedInfo.MAXIMUM_REFERENCE_COUNT + " "
+ "references per Manifest are allowed with secure validation";
throw new MarshalException(error);
}
refElem = DOMUtils.getNextSiblingElement(refElem);
}
this.references = Collections.unmodifiableList(refs);
}
public String getId() {
return id;
}
public List getReferences() {
return references;
}
public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
throws MarshalException
{
Document ownerDoc = DOMUtils.getOwnerDocument(parent);
Element manElem = DOMUtils.createElement(ownerDoc, "Manifest",
XMLSignature.XMLNS, dsPrefix);
DOMUtils.setAttributeID(manElem, "Id", id);
for (Reference ref : references) {
((DOMReference)ref).marshal(manElem, dsPrefix, context);
}
parent.appendChild(manElem);
}
@Override
public boolean equals(Object o) {
if (this == o) {