Skip to content

Commit

Permalink
Removed last Globals dependency in MetaData (#1867)
Browse files Browse the repository at this point in the history
* Removed last Globals dependency in MetaData

* Removed Globals from logic tests
  • Loading branch information
oscargus authored Aug 27, 2016
1 parent 898014b commit 55fe381
Show file tree
Hide file tree
Showing 28 changed files with 148 additions and 139 deletions.
2 changes: 1 addition & 1 deletion src/jmh/java/net/sf/jabref/benchmarks/Benchmarks.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public String htmlToLatexConversion() {
@Benchmark
public boolean keywordGroupContains() throws ParseException {
KeywordGroup group = new KeywordGroup("testGroup", "keyword", "testkeyword", false, false,
GroupHierarchyType.INDEPENDENT, Globals.prefs);
GroupHierarchyType.INDEPENDENT, Globals.prefs.get(JabRefPreferences.KEYWORD_SEPARATOR));
return group.containsAll(database.getEntries());
}

Expand Down
26 changes: 14 additions & 12 deletions src/main/java/net/sf/jabref/MetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@ public class MetaData implements Iterable<String> {
* must simply make sure the appropriate changes are reflected in the Vector
* it has been passed.
*/
private MetaData(Map<String, String> inData) throws ParseException {
private MetaData(Map<String, String> inData, String keywordSeparator) throws ParseException {
Objects.requireNonNull(inData);
setData(inData);
setData(inData, keywordSeparator);
}
private MetaData(Map<String, String> inData, Charset encoding) throws ParseException {
this(inData);

private MetaData(Map<String, String> inData, Charset encoding, String keywordSeparator) throws ParseException {
this(inData, keywordSeparator);
this.encoding = Objects.requireNonNull(encoding);
}

Expand All @@ -86,15 +87,16 @@ public MetaData(Charset encoding) {
this.encoding = encoding;
}

public static MetaData parse(Map<String, String> data) throws ParseException {
return new MetaData(data);
public static MetaData parse(Map<String, String> data, String keywordSeparator) throws ParseException {
return new MetaData(data, keywordSeparator);
}

public static MetaData parse(Map<String, String> data, Charset encoding) throws ParseException {
return new MetaData(data, encoding);
public static MetaData parse(Map<String, String> data, Charset encoding, String keywordSeparator)
throws ParseException {
return new MetaData(data, encoding, keywordSeparator);
}

public void setData(Map<String, String> inData) throws ParseException {
public void setData(Map<String, String> inData, String keywordSeparator) throws ParseException {
clearMetaData();
for (Map.Entry<String, String> entry : inData.entrySet()) {
StringReader data = new StringReader(entry.getValue());
Expand All @@ -109,7 +111,7 @@ public void setData(Map<String, String> inData) throws ParseException {
LOGGER.error("Weird error while parsing meta data.", ex);
}
if (GROUPSTREE.equals(entry.getKey())) {
putGroups(orderedData);
putGroups(orderedData, keywordSeparator);
// the keys "groupsversion" and "groups" were used in JabRef versions around 1.3, we will not support them anymore
eventBus.post(new GroupUpdatedEvent(this));
} else if (SAVE_ACTIONS.equals(entry.getKey())) {
Expand Down Expand Up @@ -186,9 +188,9 @@ public void putData(String key, List<String> orderedData) {
*
* @param orderedData The vector of metadata strings
*/
private void putGroups(List<String> orderedData) throws ParseException {
private void putGroups(List<String> orderedData, String keywordSeparator) throws ParseException {
try {
groupsRoot = GroupTreeNode.parse(orderedData, Globals.prefs);
groupsRoot = GroupTreeNode.parse(orderedData, keywordSeparator);
eventBus.post(new GroupUpdatedEvent(this));
} catch (ParseException e) {
throw new ParseException(Localization.lang(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public void actionPerformed(ActionEvent e) {
return;
}

if (SpecialFieldsUtils.keywordSyncEnabled() && !keywordsToAdd.isEmpty()) {
if (Globals.prefs.isKeywordSyncEnabled() && !keywordsToAdd.isEmpty()) {
synchronizeSpecialFields(keywordsToAdd, keywordsToRemove);
}

Expand All @@ -312,7 +312,7 @@ private NamedCompound updateKeywords(List<BibEntry> entries, Set<String> keyword
ce.addEdit(new UndoableFieldChange(change.get()));
}

if (SpecialFieldsUtils.keywordSyncEnabled()) {
if (Globals.prefs.isKeywordSyncEnabled()) {
SpecialFieldsUtils.syncSpecialFieldsFromKeywords(entry, ce);
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/net/sf/jabref/gui/groups/AutoGroupDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.layout.format.LatexToUnicodeFormatter;
import net.sf.jabref.model.entry.FieldName;
import net.sf.jabref.preferences.JabRefPreferences;

import com.jgoodies.forms.builder.ButtonBarBuilder;
import com.jgoodies.forms.builder.FormBuilder;
Expand Down Expand Up @@ -82,7 +83,8 @@ public void actionPerformed(ActionEvent e) {
try {
GroupTreeNode autoGroupsRoot = GroupTreeNode.fromGroup(
new ExplicitGroup(Localization.lang("Automatically created groups"),
GroupHierarchyType.INCLUDING, Globals.prefs));
GroupHierarchyType.INCLUDING,
Globals.prefs.get(JabRefPreferences.KEYWORD_SEPARATOR)));
Set<String> hs;
String fieldText = field.getText();
if (keywords.isSelected()) {
Expand Down Expand Up @@ -110,7 +112,7 @@ public void actionPerformed(ActionEvent e) {

for (String keyword : hs) {
KeywordGroup group = new KeywordGroup(formatter.format(keyword), fieldText, keyword, false, false,
GroupHierarchyType.INDEPENDENT, Globals.prefs);
GroupHierarchyType.INDEPENDENT, Globals.prefs.get(JabRefPreferences.KEYWORD_SEPARATOR));
autoGroupsRoot.addChild(GroupTreeNode.fromGroup(group));
}

Expand Down
11 changes: 6 additions & 5 deletions src/main/java/net/sf/jabref/gui/groups/GroupDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,15 @@ public void actionPerformed(ActionEvent e) {
isOkPressed = true;
try {
if (explicitRadioButton.isSelected()) {
resultingGroup = new ExplicitGroup(nameField.getText().trim(), getContext(), Globals.prefs);
resultingGroup = new ExplicitGroup(nameField.getText().trim(), getContext(),
Globals.prefs.get(JabRefPreferences.KEYWORD_SEPARATOR));
} else if (keywordsRadioButton.isSelected()) {
// regex is correct, otherwise OK would have been disabled
// therefore I don't catch anything here
resultingGroup = new KeywordGroup(nameField.getText().trim(), keywordGroupSearchField.getText().trim(),
keywordGroupSearchTerm.getText().trim(), keywordGroupCaseSensitive.isSelected(), keywordGroupRegExp
.isSelected(),
getContext(), Globals.prefs);
resultingGroup = new KeywordGroup(nameField.getText().trim(),
keywordGroupSearchField.getText().trim(), keywordGroupSearchTerm.getText().trim(),
keywordGroupCaseSensitive.isSelected(), keywordGroupRegExp.isSelected(), getContext(),
Globals.prefs.get(JabRefPreferences.KEYWORD_SEPARATOR));
} else if (searchRadioButton.isSelected()) {
try {
// regex is correct, otherwise OK would have been
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private static void mergeFromBibtex(JabRefFrame frame, BasePanel panel, ParserRe
// create a dummy group
try {
ExplicitGroup group = new ExplicitGroup("Imported", GroupHierarchyType.INDEPENDENT,
Globals.prefs);
Globals.prefs.get(JabRefPreferences.KEYWORD_SEPARATOR));
newGroups.setGroup(group);
group.add(appendedEntries);
} catch (ParseException e) {
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/net/sf/jabref/logic/groups/AbstractGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import net.sf.jabref.logic.search.SearchMatcher;
import net.sf.jabref.model.database.BibDatabase;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.preferences.JabRefPreferences;

/**
* A group of BibtexEntries.
Expand Down Expand Up @@ -48,9 +47,9 @@ public abstract class AbstractGroup implements SearchMatcher {
* @throws ParseException If an error occurred and a group could not be created,
* e.g. due to a malformed regular expression.
*/
public static AbstractGroup fromString(String s, JabRefPreferences jabRefPreferences) throws ParseException {
public static AbstractGroup fromString(String s, String keywordSeparator) throws ParseException {
if (s.startsWith(KeywordGroup.ID)) {
return KeywordGroup.fromString(s, jabRefPreferences);
return KeywordGroup.fromString(s, keywordSeparator);
}
if (s.startsWith(AllEntriesGroup.ID)) {
return AllEntriesGroup.fromString(s);
Expand All @@ -59,7 +58,7 @@ public static AbstractGroup fromString(String s, JabRefPreferences jabRefPrefere
return SearchGroup.fromString(s);
}
if (s.startsWith(ExplicitGroup.ID)) {
return ExplicitGroup.fromString(s, jabRefPreferences);
return ExplicitGroup.fromString(s, keywordSeparator);
}
return null; // unknown group
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/net/sf/jabref/logic/groups/ExplicitGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import net.sf.jabref.logic.util.strings.QuotedStringTokenizer;
import net.sf.jabref.logic.util.strings.StringUtil;
import net.sf.jabref.model.entry.FieldName;
import net.sf.jabref.preferences.JabRefPreferences;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand All @@ -29,12 +28,13 @@ public class ExplicitGroup extends KeywordGroup {

private static final Log LOGGER = LogFactory.getLog(ExplicitGroup.class);

public ExplicitGroup(String name, GroupHierarchyType context, JabRefPreferences jabRefPreferences)

public ExplicitGroup(String name, GroupHierarchyType context, String keywordSeparator)
throws ParseException {
super(name, FieldName.GROUPS, name, true, false, context, jabRefPreferences);
super(name, FieldName.GROUPS, name, true, false, context, keywordSeparator);
}

public static ExplicitGroup fromString(String s, JabRefPreferences jabRefPreferences) throws ParseException {
public static ExplicitGroup fromString(String s, String keywordSeparator) throws ParseException {
if (!s.startsWith(ExplicitGroup.ID)) {
throw new IllegalArgumentException("ExplicitGroup cannot be created from \"" + s + "\".");
}
Expand All @@ -43,7 +43,7 @@ public static ExplicitGroup fromString(String s, JabRefPreferences jabRefPrefere

String name = tok.nextToken();
int context = Integer.parseInt(tok.nextToken());
ExplicitGroup newGroup = new ExplicitGroup(name, GroupHierarchyType.getByNumber(context), jabRefPreferences);
ExplicitGroup newGroup = new ExplicitGroup(name, GroupHierarchyType.getByNumber(context), keywordSeparator);
newGroup.addLegacyEntryKeys(tok);
return newGroup;
}
Expand All @@ -68,7 +68,7 @@ public void addLegacyEntryKey(String key) {
@Override
public AbstractGroup deepCopy() {
try {
ExplicitGroup copy = new ExplicitGroup(getName(), getContext(), jabRefPreferences);
ExplicitGroup copy = new ExplicitGroup(getName(), getContext(), keywordSeparator);
copy.legacyEntryKeys.addAll(legacyEntryKeys);
return copy;
} catch (ParseException exception) {
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/net/sf/jabref/logic/groups/GroupTreeNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import net.sf.jabref.logic.search.matchers.MatcherSets;
import net.sf.jabref.model.database.BibDatabase;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.preferences.JabRefPreferences;

/**
* A node in the groups tree that holds exactly one AbstractGroup.
Expand Down Expand Up @@ -234,9 +233,9 @@ public GroupTreeNode copyNode() {
return GroupTreeNode.fromGroup(group);
}

public static GroupTreeNode parse(List<String> orderedData, JabRefPreferences jabRefPreferences)
public static GroupTreeNode parse(List<String> orderedData, String keywordSeparator)
throws ParseException {
return GroupsParser.importGroups(orderedData, jabRefPreferences);
return GroupsParser.importGroups(orderedData, keywordSeparator);
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/net/sf/jabref/logic/groups/GroupsParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

import net.sf.jabref.logic.importer.util.ParseException;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.preferences.JabRefPreferences;

/**
* Converts string representation of groups to a parsed {@link GroupTreeNode}.
*/
class GroupsParser {

public static GroupTreeNode importGroups(List<String> orderedData, JabRefPreferences jabRefPreferences)
public static GroupTreeNode importGroups(List<String> orderedData, String keywordSeparator)
throws ParseException {
GroupTreeNode cursor = null;
GroupTreeNode root = null;
Expand All @@ -27,7 +26,7 @@ public static GroupTreeNode importGroups(List<String> orderedData, JabRefPrefere
throw new ParseException(Localization.lang("Expected \"%0\" to contain whitespace", string));
}
int level = Integer.parseInt(string.substring(0, spaceIndex));
AbstractGroup group = AbstractGroup.fromString(string.substring(spaceIndex + 1), jabRefPreferences);
AbstractGroup group = AbstractGroup.fromString(string.substring(spaceIndex + 1), keywordSeparator);
GroupTreeNode newNode = GroupTreeNode.fromGroup(group);
if (cursor == null) {
// create new root
Expand Down
25 changes: 11 additions & 14 deletions src/main/java/net/sf/jabref/logic/groups/KeywordGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import net.sf.jabref.model.FieldChange;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.EntryUtil;
import net.sf.jabref.preferences.JabRefPreferences;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand All @@ -32,7 +31,7 @@ public class KeywordGroup extends AbstractGroup {
private final boolean regExp;
private Pattern pattern;
private final List<String> searchWords;
protected final JabRefPreferences jabRefPreferences;
protected final String keywordSeparator;

private static final Log LOGGER = LogFactory.getLog(KeywordGroup.class);

Expand All @@ -42,7 +41,7 @@ public class KeywordGroup extends AbstractGroup {
*/
public KeywordGroup(String name, String searchField,
String searchExpression, boolean caseSensitive, boolean regExp,
GroupHierarchyType context, JabRefPreferences jabRefPreferences) throws ParseException {
GroupHierarchyType context, String keywordSeparator) throws ParseException {
super(name, context);
this.searchField = searchField;
this.searchExpression = searchExpression;
Expand All @@ -51,7 +50,7 @@ public KeywordGroup(String name, String searchField,
if (this.regExp) {
compilePattern();
}
this.jabRefPreferences = jabRefPreferences;
this.keywordSeparator = keywordSeparator;
this.searchWords = EntryUtil.getStringAsWords(searchExpression);
}

Expand All @@ -70,7 +69,7 @@ private void compilePattern() throws ParseException {
* @param s The String representation obtained from
* KeywordGroup.toString()
*/
public static AbstractGroup fromString(String s, JabRefPreferences jabRefPreferences) throws ParseException {
public static AbstractGroup fromString(String s, String keywordSeparator) throws ParseException {
if (!s.startsWith(KeywordGroup.ID)) {
throw new IllegalArgumentException("KeywordGroup cannot be created from \"" + s + "\".");
}
Expand All @@ -86,7 +85,7 @@ public static AbstractGroup fromString(String s, JabRefPreferences jabRefPrefere
return new KeywordGroup(StringUtil.unquote(name, AbstractGroup.QUOTE_CHAR),
StringUtil.unquote(field, AbstractGroup.QUOTE_CHAR),
StringUtil.unquote(expression, AbstractGroup.QUOTE_CHAR), caseSensitive, regExp,
GroupHierarchyType.getByNumber(context), jabRefPreferences);
GroupHierarchyType.getByNumber(context), keywordSeparator);
}

/**
Expand Down Expand Up @@ -125,9 +124,8 @@ public Optional<EntriesGroupChange> add(List<BibEntry> entriesToAdd) {
for (BibEntry entry : entriesToAdd) {
if (!contains(entry)) {
String oldContent = entry.getFieldOptional(searchField).orElse(null);
String pre = jabRefPreferences.get(JabRefPreferences.KEYWORD_SEPARATOR);
String newContent = (oldContent == null ? "" : oldContent
+ pre)
+ keywordSeparator)
+ searchExpression;
entry.setField(searchField, newContent);

Expand Down Expand Up @@ -263,21 +261,20 @@ private void removeMatches(BibEntry entry) {
int i;
int j;
int k;
final String separator = jabRefPreferences.get(JabRefPreferences.KEYWORD_SEPARATOR);
while ((i = haystack.indexOf(needle)) >= 0) {
sbOrig.replace(i, i + needle.length(), "");
sbLower.replace(i, i + needle.length(), "");
// reduce spaces at i to 1
j = i;
k = i;
while (((j - 1) >= 0) && (separator.indexOf(haystack.charAt(j - 1)) >= 0)) {
while (((j - 1) >= 0) && (keywordSeparator.indexOf(haystack.charAt(j - 1)) >= 0)) {
--j;
}
while ((k < haystack.length()) && (separator.indexOf(haystack.charAt(k)) >= 0)) {
while ((k < haystack.length()) && (keywordSeparator.indexOf(haystack.charAt(k)) >= 0)) {
++k;
}
sbOrig.replace(j, k, (j >= 0) && (k < sbOrig.length()) ? separator : "");
sbLower.replace(j, k, (j >= 0) && (k < sbOrig.length()) ? separator : "");
sbOrig.replace(j, k, (j >= 0) && (k < sbOrig.length()) ? keywordSeparator : "");
sbLower.replace(j, k, (j >= 0) && (k < sbOrig.length()) ? keywordSeparator : "");
}

String result = sbOrig.toString().trim();
Expand All @@ -293,7 +290,7 @@ private void removeMatches(BibEntry entry) {
public AbstractGroup deepCopy() {
try {
return new KeywordGroup(getName(), searchField, searchExpression,
caseSensitive, regExp, getContext(), jabRefPreferences);
caseSensitive, regExp, getContext(), keywordSeparator);
} catch (ParseException exception) {
// this should never happen, because the constructor obviously succeeded in creating _this_ instance!
LOGGER.error("Internal error in KeywordGroup.deepCopy(). "
Expand Down
Loading

0 comments on commit 55fe381

Please sign in to comment.