Skip to content

Commit 47772fd

Browse files
authored
Fix parser exception when loading tex-groups with non-existing file (#4839)
* Fix #4735. Catch file not found exception and handle non existing aux files gracefully. * Fix build * Fix checkstyle
1 parent 54892f2 commit 47772fd

File tree

7 files changed

+62
-39
lines changed

7 files changed

+62
-39
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
7474
- 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
7575
- We fixed an issue where composite author names were sorted incorrectly. https://github.com/JabRef/jabref/issues/2828
7676
- We fixed an issue where commands followed by `-` didn't work. [#3805](https://github.com/JabRef/jabref/issues/3805)
77+
- 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)
7778
- We fixed an issue where some journal names were wrongly marked as abbreviated. [#4115](https://github.com/JabRef/jabref/issues/4115)
7879
- We fixed an issue where the custom file column were sorted incorrectly. https://github.com/JabRef/jabref/issues/3119
7980
- 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

src/main/java/org/jabref/gui/groups/GroupDialog.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ groupName, getContext(),
335335
autoGroupPersonsField.getText().trim());
336336
}
337337
} else if (texRadioButton.isSelected()) {
338-
resultingGroup = new TexGroup(groupName, getContext(),
338+
resultingGroup = TexGroup.create(groupName, getContext(),
339339
Paths.get(texGroupFilePath.getText().trim()), new DefaultAuxParser(new BibDatabase()), Globals.getFileUpdateMonitor(), basePanel.getBibDatabaseContext().getMetaData());
340340
}
341341

@@ -466,7 +466,7 @@ private VBox createOptionsTexGroup() {
466466
texGroupBrowseButton.setOnAction((ActionEvent e) -> openBrowseDialog());
467467
texGroupHBox.getChildren().add(texGroupFilePath);
468468
texGroupHBox.getChildren().add(texGroupBrowseButton);
469-
texGroupHBox.setHgrow(texGroupFilePath, Priority.ALWAYS);
469+
HBox.setHgrow(texGroupFilePath, Priority.ALWAYS);
470470
texPanel.getChildren().add(texGroupHBox);
471471
return texPanel;
472472
}

src/main/java/org/jabref/logic/auxparser/DefaultAuxParser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private AuxParserResult parseAuxFile(Path auxFile) {
7777
matchNestedAux(auxFile, result, fileList, line);
7878
}
7979
} catch (FileNotFoundException e) {
80-
LOGGER.info("Cannot locate input file", e);
80+
LOGGER.warn("Cannot locate input file", e);
8181
} catch (IOException e) {
8282
LOGGER.warn("Problem opening file", e);
8383
}

src/main/java/org/jabref/logic/importer/util/GroupsParser.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,15 @@
2828
import org.jabref.model.strings.StringUtil;
2929
import org.jabref.model.util.FileUpdateMonitor;
3030

31+
import org.slf4j.LoggerFactory;
32+
3133
/**
3234
* Converts string representation of groups to a parsed {@link GroupTreeNode}.
3335
*/
3436
public class GroupsParser {
3537

38+
private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(GroupsParser.class);
39+
3640
private GroupsParser() {
3741
}
3842

@@ -123,9 +127,18 @@ private static AbstractGroup texGroupFromString(String string, FileUpdateMonitor
123127
GroupHierarchyType context = GroupHierarchyType.getByNumberOrDefault(Integer.parseInt(tok.nextToken()));
124128
try {
125129
Path path = Paths.get(tok.nextToken());
126-
TexGroup newGroup = new TexGroup(name, context, path, new DefaultAuxParser(new BibDatabase()), fileMonitor, metaData);
127-
addGroupDetails(tok, newGroup);
128-
return newGroup;
130+
try {
131+
TexGroup newGroup = TexGroup.create(name, context, path, new DefaultAuxParser(new BibDatabase()), fileMonitor, metaData);
132+
addGroupDetails(tok, newGroup);
133+
return newGroup;
134+
} catch (IOException ex) {
135+
// Problem accessing file -> create without file monitoring
136+
LOGGER.warn("Could not access file " + path + ". The group " + name + " will not reflect changes to the aux file.", ex);
137+
138+
TexGroup newGroup = TexGroup.createWithoutFileMonitoring(name, context, path, new DefaultAuxParser(new BibDatabase()), fileMonitor, metaData);
139+
addGroupDetails(tok, newGroup);
140+
return newGroup;
141+
}
129142
} catch (InvalidPathException | IOException ex) {
130143
throw new ParseException(ex);
131144
}

src/main/java/org/jabref/model/groups/TexGroup.java

+13-4
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,29 @@ public class TexGroup extends AbstractGroup implements FileUpdateListener {
3030
private final MetaData metaData;
3131
private String user;
3232

33-
public TexGroup(String name, GroupHierarchyType context, Path filePath, AuxParser auxParser, FileUpdateMonitor fileMonitor, MetaData metaData, String user) throws IOException {
33+
TexGroup(String name, GroupHierarchyType context, Path filePath, AuxParser auxParser, FileUpdateMonitor fileMonitor, MetaData metaData, String user) {
3434
super(name, context);
3535
this.metaData = metaData;
3636
this.user = user;
3737
this.filePath = expandPath(Objects.requireNonNull(filePath));
3838
this.auxParser = auxParser;
3939
this.fileMonitor = fileMonitor;
40-
fileMonitor.addListenerForFile(this.filePath, this);
4140
}
4241

43-
public TexGroup(String name, GroupHierarchyType context, Path filePath, AuxParser auxParser, FileUpdateMonitor fileMonitor, MetaData metaData) throws IOException {
42+
TexGroup(String name, GroupHierarchyType context, Path filePath, AuxParser auxParser, FileUpdateMonitor fileMonitor, MetaData metaData) throws IOException {
4443
this(name, context, filePath, auxParser, fileMonitor, metaData, System.getProperty("user.name") + '-' + InetAddress.getLocalHost().getHostName());
4544
}
4645

46+
public static TexGroup create(String name, GroupHierarchyType context, Path filePath, AuxParser auxParser, FileUpdateMonitor fileMonitor, MetaData metaData) throws IOException {
47+
TexGroup group = new TexGroup(name, context, filePath, auxParser, fileMonitor, metaData);
48+
fileMonitor.addListenerForFile(filePath, group);
49+
return group;
50+
}
51+
52+
public static TexGroup createWithoutFileMonitoring(String name, GroupHierarchyType context, Path filePath, AuxParser auxParser, FileUpdateMonitor fileMonitor, MetaData metaData) throws IOException {
53+
return new TexGroup(name, context, filePath, auxParser, fileMonitor, metaData);
54+
}
55+
4756
@Override
4857
public boolean contains(BibEntry entry) {
4958
if (keysUsedInAux == null) {
@@ -124,7 +133,7 @@ private List<Path> getFileDirectoriesAsPaths() {
124133
List<Path> fileDirs = new ArrayList<>();
125134

126135
metaData.getLaTexFileDirectory(user)
127-
.ifPresent(laTexFileDirectory -> fileDirs.add(laTexFileDirectory));
136+
.ifPresent(fileDirs::add);
128137

129138
return fileDirs;
130139
}

src/test/java/org/jabref/logic/exporter/GroupSerializerTest.java

+16-16
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,31 @@
3030

3131
import static org.junit.jupiter.api.Assertions.assertEquals;
3232

33-
public class GroupSerializerTest {
33+
class GroupSerializerTest {
3434

3535
private GroupSerializer groupSerializer;
3636

3737
@BeforeEach
38-
public void setUp() throws Exception {
38+
void setUp() throws Exception {
3939
groupSerializer = new GroupSerializer();
4040
}
4141

4242
@Test
43-
public void serializeSingleAllEntriesGroup() {
43+
void serializeSingleAllEntriesGroup() {
4444
AllEntriesGroup group = new AllEntriesGroup("");
4545
List<String> serialization = groupSerializer.serializeTree(GroupTreeNode.fromGroup(group));
4646
assertEquals(Collections.singletonList("0 AllEntriesGroup:"), serialization);
4747
}
4848

4949
@Test
50-
public void serializeSingleExplicitGroup() {
50+
void serializeSingleExplicitGroup() {
5151
ExplicitGroup group = new ExplicitGroup("myExplicitGroup", GroupHierarchyType.INDEPENDENT, ',');
5252
List<String> serialization = groupSerializer.serializeTree(GroupTreeNode.fromGroup(group));
5353
assertEquals(Collections.singletonList("0 StaticGroup:myExplicitGroup;0;1;;;;"), serialization);
5454
}
5555

5656
@Test
57-
public void serializeSingleExplicitGroupWithIconAndDescription() {
57+
void serializeSingleExplicitGroupWithIconAndDescription() {
5858
ExplicitGroup group = new ExplicitGroup("myExplicitGroup", GroupHierarchyType.INDEPENDENT, ',');
5959
group.setIconName("test icon");
6060
group.setExpanded(true);
@@ -66,63 +66,63 @@ public void serializeSingleExplicitGroupWithIconAndDescription() {
6666

6767
@Test
6868
// For https://github.com/JabRef/jabref/issues/1681
69-
public void serializeSingleExplicitGroupWithEscapedSlash() {
69+
void serializeSingleExplicitGroupWithEscapedSlash() {
7070
ExplicitGroup group = new ExplicitGroup("B{\\\"{o}}hmer", GroupHierarchyType.INDEPENDENT, ',');
7171
List<String> serialization = groupSerializer.serializeTree(GroupTreeNode.fromGroup(group));
7272
assertEquals(Collections.singletonList("0 StaticGroup:B{\\\\\"{o}}hmer;0;1;;;;"), serialization);
7373
}
7474

7575
@Test
76-
public void serializeSingleSimpleKeywordGroup() {
76+
void serializeSingleSimpleKeywordGroup() {
7777
WordKeywordGroup group = new WordKeywordGroup("name", GroupHierarchyType.INDEPENDENT, "keywords", "test", false, ',', false);
7878
List<String> serialization = groupSerializer.serializeTree(GroupTreeNode.fromGroup(group));
7979
assertEquals(Collections.singletonList("0 KeywordGroup:name;0;keywords;test;0;0;1;;;;"), serialization);
8080
}
8181

8282
@Test
83-
public void serializeSingleRegexKeywordGroup() {
83+
void serializeSingleRegexKeywordGroup() {
8484
KeywordGroup group = new RegexKeywordGroup("myExplicitGroup", GroupHierarchyType.REFINING, "author", "asdf", false);
8585
List<String> serialization = groupSerializer.serializeTree(GroupTreeNode.fromGroup(group));
8686
assertEquals(Collections.singletonList("0 KeywordGroup:myExplicitGroup;1;author;asdf;0;1;1;;;;"), serialization);
8787
}
8888

8989
@Test
90-
public void serializeSingleSearchGroup() {
90+
void serializeSingleSearchGroup() {
9191
SearchGroup group = new SearchGroup("myExplicitGroup", GroupHierarchyType.INDEPENDENT, "author=harrer", true, true);
9292
List<String> serialization = groupSerializer.serializeTree(GroupTreeNode.fromGroup(group));
9393
assertEquals(Collections.singletonList("0 SearchGroup:myExplicitGroup;0;author=harrer;1;1;1;;;;"), serialization);
9494
}
9595

9696
@Test
97-
public void serializeSingleSearchGroupWithRegex() {
97+
void serializeSingleSearchGroupWithRegex() {
9898
SearchGroup group = new SearchGroup("myExplicitGroup", GroupHierarchyType.INCLUDING, "author=\"harrer\"", true, false);
9999
List<String> serialization = groupSerializer.serializeTree(GroupTreeNode.fromGroup(group));
100100
assertEquals(Collections.singletonList("0 SearchGroup:myExplicitGroup;2;author=\"harrer\";1;0;1;;;;"), serialization);
101101
}
102102

103103
@Test
104-
public void serializeSingleAutomaticKeywordGroup() {
104+
void serializeSingleAutomaticKeywordGroup() {
105105
AutomaticGroup group = new AutomaticKeywordGroup("myAutomaticGroup", GroupHierarchyType.INDEPENDENT, "keywords", ',', '>');
106106
List<String> serialization = groupSerializer.serializeTree(GroupTreeNode.fromGroup(group));
107107
assertEquals(Collections.singletonList("0 AutomaticKeywordGroup:myAutomaticGroup;0;keywords;,;>;1;;;;"), serialization);
108108
}
109109

110110
@Test
111-
public void serializeSingleAutomaticPersonGroup() {
111+
void serializeSingleAutomaticPersonGroup() {
112112
AutomaticPersonsGroup group = new AutomaticPersonsGroup("myAutomaticGroup", GroupHierarchyType.INDEPENDENT, "authors");
113113
List<String> serialization = groupSerializer.serializeTree(GroupTreeNode.fromGroup(group));
114114
assertEquals(Collections.singletonList("0 AutomaticPersonsGroup:myAutomaticGroup;0;authors;1;;;;"), serialization);
115115
}
116116

117117
@Test
118-
public void serializeSingleTexGroup() throws Exception {
119-
TexGroup group = new TexGroup("myTexGroup", GroupHierarchyType.INDEPENDENT, Paths.get("path", "To", "File"), new DefaultAuxParser(new BibDatabase()), new DummyFileUpdateMonitor(), new MetaData());
118+
void serializeSingleTexGroup() throws Exception {
119+
TexGroup group = TexGroup.createWithoutFileMonitoring("myTexGroup", GroupHierarchyType.INDEPENDENT, Paths.get("path", "To", "File"), new DefaultAuxParser(new BibDatabase()), new DummyFileUpdateMonitor(), new MetaData());
120120
List<String> serialization = groupSerializer.serializeTree(GroupTreeNode.fromGroup(group));
121121
assertEquals(Collections.singletonList("0 TexGroup:myTexGroup;0;path/To/File;1;;;;"), serialization);
122122
}
123123

124124
@Test
125-
public void getTreeAsStringInSimpleTree() throws Exception {
125+
void getTreeAsStringInSimpleTree() throws Exception {
126126
GroupTreeNode root = GroupTreeNodeTest.getRoot();
127127
GroupTreeNodeTest.getNodeInSimpleTree(root);
128128

@@ -136,7 +136,7 @@ public void getTreeAsStringInSimpleTree() throws Exception {
136136
}
137137

138138
@Test
139-
public void getTreeAsStringInComplexTree() throws Exception {
139+
void getTreeAsStringInComplexTree() throws Exception {
140140
GroupTreeNode root = GroupTreeNodeTest.getRoot();
141141
GroupTreeNodeTest.getNodeInComplexTree(root);
142142

src/test/java/org/jabref/logic/importer/util/GroupsParserTest.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -27,46 +27,46 @@
2727
import static org.junit.jupiter.api.Assertions.assertEquals;
2828
import static org.junit.jupiter.api.Assertions.assertThrows;
2929

30-
public class GroupsParserTest {
30+
class GroupsParserTest {
3131
private FileUpdateMonitor fileMonitor;
3232
private MetaData metaData;
3333

3434
@BeforeEach
35-
public void setUp() throws Exception {
35+
void setUp() throws Exception {
3636
fileMonitor = new DummyFileUpdateMonitor();
3737
metaData = new MetaData();
3838
}
3939

4040
@Test
4141
// For https://github.com/JabRef/jabref/issues/1681
42-
public void fromStringParsesExplicitGroupWithEscapedCharacterInName() throws Exception {
42+
void fromStringParsesExplicitGroupWithEscapedCharacterInName() throws Exception {
4343
ExplicitGroup expected = new ExplicitGroup("B{\\\"{o}}hmer", GroupHierarchyType.INDEPENDENT, ',');
4444
AbstractGroup parsed = GroupsParser.fromString("ExplicitGroup:B{\\\\\"{o}}hmer;0;", ',', fileMonitor, metaData);
4545

4646
assertEquals(expected, parsed);
4747
}
4848

4949
@Test
50-
public void keywordDelimiterThatNeedsToBeEscaped() throws Exception {
50+
void keywordDelimiterThatNeedsToBeEscaped() throws Exception {
5151
AutomaticGroup expected = new AutomaticKeywordGroup("group1", GroupHierarchyType.INDEPENDENT, "keywords", ';', '>');
5252
AbstractGroup parsed = GroupsParser.fromString("AutomaticKeywordGroup:group1;0;keywords;\\;;>;1;;;;;", ';', fileMonitor, metaData);
5353
assertEquals(expected, parsed);
5454
}
5555

5656
@Test
57-
public void hierarchicalDelimiterThatNeedsToBeEscaped() throws Exception {
57+
void hierarchicalDelimiterThatNeedsToBeEscaped() throws Exception {
5858
AutomaticGroup expected = new AutomaticKeywordGroup("group1", GroupHierarchyType.INDEPENDENT, "keywords", ',', ';');
5959
AbstractGroup parsed = GroupsParser.fromString("AutomaticKeywordGroup:group1;0;keywords;,;\\;;1;;;;;", ';', fileMonitor, metaData);
6060
assertEquals(expected, parsed);
6161
}
6262

6363
@Test
64-
public void fromStringThrowsParseExceptionForNotEscapedGroupName() throws Exception {
64+
void fromStringThrowsParseExceptionForNotEscapedGroupName() throws Exception {
6565
assertThrows(ParseException.class, () -> GroupsParser.fromString("ExplicitGroup:slit\\\\;0\\;mertsch_slit2_2007\\;;", ',', fileMonitor, metaData));
6666
}
6767

6868
@Test
69-
public void testImportSubGroups() throws Exception {
69+
void testImportSubGroups() throws Exception {
7070

7171
List<String> orderedData = Arrays.asList("0 AllEntriesGroup:", "1 ExplicitGroup:1;0;",
7272
"2 ExplicitGroup:2;0;", "0 ExplicitGroup:3;0;");
@@ -93,7 +93,7 @@ public void testImportSubGroups() throws Exception {
9393
}
9494

9595
@Test
96-
public void fromStringParsesExplicitGroupWithIconAndDescription() throws Exception {
96+
void fromStringParsesExplicitGroupWithIconAndDescription() throws Exception {
9797
ExplicitGroup expected = new ExplicitGroup("myExplicitGroup", GroupHierarchyType.INDEPENDENT, ',');
9898
expected.setIconName("test icon");
9999
expected.setExpanded(true);
@@ -105,28 +105,28 @@ public void fromStringParsesExplicitGroupWithIconAndDescription() throws Excepti
105105
}
106106

107107
@Test
108-
public void fromStringParsesAutomaticKeywordGroup() throws Exception {
108+
void fromStringParsesAutomaticKeywordGroup() throws Exception {
109109
AutomaticGroup expected = new AutomaticKeywordGroup("myAutomaticGroup", GroupHierarchyType.INDEPENDENT, "keywords", ',', '>');
110110
AbstractGroup parsed = GroupsParser.fromString("AutomaticKeywordGroup:myAutomaticGroup;0;keywords;,;>;1;;;;", ',', fileMonitor, metaData);
111111
assertEquals(expected, parsed);
112112
}
113113

114114
@Test
115-
public void fromStringParsesAutomaticPersonGroup() throws Exception {
115+
void fromStringParsesAutomaticPersonGroup() throws Exception {
116116
AutomaticPersonsGroup expected = new AutomaticPersonsGroup("myAutomaticGroup", GroupHierarchyType.INDEPENDENT, "authors");
117117
AbstractGroup parsed = GroupsParser.fromString("AutomaticPersonsGroup:myAutomaticGroup;0;authors;1;;;;", ',', fileMonitor, metaData);
118118
assertEquals(expected, parsed);
119119
}
120120

121121
@Test
122-
public void fromStringParsesTexGroup() throws Exception {
123-
TexGroup expected = new TexGroup("myTexGroup", GroupHierarchyType.INDEPENDENT, Paths.get("path", "To", "File"), new DefaultAuxParser(new BibDatabase()), fileMonitor, metaData);
122+
void fromStringParsesTexGroup() throws Exception {
123+
TexGroup expected = TexGroup.createWithoutFileMonitoring("myTexGroup", GroupHierarchyType.INDEPENDENT, Paths.get("path", "To", "File"), new DefaultAuxParser(new BibDatabase()), fileMonitor, metaData);
124124
AbstractGroup parsed = GroupsParser.fromString("TexGroup:myTexGroup;0;path/To/File;1;;;;", ',', fileMonitor, metaData);
125125
assertEquals(expected, parsed);
126126
}
127127

128128
@Test
129-
public void fromStringUnknownGroupThrowsException() throws Exception {
129+
void fromStringUnknownGroupThrowsException() throws Exception {
130130
assertThrows(ParseException.class, () -> GroupsParser.fromString("0 UnknownGroup:myUnknownGroup;0;;1;;;;", ',', fileMonitor, metaData));
131131
}
132132
}

0 commit comments

Comments
 (0)