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

Switch from JDK XML parser to aalto-xml #10489

Merged
merged 6 commits into from
Oct 19, 2023
Merged
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
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ dependencies {
implementation group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-yaml', version: '2.15.2'
implementation group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.15.2'

implementation 'com.fasterxml:aalto-xml:1.3.1'

implementation group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '2.7.9'

implementation 'org.postgresql:postgresql:42.6.0'
Expand Down
1 change: 1 addition & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

// Preferences and XML
requires java.prefs;
requires com.fasterxml.aalto;

// Annotations (@PostConstruct)
requires jakarta.annotation;
Expand Down
34 changes: 17 additions & 17 deletions src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -499,30 +499,30 @@ public void download() {

public boolean checkSSLHandshake(URLDownload urlDownload) {
try {
urlDownload.canBeReached();
} catch (kong.unirest.UnirestException ex) {
if (ex.getCause() instanceof SSLHandshakeException) {
if (dialogService.showConfirmationDialogAndWait(Localization.lang("Download file"),
Localization.lang("Unable to find valid certification path to requested target(%0), download anyway?",
urlDownload.getSource().toString()))) {
return true;
urlDownload.canBeReached();
} catch (kong.unirest.UnirestException ex) {
if (ex.getCause() instanceof SSLHandshakeException) {
if (dialogService.showConfirmationDialogAndWait(Localization.lang("Download file"),
Localization.lang("Unable to find valid certification path to requested target(%0), download anyway?",
urlDownload.getSource().toString()))) {
return true;
} else {
dialogService.notify(Localization.lang("Download operation canceled."));
return false;
}
} else {
dialogService.notify(Localization.lang("Download operation canceled."));
LOGGER.error("Error while checking if the file can be downloaded", ex);
dialogService.notify(Localization.lang("Error downloading"));
return false;
}
} else {
LOGGER.error("Error while checking if the file can be downloaded", ex);
dialogService.notify(Localization.lang("Error downloading"));
return false;
}
return true;
}
return true;
}

public BackgroundTask<Path> prepareDownloadTask(Path targetDirectory, URLDownload urlDownload) {
SSLSocketFactory defaultSSLSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
HostnameVerifier defaultHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
BackgroundTask<Path> downloadTask = BackgroundTask
return BackgroundTask
.wrap(() -> {
Optional<ExternalFileType> suggestedType = inferFileType(urlDownload);
ExternalFileType externalFileType = suggestedType.orElse(StandardExternalFileType.PDF);
Expand All @@ -533,8 +533,8 @@ public BackgroundTask<Path> prepareDownloadTask(Path targetDirectory, URLDownloa
return targetDirectory.resolve(fulltextDir).resolve(suggestedName);
})
.then(destination -> new FileDownloadTask(urlDownload.getSource(), destination))
.onFailure(ex -> LOGGER.error("Error in download", ex))
.onFinished(() -> URLDownload.setSSLVerification(defaultSSLSocketFactory, defaultHostnameVerifier));
return downloadTask;
}

private Optional<ExternalFileType> inferFileType(URLDownload urlDownload) {
Expand Down Expand Up @@ -582,7 +582,7 @@ public void parsePdfMetadataAndShowMergeDialog() {
dialog.addSource("Grobid", wrapImporterToSupplier(new PdfGrobidImporter(preferencesService.getImportFormatPreferences()), filePath));
}
dialog.addSource(Localization.lang("XMP metadata"), wrapImporterToSupplier(new PdfXmpImporter(preferencesService.getXmpPreferences()), filePath));
dialog.addSource(Localization.lang("Content"), wrapImporterToSupplier(new PdfContentImporter(preferencesService.getImportFormatPreferences()), filePath));
dialog.addSource(Localization.lang("Content"), wrapImporterToSupplier(new PdfContentImporter(), filePath));
dialogService.showCustomDialogAndWait(dialog).ifPresent(newEntry -> {
databaseContext.getDatabase().removeEntry(entry);
databaseContext.getDatabase().insertEntry(newEntry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public ImportFormatReader(ImporterPreferences importerPreferences,

public void reset() {
formats.add(new CopacImporter());
formats.add(new EndnoteImporter(importFormatPreferences));
formats.add(new EndnoteImporter());
formats.add(new EndnoteXmlImporter(importFormatPreferences));
formats.add(new InspecImporter());
formats.add(new IsiImporter());
Expand All @@ -73,7 +73,7 @@ public void reset() {
formats.add(new OvidImporter());
formats.add(new PdfMergeMetadataImporter(importFormatPreferences));
formats.add(new PdfVerbatimBibTextImporter(importFormatPreferences));
formats.add(new PdfContentImporter(importFormatPreferences));
formats.add(new PdfContentImporter());
formats.add(new PdfEmbeddedBibFileImporter(importFormatPreferences));
if (importFormatPreferences.grobidPreferences().isGrobidEnabled()) {
formats.add(new PdfGrobidImporter(importFormatPreferences));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class CitaviXmlImporter extends Importer implements Parser {
private final Map<String, Author> knownPersons = new HashMap<>();
private final Map<String, Keyword> knownKeywords = new HashMap<>();
private final Map<String, String> knownPublishers = new HashMap<>();

private final XMLInputFactory xmlInputFactory;
private Map<String, String> refIdWithAuthors = new HashMap<>();
private Map<String, String> refIdWithEditors = new HashMap<>();
private Map<String, String> refIdWithKeywords = new HashMap<>();
Expand All @@ -88,6 +88,10 @@ public class CitaviXmlImporter extends Importer implements Parser {

private Unmarshaller unmarshaller;

public CitaviXmlImporter() {
xmlInputFactory = XMLInputFactory.newFactory();
}

@Override
public String getName() {
return "Citavi XML";
Expand Down Expand Up @@ -148,7 +152,7 @@ public ParserResult importDatabase(Path filePath) throws IOException {
}

private List<BibEntry> parseDataList(CitaviExchangeData data) {
List<BibEntry> bibEntries = new ArrayList<>();
List<BibEntry> bibEntries;

persons = data.getPersons();
keywords = data.getKeywords();
Expand Down Expand Up @@ -272,7 +276,7 @@ private Map<String, String> buildPersonList(List<String> authorsOrEditors) {

for (String idStringsWithSemicolon : authorsOrEditors) {
String refId = idStringsWithSemicolon.substring(0, UUID_LENGTH);
String rest = idStringsWithSemicolon.substring(UUID_SEMICOLON_OFFSET_INDEX, idStringsWithSemicolon.length());
String rest = idStringsWithSemicolon.substring(UUID_SEMICOLON_OFFSET_INDEX);

String[] personIds = rest.split(";");

Expand All @@ -297,7 +301,7 @@ private Map<String, String> buildKeywordList(List<String> keywordsList) {

for (String idStringsWithSemicolon : keywordsList) {
String refId = idStringsWithSemicolon.substring(0, UUID_LENGTH);
String rest = idStringsWithSemicolon.substring(UUID_SEMICOLON_OFFSET_INDEX, idStringsWithSemicolon.length());
String rest = idStringsWithSemicolon.substring(UUID_SEMICOLON_OFFSET_INDEX);

String[] keywordIds = rest.split(";");

Expand All @@ -324,7 +328,7 @@ private Map<String, String> buildPublisherList(List<String> publishersList) {

for (String idStringsWithSemicolon : publishersList) {
String refId = idStringsWithSemicolon.substring(0, UUID_LENGTH);
String rest = idStringsWithSemicolon.substring(UUID_SEMICOLON_OFFSET_INDEX, idStringsWithSemicolon.length());
String rest = idStringsWithSemicolon.substring(UUID_SEMICOLON_OFFSET_INDEX);

String[] publisherIds = rest.split(";");

Expand All @@ -334,7 +338,7 @@ private Map<String, String> buildPublisherList(List<String> publishersList) {
// store publishers already encountered
knownPublishers.computeIfAbsent(pubId, k -> {
Optional<CitaviExchangeData.Publishers.Publisher> publisher = publishers.getPublisher().stream().filter(p -> p.getId().equals(k)).findFirst();
return publisher.map(p -> new String(p.getName())).orElse(null);
return publisher.map(CitaviExchangeData.Publishers.Publisher::getName).orElse(null);
});
jabrefPublishers.add(knownPublishers.get(pubId));
}
Expand Down Expand Up @@ -376,16 +380,16 @@ private String getKnowledgeItem(CitaviExchangeData.References.Reference data) {
Optional<String> text = Optional.ofNullable(knowledgeItem.getText()).filter(Predicate.not(String::isEmpty));
text.ifPresent(t -> comment.add(cleanUpText(t)));

Optional<Integer> pages = Optional.ofNullable(knowledgeItem.getPageRangeNumber()).filter(range -> range != -1);
Optional<Integer> pages = Optional.of(knowledgeItem.getPageRangeNumber()).filter(range -> range != -1);
pages.ifPresent(p -> comment.add("page range: " + p));

Optional<String> quotationTypeDesc = Optional.ofNullable(knowledgeItem.getQuotationType()).flatMap(type ->
this.QUOTATION_TYPES.stream()
Optional<String> quotationTypeDesc = Optional.of(knowledgeItem.getQuotationType()).flatMap(type ->
QUOTATION_TYPES.stream()
.filter(qt -> type == qt.getCitaviIndexType())
.map(QuotationTypeMapping::getName).findFirst());
quotationTypeDesc.ifPresent(qt -> comment.add(String.format("quotation type: %s", qt)));

Optional<Short> quotationIndex = Optional.ofNullable(knowledgeItem.getQuotationIndex());
Optional<Short> quotationIndex = Optional.of(knowledgeItem.getQuotationIndex());
quotationIndex.ifPresent(index -> comment.add(String.format("quotation index: %d", index)));
}
return comment.toString();
Expand Down Expand Up @@ -414,7 +418,6 @@ private void initUnmarshaller() throws JAXBException {
private Object unmarshallRoot(BufferedReader reader) throws XMLStreamException, JAXBException {
initUnmarshaller();

XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(reader);

// Go to the root element
Expand Down Expand Up @@ -495,8 +498,8 @@ enum QuotationTypeMapping {
HIGHLIGHT(5, "Highlight"),
HIGHLIGHT_RED(6, "Highlight in red");

int citaviType;
String name;
final int citaviType;
final String name;

QuotationTypeMapping(int citaviType, String name) {
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ public List<BibEntry> parseCiteSeerResponse(JSONArray jsonResponse) throws Parse
*
* @param jsonObj Search response as a JSON Object
* @return BibEntry
* @throws ParseException
*/
private BibEntry parseBibEntry(JSONObject jsonObj) throws ParseException {
private BibEntry parseBibEntry(JSONObject jsonObj) {
BibEntry bibEntry = new BibEntry();
bibEntry.setField(StandardField.DOI, jsonObj.optString("id"));
bibEntry.setField(StandardField.TITLE, jsonObj.optString("title"));
Expand All @@ -50,7 +49,7 @@ private BibEntry parseBibEntry(JSONObject jsonObj) throws ParseException {
}

private String parseAuthors(Optional<JSONArray> authorsOpt) {
if (!authorsOpt.isPresent()) {
if (authorsOpt.isEmpty()) {
return "";
}
String separator = " and ";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public List<BibEntry> parseEntries(InputStream inputStream) throws ParseExceptio
String author = matcherAuthors.group(1);
authors.add(author);
}
entry.setField(StandardField.AUTHOR, authors.stream().collect(Collectors.joining(" and ")));
entry.setField(StandardField.AUTHOR, String.join(" and ", authors));

return Collections.singletonList(entry);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public boolean isRecognizedFormat(BufferedReader reader) throws IOException {
}

private static void setOrAppend(BibEntry b, Field field, String value, String separator) {
if (b.hasField(field)) {
if (b.hasField(field) && b.getField(field).isPresent()) {
b.setField(field, b.getField(field).get() + separator + value);
} else {
b.setField(field, value);
Expand Down Expand Up @@ -87,7 +87,7 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException {
} else {
// beginning of a new item
if ("TI- ".equals(str.substring(0, 4))) {
if (sb.length() > 0) {
if (!sb.isEmpty()) {
entries.add(sb.toString());
}
sb = new StringBuilder();
Expand All @@ -96,7 +96,7 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException {
}
}

if (sb.length() > 0) {
if (!sb.isEmpty()) {
entries.add(sb.toString());
}

Expand All @@ -116,28 +116,29 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException {
}
String code = line.substring(0, 4);

if ("TI- ".equals(code)) {
setOrAppend(b, StandardField.TITLE, line.substring(4).trim(), ", ");
} else if ("AU- ".equals(code)) {
setOrAppend(b, StandardField.AUTHOR, line.substring(4).trim(), " and ");
} else if ("PY- ".equals(code)) {
setOrAppend(b, StandardField.YEAR, line.substring(4).trim(), ", ");
} else if ("PU- ".equals(code)) {
setOrAppend(b, StandardField.PUBLISHER, line.substring(4).trim(), ", ");
} else if ("SE- ".equals(code)) {
setOrAppend(b, StandardField.SERIES, line.substring(4).trim(), ", ");
} else if ("IS- ".equals(code)) {
setOrAppend(b, StandardField.ISBN, line.substring(4).trim(), ", ");
} else if ("KW- ".equals(code)) {
setOrAppend(b, StandardField.KEYWORDS, line.substring(4).trim(), ", ");
} else if ("NT- ".equals(code)) {
setOrAppend(b, StandardField.NOTE, line.substring(4).trim(), ", ");
} else if ("PD- ".equals(code)) {
setOrAppend(b, new UnknownField("physicaldimensions"), line.substring(4).trim(), ", ");
} else if ("DT- ".equals(code)) {
setOrAppend(b, new UnknownField("documenttype"), line.substring(4).trim(), ", ");
} else {
setOrAppend(b, FieldFactory.parseField(StandardEntryType.Book, line.substring(0, 2)), line.substring(4).trim(), ", ");
switch (code) {
case "TI- " ->
setOrAppend(b, StandardField.TITLE, line.substring(4).trim(), ", ");
case "AU- " ->
setOrAppend(b, StandardField.AUTHOR, line.substring(4).trim(), " and ");
case "PY- " ->
setOrAppend(b, StandardField.YEAR, line.substring(4).trim(), ", ");
case "PU- " ->
setOrAppend(b, StandardField.PUBLISHER, line.substring(4).trim(), ", ");
case "SE- " ->
setOrAppend(b, StandardField.SERIES, line.substring(4).trim(), ", ");
case "IS- " ->
setOrAppend(b, StandardField.ISBN, line.substring(4).trim(), ", ");
case "KW- " ->
setOrAppend(b, StandardField.KEYWORDS, line.substring(4).trim(), ", ");
case "NT- " ->
setOrAppend(b, StandardField.NOTE, line.substring(4).trim(), ", ");
case "PD- " ->
setOrAppend(b, new UnknownField("physicaldimensions"), line.substring(4).trim(), ", ");
case "DT- " ->
setOrAppend(b, new UnknownField("documenttype"), line.substring(4).trim(), ", ");
default ->
setOrAppend(b, FieldFactory.parseField(StandardEntryType.Book, line.substring(0, 2)), line.substring(4).trim(), ", ");
}
}
results.add(b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,10 @@ public boolean equals(Object other) {
return true;
}

if (!(other instanceof CustomImporter)) {
if (!(other instanceof CustomImporter otherImporter)) {
return false;
}

CustomImporter otherImporter = (CustomImporter) other;
return Objects.equals(className, otherImporter.className) && Objects.equals(basePath, otherImporter.basePath);
}

Expand Down
Loading