Skip to content

Commit

Permalink
Remove hack and document InternalEntryTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
simonharrer committed Jan 26, 2016
1 parent cd8da64 commit 38aa3c0
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 72 deletions.
95 changes: 40 additions & 55 deletions src/main/java/net/sf/jabref/JabRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,59 +17,44 @@

import com.jgoodies.looks.plastic.Plastic3DLookAndFeel;
import com.jgoodies.looks.plastic.theme.SkyBluer;

import java.awt.Font;
import java.io.File;
import java.io.IOException;
import java.net.Authenticator;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Vector;
import java.util.prefs.BackingStoreException;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;

import net.sf.jabref.exporter.*;
import net.sf.jabref.gui.*;
import net.sf.jabref.gui.remote.JabRefMessageHandler;
import net.sf.jabref.gui.util.FocusRequester;
import net.sf.jabref.importer.*;
import net.sf.jabref.importer.fetcher.EntryFetcher;
import net.sf.jabref.importer.fetcher.EntryFetchers;
import net.sf.jabref.logic.CustomEntryTypesManager;
import net.sf.jabref.logic.journals.Abbreviations;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.labelPattern.LabelPatternUtil;
import net.sf.jabref.logic.logging.JabRefLogger;
import net.sf.jabref.logic.preferences.LastFocusedTabPreferences;
import net.sf.jabref.logic.remote.RemotePreferences;
import net.sf.jabref.logic.remote.client.RemoteListenerClient;
import net.sf.jabref.logic.search.DatabaseSearcher;
import net.sf.jabref.logic.search.SearchQuery;
import net.sf.jabref.logic.util.OS;
import net.sf.jabref.logic.util.io.FileBasedLock;
import net.sf.jabref.logic.util.strings.StringUtil;
import net.sf.jabref.migrations.PreferencesMigrations;
import net.sf.jabref.model.database.BibDatabase;
import net.sf.jabref.model.entry.BibEntry;

import net.sf.jabref.util.Util;
import net.sf.jabref.wizard.auximport.AuxCommandLine;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.sf.jabref.exporter.AutoSaveManager;
import net.sf.jabref.exporter.ExportFormats;
import net.sf.jabref.exporter.FileActions;
import net.sf.jabref.exporter.IExportFormat;
import net.sf.jabref.exporter.SaveException;
import net.sf.jabref.exporter.SaveSession;
import net.sf.jabref.importer.*;
import net.sf.jabref.logic.remote.RemotePreferences;
import net.sf.jabref.logic.remote.client.RemoteListenerClient;
import net.sf.jabref.gui.remote.JabRefMessageHandler;
import net.sf.jabref.gui.util.FocusRequester;
import net.sf.jabref.logic.util.io.FileBasedLock;
import net.sf.jabref.logic.util.strings.StringUtil;
import net.sf.jabref.logic.preferences.LastFocusedTabPreferences;
import net.sf.jabref.wizard.auximport.AuxCommandLine;
import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.Authenticator;
import java.nio.charset.Charset;
import java.util.*;
import java.util.List;
import java.util.prefs.BackingStoreException;

/**
* JabRef Main Class - The application gets started here.
Expand All @@ -82,7 +67,6 @@ public class JabRef {

private JabRefCLI cli;


public void start(String[] args) {
JabRefPreferences prefs = JabRefPreferences.getInstance();

Expand Down Expand Up @@ -288,18 +272,18 @@ public Optional<Vector<ParserResult>> processArguments(String[] args, boolean in

//read in the export format, take default format if no format entered
switch (data.length) {
case 3:
formatName = data[2];
break;
case 2:
//default ExportFormat: HTML table (with Abstract & BibTeX)
formatName = "tablerefsabsbib";
break;
default:
System.err.println(
Localization.lang("Output file missing").concat(". \n \t ").concat("Usage").concat(": ")
+ JabRefCLI.getExportMatchesSyntax());
return Optional.empty();
case 3:
formatName = data[2];
break;
case 2:
//default ExportFormat: HTML table (with Abstract & BibTeX)
formatName = "tablerefsabsbib";
break;
default:
System.err.println(
Localization.lang("Output file missing").concat(". \n \t ").concat("Usage").concat(": ")
+ JabRefCLI.getExportMatchesSyntax());
return Optional.empty();
} //end switch

//export new database
Expand Down Expand Up @@ -328,7 +312,7 @@ public Optional<Vector<ParserResult>> processArguments(String[] args, boolean in
regenerateBibtexKeys(loaded);
}

if(cli.isAutomaticallySetFileLinks()) {
if (cli.isAutomaticallySetFileLinks()) {
automaticallySetFileLinks(loaded);
}

Expand All @@ -344,8 +328,9 @@ public Optional<Vector<ParserResult>> processArguments(String[] args, boolean in
if (!pr.isInvalid()) {
try {
System.out.println(Localization.lang("Saving") + ": " + data[0]);
SaveSession session = FileActions.saveDatabase(new LoadedDatabase(pr.getDatabase(), pr.getMetaData(),
new File(data[0])), Globals.prefs, false, false,
SaveSession session = FileActions.saveDatabase(
new LoadedDatabase(pr.getDatabase(), pr.getMetaData()),
new File(data[0]), Globals.prefs, false, false,
Globals.prefs.getDefaultEncoding(), false);
// Show just a warning message if encoding didn't work for all characters:
if (!session.getWriter().couldEncodeAll()) {
Expand Down Expand Up @@ -432,8 +417,8 @@ public Optional<Vector<ParserResult>> processArguments(String[] args, boolean in

try {
System.out.println(Localization.lang("Saving") + ": " + subName);
SaveSession session = FileActions.saveDatabase(new LoadedDatabase(newBase, new MetaData(),
new File(subName)), Globals.prefs, false, false,
SaveSession session = FileActions.saveDatabase(new LoadedDatabase(newBase, new MetaData()),
new File(subName), Globals.prefs, false, false,
Globals.prefs.getDefaultEncoding(), false);
// Show just a warning message if encoding didn't work for all characters:
if (!session.getWriter().couldEncodeAll()) {
Expand Down Expand Up @@ -475,12 +460,12 @@ public Optional<Vector<ParserResult>> processArguments(String[] args, boolean in
}

private void automaticallySetFileLinks(Vector<ParserResult> loaded) {
for(ParserResult parserResult: loaded) {
for (ParserResult parserResult : loaded) {
BibDatabase database = parserResult.getDatabase();

MetaData metaData = parserResult.getMetaData();

if( metaData != null){
if (metaData != null) {
LOGGER.info(Localization.lang("Automatically setting file links"));
Util.autoSetLinks(database.getEntries(), metaData);
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/net/sf/jabref/bibtex/EntryTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

public class EntryTypes {

/**
* This class is used to specify entry types for either BIBTEX and BIBLATEX.
*/
private static class InternalEntryTypes {

private final Map<String, EntryType> ALL_TYPES = new TreeMap<>();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/sf/jabref/collab/ChangeScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ private void storeTempDatabase() {
@Override
public void run() {
try {
SaveSession ss = FileActions.saveDatabase(new LoadedDatabase(inTemp, mdInTemp,
Globals.fileUpdateMonitor.getTempFile(panel.fileMonitorHandle())), Globals.prefs,
SaveSession ss = FileActions.saveDatabase(new LoadedDatabase(inTemp, mdInTemp),
Globals.fileUpdateMonitor.getTempFile(panel.fileMonitorHandle()), Globals.prefs,
false, false, panel.getEncoding(), true);
ss.commit();
} catch (SaveException ex) {
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/net/sf/jabref/exporter/AutoSaveManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,15 @@ private static boolean autoSave(BasePanel panel) {
File databaseFile = panel.getLoadedDatabase().getDatabaseFile();
File backupFile = AutoSaveManager.getAutoSaveFile(databaseFile);
try {
SaveSession ss = FileActions.saveDatabase(new LoadedDatabase(panel.database(), panel.loadedDatabase.getMetaData(),
backupFile), Globals.prefs, false, false, panel.getEncoding(), true);
SaveSession ss = FileActions.saveDatabase(new LoadedDatabase(panel.database(), panel.loadedDatabase.getMetaData()),
backupFile, Globals.prefs, false, false, panel.getEncoding(), true);
ss.commit();
} catch (SaveException e) {
e.printStackTrace();
return false;
} catch (Throwable ex) {
ex.printStackTrace();
return false;
} finally {
// HACK required to reset the file field of the metadata
panel.loadedDatabase.getMetaData().setFile(databaseFile);
}
return true;
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/net/sf/jabref/exporter/FileActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private static void writeBibFileHeader(Writer out, Charset encoding) throws IOEx
* let the user save only the results of a search. False and false means all
* entries are saved.
*/
public static SaveSession saveDatabase(LoadedDatabase loadedDatabase, JabRefPreferences prefs,
public static SaveSession saveDatabase(LoadedDatabase loadedDatabase, File target, JabRefPreferences prefs,
boolean checkSearch, boolean checkGroup, Charset encoding, boolean suppressBackup)
throws SaveException {

Expand All @@ -183,7 +183,7 @@ public static SaveSession saveDatabase(LoadedDatabase loadedDatabase, JabRefPref
SaveSession session;
BibEntry exceptionCause = null;
try {
session = new SaveSession(loadedDatabase.getDatabaseFile(), encoding, backup);
session = new SaveSession(target, encoding, backup);
} catch (Throwable e) {
if (encoding != null) {
LOGGER.error("Error from encoding: '" + encoding.displayName(), e);
Expand Down Expand Up @@ -366,7 +366,7 @@ private static List<Comparator<BibEntry>> getSaveComparators(boolean isSaveOpera
*
* @return A List containing warnings, if any.
*/
public static SaveSession savePartOfDatabase(LoadedDatabase loadedDatabase,
public static SaveSession savePartOfDatabase(LoadedDatabase loadedDatabase, File target,
JabRefPreferences prefs, BibEntry[] bes, Charset encoding, DatabaseSaveType saveType)
throws SaveException {

Expand All @@ -376,7 +376,7 @@ public static SaveSession savePartOfDatabase(LoadedDatabase loadedDatabase,

SaveSession session;
try {
session = new SaveSession(loadedDatabase.getDatabaseFile(), encoding, backup);
session = new SaveSession( target, encoding, backup);
} catch (IOException e) {
throw new SaveException(e.getMessage(), e.getLocalizedMessage());
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/net/sf/jabref/exporter/SaveDatabaseAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,12 @@ private boolean saveDatabase(File file, boolean selectedOnly, Charset encoding)
frame.block();
try {
if (selectedOnly) {
session = FileActions.savePartOfDatabase(new LoadedDatabase(panel.database(), panel.loadedDatabase.getMetaData(), file), Globals.prefs,
session = FileActions.savePartOfDatabase(new LoadedDatabase(panel.database(), panel.loadedDatabase.getMetaData()),
file, Globals.prefs,
panel.getSelectedEntries(), encoding, FileActions.DatabaseSaveType.DEFAULT);
} else {
session = FileActions.saveDatabase(new LoadedDatabase(panel.database(), panel.loadedDatabase.getMetaData(), file),
Globals.prefs, false, false, encoding, false);
session = FileActions.saveDatabase(new LoadedDatabase(panel.database(), panel.loadedDatabase.getMetaData()),
file, Globals.prefs, false, false, encoding, false);
}

} catch (UnsupportedCharsetException ex2) {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/net/sf/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -1202,10 +1202,11 @@ private boolean saveDatabase(File file, boolean selectedOnly, Charset enc, FileA
final String SAVE_DATABASE = Localization.lang("Save database");
try {
if (selectedOnly) {
session = FileActions.savePartOfDatabase(new LoadedDatabase(database, loadedDatabase.getMetaData(), file), Globals.prefs,
mainTable.getSelectedEntries(), enc, saveType);
session = FileActions.savePartOfDatabase(new LoadedDatabase(database, loadedDatabase.getMetaData()),
file, Globals.prefs, mainTable.getSelectedEntries(), enc, saveType);
} else {
session = FileActions.saveDatabase(new LoadedDatabase(database, loadedDatabase.getMetaData(), file), Globals.prefs, false, false, enc, false);
session = FileActions.saveDatabase(new LoadedDatabase(database, loadedDatabase.getMetaData()),
file, Globals.prefs, false, false, enc, false);
}

} catch (UnsupportedCharsetException ex2) {
Expand Down

0 comments on commit 38aa3c0

Please sign in to comment.