return true;
}
@Override
public DcObject parse(String filename, int directoryUsage) {
DcObject book = DcModules.get(DcModules._BOOK).getItem();
try {
book.setValue(Book._A_TITLE, getName(filename, directoryUsage));
book.setValue(Book._SYS_FILENAME, filename);
// check if the filename contains an ISBN
String isbn = String.valueOf(StringUtils.getContainedNumber(filename));
boolean isIsbn10 = ISBN.isISBN10(isbn);
boolean isIsbn13 = ISBN.isISBN13(isbn);
// this can be used later on by the online search
if (isIsbn10 || isIsbn13) {
String isbn10 = isIsbn10 ? isbn : ISBN.getISBN10(isbn);
String isbn13 = isIsbn13 ? isbn : ISBN.getISBN13(isbn);
book.setValue(Book._J_ISBN10, isbn10);
book.setValue(Book._N_ISBN13, isbn13);
}
if (filename.toLowerCase().endsWith("pdf")) {
File file = new File(filename);
FileISBNExtractor fileISBNExtractor = new FileISBNExtractor();
fileISBNExtractor.setSearchMinBytes(30000);
fileISBNExtractor.getTextReaderFactory().setPreferredPdfExtractor(new PDFBoxTextExtractor());
ISBNCandidates isbnCandidates = fileISBNExtractor.getIsbnCandidates(file);
org.chabanois.isbn.extractor.ISBN extractedISBN = isbnCandidates.getHighestScoreISBN();
if (extractedISBN != null ) {
String s = extractedISBN.getIsbn();
if (s != null && s.length() > 0)
book.setValue(Book._N_ISBN13, ISBN.isISBN10(s) ? ISBN.getISBN13(s) :
ISBN.isISBN13(s) ? s : null);
}
RandomAccessFile raf = null;
PDFFile pdffile;
try {
raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
pdffile = new PDFFile(buf);
pdffile.stop(1);
try {
book.setValue(Book._T_NROFPAGES, Long.valueOf(pdffile.getNumPages()));
Iterator<String> it = pdffile.getMetadataKeys();
while (it.hasNext()) {
String key = it.next();
String value = pdffile.getStringMetadata(key);
if (!Utilities.isEmpty(value)) {
if (key.equalsIgnoreCase("Author"))
DataManager.createReference(book, Book._G_AUTHOR, value);
if (key.equalsIgnoreCase("Title") && !value.trim().equalsIgnoreCase("untitled"))
book.setValue(Book._A_TITLE, value);
}
}
} catch (IOException ioe) {
getClient().addMessage(DcResources.getText("msgCouldNotReadInfoFrom", filename));
}
// draw the first page to an image
PDFPage page = pdffile.getPage(0);
if (page != null) {
Rectangle rect = new Rectangle(0,0, (int)page.getBBox().getWidth(), (int)page.getBBox().getHeight());
Image front = page.getImage(rect.width, rect.height, rect, null, true, true);
book.setValue(Book._K_PICTUREFRONT, new DcImageIcon(Utilities.getBytes(new DcImageIcon(front))));
}
} finally {
if (raf != null)
raf.close();