From 4cfae78819b56a2f26bb8f3f100ae1d14ae9e386 Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Tue, 2 Apr 2019 21:28:31 +0200 Subject: [PATCH] Fix #4735. Catch file not found exception and handle non existing aux files gracefully. --- CHANGELOG.md | 1 + .../org/jabref/gui/groups/GroupDialog.java | 4 ++-- .../logic/auxparser/DefaultAuxParser.java | 2 +- .../logic/importer/util/GroupsParser.java | 19 ++++++++++++++++--- .../org/jabref/model/groups/TexGroup.java | 18 ++++++++++++++---- 5 files changed, 34 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a64512c0723..b5ab5a6035b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -74,6 +74,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `# - We fixed an issue where selecting a group messed up the focus of the main table / entry editor. https://github.com/JabRef/jabref/issues/3367 - We fixed an issue where composite author names were sorted incorrectly. https://github.com/JabRef/jabref/issues/2828 - We fixed an issue where commands followed by `-` didn't work. [#3805](https://github.com/JabRef/jabref/issues/3805) +- We fixed an issue where a non-existing aux file in a group made it impossible to open the library. [#4735](https://github.com/JabRef/jabref/issues/4735) - We fixed an issue where some journal names were wrongly marked as abbreviated. [#4115](https://github.com/JabRef/jabref/issues/4115) - We fixed an issue where the custom file column were sorted incorrectly. https://github.com/JabRef/jabref/issues/3119 - We fixed an issues where the entry losses focus when a field is edited and at the same time used for sorting. https://github.com/JabRef/jabref/issues/3373 diff --git a/src/main/java/org/jabref/gui/groups/GroupDialog.java b/src/main/java/org/jabref/gui/groups/GroupDialog.java index 2285db635ac..b40755e97e4 100644 --- a/src/main/java/org/jabref/gui/groups/GroupDialog.java +++ b/src/main/java/org/jabref/gui/groups/GroupDialog.java @@ -335,7 +335,7 @@ groupName, getContext(), autoGroupPersonsField.getText().trim()); } } else if (texRadioButton.isSelected()) { - resultingGroup = new TexGroup(groupName, getContext(), + resultingGroup = TexGroup.create(groupName, getContext(), Paths.get(texGroupFilePath.getText().trim()), new DefaultAuxParser(new BibDatabase()), Globals.getFileUpdateMonitor(), basePanel.getBibDatabaseContext().getMetaData()); } @@ -466,7 +466,7 @@ private VBox createOptionsTexGroup() { texGroupBrowseButton.setOnAction((ActionEvent e) -> openBrowseDialog()); texGroupHBox.getChildren().add(texGroupFilePath); texGroupHBox.getChildren().add(texGroupBrowseButton); - texGroupHBox.setHgrow(texGroupFilePath, Priority.ALWAYS); + HBox.setHgrow(texGroupFilePath, Priority.ALWAYS); texPanel.getChildren().add(texGroupHBox); return texPanel; } diff --git a/src/main/java/org/jabref/logic/auxparser/DefaultAuxParser.java b/src/main/java/org/jabref/logic/auxparser/DefaultAuxParser.java index f8579ac68b9..054230c730b 100644 --- a/src/main/java/org/jabref/logic/auxparser/DefaultAuxParser.java +++ b/src/main/java/org/jabref/logic/auxparser/DefaultAuxParser.java @@ -77,7 +77,7 @@ private AuxParserResult parseAuxFile(Path auxFile) { matchNestedAux(auxFile, result, fileList, line); } } catch (FileNotFoundException e) { - LOGGER.info("Cannot locate input file", e); + LOGGER.warn("Cannot locate input file", e); } catch (IOException e) { LOGGER.warn("Problem opening file", e); } diff --git a/src/main/java/org/jabref/logic/importer/util/GroupsParser.java b/src/main/java/org/jabref/logic/importer/util/GroupsParser.java index 4c9365b0dd8..f7f8dc27cdb 100644 --- a/src/main/java/org/jabref/logic/importer/util/GroupsParser.java +++ b/src/main/java/org/jabref/logic/importer/util/GroupsParser.java @@ -28,11 +28,15 @@ import org.jabref.model.strings.StringUtil; import org.jabref.model.util.FileUpdateMonitor; +import org.slf4j.LoggerFactory; + /** * Converts string representation of groups to a parsed {@link GroupTreeNode}. */ public class GroupsParser { + private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(GroupsParser.class); + private GroupsParser() { } @@ -123,9 +127,18 @@ private static AbstractGroup texGroupFromString(String string, FileUpdateMonitor GroupHierarchyType context = GroupHierarchyType.getByNumberOrDefault(Integer.parseInt(tok.nextToken())); try { Path path = Paths.get(tok.nextToken()); - TexGroup newGroup = new TexGroup(name, context, path, new DefaultAuxParser(new BibDatabase()), fileMonitor, metaData); - addGroupDetails(tok, newGroup); - return newGroup; + try { + TexGroup newGroup = TexGroup.create(name, context, path, new DefaultAuxParser(new BibDatabase()), fileMonitor, metaData); + addGroupDetails(tok, newGroup); + return newGroup; + } catch (IOException ex) { + // Problem accessing file -> create without file monitoring + LOGGER.warn("Could not access file " + path + ". The group " + name + " will not reflect changes to the aux file.", ex); + + TexGroup newGroup = TexGroup.createWithoutFileMonitoring(name, context, path, new DefaultAuxParser(new BibDatabase()), fileMonitor, metaData); + addGroupDetails(tok, newGroup); + return newGroup; + } } catch (InvalidPathException | IOException ex) { throw new ParseException(ex); } diff --git a/src/main/java/org/jabref/model/groups/TexGroup.java b/src/main/java/org/jabref/model/groups/TexGroup.java index ab350c486d5..9517a90f064 100644 --- a/src/main/java/org/jabref/model/groups/TexGroup.java +++ b/src/main/java/org/jabref/model/groups/TexGroup.java @@ -30,20 +30,30 @@ public class TexGroup extends AbstractGroup implements FileUpdateListener { private final MetaData metaData; private String user; - public TexGroup(String name, GroupHierarchyType context, Path filePath, AuxParser auxParser, FileUpdateMonitor fileMonitor, MetaData metaData, String user) throws IOException { + TexGroup(String name, GroupHierarchyType context, Path filePath, AuxParser auxParser, FileUpdateMonitor fileMonitor, MetaData metaData, String user) { super(name, context); this.metaData = metaData; this.user = user; this.filePath = expandPath(Objects.requireNonNull(filePath)); this.auxParser = auxParser; this.fileMonitor = fileMonitor; - fileMonitor.addListenerForFile(this.filePath, this); } - public TexGroup(String name, GroupHierarchyType context, Path filePath, AuxParser auxParser, FileUpdateMonitor fileMonitor, MetaData metaData) throws IOException { + TexGroup(String name, GroupHierarchyType context, Path filePath, AuxParser auxParser, FileUpdateMonitor fileMonitor, MetaData metaData) throws IOException { this(name, context, filePath, auxParser, fileMonitor, metaData, System.getProperty("user.name") + '-' + InetAddress.getLocalHost().getHostName()); } + public static TexGroup create(String name, GroupHierarchyType context, Path filePath, AuxParser auxParser, FileUpdateMonitor fileMonitor, MetaData metaData) throws IOException { + TexGroup group = new TexGroup(name, context, filePath, auxParser, fileMonitor, metaData); + fileMonitor.addListenerForFile(filePath, group); + return group; + } + + public static TexGroup createWithoutFileMonitoring(String name, GroupHierarchyType context, Path filePath, AuxParser auxParser, FileUpdateMonitor fileMonitor, MetaData metaData) throws IOException { + return new TexGroup(name, context, filePath, auxParser, fileMonitor, metaData); + } + + @Override public boolean contains(BibEntry entry) { if (keysUsedInAux == null) { @@ -124,7 +134,7 @@ private List getFileDirectoriesAsPaths() { List fileDirs = new ArrayList<>(); metaData.getLaTexFileDirectory(user) - .ifPresent(laTexFileDirectory -> fileDirs.add(laTexFileDirectory)); + .ifPresent(fileDirs::add); return fileDirs; }