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

Add citation styles as git submodule #4431

Merged
merged 2 commits into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 .gitmodules
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

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
1 change: 1 addition & 0 deletions src/main/resources/csl-styles
Submodule csl-styles added at 6ed87f