1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /**
   6  * Licensed to the Apache Software Foundation (ASF) under one
   7  * or more contributor license agreements. See the NOTICE file
   8  * distributed with this work for additional information
   9  * regarding copyright ownership. The ASF licenses this file
  10  * to you under the Apache License, Version 2.0 (the
  11  * "License"); you may not use this file except in compliance
  12  * with the License. You may obtain a copy of the License at
  13  *
  14  * http://www.apache.org/licenses/LICENSE-2.0
  15  *
  16  * Unless required by applicable law or agreed to in writing,
  17  * software distributed under the License is distributed on an
  18  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  19  * KIND, either express or implied. See the License for the
  20  * specific language governing permissions and limitations
  21  * under the License.
  22  */
  23 package com.sun.org.apache.xml.internal.security.signature;
  24 
  25 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
  26 import com.sun.org.apache.xml.internal.security.utils.Constants;
  27 import com.sun.org.apache.xml.internal.security.utils.SignatureElementProxy;
  28 import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
  29 import org.w3c.dom.Attr;
  30 import org.w3c.dom.Document;
  31 import org.w3c.dom.Element;
  32 
  33 /**
  34  * Handles <code>&lt;ds:SignatureProperties&gt;</code> elements
  35  * This Element holds {@link SignatureProperty} that contian additional information items
  36  * concerning the generation of the signature.
  37  * for example, data-time stamp, serial number of cryptographic hardware.
  38  *
  39  * @author Christian Geuer-Pollmann
  40  */
  41 public class SignatureProperties extends SignatureElementProxy {
  42 
  43     /**
  44      * Constructor SignatureProperties
  45      *
  46      * @param doc
  47      */
  48     public SignatureProperties(Document doc) {
  49         super(doc);
  50 
  51         XMLUtils.addReturnToElement(this.constructionElement);
  52     }
  53 
  54     /**
  55      * Constructs {@link SignatureProperties} from {@link Element}
  56      * @param element <code>SignatureProperties</code> element
  57      * @param BaseURI the URI of the resource where the XML instance was stored
  58      * @throws XMLSecurityException
  59      */
  60     public SignatureProperties(Element element, String BaseURI) throws XMLSecurityException {
  61         super(element, BaseURI);
  62         
  63         Attr attr = element.getAttributeNodeNS(null, "Id");
  64         if (attr != null) {
  65             element.setIdAttributeNode(attr, true);
  66         }
  67         
  68         int length = getLength();
  69         for (int i = 0; i < length; i++) {
  70             Element propertyElem =
  71                 XMLUtils.selectDsNode(this.constructionElement, Constants._TAG_SIGNATUREPROPERTY, i);
  72             Attr propertyAttr = propertyElem.getAttributeNodeNS(null, "Id");
  73             if (propertyAttr != null) {
  74                 propertyElem.setIdAttributeNode(propertyAttr, true);
  75             }
  76         }
  77     }
  78 
  79     /**
  80      * Return the nonnegative number of added SignatureProperty elements.
  81      *
  82      * @return the number of SignatureProperty elements
  83      */
  84     public int getLength() {
  85         Element[] propertyElems =
  86             XMLUtils.selectDsNodes(this.constructionElement, Constants._TAG_SIGNATUREPROPERTY);
  87 
  88         return propertyElems.length;
  89     }
  90 
  91     /**
  92      * Return the <it>i</it><sup>th</sup> SignatureProperty. Valid <code>i</code>
  93      * values are 0 to <code>{link@ getSize}-1</code>.
  94      *
  95      * @param i Index of the requested {@link SignatureProperty}
  96      * @return the <it>i</it><sup>th</sup> SignatureProperty
  97      * @throws XMLSignatureException
  98      */
  99     public SignatureProperty item(int i) throws XMLSignatureException {
 100         try {
 101             Element propertyElem =
 102                 XMLUtils.selectDsNode(this.constructionElement, Constants._TAG_SIGNATUREPROPERTY, i);
 103 
 104             if (propertyElem == null) {
 105                 return null;
 106             } 
 107             return new SignatureProperty(propertyElem, this.baseURI);               
 108         } catch (XMLSecurityException ex) {
 109             throw new XMLSignatureException("empty", ex);
 110         }
 111     }
 112 
 113     /**
 114      * Sets the <code>Id</code> attribute
 115      *
 116      * @param Id the <code>Id</code> attribute
 117      */
 118     public void setId(String Id) {
 119         if (Id != null) {
 120             this.constructionElement.setAttributeNS(null, Constants._ATT_ID, Id);
 121             this.constructionElement.setIdAttributeNS(null, Constants._ATT_ID, true);
 122         }
 123     }
 124 
 125     /**
 126      * Returns the <code>Id</code> attribute
 127      *
 128      * @return the <code>Id</code> attribute
 129      */
 130     public String getId() {
 131         return this.constructionElement.getAttributeNS(null, Constants._ATT_ID);
 132     }
 133 
 134     /**
 135      * Method addSignatureProperty
 136      *
 137      * @param sp
 138      */
 139     public void addSignatureProperty(SignatureProperty sp) {
 140         this.constructionElement.appendChild(sp.getElement());
 141         XMLUtils.addReturnToElement(this.constructionElement);
 142     }
 143 
 144     /** @inheritDoc */
 145     public String getBaseLocalName() {
 146         return Constants._TAG_SIGNATUREPROPERTIES;
 147     }
 148 }