Skip to content

Commit

Permalink
Merge branch 'master' into file-based-bibtex-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
simonharrer committed Jan 30, 2016
2 parents 3f8595f + 12810a5 commit 47208c1
Show file tree
Hide file tree
Showing 46 changed files with 358 additions and 340 deletions.
26 changes: 9 additions & 17 deletions src/main/java/net/sf/jabref/exporter/AutoSaveManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@

import java.util.Timer;
import java.util.TimerTask;
import java.util.List;
import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;

/**
* Background task and utilities for autosave feature.
*/
public class AutoSaveManager {

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

private final JabRefFrame frame;
private Timer t;

Expand Down Expand Up @@ -65,12 +69,7 @@ public void run() {
// Since this method is running in the background, we must be prepared that
// there could be changes done by the user while this method is running.

List<BasePanel> panels = new ArrayList<>();
for (int i = 0; i < frame.getBasePanelCount(); i++) {
panels.add(frame.getBasePanelAt(i));
}

for (BasePanel panel : panels) {
for (BasePanel panel : frame.getBasePanelList()) {
if (panel.isModified() && (panel.getBibDatabaseContext().getDatabaseFile() != null)) {
AutoSaveManager.autoSave(panel);
}
Expand Down Expand Up @@ -101,10 +100,7 @@ private static boolean autoSave(BasePanel panel) {
backupFile, Globals.prefs, false, false, panel.getEncoding(), true);
ss.commit();
} catch (SaveException e) {
e.printStackTrace();
return false;
} catch (Throwable ex) {
ex.printStackTrace();
LOGGER.error("Problem with automatic save", e);
return false;
}
return true;
Expand Down Expand Up @@ -132,11 +128,7 @@ public static boolean deleteAutoSaveFile(BasePanel panel) {
* if they exist.
*/
public void clearAutoSaves() {
List<BasePanel> panels = new ArrayList<>();
for (int i = 0; i < frame.getBasePanelCount(); i++) {
panels.add(frame.getBasePanelAt(i));
}
for (BasePanel panel : panels) {
for (BasePanel panel : frame.getBasePanelList()) {
AutoSaveManager.deleteAutoSaveFile(panel);
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/net/sf/jabref/exporter/ExportFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,8 @@ private Reader getReader(String filename) throws IOException {
* net.sf.jabref.MetaData, java.lang.String, java.lang.String, java.util.Set)
*/
@Override
public void performExport(final BibDatabase database,
final MetaData metaData, final String file,
final Charset enc, Set<String> entryIds) throws Exception {
public void performExport(final BibDatabase database, final MetaData metaData, final String file, final Charset enc,
Set<String> entryIds) throws Exception {

File outFile = new File(file);
SaveSession ss = null;
Expand Down Expand Up @@ -363,7 +362,7 @@ public FileFilter getFileFilter() {
return fileFilter;
}

public void finalizeSaveSession(final SaveSession ss) throws Exception {
public void finalizeSaveSession(final SaveSession ss) throws SaveException, IOException {
ss.getWriter().flush();
ss.getWriter().close();

Expand Down
7 changes: 6 additions & 1 deletion src/main/java/net/sf/jabref/exporter/ExportFormats.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.sf.jabref.*;
import net.sf.jabref.gui.JabRefFrame;
import net.sf.jabref.gui.actions.MnemonicAwareAction;
Expand All @@ -42,6 +45,8 @@
*/
public class ExportFormats {

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

private static final Map<String, IExportFormat> EXPORT_FORMATS = new TreeMap<>();

// Global variable that is used for counting output entries when exporting:
Expand Down Expand Up @@ -232,7 +237,7 @@ public void run() {
finFile.getPath(), frame
.getCurrentBasePanel().getEncoding(), finEntryIDs);
} catch (Exception ex) {
ex.printStackTrace();
LOGGER.warn("Problem exporting", ex);
if (ex.getMessage() == null) {
errorMessage = ex.toString();
} else {
Expand Down
23 changes: 11 additions & 12 deletions src/main/java/net/sf/jabref/exporter/FileActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ private static void writeString(Writer fw, BibtexString bs, Map<String, BibtexSt
String suffix = suffixSB.toString();

fw.write("@String { " + bs.getName() + suffix + " = ");
if (!bs.getContent().isEmpty()) {
if (bs.getContent().isEmpty()) {
fw.write("{}");
} else {
try {
String formatted = new LatexFieldFormatter().format(bs.getContent(), LatexFieldFormatter.BIBTEX_STRING);
fw.write(formatted);
Expand All @@ -146,9 +148,6 @@ private static void writeString(Writer fw, BibtexString bs, Map<String, BibtexSt
"The # character is not allowed in BibTeX strings unless escaped as in '\\#'.\n"
+ "Before saving, please edit any strings containing the # character.");
}

} else {
fw.write("{}");
}

fw.write(" }" + Globals.NEWLINE);
Expand Down Expand Up @@ -283,10 +282,7 @@ public static SaveSession saveDatabase(BibDatabaseContext bibDatabaseContext, Fi
}

private static List<BibEntry> applySaveActions(List<BibEntry> toChange, MetaData metaData) {
if (metaData.getData(SaveActions.META_KEY) != null) {
// no save actions defined -> do nothing
return toChange;
} else {
if (metaData.getData(SaveActions.META_KEY) == null) {
// save actions defined -> apply for every entry
List<BibEntry> result = new ArrayList<>(toChange.size());

Expand All @@ -297,6 +293,9 @@ private static List<BibEntry> applySaveActions(List<BibEntry> toChange, MetaData
}

return result;
} else {
// no save actions defined -> do nothing
return toChange;
}
}

Expand Down Expand Up @@ -477,16 +476,16 @@ public static Reader getReader(String name) throws IOException {
URL reso = Globals.class.getResource(name);

// If that didn't work, try loading as a normal file URL:
if (reso != null) {
if (reso == null) {
File f = new File(name);
try {
reader = new InputStreamReader(reso.openStream());
reader = new FileReader(f);
} catch (FileNotFoundException ex) {
throw new IOException("Cannot find layout file: '" + name + "'.");
}
} else {
File f = new File(name);
try {
reader = new FileReader(f);
reader = new InputStreamReader(reso.openStream());
} catch (FileNotFoundException ex) {
throw new IOException("Cannot find layout file: '" + name + "'.");
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/sf/jabref/exporter/IExportFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public interface IExportFormat {
*/
void performExport(BibDatabase database, MetaData metaData,
String file, Charset encoding,
Set<String> entryIds) throws Exception;
Set<String> entryIds)
throws Exception;

}
6 changes: 4 additions & 2 deletions src/main/java/net/sf/jabref/exporter/MSBibExportFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

Expand Down Expand Up @@ -58,11 +60,11 @@ public void performExport(final BibDatabase database, final MetaData metaData, f
Transformer trans = TransformerFactory.newInstance().newTransformer();
trans.setOutputProperty(OutputKeys.INDENT, "yes");
trans.transform(source, result);
} catch (Exception e) {
} catch (TransformerException | IllegalArgumentException | TransformerFactoryConfigurationError e) {
throw new Error(e);
}
finalizeSaveSession(ss);
} catch (Exception ex) {
} catch (SaveException | IOException ex) {
throw new IOException(ex.getMessage());
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/net/sf/jabref/exporter/ModsExportFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.OutputKeys;
import java.util.Set;
import java.io.IOException;
Expand Down Expand Up @@ -54,11 +56,11 @@ public void performExport(final BibDatabase database, final MetaData metaData,
Transformer trans = TransformerFactory.newInstance().newTransformer();
trans.setOutputProperty(OutputKeys.INDENT, "yes");
trans.transform(source, result);
} catch (Exception e) {
} catch (TransformerException | IllegalArgumentException | TransformerFactoryConfigurationError e) {
throw new Error(e);
}
finalizeSaveSession(ss);
} catch (Exception ex) {
} catch (SaveException | IOException ex) {
throw new IOException(ex.getMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.*;
import java.net.URL;
import java.nio.charset.Charset;
Expand All @@ -38,6 +42,9 @@
*/
public class OpenDocumentSpreadsheetCreator extends ExportFormat {

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


/**
* Creates a new instance of OpenOfficeDocumentCreator
*/
Expand Down Expand Up @@ -137,7 +144,7 @@ private static void addFromResource(String resource, OutputStream out) {
}
}
} catch (IOException e) {
e.printStackTrace();
LOGGER.warn("Cannot get resource", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.*;
import java.net.URL;
import java.nio.charset.Charset;
Expand All @@ -37,6 +41,9 @@
*/
public class OpenOfficeDocumentCreator extends ExportFormat {

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


/**
* Creates a new instance of OpenOfficeDocumentCreator
*/
Expand Down Expand Up @@ -121,7 +128,7 @@ private static void addFromResource(String resource, OutputStream out) {
}
}
} catch (IOException e) {
e.printStackTrace();
LOGGER.warn("Cannot get resource", e);
}
}
}
7 changes: 6 additions & 1 deletion src/main/java/net/sf/jabref/exporter/RtfSelection.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

class RtfSelection implements Transferable {

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

private DataFlavor rtfFlavor;
private DataFlavor[] supportedFlavors;
private final String content;
Expand All @@ -34,7 +39,7 @@ public RtfSelection(String s) {
rtfFlavor = new DataFlavor("text/rtf; class=java.io.InputStream");
supportedFlavors = new DataFlavor[] {rtfFlavor, DataFlavor.stringFlavor};
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
LOGGER.warn("Cannot find class", ex);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/sf/jabref/exporter/SaveActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SaveActions {

private HashMap<String, Formatter> actions;
private final Map<String, Formatter> actions;

private List<Formatter> availableFormatters;

Expand Down

0 comments on commit 47208c1

Please sign in to comment.