-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add more file importers to JabRef #13310
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
Draft
InAnYan
wants to merge
14
commits into
JabRef:main
Choose a base branch
from
InAnYan:feat/docs-importers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7e9f579
feat(importers): add importers for ODF files
InAnYan ed19d6b
try with bom
Siedlerchr a3eb9f3
exclude jcl
Siedlerchr 6679f1e
Revert "exclude jcl"
InAnYan 5a3d35b
Revert "try with bom"
InAnYan a27c7e7
feat(importers): add document files
InAnYan 06fbb35
fix(build): change package name to just `tika-parsers`
InAnYan 8769276
feat(importers/docs): refactor ODF imports + add MsOffice importers a…
InAnYan 34039bd
feat(importers/docs): refactor ODF imports + add MsOffice importers a…
InAnYan 511e3c0
feat(importers/docs): add images importers
InAnYan 03cbaa3
feat(importers/docs): add images importers
InAnYan a5c006b
feat(importers/docs): scaffold book importers
InAnYan 6cc22ff
refactor(importer/docs): add `TikaMetadataParser` class and abstract …
InAnYan 8a4c492
Merge branch 'main' into feat/docs-importers
koppor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
jablib/src/main/java/org/jabref/logic/importer/fileformat/docs/OdfImporter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package org.jabref.logic.importer.fileformat.docs; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.jabref.logic.importer.Importer; | ||
import org.jabref.logic.importer.ParserResult; | ||
import org.jabref.logic.importer.util.Constants; | ||
import org.jabref.logic.l10n.Localization; | ||
import org.jabref.logic.util.FileType; | ||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.entry.field.StandardField; | ||
|
||
import org.apache.tika.exception.TikaException; | ||
import org.apache.tika.metadata.Metadata; | ||
import org.apache.tika.metadata.Property; | ||
import org.apache.tika.parser.AutoDetectParser; | ||
import org.apache.tika.sax.BodyContentHandler; | ||
import org.xml.sax.SAXException; | ||
|
||
/** | ||
* General importer for Open Document Format files. | ||
*/ | ||
public abstract class OdfImporter extends Importer { | ||
@Override | ||
public boolean isRecognizedFormat(BufferedReader input) throws IOException { | ||
return Constants.isZip(input); | ||
} | ||
|
||
@Override | ||
public ParserResult importDatabase(BufferedReader input) throws IOException { | ||
throw new UnsupportedOperationException("OdfImporter (and descendants) does not support importDatabase(BufferedReader reader)." | ||
+ "Instead use importDatabase(Path filePath)."); | ||
} | ||
|
||
@Override | ||
public ParserResult importDatabase(Path filePath) throws IOException { | ||
try (InputStream inputStream = new FileInputStream(filePath.toFile())) { | ||
AutoDetectParser parser = new AutoDetectParser(); | ||
Metadata metadata = new Metadata(); | ||
BodyContentHandler handler = new BodyContentHandler(); | ||
|
||
parser.parse(inputStream, handler, metadata); | ||
|
||
BibEntry entry = extractMetadata(metadata); | ||
|
||
return ParserResult.fromEntry(entry); | ||
} catch (SAXException | TikaException e) { | ||
throw new IOException("Error parsing file: " + filePath, e); | ||
} | ||
} | ||
|
||
private BibEntry extractMetadata(Metadata metadata) { | ||
Optional<String> title = Optional.ofNullable(metadata.get("dc:title")); | ||
Optional<Date> date = Optional.ofNullable(metadata.getDate(Property.internalDate("dcterms:created"))); | ||
|
||
List<String> authors = Arrays.asList(metadata.getValues("dc:contributor")); | ||
|
||
return new BibEntry() | ||
.withField(StandardField.TITLE, title) | ||
.withField(StandardField.AUTHOR, !authors.isEmpty() ? Optional.of(String.join(" and ", authors)) : Optional.empty()) | ||
.withField(StandardField.YEAR, date.map(Date::getYear).map(Object::toString)); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
jablib/src/main/java/org/jabref/logic/importer/fileformat/docs/OdpImporter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.jabref.logic.importer.fileformat.docs; | ||
|
||
import org.jabref.logic.l10n.Localization; | ||
import org.jabref.logic.util.FileType; | ||
import org.jabref.logic.util.StandardFileType; | ||
|
||
/** | ||
* Importer for OpenDocument Impress (ODP) files. | ||
*/ | ||
InAnYan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public class OdpImporter extends OdfImporter { | ||
@Override | ||
public String getId() { | ||
return "odp"; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "OpenDocument Impress"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return Localization.lang("Importer for OpenDocument Impress (ODP) files"); | ||
} | ||
|
||
@Override | ||
public FileType getFileType() { | ||
return StandardFileType.ODP; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
jablib/src/main/java/org/jabref/logic/importer/fileformat/docs/OdsImporter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.jabref.logic.importer.fileformat.docs; | ||
|
||
import org.jabref.logic.l10n.Localization; | ||
import org.jabref.logic.util.FileType; | ||
import org.jabref.logic.util.StandardFileType; | ||
|
||
/** | ||
* Importer for OpenDocument Calc (ODS) files. | ||
*/ | ||
InAnYan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public class OdsImporter extends OdfImporter { | ||
@Override | ||
public String getId() { | ||
return "ods"; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "OpenDocument Calc"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return Localization.lang("Importer for OpenDocument Calc (ODS) files"); | ||
} | ||
|
||
@Override | ||
public FileType getFileType() { | ||
return StandardFileType.ODS; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
jablib/src/main/java/org/jabref/logic/importer/fileformat/docs/OdtImporter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.jabref.logic.importer.fileformat.docs; | ||
|
||
import org.jabref.logic.l10n.Localization; | ||
import org.jabref.logic.util.FileType; | ||
import org.jabref.logic.util.StandardFileType; | ||
|
||
/** | ||
* Importer for OpenDocument Text (ODT) files. | ||
*/ | ||
InAnYan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public class OdtImporter extends OdfImporter { | ||
@Override | ||
public String getId() { | ||
return "odt"; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "OpenDocument Writer"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return Localization.lang("Importer for OpenDocument Writer (ODT) files"); | ||
} | ||
|
||
@Override | ||
public FileType getFileType() { | ||
return StandardFileType.ODT; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
jablib/src/main/java/org/jabref/logic/importer/util/Constants.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.jabref.logic.importer.util; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class Constants { | ||
public static final char[] ZIP_HEADER_MAGIC_NUMBER = {0x50, 0x4b, 0x03, 0x04}; | ||
InAnYan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public static final List<String> ZIP_FILES_EXTENSIONS = List.of( | ||
".ctv6bak", | ||
".zip", | ||
".epub", | ||
".odt", | ||
".docx", | ||
".xlsx", | ||
".pptx", | ||
".ods", | ||
".odp" | ||
); | ||
|
||
public static final String DC_NAMESPACE = "http://purl.org/dc/elements/1.1/"; | ||
|
||
public static boolean isZip(BufferedReader input) throws IOException { | ||
char[] header = new char[ZIP_HEADER_MAGIC_NUMBER.length]; | ||
int nRead = input.read(header); | ||
return nRead == ZIP_HEADER_MAGIC_NUMBER.length && Arrays.equals(header, ZIP_HEADER_MAGIC_NUMBER); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
jablib/src/test/java/org/jabref/logic/importer/fileformat/docs/OdpImporterFilesTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.jabref.logic.importer.fileformat.docs; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Stream; | ||
|
||
import org.jabref.logic.importer.ImportException; | ||
import org.jabref.logic.importer.fileformat.ImporterTestEngine; | ||
import org.jabref.logic.importer.util.Constants; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
public class OdpImporterFilesTest { | ||
private static final String FILE_ENDING = ".odp"; | ||
private static final List<String> EXCLUDE_EXTENSIONS = Constants.ZIP_FILES_EXTENSIONS | ||
.stream() | ||
.filter(ext -> !ext.equals(FILE_ENDING)) | ||
.toList(); | ||
|
||
private OdpImporter importer; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
importer = new OdpImporter(); | ||
} | ||
|
||
private static Stream<String> fileNames() throws IOException { | ||
Predicate<String> fileName = name -> name.startsWith("OdpImporterTest") && name.endsWith(FILE_ENDING); | ||
return ImporterTestEngine.getTestFiles(fileName).stream(); | ||
} | ||
|
||
private static Stream<String> invalidFileNames() throws IOException { | ||
Predicate<String> fileName = name -> !name.startsWith("OdpImporterTest") && EXCLUDE_EXTENSIONS.stream().noneMatch(name::endsWith); | ||
return ImporterTestEngine.getTestFiles(fileName).stream(); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("fileNames") | ||
void isRecognizedFormat(String fileName) throws IOException { | ||
ImporterTestEngine.testIsRecognizedFormat(importer, fileName); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("invalidFileNames") | ||
void isNotRecognizedFormat(String fileName) throws IOException { | ||
ImporterTestEngine.testIsNotRecognizedFormat(importer, fileName); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("fileNames") | ||
void importEntries(String fileName) throws ImportException, IOException { | ||
ImporterTestEngine.testImportEntries(importer, fileName, FILE_ENDING); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
jablib/src/test/java/org/jabref/logic/importer/fileformat/docs/OdpImporterTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.jabref.logic.importer.fileformat.docs; | ||
|
||
import org.jabref.logic.util.StandardFileType; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class OdpImporterTest { | ||
private OdpImporter importer; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
importer = new OdpImporter(); | ||
} | ||
|
||
@Test | ||
void getFormatName() { | ||
assertEquals("OpenDocument Impress", importer.getName()); | ||
} | ||
|
||
@Test | ||
void getCLIId() { | ||
assertEquals("odp", importer.getId()); | ||
} | ||
|
||
@Test | ||
void sGetExtensions() { | ||
InAnYan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assertEquals(StandardFileType.ODP, importer.getFileType()); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rework with the new scheme.
But the concrete versions at https://github.com/JabRef/jabref/blob/main/versions/build.gradle.kts