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

Update citeproc to fix CSL render exceptions #8532

Merged
merged 16 commits into from
Mar 14, 2022
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve

### Changed

- We changed the list of CSL styles to those that support formatting bibliographies [#8421](https://github.com/JabRef/jabref/issues/8421) [citeproc-java#116](https://github.com/michel-kraemer/citeproc-java/issues/116)
- The CSL preview styles now also support displaying data from cross references entries that are linked via the `crossref` field [#7378](https://github.com/JabRef/jabref/issues/7378)
- We made the Search button in Web Search wider. We also skewed the panel titles to the left [#8397](https://github.com/JabRef/jabref/issues/8397)
- We introduced a preference to disable fulltext indexing [#8468](https://github.com/JabRef/jabref/issues/8468)
Expand All @@ -23,6 +24,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve

### Fixed

- We fixed an issue wehre long article numbers in the `pages` field would cause an exception and preventing the citation style to display [#8381](https://github.com/JabRef/jabref/issues/8381), [citeproc-java](https://github.com/michel-kraemer/citeproc-java/issues/114)
- We fixed an issue where online links in the file field were not detected correctly and could produce an exception [#8150](https://github.com/JabRef/jabref/issues/8510)
- We fixed an issue where an exception could occur when saving the preferences [#7614](https://github.com/JabRef/jabref/issues/7614)
- We fixed an issue where "Copy DOI url" in the right-click menu of the Entry List would just copy the DOI and not the DOI url. [#8389](https://github.com/JabRef/jabref/issues/8389)
Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ dependencies {
implementation 'org.apache.pdfbox:xmpbox:3.0.0-RC1'

implementation group: 'org.apache.commons', name: 'commons-csv', version: '1.9.0'
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'
implementation 'com.h2database:h2-mvstore:2.1.210'

implementation group: 'org.apache.tika', name: 'tika-core', version: '2.3.0'
Expand Down Expand Up @@ -178,7 +179,7 @@ dependencies {
implementation "org.tinylog:slf4j-tinylog:2.4.1"
implementation "org.tinylog:tinylog-impl:2.4.1"

implementation 'de.undercouch:citeproc-java:3.0.0-alpha.3'
implementation 'de.undercouch:citeproc-java:3.0.0-alpha.6'

// jakarta.activation is already depdency of glassfish
implementation group: 'jakarta.xml.bind', name: 'jakarta.xml.bind-api', version: '3.0.1'
Expand Down
24 changes: 15 additions & 9 deletions src/main/java/org/jabref/logic/citationstyle/CitationStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

Expand All @@ -39,10 +38,11 @@
public class CitationStyle {

public static final String DEFAULT = "/ieee.csl";

private static final Logger LOGGER = LoggerFactory.getLogger(CitationStyle.class);
private static final String STYLES_ROOT = "/csl-styles";

private static final List<CitationStyle> STYLES = new ArrayList<>();
private static final DocumentBuilderFactory FACTORY = DocumentBuilderFactory.newInstance();

private final String filePath;
private final String title;
Expand All @@ -60,13 +60,19 @@ private CitationStyle(final String filename, final String title, final String so
private static Optional<CitationStyle> createCitationStyleFromSource(final String source, final String filename) {
if ((filename != null) && !filename.isEmpty() && (source != null) && !source.isEmpty()) {
try {
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(stripInvalidProlog(source)));
InputSource inputSource = new InputSource();
inputSource.setCharacterStream(new StringReader(stripInvalidProlog(source)));

Document doc = db.parse(is);
NodeList nodes = doc.getElementsByTagName("info");
Document doc = FACTORY.newDocumentBuilder().parse(inputSource);

// See CSL#canFormatBibliographies, checks if the tag exists
NodeList bibs = doc.getElementsByTagName("bibliography");
if (bibs.getLength() <= 0) {
LOGGER.debug("no bibliography element for file {} ", filename);
return Optional.empty();
}

NodeList nodes = doc.getElementsByTagName("info");
NodeList titleNode = ((Element) nodes.item(0)).getElementsByTagName("title");
String title = ((CharacterData) titleNode.item(0).getFirstChild()).getData();

Expand Down Expand Up @@ -100,6 +106,7 @@ public static Optional<CitationStyle> createCitationStyleFromFile(final String s
String text;
String internalFile = STYLES_ROOT + (styleFile.startsWith("/") ? "" : "/") + styleFile;
URL url = CitationStyle.class.getResource(internalFile);

if (url != null) {
text = CSLUtils.readURLToString(url, StandardCharsets.UTF_8.toString());
} else {
Expand All @@ -108,7 +115,7 @@ public static Optional<CitationStyle> createCitationStyleFromFile(final String s
}
return createCitationStyleFromSource(text, styleFile);
} catch (NoSuchFileException e) {
LOGGER.error("Could not find file: " + styleFile, e);
LOGGER.error("Could not find file: {}", styleFile, e);
} catch (IOException e) {
LOGGER.error("Error reading source file", e);
}
Expand Down Expand Up @@ -140,7 +147,6 @@ public static List<CitationStyle> discoverCitationStyles() {
try {
URI uri = url.toURI();
Path path = Path.of(uri).getParent();

STYLES.addAll(discoverCitationStylesInPath(path));

return STYLES;
Expand Down