package eu.europa.esig.dss.cookbook.example;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.net.URL;

import eu.europa.esig.dss.DSSDocument;
import eu.europa.esig.dss.DSSException;
import eu.europa.esig.dss.FileDocument;
import eu.europa.esig.dss.SignatureAlgorithm;
import eu.europa.esig.dss.test.gen.CertificateService;
import eu.europa.esig.dss.test.mock.MockTSPSource;
import eu.europa.esig.dss.token.AbstractSignatureTokenConnection;
import eu.europa.esig.dss.token.DSSPrivateKeyEntry;
import eu.europa.esig.dss.token.Pkcs12SignatureToken;
import eu.europa.esig.dss.validation.CommonCertificateVerifier;
import eu.europa.esig.dss.validation.SignedDocumentValidator;
import eu.europa.esig.dss.validation.report.DiagnosticData;
import eu.europa.esig.dss.validation.report.Reports;

public class CookbookTools {

	/**
	 * The document to sign
	 */
	static protected DSSDocument toSignDocument;

	/**
	 * The document to extend
	 */
	static protected DSSDocument toExtendDocument;

	/**
	 * The object which is in charge of digesting and encrypting the data to
	 * sign.
	 */
	static protected AbstractSignatureTokenConnection signingToken;

	/**
	 * This object contains the private key associated to the signing
	 * certificate.
	 */
	static protected DSSPrivateKeyEntry privateKey;

	/**
	 * This method converts the resource path to the absolute path.
	 *
	 * @param resourcePath
	 *            resource path
	 * @return
	 */
	public static String getPathFromResource(final String resourcePath) {

		URL uri = Cookbook.class.getResource(resourcePath);
		String absolutePath = uri.getPath();
		return absolutePath;
	}

	/**
	 * This method sets the common parameters.
	 */
	protected static void prepareXmlDoc() {
		String toSignFilePath = getPathFromResource("/xml_example.xml");
		toSignDocument = new FileDocument(toSignFilePath);
	}

	/**
	 * This method sets the common parameters.
	 */
	protected static void preparePdfDoc() {
		String toSignFilePath = getPathFromResource("/hello-world.pdf");
		toSignDocument = new FileDocument(toSignFilePath);
	}

	/**
	 * This method sets the common parameters.
	 */
	protected static void preparePKCS12TokenAndKey() {
		String pkcs12TokenFile = getPathFromResource("/user_a_rsa.p12");
		signingToken = new Pkcs12SignatureToken("password", pkcs12TokenFile);
		privateKey = signingToken.getKeys().get(0);
	}

	protected static MockTSPSource getMockTSPSource() throws DSSException, Exception {
		return new MockTSPSource(new CertificateService().generateTspCertificate(SignatureAlgorithm.RSA_SHA256));
	}

	protected void testFinalDocument(DSSDocument signedDocument) {
		assertNotNull(signedDocument);
		assertNotNull(signedDocument.getBytes());

		SignedDocumentValidator validator = SignedDocumentValidator.fromDocument(signedDocument);
		validator.setCertificateVerifier(new CommonCertificateVerifier());
		Reports reports = validator.validateDocument();
		assertNotNull(reports);
		
		DiagnosticData diagnosticData = reports.getDiagnosticData();
		assertTrue(diagnosticData.isBLevelTechnicallyValid(diagnosticData.getFirstSignatureId()));
	}

}

@@ -145,15 +145,15 @@
 ava.
-net.URL
+io.File
 ;%0A%0Ai
@@ -1340,344 +1340,8 @@
 y;%0A%0A
-%09/**%0A%09 * This method converts the resource path to the absolute path.%0A%09 *%0A%09 * @param resourcePath%0A%09 *            resource path%0A%09 * @return%0A%09 */%0A%09public static String getPathFromResource(final String resourcePath) %7B%0A%0A%09%09URL uri = Cookbook.class.getResource(resourcePath);%0A%09%09String absolutePath = uri.getPath();%0A%09%09return absolutePath;%0A%09%7D%0A%0A
 %09/**
@@ -1437,53 +1437,70 @@
 %7B%0A%09%09
-String toSignFilePath = getPathFromR
+toSignDocument = new FileDocument(new File(%22src/main/r
 esource
-(%22
+s
 /xml
@@ -1517,60 +1517,8 @@
 ml%22)
-;%0A%09%09toSignDocument = new FileDocument(toSignFilePath
 );%0A%09
@@ -1621,123 +1621,88 @@
 %7B%0A%09%09
-String toSignFilePath = getPathFromResource(%22/hello-world.pdf%22);%0A%09%09toSignDocument = new FileDocument(toSignFilePath
+toSignDocument = new FileDocument(new File(%22src/main/resources/hello-world.pdf%22)
 );%0A%09
@@ -1814,75 +1814,8 @@
 ) %7B%0A
-%09%09String pkcs12TokenFile = getPathFromResource(%22/user_a_rsa.p12%22);%0A
 %09%09si
@@ -1868,23 +1868,43 @@
 d%22, 
-pkcs12TokenFile
+%22src/main/resources/user_a_rsa.p12%22
 );%0A%09
@@ -2527,18 +2527,16 @@
 ports);%0A
-%09%09
 %0A%09%09Diagn

Fix paths