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;
import org.w3c.dom.NodeList;
public final class DOMXMLObject extends DOMStructure implements XMLObject {
private final String id;
private final String mimeType;
private final String encoding;
private final List<XMLStructure> content;
private Element objectElem;
public DOMXMLObject(List<? extends XMLStructure> content, String id,
String mimeType, String encoding)
{
if (content == null || content.isEmpty()) {
this.content = Collections.emptyList();
} else {
this.content = Collections.unmodifiableList(
new ArrayList<XMLStructure>(content));
for (int i = 0, size = this.content.size(); i < size; i++) {
if (!(this.content.get(i) instanceof XMLStructure)) {
throw new ClassCastException
("content["+i+"] is not a valid type");
}
}
}
this.id = id;
this.mimeType = mimeType;
this.encoding = encoding;
}
public DOMXMLObject(Element objElem, XMLCryptoContext context,
Provider provider)
throws MarshalException
{
this.encoding = DOMUtils.getAttributeValue(objElem, "Encoding");
Attr attr = objElem.getAttributeNodeNS(null, "Id");
if (attr != null) {
this.id = attr.getValue();
objElem.setIdAttributeNode(attr, true);
} else {
this.id = null;
}
this.mimeType = DOMUtils.getAttributeValue(objElem, "MimeType");
NodeList nodes = objElem.getChildNodes();
int length = nodes.getLength();
List<XMLStructure> content = new ArrayList<XMLStructure>(length);
for (int i = 0; i < length; i++) {
Node child = nodes.item(i);
if (child.getNodeType() == Node.ELEMENT_NODE) {
Element childElem = (Element)child;
String tag = childElem.getLocalName();
if (tag.equals("Manifest")) {
content.add(new DOMManifest(childElem, context, provider));
continue;
} else if (tag.equals("SignatureProperties")) {
content.add(new DOMSignatureProperties(childElem, context));
continue;
} else if (tag.equals("X509Data")) {
content.add(new DOMX509Data(childElem));
continue;
}
}
content.add(new javax.xml.crypto.dom.DOMStructure(child));
}
if (content.isEmpty()) {
this.content = Collections.emptyList();
} else {
this.content = Collections.unmodifiableList(content);
}
this.objectElem = objElem;
}
public List getContent() {
return content;
}
public String getId() {
return id;
}
public String getMimeType() {
return mimeType;
}
public String getEncoding() {
return encoding;
}
public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
throws MarshalException {
Document ownerDoc = DOMUtils.getOwnerDocument(parent);
Element objElem = objectElem != null ? objectElem : null;
if (objElem == null) {
objElem = DOMUtils.createElement(ownerDoc, "Object",
XMLSignature.XMLNS, dsPrefix);
DOMUtils.setAttributeID(objElem, "Id", id);
DOMUtils.setAttribute(objElem, "MimeType", mimeType);
DOMUtils.setAttribute(objElem, "Encoding", encoding);
for (XMLStructure object : content) {
if (object instanceof DOMStructure) {
((DOMStructure)object).marshal(objElem, dsPrefix, context);
} else {
javax.xml.crypto.dom.DOMStructure domObject =
(javax.xml.crypto.dom.DOMStructure)object;
DOMUtils.appendChild(objElem, domObject.getNode());
}
}
}
parent.appendChild(objElem);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof XMLObject)) {
return false;
}
XMLObject oxo = (XMLObject)o;
boolean idsEqual = (id == null ? oxo.getId() == null
: id.equals(oxo.getId()));
boolean encodingsEqual =
(encoding == null ? oxo.getEncoding() == null
: encoding.equals(oxo.getEncoding()));
boolean mimeTypesEqual =
(mimeType == null ? oxo.getMimeType() == null
: mimeType.equals(oxo.getMimeType()));
@SuppressWarnings("unchecked")
List<XMLStructure> oxoContent = oxo.getContent();
return (idsEqual && encodingsEqual && mimeTypesEqual &&
equalsContent(oxoContent));
}
@Override
public int hashCode() {
int result = 17;
if (id != null) {
result = 31 * result + id.hashCode();
}
if (encoding != null) {
result = 31 * result + encoding.hashCode();
}
if (mimeType != null) {
result = 31 * result + mimeType.hashCode();
}
result = 31 * result + content.hashCode();
return result;
}
private boolean equalsContent(List<XMLStructure> otherContent) {