Skip to content

Commit

Permalink
Incorporate feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez committed Jun 26, 2016
1 parent 892c593 commit a00f7ac
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 76 deletions.
4 changes: 2 additions & 2 deletions src/main/java/net/sf/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ private boolean generateAux(List<ParserResult> loaded, String[] data) {
session.getEncoding().displayName())
+ " " + session.getWriter().getProblemCharacters());
}
session.commit(new File(subName));
session.commit(subName);
} catch (SaveException ex) {
System.err.println(
Localization.lang("Could not save file.") + "\n" + ex.getLocalizedMessage());
Expand Down Expand Up @@ -344,7 +344,7 @@ private void exportFile(List<ParserResult> loaded, String[] data) {
session.getEncoding().displayName())
+ " " + session.getWriter().getProblemCharacters());
}
session.commit(new File(data[0]));
session.commit(data[0]);
} catch (SaveException ex) {
System.err.println(
Localization.lang("Could not save file.") + "\n" + ex.getLocalizedMessage());
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/sf/jabref/collab/ChangeScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -97,8 +98,8 @@ public void run() {
try {

// Parse the temporary file.
File tempFile = Globals.fileUpdateMonitor.getTempFile(panel.fileMonitorHandle());
ParserResult pr = OpenDatabaseAction.loadDatabase(tempFile, Globals.prefs.getDefaultEncoding());
Path tempFile = Globals.fileUpdateMonitor.getTempFile(panel.fileMonitorHandle());
ParserResult pr = OpenDatabaseAction.loadDatabase(tempFile.toFile(), Globals.prefs.getDefaultEncoding());
inTemp = pr.getDatabase();
mdInTemp = pr.getMetaData();
// Parse the modified file.
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/net/sf/jabref/collab/FileUpdateMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -133,7 +135,7 @@ public void updateTimeStamp(String key) {
* @throws IllegalArgumentException If the handle doesn't correspond to an entry.
* @return File The temporary file.
*/
public File getTempFile(String key) throws IllegalArgumentException {
public Path getTempFile(String key) throws IllegalArgumentException {
Object o = entries.get(key);
if (o == null) {
throw new IllegalArgumentException("Entry not found");
Expand All @@ -149,7 +151,7 @@ static class Entry {

private final FileUpdateListener listener;
private final File file;
private final File tmpFile;
private final Path tmpFile;
private long timeStamp;
private long fileSize;

Expand All @@ -161,7 +163,7 @@ public Entry(FileUpdateListener ul, File f) {
fileSize = file.length();
tmpFile = FileUpdateMonitor.getTempFile();
if (tmpFile != null) {
tmpFile.deleteOnExit();
tmpFile.toFile().deleteOnExit();
copy();
}
}
Expand Down Expand Up @@ -194,9 +196,9 @@ public boolean copy() {

boolean res = false;
try {
res = FileUtil.copyFile(file, tmpFile, true);
res = FileUtil.copyFile(file, tmpFile.toFile(), true);
} catch (IOException ex) {
LOGGER.info("Cannot copy to temporary file '" + tmpFile.getPath() + '\'', ex);
LOGGER.info("Cannot copy to temporary file '" + tmpFile + '\'', ex);
}
return res;
}
Expand All @@ -218,7 +220,7 @@ public void notifyFileRemoved() {
listener.fileRemoved();
}

public File getTmpFile() {
public Path getTmpFile() {
return tmpFile;
}

Expand All @@ -228,14 +230,14 @@ public void decreaseTimeStamp() {
}


private static synchronized File getTempFile() {
File f = null;
private static synchronized Path getTempFile() {
Path temporaryFile = null;
try {
f = File.createTempFile("jabref", null);
f.deleteOnExit();
temporaryFile = Files.createTempFile("jabref", null);
temporaryFile.toFile().deleteOnExit();
} catch (IOException ex) {
LOGGER.warn("Could not create temporary file.", ex);
}
return f;
return temporaryFile;
}
}
2 changes: 1 addition & 1 deletion src/main/java/net/sf/jabref/exporter/AutoSaveManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private static boolean autoSave(BasePanel panel) {

BibDatabaseWriter databaseWriter = new BibtexDatabaseWriter(FileSaveSession::new);
SaveSession ss = databaseWriter.saveDatabase(panel.getBibDatabaseContext(), prefs);
ss.commit(backupFile);
ss.commit(backupFile.toPath());
} catch (SaveException e) {
LOGGER.error("Problem with automatic save", e);
return false;
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/net/sf/jabref/exporter/BibDatabaseWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,7 @@ public E savePartOfDatabase(BibDatabaseContext bibDatabaseContext,
* Writes all data to the specified writer, using each object's toString() method.
*/
protected void writeMetaData(MetaData metaData) throws SaveException {
if (metaData == null) {
return;
}
Objects.requireNonNull(metaData);

Map<String, String> serializedMetaData = metaData.getAsStringMap();

Expand Down Expand Up @@ -288,10 +286,12 @@ protected void writeString(BibtexString bibtexString, boolean isFirstString, Map
String foundLabel = m.group(1);
int restIndex = content.indexOf(foundLabel) + foundLabel.length();
content = content.substring(restIndex);
Object referred = remaining.get(foundLabel.substring(1, foundLabel.length() - 1));
String label = foundLabel.substring(1, foundLabel.length() - 1);

// If the label we found exists as a key in the "remaining" Map, we go on and write it now:
if (referred != null) {
writeString((BibtexString) referred, isFirstString, remaining, maxKeyLength, reformatFile);
if (remaining.containsKey(label)) {
BibtexString referred = remaining.get(label);
writeString(referred, isFirstString, remaining, maxKeyLength, reformatFile);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/sf/jabref/exporter/ExportFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -186,7 +187,7 @@ public void performExport(final BibDatabaseContext databaseContext, final String
if (entries.isEmpty()) { // Do not export if no entries to export -- avoids exports with only template text
return;
}
File outFile = new File(file);
Path outFile = Paths.get(file);
SaveSession ss = null;
if (this.encoding != null) {
try {
Expand Down Expand Up @@ -369,7 +370,7 @@ public FileFilter getFileFilter() {
return fileFilter;
}

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

Expand Down
14 changes: 6 additions & 8 deletions src/main/java/net/sf/jabref/exporter/FileSaveSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package net.sf.jabref.exporter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -84,16 +83,15 @@ private static Path createTemporaryFile() throws SaveException {
}

@Override
public void commit(File file) throws SaveException {
public void commit(Path file) throws SaveException {
if (file == null) {
return;
}
if (file.exists() && backup) {
String name = file.getName();
String path = file.getParent();
File backupFile = new File(path, name + BACKUP_EXTENSION);
if (backup && Files.exists(file)) {
Path fileName = file.getFileName();
Path backupFile = file.resolveSibling(fileName + BACKUP_EXTENSION);
try {
FileUtil.copyFile(file, backupFile, true);
FileUtil.copyFile(file.toFile(), backupFile.toFile(), true);
} catch (IOException ex) {
LOGGER.error("Problem copying file", ex);
throw SaveException.BACKUP_CREATION;
Expand All @@ -113,7 +111,7 @@ public void commit(File file) throws SaveException {
}
}

FileUtil.copyFile(temporaryFile.toFile(), file, true);
FileUtil.copyFile(temporaryFile.toFile(), file.toFile(), true);
} catch (IOException ex2) {
// If something happens here, what can we do to correct the problem? The file is corrupted, but we still
// have a clean copy in tmp. However, we just failed to copy tmp to file, so it's not likely that
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/sf/jabref/exporter/MSBibExportFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/
package net.sf.jabref.exporter;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -66,7 +66,7 @@ public void performExport(final BibDatabaseContext databaseContext, final String
} catch (TransformerException | IllegalArgumentException | TransformerFactoryConfigurationError e) {
throw new Error(e);
}
finalizeSaveSession(session, new File(file));
finalizeSaveSession(session, Paths.get(file));
} catch (IOException ex) {
throw new SaveException(ex);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/sf/jabref/exporter/ModsExportFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/
package net.sf.jabref.exporter;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -65,7 +65,7 @@ public void performExport(final BibDatabaseContext databaseContext, final String
} catch (TransformerException | IllegalArgumentException | TransformerFactoryConfigurationError e) {
throw new Error(e);
}
finalizeSaveSession(ss, new File(file));
finalizeSaveSession(ss, Paths.get(file));
} catch (IOException ex) {
throw new SaveException(ex);
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/net/sf/jabref/exporter/SaveDatabaseAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void run() {
// lacking keys, before saving:
panel.autoGenerateKeysBeforeSaving();

if (FileBasedLock.waitForFileLock(panel.getBibDatabaseContext().getDatabaseFile(), 10)) {
if (FileBasedLock.waitForFileLock(panel.getBibDatabaseContext().getDatabaseFile().toPath(), 10)) {
// Check for external modifications to alleviate multiuser concurrency issue when near
// simultaneous saves occur to a shared database file: if true, do not perform the save
// rather return instead.
Expand Down Expand Up @@ -244,7 +244,7 @@ private boolean saveDatabase(File file, boolean selectedOnly, Charset encoding)

try {
if (commit) {
session.commit(file);
session.commit(file.toPath());
panel.getBibDatabaseContext().getMetaData().setEncoding(encoding); // Make sure to remember which encoding we used.
} else {
session.cancel();
Expand All @@ -256,7 +256,7 @@ private boolean saveDatabase(File file, boolean selectedOnly, Charset encoding)
JOptionPane.YES_NO_OPTION);
if (ans == JOptionPane.YES_OPTION) {
session.setUseBackup(false);
session.commit(file);
session.commit(file.toPath());
panel.getBibDatabaseContext().getMetaData().setEncoding(encoding);
} else {
commit = false;
Expand Down Expand Up @@ -383,7 +383,7 @@ private boolean checkExternalModification() {

JabRefExecutorService.INSTANCE.execute(() -> {

if (!FileBasedLock.waitForFileLock(panel.getBibDatabaseContext().getDatabaseFile(), 10)) {
if (!FileBasedLock.waitForFileLock(panel.getBibDatabaseContext().getDatabaseFile().toPath(), 10)) {
// TODO: GUI handling of the situation when the externally modified file keeps being locked.
LOGGER.error("File locked, this will be trouble.");
}
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/net/sf/jabref/exporter/SaveSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

package net.sf.jabref.exporter;

import java.io.File;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -50,7 +51,11 @@ public void setUseBackup(boolean useBackup) {
this.backup = useBackup;
}

public abstract void commit(File file) throws SaveException;
public abstract void commit(Path file) throws SaveException;

public void commit(String path) throws SaveException {
commit(Paths.get(path));
}

public abstract void cancel();

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/sf/jabref/exporter/StringSaveSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
package net.sf.jabref.exporter;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -52,9 +52,9 @@ public String getStringValue() {
}

@Override
public void commit(File file) throws SaveException {
public void commit(Path file) throws SaveException {
try {
Files.write(file.toPath(), outputStream.toByteArray());
Files.write(file, outputStream.toByteArray());
} catch (IOException e) {
throw new SaveException(e);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/sf/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,7 @@ private boolean saveDatabase(File file, boolean selectedOnly, Charset enc,
}

if (commit) {
session.commit(file);
session.commit(file.toPath());
this.bibDatabaseContext.getMetaData().setEncoding(enc); // Make sure to remember which encoding we used.
} else {
session.cancel();
Expand Down Expand Up @@ -2177,7 +2177,7 @@ public void fileUpdated() {

// Test: running scan automatically in background
if ((getBibDatabaseContext().getDatabaseFile() != null)
&& !FileBasedLock.waitForFileLock(getBibDatabaseContext().getDatabaseFile(), 10)) {
&& !FileBasedLock.waitForFileLock(getBibDatabaseContext().getDatabaseFile().toPath(), 10)) {
// The file is locked even after the maximum wait. Do nothing.
LOGGER.error("File updated externally, but change scan failed because the file is locked.");
// Perturb the stored timestamp so successive checks are made:
Expand Down
Loading

0 comments on commit a00f7ac

Please # to comment.