Skip to content

Commit

Permalink
Convert existing tabs to spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
runeflobakk committed Jan 11, 2017
1 parent bb4607f commit 5aafde8
Show file tree
Hide file tree
Showing 10 changed files with 959 additions and 959 deletions.
578 changes: 289 additions & 289 deletions pom.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,39 @@
*/
class EnhancedNonSequentialPDFParser extends NonSequentialPDFParser implements AutoCloseable {

static {
// Ensures that the parser does not read the entire PDF to memory.
System.setProperty(SYSPROP_PARSEMINIMAL, "true");
}
static {
// Ensures that the parser does not read the entire PDF to memory.
System.setProperty(SYSPROP_PARSEMINIMAL, "true");
}

EnhancedNonSequentialPDFParser(InputStream in) throws IOException {
super(in);
super.initialParse();
}
EnhancedNonSequentialPDFParser(InputStream in) throws IOException {
super(in);
super.initialParse();
}

public int getNumberOfPages() throws IOException {
return super.getPageNumber();
}
public int getNumberOfPages() throws IOException {
return super.getPageNumber();
}

public boolean isEncrypted() {
return super.getSecurityHandler() != null;
}
public boolean isEncrypted() {
return super.getSecurityHandler() != null;
}

@Override
public PDPage getPage(int pageNr) throws IOException {
// Releases memory regularly
if (pageNr % 5 == 0) {
Set<COSObjectKey> cosObjectKeys = super.xrefTrailerResolver.getXrefTable().keySet();
for (COSObjectKey cosObjectKey : cosObjectKeys) {
super.getDocument().removeObject(cosObjectKey);
}
}
return super.getPage(pageNr);
}
@Override
public PDPage getPage(int pageNr) throws IOException {
// Releases memory regularly
if (pageNr % 5 == 0) {
Set<COSObjectKey> cosObjectKeys = super.xrefTrailerResolver.getXrefTable().keySet();
for (COSObjectKey cosObjectKey : cosObjectKeys) {
super.getDocument().removeObject(cosObjectKey);
}
}
return super.getPage(pageNr);
}

@Override
public void close() {
this.clearResources();
}
@Override
public void close() {
this.clearResources();
}

}
144 changes: 72 additions & 72 deletions src/main/java/no/digipost/print/validate/PdfFontValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,85 +29,85 @@

class PdfFontValidator {

// Standard Type 1 Fonts (Standard 14 Fonts) -
// http://en.wikipedia.org/wiki/Portable_Document_Format#Fonts
// Times (v3) (in regular, italic, bold, and bold italic)
// Courier (final in regular, oblique, bold and bold oblique)
// Helvetica (v3) (in regular, oblique, bold and bold oblique)
// Symbol
// Zapf Dingbats
private static final Set<String> STANDARD_14_FONTS = new HashSet<>(asList("TIMES", "COURIER", "HELVETICA", "SYMBOL", "ZAPFDINGBATS"));
// Standard Type 1 Fonts (Standard 14 Fonts) -
// http://en.wikipedia.org/wiki/Portable_Document_Format#Fonts
// Times (v3) (in regular, italic, bold, and bold italic)
// Courier (final in regular, oblique, bold and bold oblique)
// Helvetica (v3) (in regular, oblique, bold and bold oblique)
// Symbol
// Zapf Dingbats
private static final Set<String> STANDARD_14_FONTS = new HashSet<>(asList("TIMES", "COURIER", "HELVETICA", "SYMBOL", "ZAPFDINGBATS"));

private static final Set<String> WHITE_LISTED_FONTS = new HashSet<>(asList("ARIAL"));
private static final Set<String> WHITE_LISTED_FONTS = new HashSet<>(asList("ARIAL"));

private static final Set<String> SUPPORTED_FONTS = new HashSet<>();
private static final Set<String> SUPPORTED_FONTS = new HashSet<>();

static {
SUPPORTED_FONTS.addAll(STANDARD_14_FONTS);
SUPPORTED_FONTS.addAll(WHITE_LISTED_FONTS);
}
static {
SUPPORTED_FONTS.addAll(STANDARD_14_FONTS);
SUPPORTED_FONTS.addAll(WHITE_LISTED_FONTS);
}

public Collection<PDFont> getPageFonts(PDPage page) throws IOException {
PDResources resources = page.getResources();
if (resources != null) {
Map<String, PDFont> fontMap = resources.getFonts();
return fontMap.values();
}
return emptySet();
}
public Collection<PDFont> getPageFonts(PDPage page) throws IOException {
PDResources resources = page.getResources();
if (resources != null) {
Map<String, PDFont> fontMap = resources.getFonts();
return fontMap.values();
}
return emptySet();
}

public List<PDFont> findNonSupportedFonts(Iterable<PDFont> fonter) {
List<PDFont> nonSupported = new ArrayList<>();
for (PDFont font : fonter) {
PDFontDescriptor fontDescriptor = font.getFontDescriptor();
if (fontDescriptor != null) {
if (!erFontDescriptorAkseptabelForPrint(fontDescriptor)) {
nonSupported.add(font);
}
} else {
if (!(font instanceof PDType0Font)) {
if (!erAkseptabelForPrint(font.getBaseFont())) {
nonSupported.add(font);
}
}
}
}
return unmodifiableList(nonSupported);
}
public List<PDFont> findNonSupportedFonts(Iterable<PDFont> fonter) {
List<PDFont> nonSupported = new ArrayList<>();
for (PDFont font : fonter) {
PDFontDescriptor fontDescriptor = font.getFontDescriptor();
if (fontDescriptor != null) {
if (!erFontDescriptorAkseptabelForPrint(fontDescriptor)) {
nonSupported.add(font);
}
} else {
if (!(font instanceof PDType0Font)) {
if (!erAkseptabelForPrint(font.getBaseFont())) {
nonSupported.add(font);
}
}
}
}
return unmodifiableList(nonSupported);
}

private boolean erFontDescriptorAkseptabelForPrint(PDFontDescriptor fontDescriptor) {
if (fontDescriptor instanceof PDFontDescriptorDictionary) {
PDFontDescriptorDictionary pdFontDescriptorDictionary = (PDFontDescriptorDictionary) fontDescriptor;
if (harIkkeEmbeddedFont(pdFontDescriptorDictionary)) {
return erAkseptabelForPrint(pdFontDescriptorDictionary.getFontName());
} else {
return true;
}
} else if (fontDescriptor instanceof PDFontDescriptorAFM) {
PDFontDescriptorAFM fontDescriptorAFM = (PDFontDescriptorAFM) fontDescriptor;
return erAkseptabelForPrint(fontDescriptorAFM.getFontName());
} else {
throw new IllegalArgumentException("Ukjent font descriptor brukt : " + fontDescriptor.getClass());
}
}
private boolean erFontDescriptorAkseptabelForPrint(PDFontDescriptor fontDescriptor) {
if (fontDescriptor instanceof PDFontDescriptorDictionary) {
PDFontDescriptorDictionary pdFontDescriptorDictionary = (PDFontDescriptorDictionary) fontDescriptor;
if (harIkkeEmbeddedFont(pdFontDescriptorDictionary)) {
return erAkseptabelForPrint(pdFontDescriptorDictionary.getFontName());
} else {
return true;
}
} else if (fontDescriptor instanceof PDFontDescriptorAFM) {
PDFontDescriptorAFM fontDescriptorAFM = (PDFontDescriptorAFM) fontDescriptor;
return erAkseptabelForPrint(fontDescriptorAFM.getFontName());
} else {
throw new IllegalArgumentException("Ukjent font descriptor brukt : " + fontDescriptor.getClass());
}
}

private boolean erAkseptabelForPrint(String fontnavn) {
if (fontnavn == null) {
return false;
}
String normalisertFontnavn = upperCase(deleteWhitespace(remove(fontnavn, "-")));
for (String supportertFontnavn : SUPPORTED_FONTS) {
if (normalisertFontnavn.contains(supportertFontnavn)) {
return true;
}
}
return false;
}
private boolean erAkseptabelForPrint(String fontnavn) {
if (fontnavn == null) {
return false;
}
String normalisertFontnavn = upperCase(deleteWhitespace(remove(fontnavn, "-")));
for (String supportertFontnavn : SUPPORTED_FONTS) {
if (normalisertFontnavn.contains(supportertFontnavn)) {
return true;
}
}
return false;
}

private boolean harIkkeEmbeddedFont(PDFontDescriptorDictionary fontDescriptor) {
return fontDescriptor.getFontFile() == null &&
fontDescriptor.getFontFile2() == null &&
fontDescriptor.getFontFile3() == null;
}
private boolean harIkkeEmbeddedFont(PDFontDescriptorDictionary fontDescriptor) {
return fontDescriptor.getFontFile() == null &&
fontDescriptor.getFontFile2() == null &&
fontDescriptor.getFontFile3() == null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

public enum PdfValidateStrategy {

FULLY_IN_MEMORY,
NON_SEQUENTIALLY;
FULLY_IN_MEMORY,
NON_SEQUENTIALLY;

}
68 changes: 34 additions & 34 deletions src/main/java/no/digipost/print/validate/PdfValidationError.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,46 +23,46 @@

public enum PdfValidationError {

PDF_IS_ENCRYPTED("The PDF document is encrypted."),
TOO_MANY_PAGES_FOR_AUTOMATED_PRINT("The PDF document contains too many pages."),
UNSUPPORTED_PDF_VERSION_FOR_PRINT("The version of the PDF document is not supported. Supported versions are "
+ StringUtils.join(PdfValidator.PDF_VERSIONS_SUPPORTED_FOR_PRINT, ", ") + "."),
INSUFFICIENT_MARGIN_FOR_PRINT("The left margin of the PDF document is too narrow. Minimum left margin is " + PdfValidator.BARCODE_AREA_WIDTH_MM
+ " mm."),
UNABLE_TO_VERIFY_SUITABLE_MARGIN_FOR_PRINT("Could not verify the left margin of the PDF document. Minimum left margin is "
+ PdfValidator.BARCODE_AREA_WIDTH_MM + " mm."),
PDF_PARSE_ERROR("Could not parse the PDF document."),
PDF_PARSE_PAGE_ERROR("Could not parse at least one of the pages in the PDF document"),
UNSUPPORTED_DIMENSIONS("The dimensions of the PDF document are not supported. Supported dimensions are A4 (" + PdfValidator.A4_WIDTH_MM + " mm x "
+ PdfValidator.A4_HEIGHT_MM + " mm). For flexibility, we allow smaller sizes down to a limit of " + PdfValidator.ALLOWED_NEGATIVE_DEVIATION_FROM_PDF_STANDARD_DIMENSIONS_MM + " mm for both dimensions"),
REFERENCES_INVALID_FONT("The document refers to a non-standard font that is not included in the PDF."),
DOCUMENT_TOO_SMALL("The PDF document size is too small."),
INVALID_PDF("The PDF document is invalid."),
DOCUMENT_HAS_NO_PAGES("The PDF document does not contain any pages. The file may be corrupt.");
PDF_IS_ENCRYPTED("The PDF document is encrypted."),
TOO_MANY_PAGES_FOR_AUTOMATED_PRINT("The PDF document contains too many pages."),
UNSUPPORTED_PDF_VERSION_FOR_PRINT("The version of the PDF document is not supported. Supported versions are "
+ StringUtils.join(PdfValidator.PDF_VERSIONS_SUPPORTED_FOR_PRINT, ", ") + "."),
INSUFFICIENT_MARGIN_FOR_PRINT("The left margin of the PDF document is too narrow. Minimum left margin is " + PdfValidator.BARCODE_AREA_WIDTH_MM
+ " mm."),
UNABLE_TO_VERIFY_SUITABLE_MARGIN_FOR_PRINT("Could not verify the left margin of the PDF document. Minimum left margin is "
+ PdfValidator.BARCODE_AREA_WIDTH_MM + " mm."),
PDF_PARSE_ERROR("Could not parse the PDF document."),
PDF_PARSE_PAGE_ERROR("Could not parse at least one of the pages in the PDF document"),
UNSUPPORTED_DIMENSIONS("The dimensions of the PDF document are not supported. Supported dimensions are A4 (" + PdfValidator.A4_WIDTH_MM + " mm x "
+ PdfValidator.A4_HEIGHT_MM + " mm). For flexibility, we allow smaller sizes down to a limit of " + PdfValidator.ALLOWED_NEGATIVE_DEVIATION_FROM_PDF_STANDARD_DIMENSIONS_MM + " mm for both dimensions"),
REFERENCES_INVALID_FONT("The document refers to a non-standard font that is not included in the PDF."),
DOCUMENT_TOO_SMALL("The PDF document size is too small."),
INVALID_PDF("The PDF document is invalid."),
DOCUMENT_HAS_NO_PAGES("The PDF document does not contain any pages. The file may be corrupt.");

static final Set<PdfValidationError> OK_FOR_PRINT = Collections.emptySet();
static final Set<PdfValidationError> OK_FOR_WEB = EnumSet.of(
PDF_IS_ENCRYPTED, TOO_MANY_PAGES_FOR_AUTOMATED_PRINT, UNSUPPORTED_PDF_VERSION_FOR_PRINT, INSUFFICIENT_MARGIN_FOR_PRINT,
UNABLE_TO_VERIFY_SUITABLE_MARGIN_FOR_PRINT, UNSUPPORTED_DIMENSIONS, PDF_PARSE_PAGE_ERROR);
static final Set<PdfValidationError> OK_FOR_PRINT = Collections.emptySet();
static final Set<PdfValidationError> OK_FOR_WEB = EnumSet.of(
PDF_IS_ENCRYPTED, TOO_MANY_PAGES_FOR_AUTOMATED_PRINT, UNSUPPORTED_PDF_VERSION_FOR_PRINT, INSUFFICIENT_MARGIN_FOR_PRINT,
UNABLE_TO_VERIFY_SUITABLE_MARGIN_FOR_PRINT, UNSUPPORTED_DIMENSIONS, PDF_PARSE_PAGE_ERROR);


public final String message;
public final String message;

PdfValidationError(String message) {
this.message = message;
}
PdfValidationError(String message) {
this.message = message;
}

public boolean isOkForWeb() {
return OK_FOR_WEB.contains(this);
}
public boolean isOkForWeb() {
return OK_FOR_WEB.contains(this);
}

public boolean isOkForPrint() {
return OK_FOR_PRINT.contains(this);
}
public boolean isOkForPrint() {
return OK_FOR_PRINT.contains(this);
}

@Override
public String toString() {
return message;
}
@Override
public String toString() {
return message;
}

}
76 changes: 38 additions & 38 deletions src/main/java/no/digipost/print/validate/PdfValidationResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,42 @@

public final class PdfValidationResult {

public static final PdfValidationResult EVERYTHING_OK = new PdfValidationResult(Collections.<PdfValidationError>emptyList(), -1);

public final List<PdfValidationError> errors;
public final boolean okForPrint;
public final boolean okForWeb;
public final int pages;


PdfValidationResult(List<PdfValidationError> errors, int pages) {
this.pages = pages;
this.errors = errors != null ? unmodifiableList(errors) : Collections.<PdfValidationError>emptyList();
this.okForPrint = PdfValidationError.OK_FOR_PRINT.containsAll(this.errors);
this.okForWeb = PdfValidationError.OK_FOR_WEB.containsAll(this.errors);
}

public boolean hasErrors() {
return !errors.isEmpty();
}



private String toStringValue;

@Override
public String toString() {
if (toStringValue == null) {
StringBuilder sb = new StringBuilder("[");
sb.append(getClass().getSimpleName());
for (PdfValidationError printPdfValideringsFeil : errors) {
PdfValidationError err = printPdfValideringsFeil;
sb.append(" ");
sb.append(err);
}
sb.append("]");
toStringValue = sb.toString();
}
return toStringValue;
}
public static final PdfValidationResult EVERYTHING_OK = new PdfValidationResult(Collections.<PdfValidationError>emptyList(), -1);

public final List<PdfValidationError> errors;
public final boolean okForPrint;
public final boolean okForWeb;
public final int pages;


PdfValidationResult(List<PdfValidationError> errors, int pages) {
this.pages = pages;
this.errors = errors != null ? unmodifiableList(errors) : Collections.<PdfValidationError>emptyList();
this.okForPrint = PdfValidationError.OK_FOR_PRINT.containsAll(this.errors);
this.okForWeb = PdfValidationError.OK_FOR_WEB.containsAll(this.errors);
}

public boolean hasErrors() {
return !errors.isEmpty();
}



private String toStringValue;

@Override
public String toString() {
if (toStringValue == null) {
StringBuilder sb = new StringBuilder("[");
sb.append(getClass().getSimpleName());
for (PdfValidationError printPdfValideringsFeil : errors) {
PdfValidationError err = printPdfValideringsFeil;
sb.append(" ");
sb.append(err);
}
sb.append("]");
toStringValue = sb.toString();
}
return toStringValue;
}
}
Loading

0 comments on commit 5aafde8

Please sign in to comment.