public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
      throws MarshalException {
    Document ownerDoc = DOMUtils.getOwnerDocument(parent);

    Element pdElem = DOMUtils.createElement(ownerDoc, "PGPData", XMLSignature.XMLNS, dsPrefix);

    // create and append PGPKeyID element
    if (keyId != null) {
      Element keyIdElem =
          DOMUtils.createElement(ownerDoc, "PGPKeyID", XMLSignature.XMLNS, dsPrefix);
      keyIdElem.appendChild(ownerDoc.createTextNode(Base64.encode(keyId)));
      pdElem.appendChild(keyIdElem);
    }

    // create and append PGPKeyPacket element
    if (keyPacket != null) {
      Element keyPktElem =
          DOMUtils.createElement(ownerDoc, "PGPKeyPacket", XMLSignature.XMLNS, dsPrefix);
      keyPktElem.appendChild(ownerDoc.createTextNode(Base64.encode(keyPacket)));
      pdElem.appendChild(keyPktElem);
    }

    // create and append any elements
    for (int i = 0, size = externalElements.size(); i < size; i++) {
      DOMUtils.appendChild(
          pdElem, ((javax.xml.crypto.dom.DOMStructure) externalElements.get(i)).getNode());
    }

    parent.appendChild(pdElem);
  }
  public void digest(XMLSignContext signContext) throws XMLSignatureException {
    Data data = null;
    if (appliedTransformData == null) {
      data = dereference(signContext);
    } else {
      data = appliedTransformData;
    }
    digestValue = transform(data, signContext);

    // insert digestValue into DigestValue element
    String encodedDV = Base64.encode(digestValue);
    if (log.isLoggable(Level.FINE)) {
      log.log(Level.FINE, "Reference object uri = " + uri);
    }
    Element digestElem = DOMUtils.getLastChildElement(refElem);
    if (digestElem == null) {
      throw new XMLSignatureException("DigestValue element expected");
    }
    DOMUtils.removeAllChildren(digestElem);
    digestElem.appendChild(refElem.getOwnerDocument().createTextNode(encodedDV));

    digested = true;
    if (log.isLoggable(Level.FINE)) {
      log.log(Level.FINE, "Reference digesting completed");
    }
  }
  private boolean equalsContent(List otherContent) {
    if (content.size() != otherContent.size()) {
      return false;
    }
    for (int i = 0, osize = otherContent.size(); i < osize; i++) {
      XMLStructure oxs = (XMLStructure) otherContent.get(i);
      XMLStructure xs = (XMLStructure) content.get(i);
      if (oxs instanceof javax.xml.crypto.dom.DOMStructure) {
        if (!(xs instanceof javax.xml.crypto.dom.DOMStructure)) {
          return false;
        }
        Node onode = ((javax.xml.crypto.dom.DOMStructure) oxs).getNode();
        Node node = ((javax.xml.crypto.dom.DOMStructure) xs).getNode();
        if (!DOMUtils.nodesEqual(node, onode)) {
          return false;
        }
      } else {
        if (!(xs.equals(oxs))) {
          return false;
        }
      }
    }

    return true;
  }
 public void init(XMLStructure parent, XMLCryptoContext context)
     throws InvalidAlgorithmParameterException {
   super.init(parent, context);
   try {
     unmarshalParams(DOMUtils.getFirstChildElement(transformElem));
   } catch (MarshalException me) {
     throw new InvalidAlgorithmParameterException(me);
   }
 }
  /**
   * Creates an <code>XMLObject</code> from an element.
   *
   * @param objElem an Object element
   * @throws MarshalException if there is an error when unmarshalling
   */
  public DOMXMLObject(Element objElem, XMLCryptoContext context, Provider provider)
      throws MarshalException {
    // unmarshal attributes
    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 content = new ArrayList(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));
          continue;
        } else if (tag.equals("X509Data")) {
          content.add(new DOMX509Data(childElem));
          continue;
        }
        // @@@FIXME: check for other dsig structures
      }
      content.add(new javax.xml.crypto.dom.DOMStructure(child));
    }
    if (content.isEmpty()) {
      this.content = Collections.EMPTY_LIST;
    } else {
      this.content = Collections.unmodifiableList(content);
    }
  }
  /**
   * Creates a <code>DOMSignatureProperty</code> from an element.
   *
   * @param propElem a SignatureProperty element
   */
  public DOMSignatureProperty(Element propElem) throws MarshalException {
    // unmarshal attributes
    target = DOMUtils.getAttributeValue(propElem, "Target");
    if (target == null) {
      throw new MarshalException("target cannot be null");
    }
    id = DOMUtils.getAttributeValue(propElem, "Id");

    NodeList nodes = propElem.getChildNodes();
    int length = nodes.getLength();
    List content = new ArrayList(length);
    for (int i = 0; i < length; i++) {
      content.add(new javax.xml.crypto.dom.DOMStructure(nodes.item(i)));
    }
    if (content.isEmpty()) {
      throw new MarshalException("content cannot be empty");
    } else {
      this.content = Collections.unmodifiableList(content);
    }
  }
  public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
      throws MarshalException {
    Document ownerDoc = DOMUtils.getOwnerDocument(parent);

    Element propElem =
        DOMUtils.createElement(ownerDoc, "SignatureProperty", XMLSignature.XMLNS, dsPrefix);

    // set attributes
    DOMUtils.setAttributeID(propElem, "Id", id);
    DOMUtils.setAttribute(propElem, "Target", target);

    // create and append any elements and mixed content
    for (int i = 0, size = content.size(); i < size; i++) {
      javax.xml.crypto.dom.DOMStructure property =
          (javax.xml.crypto.dom.DOMStructure) content.get(i);
      DOMUtils.appendChild(propElem, property.getNode());
    }

    parent.appendChild(propElem);
  }
  private void unmarshalParams(Element curXPathElem) throws MarshalException {
    List<XPathType> list = new ArrayList<XPathType>();
    while (curXPathElem != null) {
      String xPath = curXPathElem.getFirstChild().getNodeValue();
      String filterVal = DOMUtils.getAttributeValue(curXPathElem, "Filter");
      if (filterVal == null) {
        throw new MarshalException("filter cannot be null");
      }
      XPathType.Filter filter = null;
      if (filterVal.equals("intersect")) {
        filter = XPathType.Filter.INTERSECT;
      } else if (filterVal.equals("subtract")) {
        filter = XPathType.Filter.SUBTRACT;
      } else if (filterVal.equals("union")) {
        filter = XPathType.Filter.UNION;
      } else {
        throw new MarshalException("Unknown XPathType filter type" + filterVal);
      }
      NamedNodeMap attributes = curXPathElem.getAttributes();
      if (attributes != null) {
        int length = attributes.getLength();
        Map<String, String> namespaceMap = new HashMap<String, String>(length);
        for (int i = 0; i < length; i++) {
          Attr attr = (Attr) attributes.item(i);
          String prefix = attr.getPrefix();
          if (prefix != null && prefix.equals("xmlns")) {
            namespaceMap.put(attr.getLocalName(), attr.getValue());
          }
        }
        list.add(new XPathType(xPath, filter, namespaceMap));
      } else {
        list.add(new XPathType(xPath, filter));
      }

      curXPathElem = DOMUtils.getNextSiblingElement(curXPathElem);
    }
    this.params = new XPathFilter2ParameterSpec(list);
  }
  public void marshalParams(XMLStructure parent, XMLCryptoContext context) throws MarshalException {
    super.marshalParams(parent, context);
    XPathFilter2ParameterSpec xp = (XPathFilter2ParameterSpec) getParameterSpec();
    String prefix = DOMUtils.getNSPrefix(context, Transform.XPATH2);
    String qname = (prefix == null || prefix.length() == 0) ? "xmlns" : "xmlns:" + prefix;
    @SuppressWarnings("unchecked")
    List<XPathType> xpathList = xp.getXPathList();
    for (XPathType xpathType : xpathList) {
      Element elem = DOMUtils.createElement(ownerDoc, "XPath", Transform.XPATH2, prefix);
      elem.appendChild(ownerDoc.createTextNode(xpathType.getExpression()));
      DOMUtils.setAttribute(elem, "Filter", xpathType.getFilter().toString());
      elem.setAttributeNS("http://www.w3.org/2000/xmlns/", qname, Transform.XPATH2);

      // add namespace attributes, if necessary
      @SuppressWarnings("unchecked")
      Set<Map.Entry<String, String>> entries = xpathType.getNamespaceMap().entrySet();
      for (Map.Entry<String, String> entry : entries) {
        elem.setAttributeNS(
            "http://www.w3.org/2000/xmlns/", "xmlns:" + entry.getKey(), entry.getValue());
      }

      transformElem.appendChild(elem);
    }
  }
  /**
   * Creates a <code>DOMReference</code> from an element.
   *
   * @param refElem a Reference element
   */
  public DOMReference(Element refElem, XMLCryptoContext context) throws MarshalException {
    // unmarshal Transforms, if specified
    Element nextSibling = DOMUtils.getFirstChildElement(refElem);
    List transforms = new ArrayList(5);
    if (nextSibling.getLocalName().equals("Transforms")) {
      Element transformElem = DOMUtils.getFirstChildElement(nextSibling);
      while (transformElem != null) {
        transforms.add(new DOMTransform(transformElem, context));
        transformElem = DOMUtils.getNextSiblingElement(transformElem);
      }
      nextSibling = DOMUtils.getNextSiblingElement(nextSibling);
    }

    // unmarshal DigestMethod
    Element dmElem = nextSibling;
    this.digestMethod = DOMDigestMethod.unmarshal(dmElem);

    // unmarshal DigestValue
    try {
      Element dvElem = DOMUtils.getNextSiblingElement(dmElem);
      this.digestValue = Base64.decode(dvElem);
    } catch (Base64DecodingException bde) {
      throw new MarshalException(bde);
    }

    // unmarshal attributes
    this.uri = DOMUtils.getAttributeValue(refElem, "URI");
    this.id = DOMUtils.getAttributeValue(refElem, "Id");

    this.type = DOMUtils.getAttributeValue(refElem, "Type");
    this.here = refElem.getAttributeNodeNS(null, "URI");
    this.refElem = refElem;

    if (transforms.isEmpty()) {
      this.transforms = Collections.EMPTY_LIST;
    } else {
      this.transforms = Collections.unmodifiableList(transforms);
    }
    this.appliedTransforms = Collections.EMPTY_LIST;
    this.allTransforms = transforms;
    this.appliedTransformData = null;
  }
  public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
      throws MarshalException {
    if (log.isLoggable(Level.FINE)) {
      log.log(Level.FINE, "Marshalling Reference");
    }
    Document ownerDoc = DOMUtils.getOwnerDocument(parent);

    refElem = DOMUtils.createElement(ownerDoc, "Reference", XMLSignature.XMLNS, dsPrefix);

    // set attributes
    DOMUtils.setAttributeID(refElem, "Id", id);
    DOMUtils.setAttribute(refElem, "URI", uri);
    DOMUtils.setAttribute(refElem, "Type", type);

    // create and append Transforms element
    if (!transforms.isEmpty() || !appliedTransforms.isEmpty()) {
      Element transformsElem =
          DOMUtils.createElement(ownerDoc, "Transforms", XMLSignature.XMLNS, dsPrefix);
      refElem.appendChild(transformsElem);
      for (int i = 0, size = appliedTransforms.size(); i < size; i++) {
        DOMStructure transform = (DOMStructure) appliedTransforms.get(i);
        transform.marshal(transformsElem, dsPrefix, context);
      }
      for (int i = 0, size = transforms.size(); i < size; i++) {
        DOMStructure transform = (DOMStructure) transforms.get(i);
        transform.marshal(transformsElem, dsPrefix, context);
      }
    }

    // create and append DigestMethod element
    ((DOMDigestMethod) digestMethod).marshal(refElem, dsPrefix, context);

    // create and append DigestValue element
    if (log.isLoggable(Level.FINE)) {
      log.log(Level.FINE, "Adding digestValueElem");
    }
    Element digestValueElem =
        DOMUtils.createElement(ownerDoc, "DigestValue", XMLSignature.XMLNS, dsPrefix);
    if (digestValue != null) {
      digestValueElem.appendChild(ownerDoc.createTextNode(Base64.encode(digestValue)));
    }
    refElem.appendChild(digestValueElem);

    parent.appendChild(refElem);
    here = refElem.getAttributeNodeNS(null, "URI");
  }
  public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
      throws MarshalException {
    Document ownerDoc = DOMUtils.getOwnerDocument(parent);

    Element objElem = DOMUtils.createElement(ownerDoc, "Object", XMLSignature.XMLNS, dsPrefix);

    // set attributes
    DOMUtils.setAttributeID(objElem, "Id", id);
    DOMUtils.setAttribute(objElem, "MimeType", mimeType);
    DOMUtils.setAttribute(objElem, "Encoding", encoding);

    // create and append any elements and mixed content, if necessary
    for (int i = 0, size = content.size(); i < size; i++) {
      XMLStructure object = (XMLStructure) content.get(i);
      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);
  }