From ca5b33f83f6d13f585b0d7eb90220bd74ddac93d Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Mon, 29 Jan 2018 00:43:47 +0100 Subject: [PATCH] Fixes #3648: Chained modifiers work again --- CHANGELOG.md | 1 + .../bibtexkeypattern/BibtexKeyGenerator.java | 9 ++- .../BracketedPattern.java | 57 ++++++++----------- .../org/jabref/logic/util/io/FileUtil.java | 2 +- .../logic/util/io/RegExpBasedFileFinder.java | 2 +- .../BibtexKeyGeneratorTest.java | 14 +++++ .../logic/util/BracketedPatternTest.java | 4 +- 7 files changed, 49 insertions(+), 40 deletions(-) rename src/main/java/org/jabref/logic/{util => bibtexkeypattern}/BracketedPattern.java (96%) diff --git a/CHANGELOG.md b/CHANGELOG.md index e9ace2d6b02..fa73f71a687 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ For more details refer to the [field mapping help page](http://help.jabref.org/e - We fixed the missing dot in the name of an exported file. [#3576](https://github.com/JabRef/jabref/issues/3576) - Autocompletion in the search bar can now be disabled via the preferences. [#3598](https://github.com/JabRef/jabref/issues/3598) - We fixed and extended the RIS import functionality to cover more fields. [#3634](https://github.com/JabRef/jabref/issues/3634) [#2607](https://github.com/JabRef/jabref/issues/2607) +- Chaining modifiers in BibTeX key pattern now works as described in the documentation. [#3648](https://github.com/JabRef/jabref/issues/3648) ### Removed - We removed the [Look and Feels from JGoodies](http://www.jgoodies.com/freeware/libraries/looks/), because the open source version is not compatible with Java 9. diff --git a/src/main/java/org/jabref/logic/bibtexkeypattern/BibtexKeyGenerator.java b/src/main/java/org/jabref/logic/bibtexkeypattern/BibtexKeyGenerator.java index bf3bad6ad75..66d66d82ad9 100644 --- a/src/main/java/org/jabref/logic/bibtexkeypattern/BibtexKeyGenerator.java +++ b/src/main/java/org/jabref/logic/bibtexkeypattern/BibtexKeyGenerator.java @@ -6,7 +6,6 @@ import java.util.Objects; import java.util.Optional; -import org.jabref.logic.util.BracketedPattern; import org.jabref.model.FieldChange; import org.jabref.model.bibtexkeypattern.AbstractBibtexKeyPattern; import org.jabref.model.bibtexkeypattern.GlobalBibtexKeyPattern; @@ -46,13 +45,13 @@ public BibtexKeyGenerator(AbstractBibtexKeyPattern citeKeyPattern, BibDatabase d this.bibtexKeyPatternPreferences = Objects.requireNonNull(bibtexKeyPatternPreferences); } - static String generateKey(BibEntry entry, String value) { - return generateKey(entry, value, new BibDatabase()); + static String generateKey(BibEntry entry, String pattern) { + return generateKey(entry, pattern, new BibDatabase()); } - static String generateKey(BibEntry entry, String value, BibDatabase database) { + static String generateKey(BibEntry entry, String pattern, BibDatabase database) { GlobalBibtexKeyPattern keyPattern = new GlobalBibtexKeyPattern(Collections.emptyList()); - keyPattern.setDefaultValue("[" + value + "]"); + keyPattern.setDefaultValue("[" + pattern + "]"); return new BibtexKeyGenerator(keyPattern, database, new BibtexKeyPatternPreferences("", "", false, true, true, keyPattern, ',')) .generateKey(entry); } diff --git a/src/main/java/org/jabref/logic/util/BracketedPattern.java b/src/main/java/org/jabref/logic/bibtexkeypattern/BracketedPattern.java similarity index 96% rename from src/main/java/org/jabref/logic/util/BracketedPattern.java rename to src/main/java/org/jabref/logic/bibtexkeypattern/BracketedPattern.java index d4efe8a8085..624f6711e90 100644 --- a/src/main/java/org/jabref/logic/util/BracketedPattern.java +++ b/src/main/java/org/jabref/logic/bibtexkeypattern/BracketedPattern.java @@ -1,4 +1,4 @@ -package org.jabref.logic.util; +package org.jabref.logic.bibtexkeypattern; import java.util.ArrayList; import java.util.Arrays; @@ -338,40 +338,33 @@ else if (val.matches("edtr\\d+")) { * @param offset The number of initial items in the modifiers array to skip. * @return The modified label. */ - public static String applyModifiers(final String label, final List parts, final int offset) { + static String applyModifiers(final String label, final List parts, final int offset) { String resultingLabel = label; - if (parts.size() > offset) { - for (int j = offset; j < parts.size(); j++) { - String modifier = parts.get(j); - - if ("abbr".equals(modifier)) { - // Abbreviate - that is, - StringBuilder abbreviateSB = new StringBuilder(); - String[] words = resultingLabel.replaceAll("[\\{\\}']", "") - .split("[\\(\\) \r\n\"]"); - for (String word : words) { - if (!word.isEmpty()) { - abbreviateSB.append(word.charAt(0)); - } + for (int j = offset; j < parts.size(); j++) { + String modifier = parts.get(j); + + if ("abbr".equals(modifier)) { + // Abbreviate - that is, + StringBuilder abbreviateSB = new StringBuilder(); + String[] words = resultingLabel.replaceAll("[\\{\\}']", "") + .split("[\\(\\) \r\n\"]"); + for (String word : words) { + if (!word.isEmpty()) { + abbreviateSB.append(word.charAt(0)); } - resultingLabel = abbreviateSB.toString(); - } else { - Optional formatter = Formatters.getFormatterForModifier(modifier); - if (formatter.isPresent()) { - resultingLabel = formatter.get().format(label); - } else if (!modifier.isEmpty() && (modifier.length() >= 2) && (modifier.charAt(0) == '(') && modifier.endsWith(")")) { - // Alternate text modifier in parentheses. Should be inserted if - // the label is empty: - if (label.isEmpty() && (modifier.length() > 2)) { - resultingLabel = modifier.substring(1, modifier.length() - 1); - } else { - resultingLabel = label; - } - } else { - // LOGGER.info("Key generator warning: unknown modifier '" - // + modifier + "'."); - resultingLabel = label; + } + resultingLabel = abbreviateSB.toString(); + } else { + Optional formatter = Formatters.getFormatterForModifier(modifier); + if (formatter.isPresent()) { + resultingLabel = formatter.get().format(resultingLabel); + } else if (!modifier.isEmpty() && (modifier.length() >= 2) && (modifier.charAt(0) == '(') && modifier.endsWith(")")) { + // Alternate text modifier in parentheses. Should be inserted if the label is empty + if (label.isEmpty() && (modifier.length() > 2)) { + resultingLabel = modifier.substring(1, modifier.length() - 1); } + } else { + LOGGER.warn("Key generator warning: unknown modifier '" + modifier + "'."); } } } diff --git a/src/main/java/org/jabref/logic/util/io/FileUtil.java b/src/main/java/org/jabref/logic/util/io/FileUtil.java index 6daa188f5b4..2354ecb61ef 100644 --- a/src/main/java/org/jabref/logic/util/io/FileUtil.java +++ b/src/main/java/org/jabref/logic/util/io/FileUtil.java @@ -21,10 +21,10 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import org.jabref.logic.bibtexkeypattern.BracketedPattern; import org.jabref.logic.layout.Layout; import org.jabref.logic.layout.LayoutFormatterPreferences; import org.jabref.logic.layout.LayoutHelper; -import org.jabref.logic.util.BracketedPattern; import org.jabref.model.database.BibDatabase; import org.jabref.model.entry.BibEntry; import org.jabref.model.util.OptionalUtil; diff --git a/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java b/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java index b33074a7011..0c405a1b3cc 100644 --- a/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java +++ b/src/main/java/org/jabref/logic/util/io/RegExpBasedFileFinder.java @@ -17,7 +17,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -import org.jabref.logic.util.BracketedPattern; +import org.jabref.logic.bibtexkeypattern.BracketedPattern; import org.jabref.model.database.BibDatabase; import org.jabref.model.entry.BibEntry; import org.jabref.model.strings.StringUtil; diff --git a/src/test/java/org/jabref/logic/bibtexkeypattern/BibtexKeyGeneratorTest.java b/src/test/java/org/jabref/logic/bibtexkeypattern/BibtexKeyGeneratorTest.java index 7dd2729b19c..b855f9bc6d9 100644 --- a/src/test/java/org/jabref/logic/bibtexkeypattern/BibtexKeyGeneratorTest.java +++ b/src/test/java/org/jabref/logic/bibtexkeypattern/BibtexKeyGeneratorTest.java @@ -987,4 +987,18 @@ public void generateKeyStripsApostropheFromTitle() throws Exception { entry.setField("title", "Green Scheduling of `Whatever`"); assertEquals("GreenSchedulingofWhatever", BibtexKeyGenerator.generateKey(entry, "title")); } + + @Test + public void generateKeyWithOneModifier() throws Exception { + BibEntry entry = new BibEntry(); + entry.setField("title", "The Interesting Title"); + assertEquals("theinterestingtitle", BibtexKeyGenerator.generateKey(entry, "title:lower")); + } + + @Test + public void generateKeyWithTwoModifiers() throws Exception { + BibEntry entry = new BibEntry(); + entry.setField("title", "The Interesting Title"); + assertEquals("theinterestingtitle", BibtexKeyGenerator.generateKey(entry, "title:lower:(_)")); + } } diff --git a/src/test/java/org/jabref/logic/util/BracketedPatternTest.java b/src/test/java/org/jabref/logic/util/BracketedPatternTest.java index 82d223a6cc8..d10814053d1 100644 --- a/src/test/java/org/jabref/logic/util/BracketedPatternTest.java +++ b/src/test/java/org/jabref/logic/util/BracketedPatternTest.java @@ -1,5 +1,6 @@ package org.jabref.logic.util; +import org.jabref.logic.bibtexkeypattern.BracketedPattern; import org.jabref.model.database.BibDatabase; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.BibtexEntryTypes; @@ -9,7 +10,8 @@ import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; public class BracketedPatternTest { private BibEntry bibentry;