Skip to content

Commit e060dce

Browse files
EvgenyB1001Ubuntu
authored andcommitted
Remove anonymous classes usages from main (non test) code
Cover new lines with tests DEVSIX-4923
1 parent 44a2fc3 commit e060dce

File tree

15 files changed

+1374
-646
lines changed

15 files changed

+1374
-646
lines changed

kernel/src/main/java/com/itextpdf/kernel/pdf/canvas/parser/listener/LocationTextExtractionStrategy.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,7 @@ public class LocationTextExtractionStrategy implements ITextExtractionStrategy {
8282
* Creates a new text extraction renderer.
8383
*/
8484
public LocationTextExtractionStrategy() {
85-
this(new ITextChunkLocationStrategy() {
86-
public ITextChunkLocation createLocation(TextRenderInfo renderInfo, LineSegment baseline) {
87-
return new TextChunkLocationDefaultImp(baseline.getStartPoint(), baseline.getEndPoint(), renderInfo.getSingleSpaceWidth());
88-
}
89-
});
85+
this(new ITextChunkLocationStrategyImpl());
9086
}
9187

9288
/**
@@ -349,4 +345,14 @@ private static class TextChunkMarks {
349345
List<TextChunk> succeeding = new ArrayList<>();
350346
}
351347

348+
private static final class ITextChunkLocationStrategyImpl
349+
implements LocationTextExtractionStrategy.ITextChunkLocationStrategy {
350+
351+
@Override
352+
public ITextChunkLocation createLocation(TextRenderInfo renderInfo, LineSegment baseline) {
353+
return new TextChunkLocationDefaultImp(baseline.getStartPoint(), baseline.getEndPoint(),
354+
renderInfo.getSingleSpaceWidth());
355+
}
356+
}
357+
352358
}

kernel/src/main/java/com/itextpdf/kernel/pdf/canvas/parser/listener/RegexBasedLocationExtractionStrategy.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ This file is part of the iText (R) project.
6262
* This class is designed to search for the occurrences of a regular expression and return the resultant rectangles.
6363
*/
6464
public class RegexBasedLocationExtractionStrategy implements ILocationExtractionStrategy {
65-
65+
private static final float EPS = 1.0E-4F;
6666
private Pattern pattern;
6767
private List<CharacterRenderInfo> parseResult = new ArrayList<>();
6868

@@ -100,18 +100,7 @@ public Collection<IPdfTextLocation> getResultantLocations() {
100100
* This is to ensure that tests that use this functionality (for instance to generate pdf with
101101
* areas of interest highlighted) will not break when compared.
102102
*/
103-
java.util.Collections.sort(retval, new Comparator<IPdfTextLocation>() {
104-
@Override
105-
public int compare(IPdfTextLocation l1, IPdfTextLocation l2) {
106-
Rectangle o1 = l1.getRectangle();
107-
Rectangle o2 = l2.getRectangle();
108-
if (o1.getY() == o2.getY()) {
109-
return o1.getX() == o2.getX() ? 0 : (o1.getX() < o2.getX() ? -1 : 1);
110-
} else {
111-
return o1.getY() < o2.getY() ? -1 : 1;
112-
}
113-
}
114-
});
103+
Collections.sort(retval, new PdfTextLocationComparator());
115104

116105
// ligatures can produces same rectangle
117106
removeDuplicates(retval);
@@ -214,4 +203,19 @@ private static Integer getEndIndex(Map<Integer, Integer> indexMap, int index) {
214203
}
215204
return indexMap.get(index);
216205
}
206+
207+
private static final class PdfTextLocationComparator
208+
implements Comparator<com.itextpdf.kernel.pdf.canvas.parser.listener.IPdfTextLocation> {
209+
@Override
210+
public int compare(com.itextpdf.kernel.pdf.canvas.parser.listener.IPdfTextLocation l1,
211+
com.itextpdf.kernel.pdf.canvas.parser.listener.IPdfTextLocation l2) {
212+
Rectangle o1 = l1.getRectangle();
213+
Rectangle o2 = l2.getRectangle();
214+
if (Math.abs(o1.getY() - o2.getY()) < EPS) {
215+
return Math.abs(o1.getX() - o2.getX()) < EPS ? 0 : ((o2.getX() - o1.getX()) > EPS ? -1 : 1);
216+
} else {
217+
return (o2.getY() - o1.getY()) > EPS ? -1 : 1;
218+
}
219+
}
220+
}
217221
}

kernel/src/main/java/com/itextpdf/kernel/utils/PdfSplitter.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,7 @@ public void splitByPageNumbers(List<Integer> pageNumbers, IDocumentReadyListener
169169
public List<PdfDocument> splitByPageNumbers(List<Integer> pageNumbers) {
170170
final List<PdfDocument> splitDocuments = new ArrayList<>();
171171

172-
splitByPageNumbers(pageNumbers, new IDocumentReadyListener() {
173-
@Override
174-
public void documentReady(PdfDocument pdfDocument, PageRange pageRange) {
175-
splitDocuments.add(pdfDocument);
176-
}
177-
});
172+
splitByPageNumbers(pageNumbers, new SplitReadyListener(splitDocuments));
178173

179174
return splitDocuments;
180175
}
@@ -206,12 +201,7 @@ public void splitByPageCount(int pageCount, IDocumentReadyListener documentReady
206201
public List<PdfDocument> splitByPageCount(int pageCount) {
207202
final List<PdfDocument> splitDocuments = new ArrayList<>();
208203

209-
splitByPageCount(pageCount, new IDocumentReadyListener() {
210-
@Override
211-
public void documentReady(PdfDocument pdfDocument, PageRange pageRange) {
212-
splitDocuments.add(pdfDocument);
213-
}
214-
});
204+
splitByPageCount(pageCount, new SplitReadyListener(splitDocuments));
215205

216206
return splitDocuments;
217207
}
@@ -408,4 +398,18 @@ private PageRange getNextRange(int startPage, int endPage, long size) {
408398
private long xrefLength(int size) {
409399
return 20L * (size + 1);
410400
}
401+
402+
private static final class SplitReadyListener implements IDocumentReadyListener {
403+
404+
private List<PdfDocument> splitDocuments;
405+
406+
public SplitReadyListener(List<PdfDocument> splitDocuments) {
407+
this.splitDocuments = splitDocuments;
408+
}
409+
410+
@Override
411+
public void documentReady(PdfDocument pdfDocument, PageRange pageRange) {
412+
splitDocuments.add(pdfDocument);
413+
}
414+
}
411415
}

kernel/src/main/java/com/itextpdf/kernel/xmp/XMPMetaFactory.java

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -262,43 +262,59 @@ public static XMPVersionInfo getVersionInfo() {
262262
// Adobe XMP Core 5.0-jc001 DEBUG-<branch>.<changelist>, 2009 Jan 28 15:22:38-CET
263263
final String message = "Adobe XMP Core 5.1.0-jc003";
264264

265+
versionInfo = new XMPVersionInfoImpl(major, minor, micro, engBuild, debug, message);
266+
} catch (Throwable e) {
267+
// empty, severe error would be detected during the tests
268+
System.out.println(e);
269+
}
270+
}
271+
return versionInfo;
272+
}
273+
}
265274

266-
versionInfo = new XMPVersionInfo() {
267-
public int getMajor() {
268-
return major;
269-
}
275+
private static final class XMPVersionInfoImpl implements XMPVersionInfo {
276+
private final int major;
277+
private final int minor;
278+
private final int micro;
279+
private final int engBuild;
280+
private final boolean debug;
281+
private final String message;
282+
283+
public XMPVersionInfoImpl(int major, int minor, int micro, int engBuild, boolean debug, String message) {
284+
this.major = major;
285+
this.minor = minor;
286+
this.micro = micro;
287+
this.engBuild = engBuild;
288+
this.debug = debug;
289+
this.message = message;
290+
}
270291

271-
public int getMinor() {
272-
return minor;
273-
}
292+
public int getMajor() {
293+
return major;
294+
}
274295

275-
public int getMicro() {
276-
return micro;
277-
}
296+
public int getMinor() {
297+
return minor;
298+
}
278299

279-
public boolean isDebug() {
280-
return debug;
281-
}
300+
public int getMicro() {
301+
return micro;
302+
}
282303

283-
public int getBuild() {
284-
return engBuild;
285-
}
304+
public boolean isDebug() {
305+
return debug;
306+
}
286307

287-
public String getMessage() {
288-
return message;
289-
}
308+
public int getBuild() {
309+
return engBuild;
310+
}
290311

291-
public String toString() {
292-
return message;
293-
}
294-
};
312+
public String getMessage() {
313+
return message;
314+
}
295315

296-
} catch (Throwable e) {
297-
// EMTPY, severe error would be detected during the tests
298-
System.out.println(e);
299-
}
300-
}
301-
return versionInfo;
316+
public String toString() {
317+
return message;
302318
}
303319
}
304320
}

kernel/src/test/java/com/itextpdf/kernel/pdf/canvas/parser/listener/RegexBasedLocationExtractionStrategyTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,4 +333,16 @@ public void regexWithOnlyWhiteSpace() throws IOException {
333333

334334
Assert.assertEquals(0, locations.size());
335335
}
336+
337+
@Test
338+
public void sortCompareTest() throws IOException {
339+
try (PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "sortCompare.pdf"))) {
340+
RegexBasedLocationExtractionStrategy extractionStrategy = new RegexBasedLocationExtractionStrategy("a");
341+
PdfCanvasProcessor pdfCanvasProcessor = new PdfCanvasProcessor(extractionStrategy);
342+
pdfCanvasProcessor.processPageContent(pdfDocument.getPage(1));
343+
pdfCanvasProcessor.processPageContent(pdfDocument.getPage(2));
344+
List<IPdfTextLocation> locations = new ArrayList<>(extractionStrategy.getResultantLocations());
345+
Assert.assertEquals(13, locations.size());
346+
}
347+
}
336348
}

kernel/src/test/java/com/itextpdf/kernel/utils/PdfSplitterTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,26 @@ protected PdfWriter getNextPdfWriter(PageRange documentPageRange) {
256256
sourceFolder + "cmp/" + "cmp_splitBySize_part" + i + ".pdf", destinationFolder, "diff_"));
257257
}
258258
}
259+
260+
@Test
261+
@LogMessages(messages = {
262+
@LogMessage(messageTemplate = IoLogMessageConstant.SOURCE_DOCUMENT_HAS_ACROFORM_DICTIONARY , count = 10)
263+
})
264+
public void splitByPageCountTest() throws IOException {
265+
String inputFileName = sourceFolder + "iphone_user_guide.pdf";
266+
try (PdfDocument inputPdfDoc = new PdfDocument(new PdfReader(inputFileName))) {
267+
PdfSplitter splitter = new PdfSplitter(inputPdfDoc);
268+
269+
int pagesCount = inputPdfDoc.getNumberOfPages();
270+
int pagesCountInSplitDoc = 13;
271+
272+
List<PdfDocument> splitDocuments = splitter.splitByPageCount(pagesCountInSplitDoc);
273+
274+
for (PdfDocument doc : splitDocuments) {
275+
doc.close();
276+
}
277+
278+
Assert.assertEquals(pagesCount / pagesCountInSplitDoc, splitDocuments.size());
279+
}
280+
}
259281
}

layout/src/main/java/com/itextpdf/layout/renderer/ListRenderer.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -219,16 +219,7 @@ private IRenderer createListSymbolRenderer(int index, IRenderer renderer) {
219219
final String constantFont = (numberingType == ListNumberingType.GREEK_LOWER || numberingType == ListNumberingType.GREEK_UPPER) ?
220220
StandardFonts.SYMBOL : StandardFonts.ZAPFDINGBATS;
221221

222-
textRenderer = new TextRenderer(textElement) {
223-
@Override
224-
public void draw(DrawContext drawContext) {
225-
try {
226-
setProperty(Property.FONT, PdfFontFactory.createFont(constantFont));
227-
} catch (IOException ignored) {
228-
}
229-
super.draw(drawContext);
230-
}
231-
};
222+
textRenderer = new ConstantFontTextRenderer(textElement, constantFont);
232223
try {
233224
textRenderer.setProperty(Property.FONT, PdfFontFactory.createFont(constantFont));
234225
} catch (IOException exc) {
@@ -403,4 +394,23 @@ private LayoutResult initializeListSymbols(LayoutContext layoutContext) {
403394
}
404395
return null;
405396
}
397+
398+
private static final class ConstantFontTextRenderer extends TextRenderer {
399+
private String constantFontName;
400+
401+
public ConstantFontTextRenderer(Text textElement, String font) {
402+
super(textElement);
403+
constantFontName = font;
404+
}
405+
406+
@Override
407+
public void draw(DrawContext drawContext) {
408+
try {
409+
setProperty(Property.FONT, PdfFontFactory.createFont(constantFontName));
410+
} catch (IOException ignored) {
411+
// Do nothing
412+
}
413+
super.draw(drawContext);
414+
}
415+
}
406416
}

layout/src/main/java/com/itextpdf/layout/renderer/TextRenderer.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -961,12 +961,7 @@ public void draw(DrawContext drawContext) {
961961
canvas.setHorizontalScaling((float) horizontalScaling * 100);
962962
}
963963

964-
GlyphLine.IGlyphLineFilter filter = new GlyphLine.IGlyphLineFilter() {
965-
@Override
966-
public boolean accept(Glyph glyph) {
967-
return !noPrint(glyph);
968-
}
969-
};
964+
GlyphLine.IGlyphLineFilter filter = new CustomGlyphLineFilter();
970965

971966
boolean appearanceStreamLayout = Boolean.TRUE.equals(getPropertyAsBoolean(Property.APPEARANCE_STREAM_LAYOUT));
972967

@@ -1875,4 +1870,11 @@ private static class ScriptRange {
18751870
this.rangeEnd = rangeEnd;
18761871
}
18771872
}
1873+
1874+
private static final class CustomGlyphLineFilter implements GlyphLine.IGlyphLineFilter {
1875+
@Override
1876+
public boolean accept(Glyph glyph) {
1877+
return !noPrint(glyph);
1878+
}
1879+
}
18781880
}

pdfa/src/main/java/com/itextpdf/pdfa/checker/PdfA2Checker.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ This file is part of the iText (R) project.
5555
import com.itextpdf.kernel.font.PdfType3Font;
5656
import com.itextpdf.kernel.font.Type3Glyph;
5757
import com.itextpdf.kernel.geom.Rectangle;
58+
import com.itextpdf.kernel.pdf.PdfAConformanceLevel;
5859
import com.itextpdf.kernel.pdf.PdfArray;
5960
import com.itextpdf.kernel.pdf.PdfBoolean;
6061
import com.itextpdf.kernel.pdf.PdfDictionary;
@@ -73,7 +74,6 @@ This file is part of the iText (R) project.
7374
import com.itextpdf.kernel.pdf.extgstate.PdfExtGState;
7475
import com.itextpdf.pdfa.exceptions.PdfAConformanceException;
7576
import com.itextpdf.pdfa.logs.PdfAConformanceLogMessageConstant;
76-
import com.itextpdf.kernel.pdf.PdfAConformanceLevel;
7777

7878
import java.util.Collections;
7979
import org.slf4j.Logger;
@@ -188,11 +188,7 @@ public void checkColor(Color color, PdfDictionary currentColorSpaces, Boolean fi
188188
PdfObject colorSpace = shadingDictionary.get(PdfName.ColorSpace);
189189
checkColorSpace(PdfColorSpace.makeColorSpace(colorSpace), currentColorSpaces, true, true);
190190
final PdfDictionary extGStateDict = ((PdfDictionary) pattern.getPdfObject()).getAsDictionary(PdfName.ExtGState);
191-
CanvasGraphicsState gState = new CanvasGraphicsState() {
192-
{
193-
updateFromExtGState(new PdfExtGState(extGStateDict));
194-
}
195-
};
191+
CanvasGraphicsState gState = new UpdateCanvasGraphicsState(extGStateDict);
196192
checkExtGState(gState, contentStream);
197193
} else if (pattern instanceof PdfPattern.Tiling) {
198194
checkContentStream((PdfStream) pattern.getPdfObject());
@@ -1084,4 +1080,10 @@ private void checkType3FontGlyphs(PdfType3Font font, PdfStream contentStream) {
10841080
}
10851081
}
10861082
}
1083+
1084+
private static final class UpdateCanvasGraphicsState extends CanvasGraphicsState {
1085+
public UpdateCanvasGraphicsState(PdfDictionary extGStateDict) {
1086+
updateFromExtGState(new PdfExtGState(extGStateDict));
1087+
}
1088+
}
10871089
}

0 commit comments

Comments
 (0)