package testgenerator;

import iaik.ixsil.init.IXSILConstants;

import iaik.utils.Base64OutputStream;

import java.io.ByteArrayOutputStream;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
/**
 * @author knirschs
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class EtsiProperties extends TestCases {

	/**
	 * Constructor for EtsiProperties.
	 * 	 
	 */
	private Element qualprop;
	private Element signdataobjectprop;
	private static String etsinamespace = "http://uri.etsi.org/01903/v1.1.1#";
	private Document doc;
	
	public EtsiProperties() throws Exception{

		getX509Content();
		
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		//dbf.setNamespaceAware(true);
		//dbf.setValidating(true);
		
		doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
		
		
		qualprop = doc.createElementNS(etsinamespace,"etsi:QualifyingProperties");
		qualprop.setAttributeNS(null,"Target","#signature");
		qualprop.setAttributeNS(IXSILConstants.NAMESPACE_URI_NAMESPACES_,"xmlns:etsi",etsinamespace);
		//qualprop.setPrefix("etsi");*/
		//qualprop.setAttributeNodeNS(doc.createAttributeNS(etsinamespace,"etsi"));
		Element signprop = doc.createElementNS(etsinamespace,"etsi:SignedProperties");
		Element signsigprop = doc.createElementNS(etsinamespace,"etsi:SignedSignatureProperties");
		signdataobjectprop = doc.createElementNS(etsinamespace,"etsi:SignedDataObjectProperties");
		Element signdate = doc.createElementNS(etsinamespace,"etsi:SigningTime");
		Text datevalue = doc.createTextNode("2003-03-01T00:01:01");
		Element signcert = doc.createElementNS(etsinamespace,"etsi:SigningCertificate");
		Element cert = doc.createElementNS(etsinamespace,"etsi:Cert");
		Element signpolyident = doc.createElementNS(etsinamespace,"etsi:SignaturePolicyIdentifier");
		Element signpolyimp = doc.createElementNS(etsinamespace,"etsi:SignaturePolicyImplied");
		Element certdig = doc.createElementNS(etsinamespace,"etsi:CertDigest");
		Element digmeth = doc.createElementNS(etsinamespace,"etsi:DigestMethod");
		digmeth.setAttribute("Algorithm","http://www.w3.org/2000/09/xmldsig#sha1");
		Element digvalue = doc.createElementNS(etsinamespace,"etsi:DigestValue");
		
		ByteArrayOutputStream fos = new ByteArrayOutputStream();
		Base64OutputStream base64os = new Base64OutputStream(fos);
		base64os.write(this.X509hash.getBytes());
		base64os.flush();

		Text dig_value = doc.createTextNode(fos.toString());
		Element is = doc.createElementNS(etsinamespace,"etsi:IssuerSerial");
		Element i = doc.createElement("dsig:X509IssuerName");
		Text i_value = doc.createTextNode(this.X509name);
		Element s = doc.createElement("dsig:X509SerialNumber");
		Text s_value = doc.createTextNode(this.X509number.toString());
		
		
		qualprop.appendChild(signprop);
			signprop.appendChild(signsigprop);
				signsigprop.appendChild(signdate);
					signdate.appendChild(datevalue);
				signsigprop.appendChild(signcert);
					signcert.appendChild(cert);
						cert.appendChild(certdig);
							certdig.appendChild(digmeth);
							certdig.appendChild(digvalue);
								digvalue.appendChild(dig_value);
						cert.appendChild(is);
							is.appendChild(i);
								i.appendChild(i_value);
							is.appendChild(s);
								s.appendChild(s_value);
				signsigprop.appendChild(signpolyident);
					signpolyident.appendChild(signpolyimp);
			signprop.appendChild(signdataobjectprop);
										
		
	}
	public Element getQualProp()
	{
	return qualprop;	
	}
	public void addDataObjectFormat(String reference) throws Exception
	{
		Element dataobjformat = doc.createElementNS(etsinamespace,"etsi:DataObjectFormat");
		dataobjformat.setAttribute("ObjectReference",reference);
		Element mimetype = doc.createElementNS(etsinamespace,"etsi:MimeType");
		//mimetype.setNodeValue("text/plain");
		Text mimevalue = doc.createTextNode("text/html");
		signdataobjectprop.appendChild(dataobjformat);
					dataobjformat.appendChild(mimetype);						
					mimetype.appendChild(mimevalue);
	}
	
}