From 44b362fc4a03891e45b09bb565d08c89c866281a Mon Sep 17 00:00:00 2001 From: Florian Beetz Date: Mon, 29 Oct 2018 12:26:02 +0100 Subject: [PATCH 1/2] add citation styles as git submodule --- .gitmodules | 3 + build.gradle | 1 - .../logic/citationstyle/CitationStyle.java | 67 +++++++++---------- src/main/resources/csl-styles | 1 + 4 files changed, 36 insertions(+), 36 deletions(-) create mode 100644 .gitmodules create mode 160000 src/main/resources/csl-styles diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000000..09f27319d3e --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "src/main/resources/csl-styles"] + path = src/main/resources/csl-styles + url = https://github.com/citation-style-language/styles.git diff --git a/build.gradle b/build.gradle index a88052930d2..2312c985326 100644 --- a/build.gradle +++ b/build.gradle @@ -152,7 +152,6 @@ dependencies { 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' diff --git a/src/main/java/org/jabref/logic/citationstyle/CitationStyle.java b/src/main/java/org/jabref/logic/citationstyle/CitationStyle.java index 02010cbbea6..f1ebe894836 100644 --- a/src/main/java/org/jabref/logic/citationstyle/CitationStyle.java +++ b/src/main/java/org/jabref/logic/citationstyle/CitationStyle.java @@ -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; @@ -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; @@ -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 STYLES = new ArrayList<>(); @@ -88,7 +84,7 @@ private static Optional 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; } @@ -99,13 +95,13 @@ private static String stripInvalidProlog(String source) { */ public static Optional 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()); @@ -140,36 +136,37 @@ public static List 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 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 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 discoverCitationStylesInPath(Path path) throws IOException { + try (Stream 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(); } /** diff --git a/src/main/resources/csl-styles b/src/main/resources/csl-styles new file mode 160000 index 00000000000..6ed87f55fd7 --- /dev/null +++ b/src/main/resources/csl-styles @@ -0,0 +1 @@ +Subproject commit 6ed87f55fd75fcecbf8f6b8a96d5edd0c935702e From 299bdb1f27de3687ce39bd2ba3d83b6eb4e60150 Mon Sep 17 00:00:00 2001 From: Florian Beetz Date: Mon, 29 Oct 2018 17:03:16 +0100 Subject: [PATCH 2/2] add CSL locales as git submodule --- .gitmodules | 3 ++ build.gradle | 2 - .../logic/citationstyle/CSLAdapter.java | 3 +- .../citationstyle/JabRefLocaleProvider.java | 38 +++++++++++++++++++ src/main/resources/csl-locales | 1 + 5 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/jabref/logic/citationstyle/JabRefLocaleProvider.java create mode 160000 src/main/resources/csl-locales diff --git a/.gitmodules b/.gitmodules index 09f27319d3e..b37b24cf6d9 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +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 diff --git a/build.gradle b/build.gradle index 2312c985326..1c14214c094 100644 --- a/build.gradle +++ b/build.gradle @@ -151,8 +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:locales:1.0.1-SNAPSHOT' compile 'de.undercouch:citeproc-java:1.0.1' compile 'com.github.tomtung:latex2unicode_2.12:0.2.2' diff --git a/src/main/java/org/jabref/logic/citationstyle/CSLAdapter.java b/src/main/java/org/jabref/logic/citationstyle/CSLAdapter.java index 7ce1e57e6f6..cda929568d2 100644 --- a/src/main/java/org/jabref/logic/citationstyle/CSLAdapter.java +++ b/src/main/java/org/jabref/logic/citationstyle/CSLAdapter.java @@ -64,7 +64,8 @@ public synchronized List makeBibliography(List 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; } diff --git a/src/main/java/org/jabref/logic/citationstyle/JabRefLocaleProvider.java b/src/main/java/org/jabref/logic/citationstyle/JabRefLocaleProvider.java new file mode 100644 index 00000000000..ecfc18d55de --- /dev/null +++ b/src/main/java/org/jabref/logic/citationstyle/JabRefLocaleProvider.java @@ -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 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); + } + }); + } +} diff --git a/src/main/resources/csl-locales b/src/main/resources/csl-locales new file mode 160000 index 00000000000..29ed2ff4328 --- /dev/null +++ b/src/main/resources/csl-locales @@ -0,0 +1 @@ +Subproject commit 29ed2ff43284f726f9f583981650a86ffb9b236f