package eu.europa.esig.dss.validation;

import java.util.List;

import eu.europa.esig.dss.alert.SilentOnStatusAlert;
import eu.europa.esig.dss.spi.x509.CertificateSource;

public class CertificateVerifierBuilder {

	private final CertificateVerifier certificateVerifier;

	public CertificateVerifierBuilder(CertificateVerifier certificateVerifier) {
		this.certificateVerifier = certificateVerifier;
	}

	public CertificateVerifier buildCompleteCopy() {
		CertificateVerifier copy = new CommonCertificateVerifier(true);
		copy.setDefaultDigestAlgorithm(certificateVerifier.getDefaultDigestAlgorithm());
		copy.setDataLoader(certificateVerifier.getDataLoader());
		copy.setCrlSource(certificateVerifier.getCrlSource());
		copy.setOcspSource(certificateVerifier.getOcspSource());
		copy.setCheckRevocationForUntrustedChains(certificateVerifier.isCheckRevocationForUntrustedChains());
		copy.setAdjunctCertSource(certificateVerifier.getAdjunctCertSource());
		List<CertificateSource> trustedCertSources = certificateVerifier.getTrustedCertSources();
		for (CertificateSource certificateSource : trustedCertSources) {
			copy.setTrustedCertSource(certificateSource);
		}
		copy.setAlertOnInvalidTimestamp(certificateVerifier.getAlertOnInvalidTimestamp());
		copy.setAlertOnMissingRevocationData(certificateVerifier.getAlertOnMissingRevocationData());
		copy.setAlertOnNoRevocationAfterBestSignatureTime(certificateVerifier.getAlertOnNoRevocationAfterBestSignatureTime());
		copy.setAlertOnRevokedCertificate(certificateVerifier.getAlertOnRevokedCertificate());
		copy.setAlertOnUncoveredPOE(certificateVerifier.getAlertOnUncoveredPOE());
		return copy;
	}
	
	public CertificateVerifier buildOfflineAndSilentCopy() {
		CertificateVerifier offlineCertificateVerifier = new CommonCertificateVerifier(true);
		offlineCertificateVerifier.setDefaultDigestAlgorithm(certificateVerifier.getDefaultDigestAlgorithm());
		offlineCertificateVerifier.setAdjunctCertSource(certificateVerifier.getAdjunctCertSource());
		List<CertificateSource> trustedCertSources = certificateVerifier.getTrustedCertSources();
		for (CertificateSource certificateSource : trustedCertSources) {
			offlineCertificateVerifier.setTrustedCertSource(certificateSource);
		}
		// disable alerting
		offlineCertificateVerifier.setAlertOnInvalidTimestamp(new SilentOnStatusAlert());
		offlineCertificateVerifier.setAlertOnMissingRevocationData(new SilentOnStatusAlert());
		offlineCertificateVerifier.setAlertOnNoRevocationAfterBestSignatureTime(new SilentOnStatusAlert());
		offlineCertificateVerifier.setAlertOnRevokedCertificate(new SilentOnStatusAlert());
		offlineCertificateVerifier.setAlertOnUncoveredPOE(new SilentOnStatusAlert());
		return offlineCertificateVerifier;
	}
	
}

@@ -513,16 +513,54 @@
 (true);%0A
+%09%09if (certificateVerifier != null) %7B%0A%09
 %09%09copy.s
@@ -630,24 +630,25 @@
 gorithm());%0A
+%09
 %09%09copy.setDa
@@ -692,24 +692,25 @@
 oader());%0A%09%09
+%09
 copy.setCrlS
@@ -744,32 +744,33 @@
 etCrlSource());%0A
+%09
 %09%09copy.setOcspSo
@@ -806,32 +806,33 @@
 cspSource());%0A%09%09
+%09
 copy.setCheckRev
@@ -913,24 +913,25 @@
 dChains());%0A
+%09
 %09%09copy.setAd
@@ -985,32 +985,33 @@
 ertSource());%0A%09%09
+%09
 List%3CCertificate
@@ -1078,32 +1078,33 @@
 ertSources();%0A%09%09
+%09
 for (Certificate
@@ -1148,24 +1148,25 @@
 tSources) %7B%0A
+%09
 %09%09%09copy.setT
@@ -1204,18 +1204,20 @@
 rce);%0A%09%09
+%09
 %7D%0A
+%09
 %09%09copy.s
@@ -1289,24 +1289,25 @@
 mestamp());%0A
+%09
 %09%09copy.setAl
@@ -1387,24 +1387,25 @@
 nData());%0A%09%09
+%09
 copy.setAler
@@ -1505,32 +1505,33 @@
 atureTime());%0A%09%09
+%09
 copy.setAlertOnR
@@ -1601,16 +1601,17 @@
 ate());%0A
+%09
 %09%09copy.s
@@ -1675,24 +1675,28 @@
 eredPOE());%0A
+%09%09%7D%0A
 %09%09return cop
@@ -1849,16 +1849,54 @@
 (true);%0A
+%09%09if (certificateVerifier != null) %7B%0A%09
 %09%09offlin
@@ -1990,24 +1990,25 @@
 rithm());%0A%09%09
+%09
 offlineCerti
@@ -2090,16 +2090,17 @@
 e());%0A%09%09
+%09
 List%3CCer
@@ -2181,16 +2181,17 @@
 rces();%0A
+%09
 %09%09for (C
@@ -2248,24 +2248,25 @@
 urces) %7B%0A%09%09%09
+%09
 offlineCerti
@@ -2321,16 +2321,21 @@
 ource);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%09//

Add null check (cv can be null in case of DocumentValidator.getSignatures())