-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPdfParser.java
More file actions
66 lines (50 loc) · 2.22 KB
/
Copy pathPdfParser.java
File metadata and controls
66 lines (50 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.example.gpacalculator;
import android.content.ContentResolver;
import android.net.Uri;
import android.util.Log;
import com.tom_roush.pdfbox.pdmodel.PDDocument;
import com.tom_roush.pdfbox.text.PDFTextStripper;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PdfParser {
private static final String TAG = "PdfParser";
public interface PdfParseListener {
void onPdfParseSuccess(List<SubjectResult> subjectResults);
void onPdfParseError(Exception e);
}
public static void parseFromUri(Uri uri, ContentResolver contentResolver, PdfParseListener listener) {
new Thread(() -> {
try {
List<SubjectResult> results = extractFromPdf(uri, contentResolver);
listener.onPdfParseSuccess(results);
} catch (Exception e) {
Log.e(TAG, "Error parsing PDF", e);
listener.onPdfParseError(e);
}
}).start();
}
private static List<SubjectResult> extractFromPdf(Uri uri, ContentResolver contentResolver) throws Exception {
List<SubjectResult> results = new ArrayList<>();
InputStream inputStream = contentResolver.openInputStream(uri);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
PDDocument document = PDDocument.load(bufferedInputStream);
PDFTextStripper pdfTextStripper = new PDFTextStripper();
String text = pdfTextStripper.getText(document);
document.close();
String regexPattern = "(2[123][A-Z]{2,3}\\d{2,3}[TL])\\s*(?:-\\s|\\s)([A-Za-z,\\s]+)\\s(A\\+?|B\\+?|C|O|U|AB)\\s";
Pattern pattern = Pattern.compile(regexPattern);
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
String subjectCode = matcher.group(1);
String subjectName = matcher.group(2).trim();
String grade = matcher.group(3);
System.out.println(subjectCode + " " + subjectName + " " + grade);
results.add(new SubjectResult(subjectCode, subjectName, grade));
}
return results;
}
}