From ffd1e0da6b73e2737f5cad0a6d3e82dbc3de206f Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Fri, 29 Aug 2014 15:09:54 +0200 Subject: Integrated PDF-AS Testing library --- .../serialization/html/PDFAHTMLSerizalier.java | 147 +++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 pdf-as-tests/src/test/java/at/gv/egiz/param_tests/serialization/html/PDFAHTMLSerizalier.java (limited to 'pdf-as-tests/src/test/java/at/gv/egiz/param_tests/serialization/html/PDFAHTMLSerizalier.java') diff --git a/pdf-as-tests/src/test/java/at/gv/egiz/param_tests/serialization/html/PDFAHTMLSerizalier.java b/pdf-as-tests/src/test/java/at/gv/egiz/param_tests/serialization/html/PDFAHTMLSerizalier.java new file mode 100644 index 00000000..fdaa80fa --- /dev/null +++ b/pdf-as-tests/src/test/java/at/gv/egiz/param_tests/serialization/html/PDFAHTMLSerizalier.java @@ -0,0 +1,147 @@ +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 + } + +} -- cgit v1.2.3