package testgenerator;

import iaik.ixsil.algorithms.Transform;
import iaik.ixsil.algorithms.TransformImplBase64Decode;
import iaik.ixsil.algorithms.TransformImplCanonicalXML;
import iaik.ixsil.algorithms.TransformImplCanonicalXMLWithComments;
import iaik.ixsil.algorithms.TransformImplEnvelopedSignature;
import iaik.ixsil.algorithms.TransformImplExclusiveCanonicalXML;
import iaik.ixsil.algorithms.TransformImplExclusiveCanonicalXMLWithComments;
import iaik.ixsil.algorithms.TransformImplXPath;
import iaik.ixsil.algorithms.TransformImplXPath2;
import iaik.ixsil.algorithms.TransformImplXSLT;
import iaik.ixsil.core.Position;
import iaik.ixsil.util.URI;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;

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

import org.w3c.dom.Document;
import org.w3c.dom.Node;

import at.gv.egovernment.moa.util.Base64Utils;
import at.gv.egovernment.moa.util.DOMUtils;

/**
 * @author Administrator
 *
 * 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 TestGeneratorCX1 extends TestCases {
	
	String datadirectory = "data/CX1/";
	
	String base64content = "RGllcyBzaW5kIFRlc3RkYXRlbg==";
	
	String defaultblock = 
	"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
	"<CreateXMLSignatureRequest xmlns=\"http://reference.e-government.gv.at/namespace/moa/20020822#\" xmlns:dsig=\"http://www.w3.org/2000/09/xmldsig#\" >"+
	"<KeyIdentifier>PKCS12RSAKey1</KeyIdentifier>"+
	"<SingleSignatureInfo SecurityLayerConformity=\"false\">"+
	"<DataObjectInfo Structure=\"detached\" ChildOfManifest=\"false\">"+
	"<DataObject Reference=\""+configuration_.getProperty("webbaseDir")+"resources/BookData.xml"+"\">"+
	"<XMLContent xml:space=\"preserve\">"+
	"<data:book xmlns:data=\"http://uri.data.org\" data:hardback=\"yes\">"+
	"<data:title>Gesundheitliche Auswirkungen elektronischer Signaturen</data:title>"+
	"<data:first-name>Ellipse</data:first-name>"+
	"<data:last-name>Kurvenreich</data:last-name>"+
	"<data:birth-date>1951-04-16</data:birth-date>"+
	"<data:birth-location>Hinterm Berg</data:birth-location>"+
	"<data:register-number>6-930-21512-12</data:register-number>"+
	"</data:book>"+
	"</XMLContent>"+
	"</DataObject>"+
	"<CreateTransformsInfoProfile>"+
	"<CreateTransformsInfo>"+
	"<dsig:Transforms>"+
	"<!-- insert transform -->"+
	"</dsig:Transforms>"+
	"<FinalDataMetaInfo>"+
	"<MimeType>text/html</MimeType>"+
	"<Description>http://www.description.com</Description>"+
	"</FinalDataMetaInfo>"+
	"</CreateTransformsInfo>"+
	"<!-- insert supp -->"+
	"</CreateTransformsInfoProfile>"+
	"</DataObjectInfo>"+
	"<!-- insert data2 -->"+
	"</SingleSignatureInfo>"+
	"</CreateXMLSignatureRequest>";
	
	private static final int canAlgCanXML = 1;
	private static final int canAlgCanXMLwithCom = 2;
	private static final int canAlgExcXML = 3;
	private static final int canAlgExcXMLwithCom = 4;


	public String fileprefix = null;
	public String filesuffix = null;

	public TestGeneratorCX1() throws Exception {

		super();

		// Set config properties
		configuration_.setProperty("TestClass", "TestGeneratorCX1");
//		configuration_.setProperty("privateKeyFile", baseDir + "additionalFiles/keys/RSAPrivateKey.der");
//		configuration_.setProperty("publicKeyFile",  baseDir + "additionalFiles/keys/RSAPublicKey.der");
//		configuration_.setProperty("signatureFile",  baseDir + "additionalFiles/signatures/Signature.unit1.xml");
		
		iaik.security.provider.IAIK.addAsProvider();
        iaik.security.ecc.provider.ECCProvider.addAsProvider();
	}
	
	public static void main(String[] args) throws Exception {
		TestGeneratorCX1 testcase = new TestGeneratorCX1();
		
		testcase.createConfig();
		testcase.create001();
		testcase.create002();
		testcase.create003();
		testcase.create004();
		testcase.create005();
		testcase.create006();
		testcase.create007();
		testcase.create008();
		testcase.create009();
		testcase.create010();
		testcase.create011();
		testcase.create012();
		testcase.create013();
		testcase.create014();
		
		testcase.create051();
		testcase.create052();		
		testcase.create053();
		testcase.create054();
		testcase.create055();

	}
	
	public void createConfig() throws Exception {

	    String file = configuration_.getProperty("baseDir") + datadirectory +
	                  configuration_.getProperty("TestClass") + 
					  ".Config.xml";
		String config =
	
		"<MOAConfiguration xmlns=\"http://reference.e-government.gv.at/namespace/moaconfig/20021122#\">"+
		"	<KeyGroup id=\"PKCS12RSAKey1\">		<Key id=\"resources/test-ee2003_normal(buergerkarte).p12\"/>	</KeyGroup>"+
		"	<KeyGroup id=\"PKCS12RSAKeyExpired\">		<Key id=\"sicher-demo(buergerkarte).p12\"/>	</KeyGroup>"+
		
		"	<KeyGroup id=\"PKCS12ECDSAKey1\">	<Key id=\"ecc.p12\"/>						</KeyGroup> "+
		"	<KeyGroup id=\"HSMRSAKey1\">			<Key id=\"???\"/>							</KeyGroup>"+
		"	<KeyGroup id=\"HSMECDSAKey1\">		<Key id=\"???\"/>							</KeyGroup>"+
		"	<DigestMethodAlgorithm name=\"http://www.w3.org/2000/09/xmldsig#sha1\"/>"+
		"	<SignatureAlgorithm name=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\"/>"+
		"	<CanonicalizationAlgorithm name=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\"/>"+
		"	<CreateTransformsInfoProfile id=\"CreateTransformsInfoProfile1\" filename=\""+"file:/"+configuration_.getProperty("webbaseDir")+"CX1_bookprofile.xsl\"/>"+
		"</MOAConfiguration>";
	
	
		/*"<MOAConfiguration>"+
		"  <KeyGroup id=\"PKCS12RSAKey1\"/>  <!-- mapped auf \"test(1234).p12\" -->"+
		"</MOAConfiguration>"+
		"<!-- CanonicalizationMethod = http://www.w3.org/TR/2001/REC-xml-c14n-20010315 -->" +
		"<!-- SignatureMethod = http://www.w3.org/2000/09/xmldsig#rsa-sha1 -->";*/
	
		FileOutputStream fos = new FileOutputStream(file);
		fos.write(config.getBytes());
		fos.flush();
		fos.close();
		
		fileprefix = configuration_.getProperty("baseDir")+ datadirectory + configuration_.getProperty("TestClass") + ".";
		filesuffix = ".xml";
	}
	
	public void create001() throws Exception {

		String TestNumber = "001";
	    String reqFile = fileprefix + TestNumber + ".Req"+filesuffix;
	    
	    String req = replaceString(defaultblock,"<!-- insert transform -->",
	   	"<dsig:Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xslt-19991116\">"+
		"<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\"><xsl:output method=\"html\" indent=\"no\"/><xsl:template match=\"data:book\" xmlns:data=\"http://uri.data.org\"><HTML><BODY><H1>Buch: <xsl:value-of select=\"data:title\"/></H1></BODY></HTML></xsl:template></xsl:stylesheet>"+
		"</dsig:Transform>");
		
		writeFile(reqFile,req);
	
		String resFile = fileprefix + TestNumber + ".Res"+filesuffix;
		
		CreatePKCSSignature cpkcs = new CreatePKCSSignature();
		cpkcs.init("signature-1-1");
		cpkcs.setCanoncialization(canAlgCanXML);
		//cpkcs.createReference(true,"Dies sind testdaten");
		
		Transform[] tr = new Transform[1];
		TransformImplXSLT trans = new TransformImplXSLT();	
		InputStream s = new FileInputStream(configuration_.getProperty("baseDir")+"resources/CX1_book.xsl");
		trans.setInput(s,null);									
		trans.setStyleSheet(s,null);
		tr[0] = trans;

		cpkcs.createReferenceWithTransforms(new URI(configuration_.getProperty("webbaseDir")+"resources/BookData.xml"),tr,"reference-1-1");		
		
		cpkcs.setKeyInformation(1);
		
		Document doc = cpkcs.returnSig();
    	
    	String output = makeOutputFile(doc);
    	
    	writeFile(resFile, output);
	
	}
	
	public void create002() throws Exception {

		String TestNumber = "002";
	    String reqFile = fileprefix + TestNumber + ".Req"+filesuffix;
	    
	    String req = replaceString(defaultblock,"<!-- insert transform -->",
	   	"<dsig:Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xslt-19991116\">"+
		"<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">"+
		"<xsl:output method=\"html\" indent=\"no\"/>"+
		"<xsl:template match=\"data:book\" xmlns:data=\"http://uri.data.org\">"+
		"<HTML>"+
		"<BODY>"+
		"<H1>Buch: <xsl:value-of select=\"data:title\"/>"+
		"</H1>"+
		"</BODY>"+
		"</HTML>"+
		"</xsl:template>"+
		"</xsl:stylesheet>"+
		"</dsig:Transform>");
		
		req = replaceString(req,"SecurityLayerConformity=\"false\"","SecurityLayerConformity=\"true\"");
		
		writeFile(reqFile,req);
	
		String resFile = fileprefix + TestNumber + ".Res"+filesuffix;
		
		CreatePKCSSignature cpkcs = new CreatePKCSSignature();
		cpkcs.init("signature-1-1");
		cpkcs.setCanoncialization(canAlgCanXML);
		//cpkcs.createReference(true,"Dies sind testdaten");
		
		Transform[] tr = new Transform[1];
		TransformImplXSLT trans = new TransformImplXSLT();	
		InputStream s = new FileInputStream(configuration_.getProperty("baseDir")+"resources/CX1_book.xsl");
		trans.setInput(s,null);									
		trans.setStyleSheet(s,null);
		tr[0] = trans;
		
		cpkcs.createReferenceWithTransforms(new URI(configuration_.getProperty("webbaseDir")+"resources/BookData.xml"),tr,"reference-1-1");		
		//cpkcs.createReferenceWithManifest(new URI(configuration_.getProperty("webbaseDir")+"resources/BookData.xml"),"manifestObject");
		cpkcs.createETSIObjects("file:/"+configuration_.getProperty("webbaseDir")+"resources/BookData.xml");
		cpkcs.setKeyInformation(1);
		
		cpkcs.signer.getSignature().setId("signature-1-1");
		
		Document doc = cpkcs.returnSig();
    	
    	String output = makeOutputFile(doc);
    	
    	writeFile(resFile, output);
	
	}
	
	public void create003() throws Exception {

		String TestNumber = "003";
	    String reqFile = fileprefix + TestNumber + ".Req"+filesuffix;
	    
	    String req = replaceString(defaultblock,"<!-- insert transform -->",
	   	"<dsig:Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xpath-19991116\">"+
		"<dsig:XPath xmlns:data=\"http://uri.data.org\">//data:book/data:first-name</dsig:XPath>"+
		"</dsig:Transform>");
		
		req = replaceString(req,"<MimeType>text/html</MimeType>","<MimeType>text/plain</MimeType>");
		
		writeFile(reqFile,req);
	
		String resFile = fileprefix + TestNumber + ".Res"+filesuffix;

		CreatePKCSSignature cpkcs = new CreatePKCSSignature();
		cpkcs.init("signature-1-1");
		cpkcs.setCanoncialization(canAlgCanXML);
		//cpkcs.createReference(true,"Dies sind testdaten");
		
		Transform[] tr = new Transform[1];
		TransformImplXPath trans = new TransformImplXPath();	
		InputStream s = new FileInputStream(configuration_.getProperty("baseDir")+"resources/BookData.xml");
		trans.setInput(s,null);									
		trans.addNSDeclaration("data",new URI("http://uri.data.org"));
		trans.setXPath("//data:book/data:first-name");
		//trans.setXPath("ancestor-or-self::data:first-name");
		
		tr[0] = trans;
		
		cpkcs.createReferenceWithTransforms(new URI(configuration_.getProperty("webbaseDir")+"resources/BookData.xml"),tr,"reference-1-1");		
		cpkcs.setKeyInformation(1);
		
		Document doc = cpkcs.returnSig();
    	
    	String output = makeOutputFile(doc);
    	
    	output = replaceString(output,"<dsig:XPath xmlns:data=\"http://uri.data.org\">","<dsig:XPath>"); // workaround
    	
    	writeFile(resFile, output);
	
	}

	public void create004() throws Exception {

		String TestNumber = "004";
	    String reqFile = fileprefix + TestNumber + ".Req"+filesuffix;
	    
	    String req = replaceString(defaultblock,"<!-- insert transform -->",
		"<dsig:Transform Algorithm=\"http://www.w3.org/2002/06/xmldsig-filter2\">"+
		"<xf2:XPath Filter=\"subtract\" xmlns:xf2=\"http://www.w3.org/2002/06/xmldsig-filter2\" xmlns:data=\"http://uri.data.org\">//data:book/data:first-name</xf2:XPath>"+
		"</dsig:Transform>");
		
		req = replaceString(req,"<MimeType>text/html</MimeType>","<MimeType>text/xml</MimeType>");
		
		writeFile(reqFile,req);
	
		String resFile = fileprefix + TestNumber + ".Res"+filesuffix;
		
		CreatePKCSSignature cpkcs = new CreatePKCSSignature();
		cpkcs.init("signature-1-1");
		cpkcs.setCanoncialization(canAlgCanXML);
		//cpkcs.createReference(true,"Dies sind testdaten");
		
		Transform[] tr = new Transform[1];
		TransformImplXPath2 trans = new TransformImplXPath2();	
		InputStream s = new FileInputStream(configuration_.getProperty("baseDir")+"resources/BookData.xml");
		trans.setInput(s,null);									
		//trans.addNSDeclaration("data",new URI(configuration_.getProperty("webbaseDir")+"resources/BookData.xml"));
		//trans.setXPath("/data:book/data:first-name/node()");
		HashMap temp = new HashMap();
		temp.put("data",new URI("http://uri.data.org"));
		trans.addXPathFilter("//data:book/data:first-name",TransformImplXPath2.FILTER_MODE_SUBTRACT_,temp);
		tr[0] = trans;
		
		cpkcs.createReferenceWithTransforms(new URI(configuration_.getProperty("webbaseDir")+"resources/BookData.xml"),tr,"reference-1-1");		
		cpkcs.setKeyInformation(1);
		
		Document doc = cpkcs.returnSig();
    	
    	String output = makeOutputFile(doc);
    	
    	output = replaceString(output,"<xf2:XPath Filter=\"subtract\" xmlns:data=\"http://uri.data.org\"","<xf2:XPath Filter=\"subtract\" ");
    	
    	writeFile(resFile, output);
	
	}

	public void create005() throws Exception {

		String TestNumber = "005";
	    String reqFile = fileprefix + TestNumber + ".Req"+filesuffix;
	    
	    String req = replaceString(defaultblock,"<!-- insert transform -->",
		"<dsig:Transform Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\"/>");
		
		req = replaceString(req,"<MimeType>text/html</MimeType>","<MimeType>text/xml</MimeType>");
		
		writeFile(reqFile,req);
	
		String resFile = fileprefix + TestNumber + ".Res"+filesuffix;
		
		CreatePKCSSignature cpkcs = new CreatePKCSSignature();
		cpkcs.init("signature-1-1");
		cpkcs.setCanoncialization(canAlgCanXML);
		//cpkcs.createReference(true,"Dies sind testdaten");
		
		Transform[] tr = new Transform[1];
		TransformImplCanonicalXML trans = new TransformImplCanonicalXML();	
		InputStream s = new FileInputStream(configuration_.getProperty("baseDir")+"resources/CX1_book.xsl");
		trans.setInput(s,null);									
		/*trans.addNSDeclaration("data",new URI(configuration_.getProperty("webbaseDir")+"resources/BookData.xml"));
		trans.setXPath("/data:book/data:first-name/node()");
		trans.*/
		tr[0] = trans;
		
		cpkcs.createReferenceWithTransforms(new URI(configuration_.getProperty("webbaseDir")+"resources/BookData.xml"),tr,"reference-1-1");		
		cpkcs.setKeyInformation(1);
		
		Document doc = cpkcs.returnSig();
    	
    	String output = makeOutputFile(doc);
    	
    	writeFile(resFile, output);
	
	}
	
	public void create006() throws Exception {

		String TestNumber = "006";
	    String reqFile = fileprefix + TestNumber + ".Req"+filesuffix;
	    
	    String req = replaceString(defaultblock,"<!-- insert transform -->",
		"<dsig:Transform Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments\"/>");
		
		req = replaceString(req,"<MimeType>text/html</MimeType>","<MimeType>text/xml</MimeType>");
		
		writeFile(reqFile,req);
	
		String resFile = fileprefix + TestNumber + ".Res"+filesuffix;
		
		CreatePKCSSignature cpkcs = new CreatePKCSSignature();
		cpkcs.init("signature-1-1");
		cpkcs.setCanoncialization(canAlgCanXML);
		//cpkcs.createReference(true,"Dies sind testdaten");
		
		Transform[] tr = new Transform[1];
		TransformImplCanonicalXMLWithComments trans = new TransformImplCanonicalXMLWithComments();	
		InputStream s = new FileInputStream(configuration_.getProperty("baseDir")+"resources/CX1_book.xsl");
		trans.setInput(s,null);									
		/*trans.addNSDeclaration("data",new URI(configuration_.getProperty("webbaseDir")+"resources/BookData.xml"));
		trans.setXPath("/data:book/data:first-name/node()");
		trans.*/
		tr[0] = trans;
		
		cpkcs.createReferenceWithTransforms(new URI(configuration_.getProperty("webbaseDir")+"resources/BookData.xml"),tr,"reference-1-1");		
		cpkcs.setKeyInformation(1);
		
		Document doc = cpkcs.returnSig();
    	
    	String output = makeOutputFile(doc);
    	
    	writeFile(resFile, output);
	
	}
	
	public void create007() throws Exception {

		String TestNumber = "007";
	    String reqFile = fileprefix + TestNumber + ".Req"+filesuffix;
	    
	    String req = replaceString(defaultblock,"<!-- insert transform -->",
		"<dsig:Transform Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#\"><ec:InclusiveNamespaces PrefixList=\"dsig\" xmlns:ec=\"http://www.w3.org/2001/10/xml-exc-c14n#\"/></dsig:Transform>");
		
		req = replaceString(req,"<MimeType>text/html</MimeType>","<MimeType>text/xml</MimeType>");
		
		writeFile(reqFile,req);
	
		String resFile = fileprefix + TestNumber + ".Res"+filesuffix;
		
		CreatePKCSSignature cpkcs = new CreatePKCSSignature();
		cpkcs.init("signature-1-1");
		cpkcs.setCanoncialization(canAlgCanXML);
		//cpkcs.createReference(true,"Dies sind testdaten");
		
		Transform[] tr = new Transform[1];
		TransformImplExclusiveCanonicalXML trans = new TransformImplExclusiveCanonicalXML();	
		InputStream s = new FileInputStream(configuration_.getProperty("baseDir")+"resources/CX1_book.xsl");
		trans.setInput(s,null);									
		trans.setInclusiveNamespacePrefixes("dsig");
		/*trans.addNSDeclaration("data",new URI(configuration_.getProperty("webbaseDir")+"resources/BookData.xml"));
		trans.setXPath("/data:book/data:first-name/node()");
		trans.*/
		tr[0] = trans;
		
		cpkcs.createReferenceWithTransforms(new URI(configuration_.getProperty("webbaseDir")+"resources/BookData.xml"),tr,"reference-1-1");		
		cpkcs.setKeyInformation(1);
		
		Document doc = cpkcs.returnSig();
    	
    	String output = makeOutputFile(doc);
    	
    	writeFile(resFile, output);
	
	}

	public void create008() throws Exception {

		String TestNumber = "008";
	    String reqFile = fileprefix + TestNumber + ".Req"+filesuffix;
	    
	    String req = replaceString(defaultblock,"<!-- insert transform -->",
		"<dsig:Transform Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#WithComments\"><ec:InclusiveNamespaces PrefixList=\"dsig\" xmlns:ec=\"http://www.w3.org/2001/10/xml-exc-c14n#\"/></dsig:Transform>");
		
		req = replaceString(req,"<MimeType>text/html</MimeType>","<MimeType>text/xml</MimeType>");
		
		writeFile(reqFile,req);
	
		String resFile = fileprefix + TestNumber + ".Res"+filesuffix;
		
		CreatePKCSSignature cpkcs = new CreatePKCSSignature();
		cpkcs.init("signature-1-1");
		cpkcs.setCanoncialization(canAlgCanXML);
		//cpkcs.createReference(true,"Dies sind testdaten");
		
		Transform[] tr = new Transform[1];
		TransformImplExclusiveCanonicalXMLWithComments trans = new TransformImplExclusiveCanonicalXMLWithComments();	
		InputStream s = new FileInputStream(configuration_.getProperty("baseDir")+"resources/CX1_book.xsl");
		trans.setInput(s,null);		
		trans.setInclusiveNamespacePrefixes("dsig");
		/*trans.addNSDeclaration("data",new URI(configuration_.getProperty("webbaseDir")+"resources/BookData.xml"));
		trans.setXPath("/data:book/data:first-name/node()");
		trans.*/
		tr[0] = trans;
		
		cpkcs.createReferenceWithTransforms(new URI(configuration_.getProperty("webbaseDir")+"resources/BookData.xml"),tr,"reference-1-1");		
		cpkcs.setKeyInformation(1);
		
		Document doc = cpkcs.returnSig();
    	
    	String output = makeOutputFile(doc);
    	
    	writeFile(resFile, output);
	
	}
	
	public void create009() throws Exception {

		String TestNumber = "009";
	    String reqFile = fileprefix + TestNumber + ".Req"+filesuffix;
	    
	    String req = replaceString(defaultblock,"<!-- insert transform -->",
		"<dsig:Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\"/>");
		
		req = replaceString(req,"<XMLContent xml:space=\"preserve\">"+
		"<data:book xmlns:data=\"http://uri.data.org\" data:hardback=\"yes\">"+
		"<data:title>Gesundheitliche Auswirkungen elektronischer Signaturen</data:title>"+
		"<data:first-name>Ellipse</data:first-name>"+
		"<data:last-name>Kurvenreich</data:last-name>"+
		"<data:birth-date>1951-04-16</data:birth-date>"+
		"<data:birth-location>Hinterm Berg</data:birth-location>"+
		"<data:register-number>6-930-21512-12</data:register-number>"+
		"</data:book>"+
		"</XMLContent>","");
		
		req = replaceString(req,
		"<DataObject Reference=\""+configuration_.getProperty("webbaseDir")+"resources/BookData.xml"+"\">",
		"<DataObject Reference=\"\">");
		
		//req = replaceString(req,"Structure=\"detached\"","Structure=\"enveloping\"");
		
		
		req = replaceString(req,"<!-- insert data2 -->",
		"<CreateSignatureInfo><CreateSignatureEnvironment><XMLContent xml:space=\"preserve\"><env:Document xmlns:env=\"http://uri.env.org\">Text</env:Document></XMLContent></CreateSignatureEnvironment><CreateSignatureEnvironmentProfile><CreateSignatureLocation xmlns:env=\"http://uri.env.org\" Index=\"1\">//env:Document</CreateSignatureLocation></CreateSignatureEnvironmentProfile></CreateSignatureInfo>");		
		writeFile(reqFile,req);
	
		String resFile = fileprefix + TestNumber + ".Res"+filesuffix;
		
		InputStream bis = new ByteArrayInputStream(("<env:Document xmlns:env=\"http://uri.env.org\">Text</env:Document>").getBytes());
	    //Document-Builder holen
	    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	    factory.setNamespaceAware(true);
	    DocumentBuilder builder = factory.newDocumentBuilder();      
	    //Dokument Parsen
	    Document reqDoc = builder.parse(bis);
	    
	    System.out.println(DOMUtils.serializeNode(reqDoc.getDocumentElement()));
	    //Position f�r die Signatur angeben
	    String xPath_Pos = "/node()";
	    //XPAth-Prefixe deklarieren
	    String xPath_Prefix = "env xmlns:env=\"http://uri.env.org\"";
	    Position pos = new Position(xPath_Pos, xPath_Prefix,0);   
		
		CreatePKCSSignature cpkcs = new CreatePKCSSignature();
		cpkcs.init(reqDoc,pos,"signature-1-1");
		cpkcs.setCanoncialization(canAlgCanXML);
		//cpkcs.createReference(true,"Dies sind testdaten");
		
		Transform[] tr = new Transform[1];
		TransformImplEnvelopedSignature trans = new TransformImplEnvelopedSignature();	
								
		/*trans.addNSDeclaration("data",new URI(configuration_.getProperty("webbaseDir")+"resources/BookData.xml"));
		trans.setXPath("/data:book/data:first-name/node()");
		trans.*/
		tr[0] = trans;
		
		cpkcs.createReferenceWithTransforms(new URI(""),tr,"reference-1-1");		
		cpkcs.setKeyInformation(1);
		
		Document doc = cpkcs.returnSig();
    	
    	String output = makeOutputFile(doc);
    	
    	output = replaceString(output,"<dsig:Signature","<env:Document xmlns:env=\"http://uri.env.org\">Text<dsig:Signature");
    	output = replaceString(output,"</dsig:Signature>","</dsig:Signature></env:Document>");
    	
    	
    	
    	writeFile(resFile, output);
	
	}

	public void create010() throws Exception {

		String TestNumber = "010";
	    String reqFile = fileprefix + TestNumber + ".Req"+filesuffix;
	    
	    String req = replaceString(defaultblock,"<!-- insert transform -->",
		"<dsig:Transform Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\"/>"+
		"<dsig:Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xslt-19991116\">"+
		"<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">"+
		"<xsl:output method=\"html\" indent=\"no\"/>"+
		"<xsl:template match=\"data:book\" xmlns:data=\"http://uri.data.org\">"+
		"<HTML>"+
		"<BODY>"+
		"<H1>Buch: <xsl:value-of select=\"data:title\"/>"+
		"</H1>"+
		"</BODY>"+
		"</HTML>"+
		"</xsl:template>"+
		"</xsl:stylesheet>"+
		"</dsig:Transform>");
		
		writeFile(reqFile,req);
	
		String resFile = fileprefix + TestNumber + ".Res"+filesuffix;
		
		CreatePKCSSignature cpkcs = new CreatePKCSSignature();
		cpkcs.init("signature-1-1");
		cpkcs.setCanoncialization(canAlgCanXML);
		//cpkcs.createReference(true,"Dies sind testdaten");
		
		Transform[] tr = new Transform[2];
		TransformImplCanonicalXML trans = new TransformImplCanonicalXML();	
		InputStream s = new FileInputStream(configuration_.getProperty("baseDir")+"resources/CX1_book.xsl");
		trans.setInput(s,null);									
		/*trans.addNSDeclaration("data",new URI(configuration_.getProperty("webbaseDir")+"resources/BookData.xml"));
		trans.setXPath("/data:book/data:first-name/node()");
		trans.*/
		tr[0] = trans;
		
		TransformImplXSLT tr2 = new TransformImplXSLT();	
		InputStream s2 = new FileInputStream(configuration_.getProperty("baseDir")+"resources/CX1_book.xsl");
		tr2.setInput(s2,null);									
		tr2.setStyleSheet(s2,null);
		tr[1] = tr2;
		
		cpkcs.createReferenceWithTransforms(new URI(configuration_.getProperty("webbaseDir")+"resources/BookData.xml"),tr,"reference-1-1");		
		cpkcs.setKeyInformation(1);
		
		Document doc = cpkcs.returnSig();
    	
    	String output = makeOutputFile(doc);
    	
    	writeFile(resFile, output);
	
	}
	
	public void create011() throws Exception {

		String TestNumber = "011";
	    String reqFile = fileprefix + TestNumber + ".Req"+filesuffix;
	    
	    String req = replaceString(defaultblock,"<!-- insert transform -->",
		"<dsig:Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xslt-19991116\">"+
		"<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">"+
		"<xsl:include href=\""+configuration_.getProperty("webbaseDir")+"resources/CX1_book_not_here.xsl"+"\"/>"+
		"</xsl:stylesheet>"+
		"</dsig:Transform>");
		
		req = replaceString(req,"<!-- insert supp -->",
		"<Supplement>"+
		"<Content Reference=\""+configuration_.getProperty("webbaseDir")+"resources/CX1_book_not_here.xsl"+"\">"+
		"<Base64Content>"+
		Base64Utils.encode(("<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">"+
		"<xsl:output method=\"html\" indent=\"no\"/>"+
		"<xsl:template match=\"data:book\" xmlns:data=\"http://uri.data.org\">"+
		"<HTML>"+
		"<BODY>"+
		"<H1>Buch: <xsl:value-of select=\"data:title\"/>"+
		"</H1>"+
		"</BODY>"+
		"</HTML>"+
		"</xsl:template>"+
		"</xsl:stylesheet>").getBytes())+
		"</Base64Content>"+
		"</Content>"+
		"</Supplement>");
		
		writeFile(reqFile,req);
	
		String resFile = fileprefix + TestNumber + ".Res"+filesuffix;

		CreatePKCSSignature cpkcs = new CreatePKCSSignature();
		cpkcs.init("signature-1-1");
		cpkcs.setCanoncialization(canAlgCanXML);
		//cpkcs.createReference(true,"Dies sind testdaten");
		
		Transform[] tr = new Transform[1];
		TransformImplXSLT trans = new TransformImplXSLT();	
		URL u = new URL(configuration_.getProperty("webbaseDir")+"resources/CX1_bookref_not_here.xsl");
		URLConnection hu = u.openConnection();
		//trans.setInput(s,null);									
		trans.setStyleSheet(hu.getInputStream(),new URI(configuration_.getProperty("webbaseDir")+"resources/CX1_bookref_not_here.xsl"));
		tr[0] = trans;
		
		cpkcs.createReferenceWithTransforms(new URI(configuration_.getProperty("webbaseDir")+"resources/BookData.xml"),tr,"reference-1-1");		
		//cpkcs.createReferenceWithManifest(new URI(configuration_.getProperty("webbaseDir")+"resources/BookData.xml"),"manifestObject");
		cpkcs.setKeyInformation(1);
		
		Document doc = cpkcs.returnSig();
    	
    	String output = makeOutputFile(doc);
    	
    	writeFile(resFile, output);	
	}
	
	public void create012() throws Exception {

		String TestNumber = "012";
	    String reqFile = fileprefix + TestNumber + ".Req"+filesuffix;
	    
	    String req = replaceString(defaultblock,"<!-- insert transform -->",
		"<dsig:Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xslt-19991116\">"+
		"<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">"+
		"<xsl:include href=\""+configuration_.getProperty("webbaseDir")+"resources/CX1_book_not_here.xsl"+"\"/>"+
		"</xsl:stylesheet>"+
		"</dsig:Transform>");
		
		req = replaceString(req,"<!-- insert data2 -->",
		"<DataObjectInfo Structure=\"detached\" ChildOfManifest=\"false\">"+
		"<DataObject Reference=\""+configuration_.getProperty("webbaseDir")+"resources/BookData.xml"+"\">"+
		"<XMLContent xml:space=\"preserve\">"+
		"<data:book xmlns:data=\"http://uri.data.org\" data:hardback=\"yes\">"+
		"<data:title>Gesundheitliche Auswirkungen elektronischer Signaturen</data:title>"+
		"<data:first-name>Ellipse</data:first-name>"+
		"<data:last-name>Kurvenreich</data:last-name>"+
		"<data:birth-date>1951-04-16</data:birth-date>"+
		"<data:birth-location>Hinterm Berg</data:birth-location>"+
		"<data:register-number>6-930-21512-12</data:register-number>"+
		"</data:book>"+
		"</XMLContent>"+
		"</DataObject>"+
		"<CreateTransformsInfoProfile>"+
		"<CreateTransformsInfo>"+
		"<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\" version=\"1.0\">"+
		"<xsl:include href=\""+configuration_.getProperty("webbaseDir")+"resources/CX1_book_not_here.xsl\"/>"+
		"</xsl:stylesheet>"+
		"</dsig:Transform>"+
		"</dsig:Transforms>"+
		"<FinalDataMetaInfo>"+
		"<MimeType>text/html</MimeType>"+
		"</FinalDataMetaInfo>"+
		"</CreateTransformsInfo>"+
		"<Supplement>"+
		"<Content Reference=\""+configuration_.getProperty("webbaseDir")+"resources/CX1_book_not_here.xsl"+"\">"+
		"<Base64Content>"+
		Base64Utils.encode(("<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">"+
		"<xsl:output method=\"html\" indent=\"no\"/>"+
		"<xsl:template match=\"data:book\" xmlns:data=\"http://uri.data.org\">"+
		"<HTML>"+
		"<BODY>"+
		"<H1>Buch: <xsl:value-of select=\"data:title\"/>"+
		"</H1>"+
		"</BODY>"+
		"</HTML>"+
		"</xsl:template>"+
		"</xsl:stylesheet>").getBytes())+
		"</Base64Content>"+
		"</Content>"+
		"</Supplement>"+
		"</CreateTransformsInfoProfile>"+
		"</DataObjectInfo>");
		
		writeFile(reqFile,req);
	
		String resFile = fileprefix + TestNumber + ".Res"+filesuffix;
		
		CreatePKCSSignature cpkcs = new CreatePKCSSignature();
		cpkcs.init("signature-1-1");
		cpkcs.setCanoncialization(canAlgCanXML);
		//cpkcs.createReference(true,"Dies sind testdaten");
		
		Transform[] tr = new Transform[1];
		TransformImplXSLT trans = new TransformImplXSLT();	
				URL u = new URL(configuration_.getProperty("webbaseDir")+"resources/CX1_bookref_not_here.xsl");
		URLConnection hu = u.openConnection();
		//trans.setInput(s,null);									
		trans.setStyleSheet(hu.getInputStream(),new URI(configuration_.getProperty("webbaseDir")+"resources/CX1_bookref_not_here.xsl"));

		tr[0] = trans;
		
		cpkcs.createReferenceWithTransforms(new URI(configuration_.getProperty("webbaseDir")+"resources/BookData.xml"),tr,"reference-1-1");		
		cpkcs.createReferenceWithTransforms(new URI(configuration_.getProperty("webbaseDir")+"resources/BookData.xml"),tr,"reference-1-2");
		//cpkcs.createReferenceWithManifest(new URI(configuration_.getProperty("webbaseDir")+"resources/BookData.xml"),"manifestObject");
		cpkcs.setKeyInformation(1);
		
		Document doc = cpkcs.returnSig();
    	
    	String output = makeOutputFile(doc);
    	
    	writeFile(resFile, output);	
	
	}

	public void create013() throws Exception {

		String TestNumber = "013";
	    String reqFile = fileprefix + TestNumber + ".Req"+filesuffix;
	    
	    String req = replaceString(defaultblock,
	    "<CreateTransformsInfoProfile>"+
		"<CreateTransformsInfo>"+
		"<dsig:Transforms>"+
		"<!-- insert transform -->"+
		"</dsig:Transforms>"+
		"<FinalDataMetaInfo>"+
		"<MimeType>text/html</MimeType>"+
		"<Description>http://www.description.com</Description>"+
		"</FinalDataMetaInfo>"+
		"</CreateTransformsInfo>"+
		"<!-- insert supp -->"+
		"</CreateTransformsInfoProfile>",
	    "<CreateTransformsInfoProfileID>CreateTransformsInfoProfile1</CreateTransformsInfoProfileID>");
	    
		writeFile(reqFile,req);
	
		String resFile = fileprefix + TestNumber + ".Res"+filesuffix;
		
		CreatePKCSSignature cpkcs = new CreatePKCSSignature();
		cpkcs.init("signature-1-1");
		cpkcs.setCanoncialization(canAlgCanXML);
		//cpkcs.createReference(true,"Dies sind testdaten");
		
		Transform[] tr = new Transform[1];
		TransformImplXSLT trans = new TransformImplXSLT();	
		InputStream s = new FileInputStream(configuration_.getProperty("baseDir")+"resources/CX1_book.xsl");
		trans.setInput(s,null);									
		trans.setStyleSheet(s,null);
		tr[0] = trans;
		
		cpkcs.createReferenceWithTransforms(new URI(configuration_.getProperty("webbaseDir")+"resources/BookData.xml"),tr,"reference-1-1");		
		//cpkcs.createReferenceWithManifest(new URI(configuration_.getProperty("webbaseDir")+"resources/BookData.xml"),"manifestObject");
		cpkcs.setKeyInformation(1);
		
		Document doc = cpkcs.returnSig();
    	
    	String output = makeOutputFile(doc);
    	
    	writeFile(resFile, output);	
	
	}
	
	public void create014() throws Exception {

		String TestNumber = "014";
	    String reqFile = fileprefix + TestNumber + ".Req"+filesuffix;
	    
	    String req = replaceString(defaultblock,"<!-- insert transform -->",
		"<dsig:Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#base64\"/>");
		
		req = replaceString(req,
		"<DataObject Reference=\""+configuration_.getProperty("webbaseDir")+"resources/BookData.xml"+"\">",
		"<DataObject Reference=\""+configuration_.getProperty("webbaseDir")+"resources/testBase64.txt"+"\">");
		
		req = replaceString(req,
		"<XMLContent xml:space=\"preserve\">"+
		"<data:book xmlns:data=\"http://uri.data.org\" data:hardback=\"yes\">"+
		"<data:title>Gesundheitliche Auswirkungen elektronischer Signaturen</data:title>"+
		"<data:first-name>Ellipse</data:first-name>"+
		"<data:last-name>Kurvenreich</data:last-name>"+
		"<data:birth-date>1951-04-16</data:birth-date>"+
		"<data:birth-location>Hinterm Berg</data:birth-location>"+
		"<data:register-number>6-930-21512-12</data:register-number>"+
		"</data:book>"+
		"</XMLContent>",
		/*"<Base64Content>RGllcyBzaW5kIFRlc3RkYXRlbg==</Base64Content>"*/"");

		req = replaceString(req,"<MimeType>text/html</MimeType>","<MimeType>text/plain</MimeType>");
		
		writeFile(reqFile,req);
	
		String resFile = fileprefix + TestNumber + ".Res"+filesuffix;
		
		CreatePKCSSignature cpkcs = new CreatePKCSSignature();
		cpkcs.init("signature-1-1");
		cpkcs.setCanoncialization(canAlgCanXML);
		//cpkcs.createReference(true,"Dies sind testdaten");
		
		Transform[] tr = new Transform[1];
		TransformImplBase64Decode trans = new TransformImplBase64Decode();	
		InputStream s = new FileInputStream(configuration_.getProperty("baseDir")+"resources/testBase64.txt");
		trans.setInput(s,null);
		/*trans.addNSDeclaration("data",new URI(configuration_.getProperty("webbaseDir")+"resources/BookData.xml"));
		trans.setXPath("/data:book/data:first-name/node()");
		trans.*/
		tr[0] = trans;
		
		cpkcs.createReferenceWithTransforms(new URI(configuration_.getProperty("webbaseDir")+"resources/testBase64.txt"),tr,"reference-1-1");		
		cpkcs.setKeyInformation(1);
		
		Document doc = cpkcs.returnSig();
    	
    	String output = makeOutputFile(doc);
    	
    	writeFile(resFile, output);
	
	}

	public void create051() throws Exception {

		String TestNumber = "051";
	    String reqFile = fileprefix + TestNumber + ".Req"+filesuffix;
	    
	    String req = replaceString(defaultblock,"<!-- insert transform -->",
	   	"<dsig:Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xpath-19991116\">"+
		"<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">"+
		"<xsl:output method=\"html\" indent=\"no\"/>"+
		"<xsl:template match=\"data:book\" xmlns:data=\"http://uri.data.org\">"+
		"<HTML>"+
		"<BODY>"+
		"<H1>Buch: <xsl:value-of select=\"data:title\"/>"+
		"</H1>"+
		"</BODY>"+
		"</HTML>"+
		"</xsl:template>"+
		"</xsl:stylesheet>"+
		"</dsig:Transform>");
		
		req = replaceString(req,"SecurityLayerConformity=\"false\"","SecurityLayerConformity=\"true\"");
		
		writeFile(reqFile,req);
	
		String resFile = fileprefix + TestNumber + ".ErrRes"+filesuffix;
		
		writeFile(resFile,"2202");
	}
	
	public void create052() throws Exception {

		String TestNumber = "052";
	    String reqFile = fileprefix + TestNumber + ".Req"+filesuffix;
	    
	    String req = replaceString(defaultblock,"<!-- insert transform -->",
	   	"<dsig:Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xslt-19991116\">"+
		"<XPath>/data:book/data:first-name/node()</XPath>"+
		"</dsig:Transform>");
		
		writeFile(reqFile,req);
	
		String resFile = fileprefix + TestNumber + ".ErrRes"+filesuffix;
		
		writeFile(resFile,"2215");

	
	}

	public void create053() throws Exception {

		String TestNumber = "053";
	    String reqFile = fileprefix + TestNumber + ".Req"+filesuffix;
	    
	    String req = replaceString(defaultblock,"<!-- insert transform -->",
		"						<dsig:Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xslt-19991116\">"+
		"							<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">"+
		"								<xsl:include href=\"file:/transform.xsl\"/>"+
		"							</xsl:stylesheet>"+
		"						</dsig:Transform>");
		
		writeFile(reqFile,req);
	
		String resFile = fileprefix + TestNumber + ".ErrRes"+filesuffix;
		
		writeFile(resFile,"2220");
	
	}

	public void create054() throws Exception {

		String TestNumber = "054";
	    String reqFile = fileprefix + TestNumber + ".Req"+filesuffix;
	    
	    String req = replaceString(defaultblock,
	    "<CreateTransformsInfoProfile>"+
		"<CreateTransformsInfo>"+
		"<dsig:Transforms>"+
		"<!-- insert transform -->"+
		"</dsig:Transforms>"+
		"<FinalDataMetaInfo>"+
		"<MimeType>text/html</MimeType>"+
		"<Description>http://www.description.com</Description>"+
		"</FinalDataMetaInfo>"+
		"</CreateTransformsInfo>"+
		"<!-- insert supp -->"+
		"</CreateTransformsInfoProfile>",
	    "<CreateTransformsInfoProfileID>CreateTransformsInfoProfileNonValid</CreateTransformsInfoProfileID>");
	    
		writeFile(reqFile,req);
	
		String resFile = fileprefix + TestNumber + ".ErrRes"+filesuffix;
		
		writeFile(resFile,"2234");
	
	}
	
	public void create055() throws Exception {

		String TestNumber = "055";
	    String reqFile = fileprefix + TestNumber + ".Req"+filesuffix;
	    
	    String req = replaceString(defaultblock,"<!-- insert transform -->",
	   	"<dsig:Transform Algorithm=\"http://www.w3.org/2002/06/xmldsig-filter2\">"+
		"<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">"+
		"<xsl:output method=\"html\" indent=\"no\"/>"+
		"<xsl:template match=\"data:book\" xmlns:data=\"http://uri.data.org\">"+
		"<HTML>"+
		"<BODY>"+
		"<H1>Buch: <xsl:value-of select=\"data:title\"/>"+
		"</H1>"+
		"</BODY>"+
		"</HTML>"+
		"</xsl:template>"+
		"</xsl:stylesheet>"+
		"</dsig:Transform>");
		
		req = replaceString(req,"SecurityLayerConformity=\"false\"","SecurityLayerConformity=\"true\"");
		
		writeFile(reqFile,req);
	
		String resFile = fileprefix + TestNumber + ".ErrRes"+filesuffix;
		
		writeFile(resFile,"2216");
	}

	public void writeFile(String filename,String data) throws Exception
	{
		BufferedWriter bw = new BufferedWriter(new FileWriter(filename));
		bw.write(data);
		bw.close();		
	}
	
	public String makeOutputFile(Document signatureDoc) throws Exception
	{
		ArrayList foundNodes = new ArrayList();
		findNode(signatureDoc.getDocumentElement(),"dsig:Signature",foundNodes);
		
		Node outputNode = null;
		
		if(foundNodes.size()>0)
		{
			outputNode = (Node)foundNodes.get(0);	
		}
		
		String erg = Node2String(outputNode);
	
		// remove the <?xml	?> Prologue
		int pos = erg.indexOf("<dsig");
		erg = erg.substring(pos);
		
		// insert the dsig:signature element in a MOA Response Message
		erg = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
		      "<CreateXMLSignatureResponse" +
		      " xmlns=\"http://reference.e-government.gv.at/namespace/moa/20020822#\""+
		      ">"+
		      //"   xmlns:etsi=\"http://uri.etsi.org/01903/v1.1.1#\">"+
	          "<SignatureEnvironment>" + 
	          erg + "</SignatureEnvironment> </CreateXMLSignatureResponse>";
	          
	    return erg;
	}
}