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: DOMXMLObject.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/Aimport org.w3c.dom.NodeList;
0N/A
0N/A/**
0N/A * DOM-based implementation of XMLObject.
0N/A *
0N/A * @author Sean Mullan
0N/A */
0N/Apublic final class DOMXMLObject extends DOMStructure implements XMLObject {
0N/A
0N/A private final String id;
0N/A private final String mimeType;
0N/A private final String encoding;
0N/A private final List content;
0N/A
0N/A /**
0N/A * Creates an <code>XMLObject</code> from the specified parameters.
0N/A *
0N/A * @param content a list of {@link XMLStructure}s. The list
0N/A * is defensively copied to protect against subsequent modification.
0N/A * May be <code>null</code> or empty.
0N/A * @param id the Id (may be <code>null</code>)
0N/A * @param mimeType the mime type (may be <code>null</code>)
0N/A * @param encoding the encoding (may be <code>null</code>)
0N/A * @return an <code>XMLObject</code>
0N/A * @throws ClassCastException if <code>content</code> contains any
0N/A * entries that are not of type {@link XMLStructure}
0N/A */
0N/A public DOMXMLObject(List content, String id, String mimeType,
0N/A String encoding) {
0N/A if (content == null || content.isEmpty()) {
0N/A this.content = Collections.EMPTY_LIST;
0N/A } else {
0N/A List contentCopy = new ArrayList(content);
0N/A for (int i = 0, size = contentCopy.size(); i < size; i++) {
0N/A if (!(contentCopy.get(i) instanceof XMLStructure)) {
0N/A throw new ClassCastException
0N/A ("content["+i+"] is not a valid type");
0N/A }
0N/A }
0N/A this.content = Collections.unmodifiableList(contentCopy);
0N/A }
0N/A this.id = id;
0N/A this.mimeType = mimeType;
0N/A this.encoding = encoding;
0N/A }
0N/A
0N/A /**
0N/A * Creates an <code>XMLObject</code> from an element.
0N/A *
0N/A * @param objElem an Object element
0N/A * @throws MarshalException if there is an error when unmarshalling
0N/A */
661N/A public DOMXMLObject(Element objElem, XMLCryptoContext context,
661N/A Provider provider) throws MarshalException {
0N/A // unmarshal attributes
0N/A this.encoding = DOMUtils.getAttributeValue(objElem, "Encoding");
6159N/A
6159N/A Attr attr = objElem.getAttributeNodeNS(null, "Id");
6159N/A if (attr != null) {
6159N/A this.id = attr.getValue();
6159N/A objElem.setIdAttributeNode(attr, true);
6159N/A } else {
6159N/A this.id = null;
6159N/A }
0N/A this.mimeType = DOMUtils.getAttributeValue(objElem, "MimeType");
0N/A
0N/A NodeList nodes = objElem.getChildNodes();
0N/A int length = nodes.getLength();
0N/A List content = new ArrayList(length);
0N/A for (int i = 0; i < length; i++) {
0N/A Node child = nodes.item(i);
0N/A if (child.getNodeType() == Node.ELEMENT_NODE) {
0N/A Element childElem = (Element) child;
0N/A String tag = childElem.getLocalName();
0N/A if (tag.equals("Manifest")) {
661N/A content.add(new DOMManifest(childElem, context, provider));
0N/A continue;
0N/A } else if (tag.equals("SignatureProperties")) {
0N/A content.add(new DOMSignatureProperties(childElem));
0N/A continue;
0N/A } else if (tag.equals("X509Data")) {
0N/A content.add(new DOMX509Data(childElem));
0N/A continue;
0N/A }
0N/A //@@@FIXME: check for other dsig structures
0N/A }
0N/A content.add(new javax.xml.crypto.dom.DOMStructure(child));
0N/A }
0N/A if (content.isEmpty()) {
0N/A this.content = Collections.EMPTY_LIST;
0N/A } else {
0N/A this.content = Collections.unmodifiableList(content);
0N/A }
0N/A }
0N/A
0N/A public List getContent() {
0N/A return content;
0N/A }
0N/A
0N/A public String getId() {
0N/A return id;
0N/A }
0N/A
0N/A public String getMimeType() {
0N/A return mimeType;
0N/A }
0N/A
0N/A public String getEncoding() {
0N/A return encoding;
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 objElem = DOMUtils.createElement
0N/A (ownerDoc, "Object", XMLSignature.XMLNS, dsPrefix);
0N/A
0N/A // set attributes
0N/A DOMUtils.setAttributeID(objElem, "Id", id);
0N/A DOMUtils.setAttribute(objElem, "MimeType", mimeType);
0N/A DOMUtils.setAttribute(objElem, "Encoding", encoding);
0N/A
0N/A // create and append any elements and mixed content, if necessary
0N/A for (int i = 0, size = content.size(); i < size; i++) {
0N/A XMLStructure object = (XMLStructure) content.get(i);
0N/A if (object instanceof DOMStructure) {
0N/A ((DOMStructure) object).marshal(objElem, dsPrefix, context);
0N/A } else {
0N/A javax.xml.crypto.dom.DOMStructure domObject =
0N/A (javax.xml.crypto.dom.DOMStructure) object;
0N/A DOMUtils.appendChild(objElem, domObject.getNode());
0N/A }
0N/A }
0N/A
0N/A parent.appendChild(objElem);
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 XMLObject)) {
0N/A return false;
0N/A }
0N/A XMLObject oxo = (XMLObject) o;
0N/A
0N/A boolean idsEqual = (id == null ? oxo.getId() == null :
0N/A id.equals(oxo.getId()));
0N/A boolean encodingsEqual = (encoding == null ? oxo.getEncoding() == null :
0N/A encoding.equals(oxo.getEncoding()));
0N/A boolean mimeTypesEqual = (mimeType == null ? oxo.getMimeType() == null :
0N/A mimeType.equals(oxo.getMimeType()));
0N/A
0N/A return (idsEqual && encodingsEqual && mimeTypesEqual &&
0N/A equalsContent(oxo.getContent()));
0N/A }
0N/A
0N/A private boolean equalsContent(List otherContent) {
0N/A if (content.size() != otherContent.size()) {
0N/A return false;
0N/A }
0N/A for (int i = 0, osize = otherContent.size(); i < osize; i++) {
0N/A XMLStructure oxs = (XMLStructure) otherContent.get(i);
0N/A XMLStructure xs = (XMLStructure) content.get(i);
0N/A if (oxs instanceof javax.xml.crypto.dom.DOMStructure) {
0N/A if (!(xs instanceof javax.xml.crypto.dom.DOMStructure)) {
0N/A return false;
0N/A }
0N/A Node onode =
0N/A ((javax.xml.crypto.dom.DOMStructure) oxs).getNode();
0N/A Node node =
0N/A ((javax.xml.crypto.dom.DOMStructure) xs).getNode();
0N/A if (!DOMUtils.nodesEqual(node, onode)) {
0N/A return false;
0N/A }
0N/A } else {
0N/A if (!(xs.equals(oxs))) {
0N/A return false;
0N/A }
0N/A }
0N/A }
0N/A
0N/A return true;
0N/A }
0N/A}