Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various fixes and compilation issues resolution. #22

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ pom.xml.tag
pom.xml.releaseBackup
pom.xml.next
release.properties
.project
.classpath
.settings/
.vscode/
112 changes: 100 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
<version>31.1-jre</version>
</dependency>

<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.6</version>
<version>2.0.27</version>
</dependency>

<dependency>
Expand All @@ -35,28 +35,116 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>4.13.2</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.giaybac.traprange.MAIN</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<excludes>
<exclude>**/com/giaybac/traprange/MAIN.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0-M1</version>
<executions>
<execution>
<id>enforce-maven</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireMavenVersion>
<version>2.2.0</version>
</requireMavenVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<systemProperties>
<property>
<name>loggerPath</name>
<value>conf/log4j.properties</value>
</property>
</systemProperties>
<argLine>-Xms512m -Xmx1500m</argLine>
<parallel>methods</parallel>
<forkMode>pertest</forkMode>
</configuration>
</plugin>
<!-- attach test jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<goals>
<goal>jar</goal>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
<configuration></configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add_sources</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add_test_sources</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/test/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
Expand Down
36 changes: 22 additions & 14 deletions src/main/java/com/giaybac/traprange/PDFTableExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.logging.Level;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.TextPosition;

Expand All @@ -45,14 +43,20 @@ public class PDFTableExtractor {
private final List<Integer> extractedPages = new ArrayList<>();
private final List<Integer> exceptedPages = new ArrayList<>();
//contains avoided line idx-s for each page,
//if this multimap contains only one element and key of this element equals -1
//if this multimap contains only one element and key of this element equals magicPageNumber
//then all lines in extracted pages contains in multi-map value will be avoided
private final Multimap<Integer, Integer> pageNExceptedLinesMap = HashMultimap.create();

private InputStream inputStream;
private PDDocument document;
private String password;

private final int magicPageNumber = Integer.MAX_VALUE; //--------------------------------------------------------------------------
// Initialization and releasation
//--------------------------------------------------------------------------
// Getter N Setter
//--------------------------------------------------------------------------
// Method binding
public PDFTableExtractor setSource(InputStream inputStream) {
this.inputStream = inputStream;
return this;
Expand Down Expand Up @@ -126,8 +130,8 @@ public PDFTableExtractor exceptLine(int pageIdx, int[] lineIdxes) {
* @param lineIdxes
* @return
*/
public PDFTableExtractor exceptLine(int[] lineIdxes) {
this.exceptLine(-1, lineIdxes);
public PDFTableExtractor exceptLine(int[] lineIdxs) {
this.exceptLine(magicPageNumber, lineIdxs);
return this;
}

Expand All @@ -138,11 +142,11 @@ public List<Table> extract() {
try {
this.document = this.password!=null?PDDocument.load(inputStream,this.password):PDDocument.load(inputStream);
for (int pageId = 0; pageId < document.getNumberOfPages(); pageId++) {
boolean b = !exceptedPages.contains(pageId) && (extractedPages.isEmpty() || extractedPages.contains(pageId));
boolean b = !(exceptedPages.contains(pageId) || exceptedPages.contains(pageId - document.getNumberOfPages())) && (extractedPages.isEmpty() || extractedPages.contains(pageId));
if (b) {
List<TextPosition> texts = extractTextPositions(pageId);//sorted by .getY() ASC
//extract line ranges
List<Range<Integer>> lineRanges = getLineRanges(pageId, texts);
List<Range<Integer>> lineRanges = getLineRanges(pageId, document.getNumberOfPages(), texts);
//extract column ranges
List<TextPosition> textsByLineRanges = getTextsByLineRanges(lineRanges, texts);

Expand All @@ -153,7 +157,7 @@ public List<Table> extract() {
//Calculate columnRanges
List<Range<Integer>> columnRanges = getColumnRanges(pageIdNTextsMap.values());
for (int pageId : pageIdNTextsMap.keySet()) {
Table table = buildTable(pageId, (List) pageIdNTextsMap.get(pageId), (List) pageIdNLineRangesMap.get(pageId), columnRanges);
Table table = buildTable(pageId, (List<TextPosition>) pageIdNTextsMap.get(pageId), (List<Range<Integer>>) pageIdNLineRangesMap.get(pageId), columnRanges);
retVal.add(table);
//debug
logger.debug("Found " + table.getRows().size() + " row(s) and " + columnRanges.size()
Expand Down Expand Up @@ -292,7 +296,7 @@ private List<TextPosition> extractTextPositions(int pageId) throws IOException {

private boolean isExceptedLine(int pageIdx, int lineIdx) {
boolean retVal = this.pageNExceptedLinesMap.containsEntry(pageIdx, lineIdx)
|| this.pageNExceptedLinesMap.containsEntry(-1, lineIdx);
|| this.pageNExceptedLinesMap.containsEntry(magicPageNumber, lineIdx);
return retVal;
}

Expand Down Expand Up @@ -341,7 +345,7 @@ private List<Range<Integer>> getColumnRanges(Collection<TextPosition> texts) {
return rangesBuilder.build();
}

private List<Range<Integer>> getLineRanges(int pageId, List<TextPosition> pageContent) {
private List<Range<Integer>> getLineRanges(int pageId, int numberOfPages, List<TextPosition> pageContent) {
TrapRangeBuilder lineTrapRangeBuilder = new TrapRangeBuilder();
for (TextPosition textPosition : pageContent) {
Range<Integer> lineRange = Range.closed((int) textPosition.getY(),
Expand All @@ -350,15 +354,19 @@ private List<Range<Integer>> getLineRanges(int pageId, List<TextPosition> pageCo
lineTrapRangeBuilder.addRange(lineRange);
}
List<Range<Integer>> lineTrapRanges = lineTrapRangeBuilder.build();
List<Range<Integer>> retVal = removeExceptedLines(pageId, lineTrapRanges);
List<Range<Integer>> retVal = removeExceptedLines(pageId, numberOfPages, lineTrapRanges);
return retVal;
}

private List<Range<Integer>> removeExceptedLines(int pageIdx, List<Range<Integer>> lineTrapRanges) {
private List<Range<Integer>> removeExceptedLines(int pageIdx, int numberOfPages, List<Range<Integer>> lineTrapRanges) {
List<Range<Integer>> retVal = new ArrayList<>();
for (int lineIdx = 0; lineIdx < lineTrapRanges.size(); lineIdx++) {
boolean isExceptedLine = isExceptedLine(pageIdx, lineIdx)
|| isExceptedLine(pageIdx, lineIdx - lineTrapRanges.size());
boolean isExceptedLine = (
isExceptedLine(pageIdx, lineIdx)
|| isExceptedLine(pageIdx, lineIdx - lineTrapRanges.size())
|| isExceptedLine(pageIdx - numberOfPages, lineIdx)
|| isExceptedLine(pageIdx - numberOfPages, lineIdx - lineTrapRanges.size())
);
if (!isExceptedLine) {
retVal.add(lineTrapRanges.get(lineIdx));
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/giaybac/traprange/TrapRangeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public TrapRangeBuilder addRange(Range<Integer> range) {
public List<Range<Integer>> build() {
List<Range<Integer>> retVal = new ArrayList<>();
//order range by lower Bound
Collections.sort(ranges, new Comparator<Range>() {
Collections.sort(ranges, new Comparator<Range<Integer>>() {
@Override
public int compare(Range o1, Range o2) {
public int compare(Range<Integer> o1, Range<Integer> o2) {
return o1.lowerEndpoint().compareTo(o2.lowerEndpoint());
}
});
Expand All @@ -47,7 +47,7 @@ public int compare(Range o1, Range o2) {
} else {
Range<Integer> lastRange = retVal.get(retVal.size() - 1);
if (lastRange.isConnected(range)) {
Range newLastRange = lastRange.span(range);
Range<Integer> newLastRange = lastRange.span(range);
retVal.set(retVal.size() - 1, newLastRange);
} else {
retVal.add(range);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
*/


package com.giaybac.traprange;
package com.giaybac.traprange.invoice;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -124,7 +124,6 @@ private void writeToOutputStream(final List<TextLine> textLineList) throws IOExc
* In order to get rid of the warning:
* TextPositionComparator class should implement Comparator<TextPosition> instead of Comparator
*/
@SuppressWarnings("unchecked")
private void sortTextPositionList(final List<TextPosition> textList) {
TextPositionComparator comparator = new TextPositionComparator();
Collections.sort(textList, comparator);
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/com/giaybac/traprange/test/TESTPDFBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,25 @@ public void test() throws IOException {

this.processPage(page);
//Print out all text
Collections.sort(ranges, new Comparator<Range>() {
Collections.sort(ranges, new Comparator<Range<Integer>>() {
@Override
public int compare(Range o1, Range o2) {
public int compare(Range<Integer> o1, Range<Integer> o2) {
return o1.lowerEndpoint().compareTo(o2.lowerEndpoint());
}
});
for (Range range : ranges) {
for (Range<Integer> range : ranges) {
System.out.println("> " + range);
}
//Print out all ranges
List<Range<Integer>> trapRanges = trapRangeBuilder.build();
for (Range trapRange : trapRanges) {
for (Range<Integer> trapRange : trapRanges) {
System.out.println("TrapRange: " + trapRange);
}
}

@Override
protected void processTextPosition(TextPosition text) {
Range range = Range.closed((int) text.getY(), (int) (text.getY() + text.getHeight()));
Range<Integer> range = Range.closed((int) text.getY(), (int) (text.getY() + text.getHeight()));
System.out.println("Text: " + text.getUnicode());
trapRangeBuilder.addRange(range);
}
Expand Down
4 changes: 1 addition & 3 deletions src/test/java/com/giaybac/traprange/test/TestInvoice.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import com.giaybac.traprange.PDFLayoutTextStripper;
import com.giaybac.traprange.invoice.PDFLayoutTextStripper;
import org.junit.Test;
import java.nio.file.Paths;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -169,12 +169,10 @@ private static class Row {
String barcode;
float quantity = -1;
List<String> lines = new ArrayList<>();
int lineIdx;
Map<String, int[]> headerPositions = null;

Row(Map<String, int[]> headerPositions, int lineIdx, String barcode) {
this.headerPositions = headerPositions;
this.lineIdx = lineIdx;
this.barcode = barcode;
}

Expand Down
5 changes: 1 addition & 4 deletions src/test/java/com/giaybac/traprange/test/TestInvoice2.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import com.giaybac.traprange.PDFLayoutTextStripper;
import com.giaybac.traprange.invoice.PDFLayoutTextStripper;
import org.junit.Test;
import java.nio.file.Paths;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.*;

/**
* How to run this file:
Expand All @@ -26,8 +25,6 @@

public class TestInvoice2 {

private static final int spaceTolerance = 5;

@Test
public void test() throws IOException {
try {
Expand Down