/*
* Copyright (c) 1997-2024 IDRsolutions (https://www.idrsolutions.com)
*/
package org.jpedal.examples.handlers;
import org.jpedal.PdfDecoder;
import org.jpedal.examples.viewer.Viewer;
import org.jpedal.examples.viewer.ViewerCommands;
import org.jpedal.io.DefaultErrorTracker;
import java.io.File;
public final class ExampleErrorTracker {
@SuppressWarnings("unused")
public static void main(final String[] args) {
new ExampleErrorTracker();
}
@SuppressWarnings("FieldCanBeLocal")
private final Viewer myViewer = new Viewer();
public ExampleErrorTracker() {
//initialise JPedal viewer component
myViewer.setupViewer();
// add in a monitor
//can also be PdfDecoderServer of PdfDecoderFX
final PdfDecoder decode_pdf = (PdfDecoder) myViewer.getPdfDecoder();
decode_pdf.addExternalHandler(new UserErrorTracker(), org.jpedal.external.Options.ErrorTracker);
final String file;
if (System.getenv("PDF_DATA") != null) {
file = System.getenv("PDF_DATA") + "test_data/Hand_Test/awjune2003.pdf";
} else {
file = "/PDFdata/test_data/Hand_Test/awjune2003.pdf";
}
//open a file
myViewer.executeCommand(ViewerCommands.OPENFILE, new File(file));
}
/**
* example implementation
*/
private class UserErrorTracker extends DefaultErrorTracker {
long timeAtStart;
boolean userFailedPage;
UserErrorTracker() {
}
@Override
public String getPageFailureMessage() {
if (userFailedPage) {
return "Timed out on page";
} else {
return super.getPageFailureMessage();
}
}
@Override
//use to see if page failed
public boolean ispageSuccessful() {
return !userFailedPage && super.ispageSuccessful();
}
@Override
// called every time we execute a Postscript command in data streams
// (dataPointer/streamSize) gives indicator of amount decoded but page can
// contain multiple streams
public boolean checkForExitRequest(final int dataPointer, final int streamSize) {
//gracefully allow us to fail if over certain time to decode
final long timeElapsed = System.currentTimeMillis() - timeAtStart;
System.out.println("Mill-Seconds elapsed=" + timeElapsed);
if (timeElapsed > 1_500) {
userFailedPage = true;
return true;
} else {
return false;
}
}
@Override
public void finishedPageDecoding(final int rawPage) {
System.out.println("----Completed page " + rawPage);
}
@Override
//called when we start decoding page
public void startedPageDecoding(final int rawPage) {
System.out.println("----Started page " + rawPage);
// get the current time
timeAtStart = System.currentTimeMillis();
}
}
}
|