package testgenerator;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.xml.serialize.DOMWriterImpl;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;


/**
 * @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 SignatureManifestCheckParams {
/*  So solls mal aussehen :-)

	<SignatureManifestCheckParams ReturnReferenceInputData="true">
		<ReferenceInfo>
			<VerifyTransformsInfoProfile>
				<dsig:Transforms>
					<dsig:Transform Algorithm="http://www.w3.org/TR/1999/REC-xslt-19991116">
						<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"/>
					</dsig:Transform>
				</dsig:Transforms>
				<TransformParameter URI="validURI">
					<Base64Content>validBase64Data</Base64Content>
				</TransformParameter>
			</VerifyTransformsInfoProfile>
			<VerifyTransformsInfoProfileID>validID</VerifyTransformsInfoProfileID>
		</ReferenceInfo>
	</SignatureManifestCheckParams>
*/
	private Document document;
	private Element refInfo;
	private Element verifyTrans;
	private Element transPara;
	private Element dsigTrans;

	public SignatureManifestCheckParams(String returnReferenceInputData) throws Exception{
	
	 // Find the implementation
      DocumentBuilderFactory factory 
       = DocumentBuilderFactory.newInstance();
      factory.setNamespaceAware(true);
      DocumentBuilder builder = factory.newDocumentBuilder();
      DOMImplementation impl = builder.getDOMImplementation();
      
      // Create the document
    document = impl.createDocument(null, "SignatureManifestCheckParams", null);

	//document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();  // Create from whole cloth
	Element root = document.getDocumentElement();
    if (returnReferenceInputData!=null)
    root.setAttribute("ReturnReferenceInputData",returnReferenceInputData);
   
    refInfo = (Element) document.createElement("ReferenceInfo");  
    root.appendChild(refInfo);
     
    verifyTrans = (Element) document.createElement("VerifyTransformsInfoProfile");
  	 	        

	}
	
	public void addTransPara(String URI)
	{
     transPara = document.createElement("TransformParameter");
	 transPara.setAttribute("URI",URI);
	 verifyTrans.appendChild(transPara);
		
	}
	
	
	/**
	 * Method addDsigTransform: To add an dsig:Tranform element with a special algorithm
	 * @param algName
	 */
	public void addDsigTransform(String algName, Element el)
	{	
	if (dsigTrans==null) 
	{
	 dsigTrans = (Element) document.createElementNS("http://www.w3.org/2000/09/xmldsig#","Transforms");
  	 dsigTrans.setPrefix("dsig");
  	 verifyTrans.appendChild(dsigTrans);
     
     refInfo.appendChild(verifyTrans);
	}


		Element trans =  document.createElementNS("http://www.w3.org/2000/09/xmldsig#","Transform");
		trans.setPrefix("dsig");
		trans.setAttribute("Algorithm",algName);
		if (el!=null) trans.appendChild(el);
		dsigTrans.appendChild(trans);
	}
	
	public void addDisgTransform(Element transformElement)
	{
	if (dsigTrans==null) 
	{
	 dsigTrans = (Element) document.createElementNS("http://www.w3.org/2000/09/xmldsig#","Transforms");
  	 dsigTrans.setPrefix("dsig");
  	 verifyTrans.appendChild(dsigTrans);
     
     refInfo.appendChild(verifyTrans);
	}

		dsigTrans.appendChild(transformElement);	
	}
	
	/**
	 * Method addTransformsProfileID.
	 * @param id
	 */
	public void addTransformsProfileID(String id)
	{
		Element profID = (Element) document.createElement("VerifyTransformsInfoProfileID");
		profID.appendChild(document.createTextNode(id)); 
		refInfo.appendChild(profID);
	}
	
	public void addTransParaElement(String elemName, String content)
	{
	if (elemName!="Hash") {
		
		Element eleName = document.createElement(elemName);
		eleName.appendChild(document.createTextNode(content));
		transPara.appendChild(eleName);
	
	} else {
		
		Element eleName = document.createElement(elemName);
			Element digMethod = document.createElementNS("http://www.w3.org/2000/09/xmldsig#","DigestMethod");
			digMethod.setPrefix("dsig");
			digMethod.setAttribute("Algorithm","http://www.w3.org/2000/09/xmldsig#sha1");
		eleName.appendChild(digMethod);
			Element hash = document.createElementNS("http://www.w3.org/2000/09/xmldsig#","DigestValue");	
			hash.setPrefix("dsig");
			hash.appendChild(document.createTextNode(content));
		eleName.appendChild(hash);
		
		transPara.appendChild(eleName);
	
	
	} 
	
	}
	
public String returnDoc() throws Exception
	{
      String result =(new DOMWriterImpl(true)).writeToString(document.getDocumentElement());      

      TestCases tc = new TestCases();

      result = TestCases.replaceString(result,"<?xml version=\"1.0\"?>","");
      try {
			 result = TestCases.replaceString(result," xmlns:dsig=\"http://www.w3.org/2000/09/xmldsig#\"","");	
		 	 result = TestCases.replaceString(result," xmlns:dsig=\"http://www.w3.org/2000/09/xmldsig#\"","");	
		 	 result = TestCases.replaceString(result," xmlns:dsig=\"http://www.w3.org/2000/09/xmldsig#\"","");	
		} catch (Exception e) {
	}
     

	return	result;
	}
	
	public Document getDoc() throws Exception
	{
     	return document;
	}
}