Skip to content

Commit

Permalink
Added LayoutFormatterPreferences (and related files) (#1608)
Browse files Browse the repository at this point in the history
* Added LayoutFormatterPreferences (and related files)

* Rebased

* Included JournalAbbreviationLoader in LayoutPreferences
  • Loading branch information
oscargus committed Jul 22, 2016
1 parent c6aa7da commit 23fdfe2
Show file tree
Hide file tree
Showing 21 changed files with 308 additions and 181 deletions.
5 changes: 4 additions & 1 deletion src/main/java/net/sf/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.labelpattern.LabelPatternUtil;
import net.sf.jabref.logic.layout.Layout;
import net.sf.jabref.logic.layout.LayoutFormatterPreferences;
import net.sf.jabref.logic.layout.LayoutHelper;
import net.sf.jabref.logic.util.UpdateField;
import net.sf.jabref.logic.util.io.FileBasedLock;
Expand Down Expand Up @@ -991,7 +992,9 @@ private void copyKeyAndTitle() {
"\\bibtexkey - \\begin{title}\\format[RemoveBrackets]{\\title}\\end{title}\n");
Layout layout;
try {
layout = new LayoutHelper(sr, Globals.prefs, Globals.journalAbbreviationLoader).getLayoutFromText();
layout = new LayoutHelper(sr,
LayoutFormatterPreferences.fromPreferences(Globals.prefs, Globals.journalAbbreviationLoader))
.getLayoutFromText();
} catch (IOException e) {
LOGGER.info("Could not get layout", e);
return;
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/net/sf/jabref/gui/PreviewPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import net.sf.jabref.logic.exporter.ExportFormats;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.layout.Layout;
import net.sf.jabref.logic.layout.LayoutFormatterPreferences;
import net.sf.jabref.logic.layout.LayoutHelper;
import net.sf.jabref.logic.search.SearchQueryHighlightListener;
import net.sf.jabref.model.entry.BibEntry;
Expand Down Expand Up @@ -263,7 +264,8 @@ private void updateLayout() {
StringReader sr = new StringReader(layoutFile.replace("__NEWLINE__", "\n"));
try {
layout = Optional
.of(new LayoutHelper(sr, Globals.prefs, Globals.journalAbbreviationLoader).getLayoutFromText());
.of(new LayoutHelper(sr, LayoutFormatterPreferences.fromPreferences(Globals.prefs,
Globals.journalAbbreviationLoader)).getLayoutFromText());
} catch (IOException e) {
layout = Optional.empty();
LOGGER.debug("no layout could be set", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import net.sf.jabref.gui.worker.AbstractWorker;
import net.sf.jabref.logic.help.HelpFile;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.layout.LayoutFormatterPreferences;
import net.sf.jabref.logic.openoffice.OOBibStyle;
import net.sf.jabref.logic.openoffice.OpenOfficePreferences;
import net.sf.jabref.logic.openoffice.StyleLoader;
Expand Down Expand Up @@ -126,7 +127,8 @@ private OpenOfficePanel() {
update = new JButton(IconTheme.JabRefIcon.REFRESH.getSmallIcon());
update.setToolTipText(Localization.lang("Sync OpenOffice/LibreOffice bibliography"));
preferences = new OpenOfficePreferences(Globals.prefs);
loader = new StyleLoader(preferences, Globals.prefs, Globals.journalAbbreviationLoader,
loader = new StyleLoader(preferences,
LayoutFormatterPreferences.fromPreferences(Globals.prefs, Globals.journalAbbreviationLoader),
Globals.prefs.getDefaultEncoding());
}

Expand Down
13 changes: 9 additions & 4 deletions src/main/java/net/sf/jabref/logic/exporter/ExportFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import net.sf.jabref.BibDatabaseContext;
import net.sf.jabref.Globals;
import net.sf.jabref.logic.layout.Layout;
import net.sf.jabref.logic.layout.LayoutFormatterPreferences;
import net.sf.jabref.logic.layout.LayoutHelper;
import net.sf.jabref.model.entry.BibEntry;

Expand Down Expand Up @@ -219,7 +220,8 @@ public void performExport(final BibDatabaseContext databaseContext, final String

// Print header
try (Reader reader = getReader(lfFileName + ".begin.layout")) {
LayoutHelper layoutHelper = new LayoutHelper(reader, Globals.prefs, Globals.journalAbbreviationLoader);
LayoutHelper layoutHelper = new LayoutHelper(reader,
LayoutFormatterPreferences.fromPreferences(Globals.prefs, Globals.journalAbbreviationLoader));
beginLayout = layoutHelper.getLayoutFromText();
} catch (IOException ex) {
// If an exception was cast, export filter doesn't have a begin
Expand All @@ -245,7 +247,8 @@ public void performExport(final BibDatabaseContext databaseContext, final String
Layout defLayout;
LayoutHelper layoutHelper;
try (Reader reader = getReader(lfFileName + ".layout")) {
layoutHelper = new LayoutHelper(reader, Globals.prefs, Globals.journalAbbreviationLoader);
layoutHelper = new LayoutHelper(reader,
LayoutFormatterPreferences.fromPreferences(Globals.prefs, Globals.journalAbbreviationLoader));
defLayout = layoutHelper.getLayoutFromText();
}
if (defLayout != null) {
Expand All @@ -267,7 +270,8 @@ public void performExport(final BibDatabaseContext databaseContext, final String
} else {
try (Reader reader = getReader(lfFileName + '.' + type + ".layout")) {
// We try to get a type-specific layout for this entry.
layoutHelper = new LayoutHelper(reader, Globals.prefs, Globals.journalAbbreviationLoader);
layoutHelper = new LayoutHelper(reader, LayoutFormatterPreferences
.fromPreferences(Globals.prefs, Globals.journalAbbreviationLoader));
layout = layoutHelper.getLayoutFromText();
layouts.put(type, layout);
if (layout != null) {
Expand All @@ -291,7 +295,8 @@ public void performExport(final BibDatabaseContext databaseContext, final String
// changed section - begin (arudert)
Layout endLayout = null;
try (Reader reader = getReader(lfFileName + ".end.layout")) {
layoutHelper = new LayoutHelper(reader, Globals.prefs, Globals.journalAbbreviationLoader);
layoutHelper = new LayoutHelper(reader,
LayoutFormatterPreferences.fromPreferences(Globals.prefs, Globals.journalAbbreviationLoader));
endLayout = layoutHelper.getLayoutFromText();
} catch (IOException ex) {
// If an exception was thrown, export filter doesn't have an end
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/net/sf/jabref/logic/layout/Layout.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
import java.util.regex.Pattern;

import net.sf.jabref.BibDatabaseContext;
import net.sf.jabref.logic.journals.JournalAbbreviationLoader;
import net.sf.jabref.model.database.BibDatabase;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.preferences.JabRefPreferences;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand All @@ -42,7 +40,7 @@ public class Layout {
private static final Log LOGGER = LogFactory.getLog(Layout.class);


public Layout(List<StringInt> parsedEntries, JabRefPreferences prefs, JournalAbbreviationLoader repositoryLoader) {
public Layout(List<StringInt> parsedEntries, LayoutFormatterPreferences prefs) {
List<LayoutEntry> tmpEntries = new ArrayList<>(parsedEntries.size());

List<StringInt> blockEntries = null;
Expand All @@ -68,7 +66,7 @@ public Layout(List<StringInt> parsedEntries, JabRefPreferences prefs, JournalAbb
blockEntries.add(parsedEntry);
le = new LayoutEntry(blockEntries,
parsedEntry.i == LayoutHelper.IS_FIELD_END ? LayoutHelper.IS_FIELD_START : LayoutHelper.IS_GROUP_START,
prefs, repositoryLoader);
prefs);
tmpEntries.add(le);
blockEntries = null;
} else {
Expand All @@ -83,7 +81,7 @@ public Layout(List<StringInt> parsedEntries, JabRefPreferences prefs, JournalAbb
}

if (blockEntries == null) {
tmpEntries.add(new LayoutEntry(parsedEntry, prefs, repositoryLoader));
tmpEntries.add(new LayoutEntry(parsedEntry, prefs));
} else {
blockEntries.add(parsedEntry);
}
Expand Down
31 changes: 12 additions & 19 deletions src/main/java/net/sf/jabref/logic/layout/LayoutEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import net.sf.jabref.BibDatabaseContext;
import net.sf.jabref.logic.formatter.bibtexfields.HtmlToLatexFormatter;
import net.sf.jabref.logic.formatter.bibtexfields.UnicodeToLatexFormatter;
import net.sf.jabref.logic.journals.JournalAbbreviationLoader;
import net.sf.jabref.logic.journals.JournalAbbreviationPreferences;
import net.sf.jabref.logic.layout.format.AuthorAbbreviator;
import net.sf.jabref.logic.layout.format.AuthorAndsCommaReplacer;
import net.sf.jabref.logic.layout.format.AuthorAndsReplacer;
Expand Down Expand Up @@ -95,7 +93,6 @@
import net.sf.jabref.logic.util.strings.StringUtil;
import net.sf.jabref.model.database.BibDatabase;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.preferences.JabRefPreferences;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand All @@ -117,13 +114,10 @@ class LayoutEntry {

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

private final JournalAbbreviationLoader repositoryLoader;
private final LayoutFormatterPreferences prefs;

private final JabRefPreferences prefs;


public LayoutEntry(StringInt si, JabRefPreferences prefs, JournalAbbreviationLoader repositoryLoader) {
this.repositoryLoader = repositoryLoader;
public LayoutEntry(StringInt si, LayoutFormatterPreferences prefs) {
this.prefs = prefs;
type = si.i;
switch (type) {
Expand All @@ -143,9 +137,7 @@ public LayoutEntry(StringInt si, JabRefPreferences prefs, JournalAbbreviationLoa
}
}

public LayoutEntry(List<StringInt> parsedEntries, int layoutType, JabRefPreferences prefs,
JournalAbbreviationLoader repositoryLoader) {
this.repositoryLoader = repositoryLoader;
public LayoutEntry(List<StringInt> parsedEntries, int layoutType, LayoutFormatterPreferences prefs) {
this.prefs = prefs;
List<LayoutEntry> tmpEntries = new ArrayList<>();
String blockStart = parsedEntries.get(0).s;
Expand All @@ -171,7 +163,7 @@ public LayoutEntry(List<StringInt> parsedEntries, int layoutType, JabRefPreferen
blockEntries.add(parsedEntry);
int groupType = parsedEntry.i == LayoutHelper.IS_GROUP_END ? LayoutHelper.IS_GROUP_START :
LayoutHelper.IS_FIELD_START;
LayoutEntry le = new LayoutEntry(blockEntries, groupType, prefs, repositoryLoader);
LayoutEntry le = new LayoutEntry(blockEntries, groupType, prefs);
tmpEntries.add(le);
blockEntries = null;
} else {
Expand All @@ -187,7 +179,7 @@ public LayoutEntry(List<StringInt> parsedEntries, int layoutType, JabRefPreferen
}

if (blockEntries == null) {
tmpEntries.add(new LayoutEntry(parsedEntry, prefs, repositoryLoader));
tmpEntries.add(new LayoutEntry(parsedEntry, prefs));
} else {
blockEntries.add(parsedEntry);
}
Expand Down Expand Up @@ -514,7 +506,8 @@ private LayoutFormatter getLayoutFormatterByName(String name) throws Exception {
case "Iso690NamesAuthors":
return new Iso690NamesAuthors();
case "JournalAbbreviator":
return new JournalAbbreviator(repositoryLoader, JournalAbbreviationPreferences.fromPreferences(prefs));
return new JournalAbbreviator(prefs.getJournalAbbreviationLoader(),
prefs.getJournalAbbreviationPreferences());
case "LastPage":
return new LastPage();
case "FormatChars": // For backward compatibility
Expand Down Expand Up @@ -551,7 +544,7 @@ private LayoutFormatter getLayoutFormatterByName(String name) throws Exception {
case "Default":
return new Default();
case "FileLink":
return new FileLink(prefs);
return new FileLink(prefs.getFileLinkPreferences());
case "Number":
return new Number();
case "RisAuthors":
Expand All @@ -565,7 +558,7 @@ private LayoutFormatter getLayoutFormatterByName(String name) throws Exception {
case "WrapContent":
return new WrapContent();
case "WrapFileLinks":
return new WrapFileLinks(prefs);
return new WrapFileLinks(prefs.getFileLinkPreferences());
default:
return new NotFoundFormatter(name);
}
Expand All @@ -582,15 +575,15 @@ private List<LayoutFormatter> getOptionalLayout(String formatterName) {

List<LayoutFormatter> results = new ArrayList<>(formatterStrings.size());

Map<String, String> userNameFormatter = NameFormatter.getNameFormatters(prefs);
Map<String, String> userNameFormatter = NameFormatter.getNameFormatters(prefs.getNameFormatterPreferences());

for (List<String> strings : formatterStrings) {

String className = strings.get(0).trim();

// Check if this is a name formatter defined by this export filter:
if (prefs.customExportNameFormatters != null) {
String contents = prefs.customExportNameFormatters.get(className);
if (prefs.getCustomExportNameFormatters() != null) {
String contents = prefs.getCustomExportNameFormatters().get(className);
if (contents != null) {
NameFormatter nf = new NameFormatter();
nf.setParameter(contents);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package net.sf.jabref.logic.layout;

import java.util.Map;
import java.util.Objects;

import net.sf.jabref.logic.journals.JournalAbbreviationLoader;
import net.sf.jabref.logic.journals.JournalAbbreviationPreferences;
import net.sf.jabref.logic.layout.format.FileLinkPreferences;
import net.sf.jabref.logic.layout.format.NameFormatterPreferences;
import net.sf.jabref.preferences.JabRefPreferences;

public class LayoutFormatterPreferences {

private final NameFormatterPreferences nameFormatterPreferences;
private final JournalAbbreviationPreferences journalAbbreviationPreferences;
private final FileLinkPreferences fileLinkPreferences;
private final Map<String, String> customExportNameFormatters;
private final JournalAbbreviationLoader journalAbbreviationLoader;

public LayoutFormatterPreferences(NameFormatterPreferences nameFormatterPreferences,
JournalAbbreviationPreferences journalAbbreviationPreferences, FileLinkPreferences fileLinkPreferences,
Map<String, String> customExportNameFormatters, JournalAbbreviationLoader journalAbbreviationLoader) {
this.nameFormatterPreferences = nameFormatterPreferences;
this.journalAbbreviationPreferences = journalAbbreviationPreferences;
this.customExportNameFormatters = customExportNameFormatters;
this.fileLinkPreferences = fileLinkPreferences;
this.journalAbbreviationLoader = journalAbbreviationLoader;
}

public static LayoutFormatterPreferences fromPreferences(JabRefPreferences jabRefPreferences,
JournalAbbreviationLoader journalAbbreviationLoader) {
Objects.requireNonNull(jabRefPreferences);
Objects.requireNonNull(journalAbbreviationLoader);
return new LayoutFormatterPreferences(NameFormatterPreferences.fromPreferences(jabRefPreferences),
JournalAbbreviationPreferences.fromPreferences(jabRefPreferences),
FileLinkPreferences.fromPreferences(jabRefPreferences),
jabRefPreferences.customExportNameFormatters, journalAbbreviationLoader);
}

public NameFormatterPreferences getNameFormatterPreferences() {
return nameFormatterPreferences;
}

public JournalAbbreviationPreferences getJournalAbbreviationPreferences() {
return journalAbbreviationPreferences;
}

public Map<String, String> getCustomExportNameFormatters() {
return customExportNameFormatters;
}

public FileLinkPreferences getFileLinkPreferences() {
return fileLinkPreferences;
}

public JournalAbbreviationLoader getJournalAbbreviationLoader() {
return journalAbbreviationLoader;
}
}
11 changes: 3 additions & 8 deletions src/main/java/net/sf/jabref/logic/layout/LayoutHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
import java.util.List;
import java.util.Objects;

import net.sf.jabref.logic.journals.JournalAbbreviationLoader;
import net.sf.jabref.preferences.JabRefPreferences;

/**
* Helper class to get a Layout object.
*
Expand All @@ -51,14 +48,12 @@ public class LayoutHelper {

private final PushbackReader in;
private final List<StringInt> parsedEntries = new ArrayList<>();
private final JournalAbbreviationLoader repositoryLoader;
private final JabRefPreferences prefs;
private final LayoutFormatterPreferences prefs;
private boolean endOfFile;


public LayoutHelper(Reader in, JabRefPreferences prefs, JournalAbbreviationLoader repositoryLoader) {
public LayoutHelper(Reader in, LayoutFormatterPreferences prefs) {
this.in = new PushbackReader(Objects.requireNonNull(in));
this.repositoryLoader = Objects.requireNonNull(repositoryLoader);
this.prefs = Objects.requireNonNull(prefs);
}

Expand All @@ -73,7 +68,7 @@ public Layout getLayoutFromText() throws IOException {
}
}

return new Layout(parsedEntries, prefs, repositoryLoader);
return new Layout(parsedEntries, prefs);
}

public static String getCurrentGroup() {
Expand Down
16 changes: 6 additions & 10 deletions src/main/java/net/sf/jabref/logic/layout/format/FileLink.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,13 @@

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import net.sf.jabref.Globals;
import net.sf.jabref.logic.layout.ParamLayoutFormatter;
import net.sf.jabref.logic.util.io.FileUtil;
import net.sf.jabref.model.entry.FieldName;
import net.sf.jabref.model.entry.FileField;
import net.sf.jabref.model.entry.ParsedFileField;
import net.sf.jabref.preferences.JabRefPreferences;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand All @@ -41,11 +37,11 @@ public class FileLink implements ParamLayoutFormatter {
private static final Log LOGGER = LogFactory.getLog(FileLink.class);

private String fileType;
private final JabRefPreferences prefs;
private final FileLinkPreferences prefs;


public FileLink(JabRefPreferences prefs) {
this.prefs = prefs;
public FileLink(FileLinkPreferences fileLinkPreferences) {
this.prefs = fileLinkPreferences;
}

@Override
Expand Down Expand Up @@ -82,10 +78,10 @@ public String format(String field) {
// but that is not available from a formatter. Therefore, as an
// ugly hack, the export routine has set a global variable before
// starting the export, which contains the database's file directory:
if (prefs.fileDirForDatabase == null) {
dirs = Collections.singletonList(prefs.get(FieldName.FILE + Globals.DIR_SUFFIX));
if (prefs.getFileDirForDatabase() == null) {
dirs = prefs.getGeneratedDirForDatabase();
} else {
dirs = prefs.fileDirForDatabase;
dirs = prefs.getFileDirForDatabase();
}

Optional<File> f = FileUtil.expandFilename(link, dirs);
Expand Down
Loading

0 comments on commit 23fdfe2

Please sign in to comment.