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

Jaxb context 9539 #11552

Closed
wants to merge 18 commits into from
Closed
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
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ dependencies {
implementation group: 'org.eclipse.jgit', name: 'org.eclipse.jgit', version: '6.10.0.202406032230-r'

implementation group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-yaml', version: '2.17.2'
implementation group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version: '2.17.2'
implementation group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.17.2'
implementation group: 'com.fasterxml.jackson.module', name: 'jackson-module-jaxb-annotations', version: '2.17.2'


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

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@
requires com.google.gson;
requires com.fasterxml.jackson.databind;
requires com.fasterxml.jackson.dataformat.yaml;
requires com.fasterxml.jackson.dataformat.xml;
requires com.fasterxml.jackson.datatype.jsr310;
requires com.fasterxml.jackson.module.jaxb;
// needs to be loaded here as it's otherwise not found at runtime
requires org.glassfish.jaxb.runtime;
// endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.jabref.logic.importer.fileformat;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PushbackInputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -49,9 +52,8 @@
import org.jabref.model.entry.types.StandardEntryType;
import org.jabref.model.strings.StringUtil;

import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Unmarshaller;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
import org.apache.commons.io.ByteOrderMark;
import org.apache.commons.io.input.BOMInputStream;
import org.slf4j.Logger;
Expand Down Expand Up @@ -85,7 +87,7 @@ public class CitaviXmlImporter extends Importer implements Parser {
private CitaviExchangeData.ReferenceKeywords refKeywords;
private CitaviExchangeData.ReferencePublishers refPublishers;

private Unmarshaller unmarshaller;
private XmlMapper mapper;

public CitaviXmlImporter() {
xmlInputFactory = XMLInputFactory.newFactory();
Expand Down Expand Up @@ -135,16 +137,13 @@ public boolean isRecognizedFormat(Path filePath) throws IOException {
@Override
public ParserResult importDatabase(Path filePath) throws IOException {
try (BufferedReader reader = getReaderFromZip(filePath)) {
Object unmarshalledObject = unmarshallRoot(reader);

if (unmarshalledObject instanceof CitaviExchangeData data) {
if (mapperRoot(reader) instanceof CitaviExchangeData data) {
List<BibEntry> bibEntries = parseDataList(data);

return new ParserResult(bibEntries);
} else {
return ParserResult.fromErrorMessage("File does not start with xml tag.");
}
} catch (JAXBException | XMLStreamException e) {
} catch (XMLStreamException e) {
LOGGER.debug("could not parse document", e);
return ParserResult.fromError(e);
}
Expand Down Expand Up @@ -406,25 +405,31 @@ private String removeSpacesBeforeLineBreak(String string) {
.replaceAll(" +\n", "\n");
}

private void initUnmarshaller() throws JAXBException {
if (unmarshaller == null) {
// Lazy init because this is expensive
JAXBContext context = JAXBContext.newInstance("org.jabref.logic.importer.fileformat.citavi");
unmarshaller = context.createUnmarshaller();
private void initMapper() throws Exception {
// Lazy init because this is expensive
if (mapper == null) {
mapper = XmlMapper.xmlBuilder().addModule(new JaxbAnnotationModule()).build();
}
}

private Object unmarshallRoot(BufferedReader reader) throws XMLStreamException, JAXBException {
initUnmarshaller();
private Object mapperRoot(BufferedReader reader) throws XMLStreamException {
Objects.requireNonNull(reader);

XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(reader);
try {
initMapper();

// Go to the root element
while (!xmlStreamReader.isStartElement()) {
xmlStreamReader.next();
}
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(reader);

return unmarshaller.unmarshal(xmlStreamReader);
// Go to the root element
while (!xmlStreamReader.isStartElement()) {
xmlStreamReader.next();
}

return mapper.readValue(xmlStreamReader, Object.class);
} catch (Exception e) {
LOGGER.debug("could not read document", e);
return ParserResult.fromError(e);
}
}

@Override
Expand Down
Loading