Skip to content

Commit

Permalink
Add citation styles as git submodule (#4431)
Browse files Browse the repository at this point in the history
* add citation styles as git submodule

* add CSL locales as git submodule
  • Loading branch information
florian-beetz authored and tobiasdiez committed Oct 29, 2018
1 parent f8b1e2d commit 11a62dd
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 39 deletions.
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "src/main/resources/csl-styles"]
path = src/main/resources/csl-styles
url = https://github.com/citation-style-language/styles.git
[submodule "src/main/resources/csl-locales"]
path = src/main/resources/csl-locales
url = https://github.com/citation-style-language/locales.git
3 changes: 0 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,6 @@ dependencies {
compile 'org.apache.logging.log4j:log4j-api:2.11.1'
compile 'org.apache.logging.log4j:log4j-core:2.11.1'

// need to use snapshots as the stable version is from 2013 and doesn't support v1.0.1 CitationStyles
compile 'org.citationstyles:styles:1.0.1-SNAPSHOT'
compile 'org.citationstyles:locales:1.0.1-SNAPSHOT'
compile 'de.undercouch:citeproc-java:1.0.1'

compile 'com.github.tomtung:latex2unicode_2.12:0.2.2'
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/logic/citationstyle/CSLAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public synchronized List<String> makeBibliography(List<BibEntry> bibEntries, Str
*/
private void initialize(String newStyle, CitationStyleOutputFormat newFormat) throws IOException {
if (cslInstance == null || !Objects.equals(newStyle, style)) {
cslInstance = new CSL(dataProvider, newStyle);
// lang and forceLang are set to the default values of other CSL constructors
cslInstance = new CSL(dataProvider, new JabRefLocaleProvider(), newStyle, "en-US", false);
style = newStyle;
}

Expand Down
67 changes: 32 additions & 35 deletions src/main/java/org/jabref/logic/citationstyle/CitationStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.io.IOException;
import java.io.StringReader;
import java.io.UncheckedIOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
Expand All @@ -13,12 +13,10 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -39,15 +37,13 @@
import org.xml.sax.SAXException;

/**
* Representation of a CitationStyle.
* Stores its name, the file path and the style itself
* Representation of a CitationStyle. Stores its name, the file path and the style itself
*/
public class CitationStyle {

public static final String DEFAULT = "/ieee.csl";
private static final Logger LOGGER = LoggerFactory.getLogger(CitationStyle.class);

private static final Pattern SNAPSHOT_NAME = Pattern.compile(".*styles-1\\.0\\.1-SNAPSHOT\\.jar");
private static final String STYLES_ROOT = "/csl-styles";

private static final List<CitationStyle> STYLES = new ArrayList<>();

Expand Down Expand Up @@ -88,7 +84,7 @@ private static Optional<CitationStyle> createCitationStyleFromSource(final Strin
private static String stripInvalidProlog(String source) {
int startIndex = source.indexOf("<");
if (startIndex > 0) {
return source.substring(startIndex, source.length());
return source.substring(startIndex);
} else {
return source;
}
Expand All @@ -99,13 +95,13 @@ private static String stripInvalidProlog(String source) {
*/
public static Optional<CitationStyle> createCitationStyleFromFile(final String styleFile) {
if (!isCitationStyleFile(styleFile)) {
LOGGER.error("Can only load style files: " + styleFile);
LOGGER.error("Can only load style files: {}", styleFile);
return Optional.empty();
}

try {
String text;
String internalFile = (styleFile.startsWith("/") ? "" : "/") + styleFile;
String internalFile = STYLES_ROOT + (styleFile.startsWith("/") ? "" : "/") + styleFile;
URL url = CitationStyle.class.getResource(internalFile);
if (url != null) {
text = CSLUtils.readURLToString(url, StandardCharsets.UTF_8.toString());
Expand Down Expand Up @@ -140,36 +136,37 @@ public static List<CitationStyle> discoverCitationStyles() {
if (!STYLES.isEmpty()) {
return STYLES;
}
try {

Path filePath = Paths.get(CitationStyle.class.getProtectionDomain().getCodeSource().getLocation().toURI());
String path = filePath.toString();

// This is a quick fix to have the styles when running JabRef in a development environment.
// The styles.jar is not extracted into the JabRef.jar and therefore, we search the classpath for it.
if (Files.isDirectory(filePath)) {
final String cp = System.getProperty("java.class.path");
final String[] entries = cp.split(System.getProperty("path.separator"));

Optional<String> foundStyle = Arrays.stream(entries).filter(entry -> SNAPSHOT_NAME.matcher(entry).matches()).findFirst();
path = foundStyle.orElse(path);
}

try (FileSystem jarFs = FileSystems.newFileSystem(Paths.get(path), null)) {

try (Stream<Path> stylefileStream = Files.find(jarFs.getRootDirectories().iterator().next(), 1, (file, attr) -> file.toString().endsWith("csl"))) {
for (Path style : stylefileStream.collect(Collectors.toList())) {
CitationStyle.createCitationStyleFromFile(style.getFileName().toString()).ifPresent(STYLES::add);
}
} catch (UncheckedIOException e) {
throw new IOException(e);
URL url = CitationStyle.class.getResource(STYLES_ROOT);
if (url == null) {
return Collections.emptyList();
}
try {
URI uri = url.toURI();
if ("jar".equals(uri.getScheme())) {
try (FileSystem fs = FileSystems.newFileSystem(uri, Collections.emptyMap())) {
Path path = fs.getPath(STYLES_ROOT);
STYLES.addAll(discoverCitationStylesInPath(path));
}
} else {
STYLES.addAll(discoverCitationStylesInPath(Paths.get(uri)));
}
return STYLES;
} catch (UncheckedIOException | IOException | URISyntaxException ex) {
LOGGER.error("something went wrong while searching available CitationStyles. Are you running directly from source code?", ex);
} catch (URISyntaxException | IOException e) {
LOGGER.error("something went wrong while searching available CitationStyles. Are you running directly from source code?", e);
return Collections.emptyList();
}
}

private static List<CitationStyle> discoverCitationStylesInPath(Path path) throws IOException {
try (Stream<Path> stream = Files.find(path, 1, (file, attr) -> file.toString().endsWith("csl"))) {
return stream.map(Path::getFileName)
.map(Path::toString)
.map(CitationStyle::createCitationStyleFromFile)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
}
return Collections.emptyList();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.jabref.logic.citationstyle;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import de.undercouch.citeproc.LocaleProvider;
import de.undercouch.citeproc.helper.CSLUtils;

/**
* A {@link LocaleProvider} that loads locales from a directory in the current module.
*
* This implementation is only a slight adaption of {@link de.undercouch.citeproc.DefaultLocaleProvider}.
*/
public class JabRefLocaleProvider implements LocaleProvider {

private static final String LOCALES_ROOT = "/csl-locales";

private final Map<String, String> locales = new HashMap<>();

@Override
public String retrieveLocale(String lang) {
return locales.computeIfAbsent(lang, locale -> {
try {
URL url = getClass().getResource(LOCALES_ROOT + "/locales-" + locale + ".xml");
if (url == null) {
throw new IllegalArgumentException("Unable to load locale " + locale);
}

return CSLUtils.readURLToString(url, "UTF-8");
} catch (IOException e) {
throw new UncheckedIOException("failed to read locale " + locale, e);
}
});
}
}
1 change: 1 addition & 0 deletions src/main/resources/csl-locales
Submodule csl-locales added at 29ed2f
1 change: 1 addition & 0 deletions src/main/resources/csl-styles
Submodule csl-styles added at 6ed87f

0 comments on commit 11a62dd

Please sign in to comment.