Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ to [sourceforge feature requests](https://sourceforge.net/p/jabref/features/) by
## [Unreleased]

### Changed
- Implemented [#756](https://github.com/JabRef/jabref/issues/756): Add possibility to reformat all entries on save (under Preferences, File)
- Comments and preamble are serialized with capitalized first letter, i.e. `@Comment` instead of `@comment` and `@Preamble` instead of `@PREAMBLE`.
- Global sorting options and preferences are removed. Databases can still be sorted on save, but this is configured locally and stored in the file
- OvidImporter now also imports fields: doi, issn, language and keywords
- Implemented [#647](https://github.com/JabRef/jabref/issues/647): The preview can now be copied
Expand All @@ -26,6 +28,7 @@ to [sourceforge feature requests](https://sourceforge.net/p/jabref/features/) by


### Fixed
- Fixed [#621](https://github.com/JabRef/jabref/issues/621) and [#669](https://github.com/JabRef/jabref/issues/669): Encoding and preamble now end with newline.
- Make BibTex parser more robust against missing newlines
- Fix bug that prevented the import of BibTex entries having only a key as content
- Fixed [#666](https://github.com/JabRef/jabref/issues/666): MS Office 2007 export is working again
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/net/sf/jabref/BibDatabaseContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public BibDatabaseContext(BibDatabase database, MetaData metaData, Defaults defa
this.defaults = Objects.requireNonNull(defaults);
this.database = Objects.requireNonNull(database);
this.metaData = Objects.requireNonNull(metaData);

this.setMode(getMode());
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you delete this? What happens if you load a bib file and save it afterwards - is there a comment about the type stored in the file then still?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the line because otherwise the biblatex mode would be written every time even if it is the default one. In addition, I found it irritating that a constructor changed values in the parameters passed to it (just using a metadata in a specific context shouldn't change it values).

But yes, the biblatex mode is correctly written. I added a test for it now.

public BibDatabaseContext(BibDatabase database, MetaData metaData, File file, Defaults defaults) {
Expand Down
28 changes: 17 additions & 11 deletions src/main/java/net/sf/jabref/JabRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,13 @@ public Optional<Vector<ParserResult>> processArguments(String[] args, boolean in
if (!pr.isInvalid()) {
try {
System.out.println(Localization.lang("Saving") + ": " + data[0]);
Defaults defaults = new Defaults(BibDatabaseMode.fromPreference(Globals.prefs.getBoolean(JabRefPreferences.BIBLATEX_MODE)));
SaveSession session = FileActions.saveDatabase(
new BibDatabaseContext(pr.getDatabase(), pr.getMetaData(), defaults),
new File(data[0]), Globals.prefs, false, false,
Globals.prefs.getDefaultEncoding(), false);
SavePreferences prefs = SavePreferences.loadForSaveFromPreferences(Globals.prefs);
Defaults defaults = new Defaults(BibDatabaseMode
.fromPreference(Globals.prefs.getBoolean(JabRefPreferences.BIBLATEX_MODE)));
BibDatabaseWriter databaseWriter = new BibDatabaseWriter();
SaveSession session = databaseWriter.saveDatabase(
new BibDatabaseContext(pr.getDatabase(), pr.getMetaData(), defaults), prefs);

// Show just a warning message if encoding didn't work for all characters:
if (!session.getWriter().couldEncodeAll()) {
System.err.println(Localization.lang("Warning") + ": "
Expand All @@ -323,7 +325,7 @@ public Optional<Vector<ParserResult>> processArguments(String[] args, boolean in
+ " "
+ session.getWriter().getProblemCharacters());
}
session.commit();
session.commit(new File(data[0]));
} catch (SaveException ex) {
System.err.println(Localization.lang("Could not save file.") + "\n"
+ ex.getLocalizedMessage());
Expand Down Expand Up @@ -399,10 +401,14 @@ public Optional<Vector<ParserResult>> processArguments(String[] args, boolean in

try {
System.out.println(Localization.lang("Saving") + ": " + subName);
Defaults defaults = new Defaults(BibDatabaseMode.fromPreference(Globals.prefs.getBoolean(JabRefPreferences.BIBLATEX_MODE)));
SaveSession session = FileActions.saveDatabase(new BibDatabaseContext(newBase, defaults),
new File(subName), Globals.prefs, false, false,
Globals.prefs.getDefaultEncoding(), false);
SavePreferences prefs = SavePreferences.loadForSaveFromPreferences(Globals.prefs);
BibDatabaseWriter databaseWriter = new BibDatabaseWriter();
Defaults defaults = new Defaults(BibDatabaseMode
.fromPreference(Globals.prefs.getBoolean(JabRefPreferences.BIBLATEX_MODE)));
SaveSession session = databaseWriter.saveDatabase(
new BibDatabaseContext(newBase, defaults), prefs);


// Show just a warning message if encoding didn't work for all characters:
if (!session.getWriter().couldEncodeAll()) {
System.err.println(Localization.lang("Warning") + ": "
Expand All @@ -412,7 +418,7 @@ public Optional<Vector<ParserResult>> processArguments(String[] args, boolean in
+ " "
+ session.getWriter().getProblemCharacters());
}
session.commit();
session.commit(new File(subName));
} catch (SaveException ex) {
System.err.println(Localization.lang("Could not save file.") + "\n"
+ ex.getLocalizedMessage());
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/net/sf/jabref/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
public class JabRefPreferences {
private static final Log LOGGER = LogFactory.getLog(JabRefPreferences.class);
public static final String EXTERNAL_FILE_TYPES = "externalFileTypes";

/**
* HashMap that contains all preferences which are set by default
*/
Expand Down Expand Up @@ -110,6 +109,7 @@ public class JabRefPreferences {
public static final String TABLE_SECONDARY_SORT_DESCENDING = "secDescending";
public static final String TABLE_TERTIARY_SORT_FIELD = "terSort";
public static final String TABLE_TERTIARY_SORT_DESCENDING = "terDescending";
public static final String REFORMAT_FILE_ON_SAVE_AND_EXPORT = "reformatFileOnSaveAndExport";
public static final String EXPORT_IN_ORIGINAL_ORDER = "exportInOriginalOrder";
public static final String EXPORT_IN_SPECIFIED_ORDER = "exportInSpecifiedOrder";
public static final String EXPORT_PRIMARY_SORT_FIELD = "exportPriSort";
Expand Down Expand Up @@ -505,6 +505,8 @@ private JabRefPreferences() {
defaults.put(TABLE_TERTIARY_SORT_FIELD, "title");
defaults.put(TABLE_TERTIARY_SORT_DESCENDING, Boolean.FALSE);

defaults.put(REFORMAT_FILE_ON_SAVE_AND_EXPORT, Boolean.FALSE);

// export order
defaults.put(EXPORT_IN_ORIGINAL_ORDER, Boolean.FALSE);
defaults.put(EXPORT_IN_SPECIFIED_ORDER, Boolean.FALSE);
Expand Down
60 changes: 7 additions & 53 deletions src/main/java/net/sf/jabref/MetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@
import net.sf.jabref.logic.labelPattern.DatabaseLabelPattern;
import net.sf.jabref.model.database.BibDatabase;
import net.sf.jabref.sql.DBStrings;
import net.sf.jabref.logic.util.strings.StringUtil;

public class MetaData implements Iterable<String> {

public static final String META_FLAG = "jabref-meta: ";
public static final String SAVE_ORDER_CONFIG = "saveOrderConfig";
private static final String PREFIX_KEYPATTERN = "keypattern_";
private static final String KEYPATTERNDEFAULT = "keypatterndefault";
static final String DATABASE_TYPE = "DATABASE_TYPE";
public static final String GROUPSVERSION = "groupsversion";
public static final String GROUPSTREE = "groupstree";
public static final String GROUPS = "groups";

private final Map<String, List<String>> metaData = new HashMap<>();
private GroupTreeNode groupsRoot;
Expand Down Expand Up @@ -70,16 +73,16 @@ public MetaData(Map<String, String> inData, BibDatabase db) {
} catch (IOException ex) {
System.err.println("Weird error while parsing meta data.");
}
if ("groupsversion".equals(entry.getKey())) {
if (GROUPSVERSION.equals(entry.getKey())) {
if (!orderedData.isEmpty()) {
groupsVersionOnDisk = Integer.parseInt(orderedData.get(0));
}
} else if ("groupstree".equals(entry.getKey())) {
} else if (GROUPSTREE.equals(entry.getKey())) {
groupsTreePresent = true;
treeGroupsData = orderedData; // save for later user
// actual import operation is handled later because "groupsversion"
// tag might not yet have been read
} else if ("groups".equals(entry.getKey())) {
} else if (GROUPS.equals(entry.getKey())) {
flatGroupsData = orderedData;
} else {
putData(entry.getKey(), orderedData);
Expand Down Expand Up @@ -260,55 +263,6 @@ public void setGroups(GroupTreeNode root) {
groupTreeValid = true;
}

/**
* Writes all data to the specified writer, using each object's toString()
* method.
*/
public void writeMetaData(Writer out) throws IOException {
// write all meta data except groups
SortedSet<String> sortedKeys = new TreeSet<>(metaData.keySet());

for (String key : sortedKeys) {

StringBuffer sb = new StringBuffer();
sb.append(Globals.NEWLINE).append(Globals.NEWLINE);
List<String> orderedData = metaData.get(key);
sb.append("@comment{").append(META_FLAG).append(key).append(':');
for (String data : orderedData) {
sb.append(StringUtil.quote(data, ";", '\\')).append(';');
}
sb.append('}');

out.write(sb.toString());
}
// write groups if present. skip this if only the root node exists
// (which is always the AllEntriesGroup).
if ((groupsRoot != null) && (groupsRoot.getChildCount() > 0)) {
StringBuffer sb = new StringBuffer();
// write version first
sb.append(Globals.NEWLINE).append(Globals.NEWLINE);
sb.append("@comment{").append(META_FLAG).append("groupsversion:");
sb.append(VersionHandling.CURRENT_VERSION).append(";}");

out.write(sb.toString());

// now write actual groups
sb = new StringBuffer();
sb.append(Globals.NEWLINE).append(Globals.NEWLINE);
sb.append("@comment{").append(META_FLAG).append("groupstree:");
sb.append(Globals.NEWLINE);
// GroupsTreeNode.toString() uses "\n" for separation
StringTokenizer tok = new StringTokenizer(groupsRoot.getTreeAsString(), Globals.NEWLINE);
while (tok.hasMoreTokens()) {
StringBuffer s = new StringBuffer(StringUtil.quote(tok.nextToken(), ";", '\\')).append(';');
sb.append(s);
sb.append(Globals.NEWLINE);
}
sb.append('}');
out.write(sb.toString());
}
}

/**
* Reads the next unit. Units are delimited by ';'.
*/
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/net/sf/jabref/bibtex/BibEntryWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ public BibEntryWriter(LatexFieldFormatter fieldFormatter, boolean write) {
}

public void write(BibEntry entry, Writer out, BibDatabaseMode bibDatabaseMode) throws IOException {
write(entry, out, bibDatabaseMode, false);
}
public void write(BibEntry entry, Writer out, BibDatabaseMode bibDatabaseMode, Boolean reformat) throws IOException {
// if the entry has not been modified, write it as it was
if (!entry.hasChanged()) {
if (!reformat && !entry.hasChanged()) {
out.write(entry.getParsedSerialization());
return;
}
out.write(Globals.NEWLINE + Globals.NEWLINE);

out.write(Globals.NEWLINE);
writeRequiredFieldsFirstRemainingFieldsSecond(entry, out, bibDatabaseMode);
out.write(Globals.NEWLINE);
}

public void writeWithoutPrependedNewlines(BibEntry entry, Writer out, BibDatabaseMode bibDatabaseMode) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import net.sf.jabref.gui.InternalBibtexFields;
import net.sf.jabref.gui.maintable.MainTableFormat;
import net.sf.jabref.logic.config.SaveOrderConfig;
import net.sf.jabref.logic.util.strings.StringUtil;
import net.sf.jabref.model.entry.AuthorList;
import net.sf.jabref.model.entry.MonthUtil;
Expand All @@ -27,6 +28,7 @@
import java.text.ParseException;
import java.text.RuleBasedCollator;
import java.util.Comparator;
import java.util.Objects;

/**
*
Expand Down Expand Up @@ -71,7 +73,7 @@ public FieldComparator(String field) {
}

public FieldComparator(String field, boolean reversed) {
this.fieldName = field;
this.fieldName = Objects.requireNonNull(field);
this.field = field.split(MainTableFormat.COL_DEFINITION_FIELD_SEPARATOR);
multiplier = reversed ? -1 : 1;
isTypeHeader = this.field[0].equals(BibEntry.TYPE_HEADER);
Expand All @@ -82,6 +84,10 @@ public FieldComparator(String field, boolean reversed) {
isNumeric = InternalBibtexFields.isNumeric(this.field[0]);
}

public FieldComparator(SaveOrderConfig.SortCriterion sortCriterion) {
this(sortCriterion.field, sortCriterion.descending);
}

@Override
public int compare(BibEntry e1, BibEntry e2) {
Object f1;
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/net/sf/jabref/collab/ChangeScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
import org.apache.commons.logging.LogFactory;

import net.sf.jabref.*;
import net.sf.jabref.exporter.FileActions;
import net.sf.jabref.exporter.BibDatabaseWriter;
import net.sf.jabref.exporter.SaveException;
import net.sf.jabref.exporter.SavePreferences;
import net.sf.jabref.exporter.SaveSession;
import net.sf.jabref.groups.GroupTreeNode;
import net.sf.jabref.gui.BasePanel;
Expand Down Expand Up @@ -158,11 +159,14 @@ private void storeTempDatabase() {
@Override
public void run() {
try {
SavePreferences prefs = SavePreferences.loadForSaveFromPreferences(Globals.prefs)
.withMakeBackup(false)
.withEncoding(panel.getEncoding());

Defaults defaults = new Defaults(BibDatabaseMode.fromPreference(Globals.prefs.getBoolean(JabRefPreferences.BIBLATEX_MODE)));
SaveSession ss = FileActions.saveDatabase(new BibDatabaseContext(inTemp, mdInTemp, defaults),
Globals.fileUpdateMonitor.getTempFile(panel.fileMonitorHandle()), Globals.prefs, false,
false, panel.getEncoding(), true);
ss.commit();
BibDatabaseWriter databaseWriter = new BibDatabaseWriter();
SaveSession ss = databaseWriter.saveDatabase(new BibDatabaseContext(inTemp, mdInTemp, defaults), prefs);
ss.commit(Globals.fileUpdateMonitor.getTempFile(panel.fileMonitorHandle()));
} catch (SaveException ex) {
LOGGER.warn("Problem updating tmp file after accepting external changes", ex);
}
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/net/sf/jabref/exporter/AutoSaveManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,13 @@ private static boolean autoSave(BasePanel panel) {
File databaseFile = panel.getBibDatabaseContext().getDatabaseFile();
File backupFile = AutoSaveManager.getAutoSaveFile(databaseFile);
try {
SaveSession ss = FileActions.saveDatabase(panel.getBibDatabaseContext(),
backupFile, Globals.prefs, false, false, panel.getEncoding(), true);
ss.commit();
SavePreferences prefs = SavePreferences.loadForSaveFromPreferences(Globals.prefs)
.withMakeBackup(false)
.withEncoding(panel.getEncoding());

BibDatabaseWriter databaseWriter = new BibDatabaseWriter();
SaveSession ss = databaseWriter.saveDatabase(panel.getBibDatabaseContext(), prefs);

} catch (SaveException e) {
LOGGER.error("Problem with automatic save", e);
return false;
Expand Down
Loading