package at.gv.egiz.param_tests.serialization.html; import java.io.PrintWriter; import java.util.List; import org.apache.commons.lang3.tuple.Pair; import org.apache.pdfbox.preflight.ValidationResult; import org.apache.pdfbox.preflight.ValidationResult.ValidationError; import at.gv.egiz.param_tests.serialization.TestInfoSerializer; import at.gv.egiz.param_tests.testinfo.PDFATestInfo; /** * Concrete test information serializer for PDF-A conformance tests using HTML * and Twitter-bootstrap. * * @author mtappler * */ public class PDFAHTMLSerizalier extends HTMLSerializer { /** * the test information object which is serialized, it is the same as * TestInfoSerializer.baseTestInfo */ private PDFATestInfo testInfo; /** * Default contructor used for registering it as prototype. */ public PDFAHTMLSerizalier() { this(null); } /** * Package protected constructor used for cloning * * @param testInfo */ PDFAHTMLSerizalier(PDFATestInfo testInfo) { this.testInfo = testInfo; } @Override public PDFATestInfo createTestInfo() { testInfo = new PDFATestInfo(); baseTestInfo = testInfo; return testInfo; } @Override public TestInfoSerializer cloneSerializer() { return new PDFAHTMLSerizalier(testInfo); } @Override protected void writeTestData(PrintWriter pw) { pw.println("

Validation status before signing

"); writeValidationResult(pw, testInfo.getResultBeforeSign()); if (testInfo.getResultAfterSign() != null) { pw.println("

Validation status after signing

"); writeValidationResult(pw, testInfo.getResultAfterSign()); } } /** * This method writes the validation result to the given print writer, i.e. * an information about the success of the validation or an exception which * was thrown during the validation or a list of validation errors. * * @param pw * the PrintWriter-object to which the HTML-code is * written * @param validationResult * the pair which defines the result of a validation */ private void writeValidationResult(PrintWriter pw, Pair validationResult) { if (validationResult.getRight() != null) { StringBuilder exceptionString = new StringBuilder(); exceptionString .append("p>An exception happened during the validation process:

"); exceptionString.append(createExceptionDataString(validationResult .getRight())); writePanel(pw, "Validation exception details", exceptionString.toString()); } if (validationResult.getLeft() != null) { StringBuilder conformanceString = new StringBuilder(); if (validationResult.getLeft().isValid()) { conformanceString .append("

The document conforms to the PDF-A standard.

"); } else { List errors = validationResult.getLeft() .getErrorsList(); conformanceString.append("

With to the PDF-A standard, " + "the document contains the following errors:

"); conformanceString .append(""); conformanceString.append(""); conformanceString.append(""); conformanceString.append(""); conformanceString.append(""); conformanceString.append(""); conformanceString.append(""); conformanceString.append(""); for (ValidationError error : errors) { writeValidationError(conformanceString, error); } conformanceString.append("
Error codeError detailsWarning
"); } writePanel(pw, "PDF-A conformance", conformanceString.toString()); } } /** * Helper method for writing a table line for a single validation error. * * @param conformanceString * the string builder to which the table line is appended * @param error * the error which should be serialized */ private void writeValidationError(StringBuilder conformanceString, ValidationError error) { conformanceString.append(""); conformanceString.append(String.format("%s", error.getErrorCode())); conformanceString.append(String.format("%s", error.getDetails())); conformanceString.append(String.format("%s", error.isWarning() ? "x" : "")); conformanceString.append(""); } @Override public String getTestType() { return "PDF-A"; } @Override protected void writeTestSpecificParameters(PrintWriter pw) { // intentionally left blank, no pdf-a specific parameters } }