Skip to content

Commit

Permalink
Unify ParserException and ParseException (JabRef#2124)
Browse files Browse the repository at this point in the history
* Unify ParserException and ParseException
* Fix package import order in Benchmarks (checkstyle)

* More checkstyle fixes

* If only travis-ci would report all checkstyle errors at once...
  • Loading branch information
lenhard authored and zesaro committed Oct 27, 2016
1 parent ade63f3 commit f7c443d
Show file tree
Hide file tree
Showing 20 changed files with 48 additions and 67 deletions.
2 changes: 1 addition & 1 deletion src/jmh/java/net/sf/jabref/benchmarks/Benchmarks.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
import net.sf.jabref.logic.exporter.SavePreferences;
import net.sf.jabref.logic.exporter.StringSaveSession;
import net.sf.jabref.logic.formatter.bibtexfields.HtmlToLatexFormatter;
import net.sf.jabref.logic.importer.ParseException;
import net.sf.jabref.logic.importer.ParserResult;
import net.sf.jabref.logic.importer.fileformat.BibtexParser;
import net.sf.jabref.logic.layout.format.HTMLChars;
import net.sf.jabref.logic.layout.format.LatexToUnicodeFormatter;
import net.sf.jabref.logic.search.SearchQuery;
import net.sf.jabref.model.Defaults;
import net.sf.jabref.model.ParseException;
import net.sf.jabref.model.database.BibDatabase;
import net.sf.jabref.model.database.BibDatabaseContext;
import net.sf.jabref.model.database.BibDatabaseMode;
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/net/sf/jabref/gui/groups/AutoGroupDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import net.sf.jabref.gui.undo.NamedCompound;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.layout.format.LatexToUnicodeFormatter;
import net.sf.jabref.model.ParseException;
import net.sf.jabref.model.entry.FieldName;
import net.sf.jabref.model.groups.ExplicitGroup;
import net.sf.jabref.model.groups.GroupHierarchyType;
Expand Down Expand Up @@ -121,7 +120,7 @@ public AutoGroupDialog(JabRefFrame jabrefFrame, BasePanel basePanel,
frame.output(Localization.lang("Created groups."));
ce.end();
panel.getUndoManager().addEdit(ce);
} catch (ParseException exception) {
} catch (IllegalArgumentException exception) {
frame.showMessage(exception.getLocalizedMessage());
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/sf/jabref/gui/groups/GroupDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import net.sf.jabref.logic.groups.GroupDescriptions;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.search.SearchQuery;
import net.sf.jabref.model.ParseException;

import net.sf.jabref.model.entry.FieldName;
import net.sf.jabref.model.groups.AbstractGroup;
import net.sf.jabref.model.groups.ExplicitGroup;
Expand Down Expand Up @@ -285,7 +285,7 @@ public void actionPerformed(ActionEvent e) {
}
}
dispose();
} catch (ParseException exception) {
} catch (IllegalArgumentException exception) {
jabrefFrame.showMessage(exception.getLocalizedMessage());
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.util.FileExtensions;
import net.sf.jabref.logic.util.UpdateField;
import net.sf.jabref.model.ParseException;
import net.sf.jabref.model.database.BibDatabase;
import net.sf.jabref.model.database.KeyCollisionException;
import net.sf.jabref.model.entry.BibEntry;
Expand Down Expand Up @@ -142,7 +141,7 @@ private static void mergeFromBibtex(JabRefFrame frame, BasePanel panel, ParserRe
Globals.prefs.getKeywordDelimiter());
newGroups.setGroup(group);
group.add(appendedEntries);
} catch (ParseException e) {
} catch (IllegalArgumentException e) {
LOGGER.error(e);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/sf/jabref/logic/groups/GroupsParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import java.util.List;

import net.sf.jabref.logic.importer.ParseException;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.util.strings.QuotedStringTokenizer;
import net.sf.jabref.model.ParseException;
import net.sf.jabref.model.groups.AbstractGroup;
import net.sf.jabref.model.groups.AllEntriesGroup;
import net.sf.jabref.model.groups.ExplicitGroup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ default List<BibEntry> performSearch(BibEntry entry) throws FetcherException {
} catch (IOException e) {
// TODO: Catch HTTP Response 401 errors and report that user has no rights to access resource
throw new FetcherException("An I/O exception occurred", e);
} catch (ParserException e) {
} catch (ParseException e) {
throw new FetcherException("An internal parser error occurred", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ default Optional<BibEntry> performSearchById(String identifier) throws FetcherEx
} catch (IOException e) {
// TODO: Catch HTTP Response 401 errors and report that user has no rights to access resource
throw new FetcherException("An I/O exception occurred", e);
} catch (ParserException e) {
} catch (ParseException e) {
throw new FetcherException("An internal parser error occurred", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package net.sf.jabref.model;
package net.sf.jabref.logic.importer;

public class ParseException extends Exception {

public ParseException(Throwable cause) {
super(cause);
}

public ParseException(String message, Throwable cause) {
super(message, cause);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/sf/jabref/logic/importer/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
*/
public interface Parser {

List<BibEntry> parseEntries(InputStream inputStream) throws ParserException;
List<BibEntry> parseEntries(InputStream inputStream) throws ParseException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ default List<BibEntry> performSearch(String query) throws FetcherException {
} catch (IOException e) {
// TODO: Catch HTTP Response 401 errors and report that user has no rights to access resource
throw new FetcherException("An I/O exception occurred", e);
} catch (ParserException e) {
} catch (ParseException e) {
throw new FetcherException("An internal parser error occurred", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@
import net.sf.jabref.logic.bibtex.FieldContentParser;
import net.sf.jabref.logic.exporter.SavePreferences;
import net.sf.jabref.logic.importer.ImportFormatPreferences;
import net.sf.jabref.logic.importer.ParseException;
import net.sf.jabref.logic.importer.Parser;
import net.sf.jabref.logic.importer.ParserException;
import net.sf.jabref.logic.importer.ParserResult;
import net.sf.jabref.logic.importer.util.MetaDataParser;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.model.ParseException;
import net.sf.jabref.model.database.BibDatabase;
import net.sf.jabref.model.database.KeyCollisionException;
import net.sf.jabref.model.entry.BibEntry;
Expand Down Expand Up @@ -126,20 +125,20 @@ public static Optional<BibEntry> singleFromString(String bibtexString,
}

@Override
public List<BibEntry> parseEntries(InputStream inputStream) throws ParserException {
public List<BibEntry> parseEntries(InputStream inputStream) throws ParseException {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
return parseEntries(reader);
}

public List<BibEntry> parseEntries(Reader reader) throws ParserException {
public List<BibEntry> parseEntries(Reader reader) throws ParseException {
try {
return parse(reader).getDatabase().getEntries();
} catch (IOException e) {
throw new ParserException(e);
throw new ParseException(e);
}
}

public List<BibEntry> parseEntries(String bibtexString) throws ParserException {
public List<BibEntry> parseEntries(String bibtexString) throws ParseException {
return parseEntries(new StringReader(bibtexString));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import net.sf.jabref.logic.importer.ParseException;
import net.sf.jabref.logic.importer.Parser;
import net.sf.jabref.logic.importer.ParserException;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.FieldName;
import net.sf.jabref.model.entry.IdGenerator;
Expand All @@ -28,13 +28,13 @@ public class GvkParser implements Parser {
private static final Log LOGGER = LogFactory.getLog(GvkParser.class);

@Override
public List<BibEntry> parseEntries(InputStream inputStream) throws ParserException {
public List<BibEntry> parseEntries(InputStream inputStream) throws ParseException {
try {
DocumentBuilder dbuild = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document content = dbuild.parse(inputStream);
return this.parseEntries(content);
} catch (ParserConfigurationException|SAXException|IOException exception) {
throw new ParserException(exception);
throw new ParseException(exception);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import net.sf.jabref.logic.cleanup.Cleanups;
import net.sf.jabref.logic.groups.GroupsParser;
import net.sf.jabref.model.ParseException;
import net.sf.jabref.logic.importer.ParseException;
import net.sf.jabref.model.database.BibDatabaseMode;
import net.sf.jabref.model.metadata.MetaData;
import net.sf.jabref.model.metadata.SaveOrderConfig;
Expand Down
17 changes: 4 additions & 13 deletions src/main/java/net/sf/jabref/model/groups/ExplicitGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.Set;
import java.util.TreeSet;

import net.sf.jabref.model.ParseException;
import net.sf.jabref.model.entry.FieldName;
import net.sf.jabref.model.strings.StringUtil;

Expand All @@ -27,8 +26,7 @@ public class ExplicitGroup extends KeywordGroup {
private static final Log LOGGER = LogFactory.getLog(ExplicitGroup.class);


public ExplicitGroup(String name, GroupHierarchyType context, Character keywordSeparator)
throws ParseException {
public ExplicitGroup(String name, GroupHierarchyType context, Character keywordSeparator) {
super(name, FieldName.GROUPS, name, true, false, context, keywordSeparator);
}

Expand All @@ -38,16 +36,9 @@ public void addLegacyEntryKey(String key) {

@Override
public AbstractGroup deepCopy() {
try {
ExplicitGroup copy = new ExplicitGroup(getName(), getContext(), keywordSeparator);
copy.legacyEntryKeys.addAll(legacyEntryKeys);
return copy;
} catch (ParseException exception) {
// this should never happen, because the constructor obviously succeeded in creating _this_ instance!
LOGGER.error("Internal error in ExplicitGroup.deepCopy(). "
+ "Please report this on https://github.com/JabRef/jabref/issues", exception);
return null;
}
ExplicitGroup copy = new ExplicitGroup(getName(), getContext(), keywordSeparator);
copy.legacyEntryKeys.addAll(legacyEntryKeys);
return copy;
}

@Override
Expand Down
18 changes: 5 additions & 13 deletions src/main/java/net/sf/jabref/model/groups/KeywordGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.regex.PatternSyntaxException;

import net.sf.jabref.model.FieldChange;
import net.sf.jabref.model.ParseException;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.strings.StringUtil;

Expand Down Expand Up @@ -38,7 +37,7 @@ public class KeywordGroup extends AbstractGroup {
*/
public KeywordGroup(String name, String searchField,
String searchExpression, boolean caseSensitive, boolean regExp,
GroupHierarchyType context, Character keywordSeparator) throws ParseException {
GroupHierarchyType context, Character keywordSeparator) {
super(name, context);
this.searchField = searchField;
this.searchExpression = searchExpression;
Expand All @@ -51,12 +50,12 @@ public KeywordGroup(String name, String searchField,
this.searchWords = StringUtil.getStringAsWords(searchExpression);
}

private void compilePattern() throws ParseException {
private void compilePattern() throws IllegalArgumentException {
try {
pattern = caseSensitive ? Pattern.compile("\\b" + searchExpression + "\\b") : Pattern.compile(
"\\b" + searchExpression + "\\b", Pattern.CASE_INSENSITIVE);
} catch (PatternSyntaxException exception) {
throw new ParseException("Syntax error in regular-expression pattern: " + searchExpression);
throw new IllegalArgumentException("Syntax error in regular-expression pattern: " + searchExpression);
}
}

Expand Down Expand Up @@ -260,15 +259,8 @@ private void removeMatches(BibEntry entry) {

@Override
public AbstractGroup deepCopy() {
try {
return new KeywordGroup(getName(), searchField, searchExpression,
caseSensitive, regExp, getContext(), keywordSeparator);
} catch (ParseException exception) {
// this should never happen, because the constructor obviously succeeded in creating _this_ instance!
LOGGER.error("Internal error in KeywordGroup.deepCopy(). "
+ "Please report this on https://github.com/JabRef/jabref/issues", exception);
return null;
}
return new KeywordGroup(getName(), searchField, searchExpression,
caseSensitive, regExp, getContext(), keywordSeparator);
}

public boolean isCaseSensitive() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/sf/jabref/shared/DBMSSynchronizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

import net.sf.jabref.logic.exporter.BibDatabaseWriter;
import net.sf.jabref.logic.exporter.MetaDataSerializer;
import net.sf.jabref.logic.importer.ParseException;
import net.sf.jabref.logic.importer.util.MetaDataParser;
import net.sf.jabref.model.ParseException;
import net.sf.jabref.model.bibtexkeypattern.GlobalBibtexKeyPattern;
import net.sf.jabref.model.database.BibDatabase;
import net.sf.jabref.model.database.BibDatabaseContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import net.sf.jabref.logic.exporter.SavePreferences;
import net.sf.jabref.logic.formatter.casechanger.LowerCaseFormatter;
import net.sf.jabref.logic.importer.ImportFormatPreferences;
import net.sf.jabref.logic.importer.ParseException;
import net.sf.jabref.logic.importer.ParserResult;
import net.sf.jabref.logic.util.OS;
import net.sf.jabref.model.ParseException;
import net.sf.jabref.model.bibtexkeypattern.AbstractBibtexKeyPattern;
import net.sf.jabref.model.bibtexkeypattern.DatabaseBibtexKeyPattern;
import net.sf.jabref.model.bibtexkeypattern.GlobalBibtexKeyPattern;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.sf.jabref.model.groups;

import net.sf.jabref.model.ParseException;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.BibtexEntryTypes;
import net.sf.jabref.model.entry.IdGenerator;
Expand All @@ -13,13 +12,13 @@ public class ExplicitGroupTest {


@Test
public void testToStringSimple() throws ParseException {
public void testToStringSimple() {
ExplicitGroup group = new ExplicitGroup("myExplicitGroup", GroupHierarchyType.INDEPENDENT, ',');
assertEquals("ExplicitGroup:myExplicitGroup;0;", group.toString());
}

@Test
public void toStringDoesNotWriteAssignedEntries() throws ParseException {
public void toStringDoesNotWriteAssignedEntries() {
ExplicitGroup group = new ExplicitGroup("myExplicitGroup", GroupHierarchyType.INCLUDING, ',');
group.add(makeBibtexEntry());
assertEquals("ExplicitGroup:myExplicitGroup;2;", group.toString());
Expand Down
Loading

0 comments on commit f7c443d

Please sign in to comment.