Skip to content

Commit

Permalink
Fix OO default jstyle loading from resources (#12308)
Browse files Browse the repository at this point in the history
* Fix OO default jstyle loading from resources

Fixes #12307

* fix architecture violation
  • Loading branch information
Siedlerchr authored Dec 17, 2024
1 parent 006bbd0 commit e3ba5e6
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/main/java/org/jabref/logic/openoffice/style/JStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -19,6 +21,7 @@
import java.util.TreeSet;
import java.util.regex.Pattern;

import org.jabref.architecture.AllowedToUseClassGetResource;
import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.logic.layout.Layout;
import org.jabref.logic.layout.LayoutFormatter;
Expand Down Expand Up @@ -55,6 +58,7 @@
* 3) If the entries are not numbered, a citation marker must be produced for each entry. This
* operation is performed for each JabRef BibEntry.
*/
@AllowedToUseClassGetResource("Required for jstyle loading")
public class JStyle implements Comparable<JStyle>, OOStyle {

public static final String ITALIC_ET_AL = "ItalicEtAl";
Expand Down Expand Up @@ -160,7 +164,7 @@ public JStyle(String resourcePath, LayoutFormatterPreferences layoutPreferences,
setDefaultProperties();
// we need to distinguish if it's a style from the local resources or a file on disk
InputStream stream = JStyle.class.getResourceAsStream(resourcePath);
styleFile = Path.of(resourcePath);
styleFile = Path.of(resourcePath).toAbsolutePath();
fromResource = true;
if (stream == null) {
stream = Files.newInputStream(styleFile);
Expand Down Expand Up @@ -274,8 +278,19 @@ public void ensureUpToDate() throws IOException {
*/
private void reload() throws IOException {
if (styleFile != null) {
this.styleFileModificationTime = Files.getLastModifiedTime(styleFile).toMillis();
try (InputStream stream = Files.newInputStream(styleFile)) {
Path resourcePath = styleFile;
if (fromResource) {
try {
URL resUrl = JStyle.class.getResource(path);
if (resUrl != null) {
resourcePath = Path.of(resUrl.toURI());
}
} catch (URISyntaxException ex) {
LOGGER.error("Couldn't resolve resource path for style {}", path, ex);
}
}
this.styleFileModificationTime = Files.getLastModifiedTime(resourcePath).toMillis();
try (InputStream stream = Files.newInputStream(resourcePath)) {
initialize(stream);
}
}
Expand Down

0 comments on commit e3ba5e6

Please sign in to comment.