diff --git a/src/main/java/org/jabref/cli/ArgumentProcessor.java b/src/main/java/org/jabref/cli/ArgumentProcessor.java index fad33ed0d0d..adaa4aa1b3d 100644 --- a/src/main/java/org/jabref/cli/ArgumentProcessor.java +++ b/src/main/java/org/jabref/cli/ArgumentProcessor.java @@ -10,7 +10,6 @@ import java.util.Locale; import java.util.Optional; import java.util.Set; -import java.util.Vector; import java.util.prefs.BackingStoreException; import org.jabref.gui.Globals; @@ -288,8 +287,8 @@ private void writeMetadatatoPdf(List loaded, String filesAndCiteke return; } - Vector citeKeys = new Vector<>(); - Vector pdfs = new Vector<>(); + List citeKeys = new ArrayList<>(); + List pdfs = new ArrayList<>(); for (String fileOrCiteKey : filesAndCitekeys.split(",")) { if (fileOrCiteKey.toLowerCase(Locale.ROOT).endsWith(".pdf")) { pdfs.add(fileOrCiteKey); @@ -323,7 +322,7 @@ private void writeMetadatatoPDFsOfEntry(BibDatabaseContext databaseContext, Stri } } - private void writeMetadatatoPdfByCitekey(BibDatabaseContext databaseContext, BibDatabase dataBase, Vector citeKeys, FilePreferences filePreferences, XmpPdfExporter xmpPdfExporter, EmbeddedBibFilePdfExporter embeddedBibFilePdfExporter, boolean writeXMP, boolean embeddBibfile) { + private void writeMetadatatoPdfByCitekey(BibDatabaseContext databaseContext, BibDatabase dataBase, List citeKeys, FilePreferences filePreferences, XmpPdfExporter xmpPdfExporter, EmbeddedBibFilePdfExporter embeddedBibFilePdfExporter, boolean writeXMP, boolean embeddBibfile) { for (String citeKey : citeKeys) { List bibEntryList = dataBase.getEntriesByCitationKey(citeKey); if (bibEntryList.isEmpty()) { @@ -336,8 +335,8 @@ private void writeMetadatatoPdfByCitekey(BibDatabaseContext databaseContext, Bib } } - private void writeMetadatatoPdfByFileNames(BibDatabaseContext databaseContext, BibDatabase dataBase, Vector fileNames, FilePreferences filePreferences, XmpPdfExporter xmpPdfExporter, EmbeddedBibFilePdfExporter embeddedBibFilePdfExporter, boolean writeXMP, boolean embeddBibfile) { - for (String fileName : fileNames) { + private void writeMetadatatoPdfByFileNames(BibDatabaseContext databaseContext, BibDatabase dataBase, List pdfs, FilePreferences filePreferences, XmpPdfExporter xmpPdfExporter, EmbeddedBibFilePdfExporter embeddedBibFilePdfExporter, boolean writeXMP, boolean embeddBibfile) { + for (String fileName : pdfs) { Path filePath = Path.of(fileName); if (!filePath.isAbsolute()) { filePath = FileHelper.find(fileName, databaseContext.getFileDirectories(filePreferences)).orElse(FileHelper.find(fileName, List.of(Path.of("").toAbsolutePath())).orElse(filePath)); diff --git a/src/main/java/org/jabref/cli/CrossrefFetcherEvaluator.java b/src/main/java/org/jabref/cli/CrossrefFetcherEvaluator.java deleted file mode 100644 index f4fda96c23e..00000000000 --- a/src/main/java/org/jabref/cli/CrossrefFetcherEvaluator.java +++ /dev/null @@ -1,100 +0,0 @@ -package org.jabref.cli; - -import java.io.FileReader; -import java.io.IOException; -import java.util.List; -import java.util.Optional; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.atomic.AtomicInteger; - -import org.jabref.gui.Globals; -import org.jabref.logic.importer.FetcherException; -import org.jabref.logic.importer.ParserResult; -import org.jabref.logic.importer.fetcher.CrossRef; -import org.jabref.logic.importer.fileformat.BibtexParser; -import org.jabref.model.database.BibDatabase; -import org.jabref.model.entry.BibEntry; -import org.jabref.model.entry.field.StandardField; -import org.jabref.model.entry.identifier.DOI; -import org.jabref.preferences.JabRefPreferences; - -/** - * Useful for checking out new algorithm improvements and thresholds. Not used inside the JabRef code itself. - */ -public class CrossrefFetcherEvaluator { - - private CrossrefFetcherEvaluator() { - } - - public static void main(String[] args) throws IOException, InterruptedException { - Globals.prefs = JabRefPreferences.getInstance(); - try (FileReader reader = new FileReader(args[0])) { - BibtexParser parser = new BibtexParser(Globals.prefs.getImportFormatPreferences(), Globals.getFileUpdateMonitor()); - ParserResult result = parser.parse(reader); - BibDatabase db = result.getDatabase(); - - List entries = db.getEntries(); - - AtomicInteger dois = new AtomicInteger(); - AtomicInteger doiFound = new AtomicInteger(); - AtomicInteger doiNew = new AtomicInteger(); - AtomicInteger doiIdentical = new AtomicInteger(); - - int total = entries.size(); - - CountDownLatch countDownLatch = new CountDownLatch(total); - - ExecutorService executorService = Executors.newFixedThreadPool(5); - - for (BibEntry entry : entries) { - executorService.execute(new Runnable() { - @Override - public void run() { - Optional origDOI = entry.getField(StandardField.DOI).flatMap(DOI::parse); - if (origDOI.isPresent()) { - dois.incrementAndGet(); - try { - Optional crossrefDOI = new CrossRef().findIdentifier(entry); - if (crossrefDOI.isPresent()) { - doiFound.incrementAndGet(); - if (origDOI.get().getDOI().equalsIgnoreCase(crossrefDOI.get().getDOI())) { - doiIdentical.incrementAndGet(); - } else { - System.out.println("DOI not identical for : " + entry); - } - } else { - System.out.println("DOI not found for: " + entry); - } - } catch (FetcherException e) { - e.printStackTrace(); - } - } else { - try { - Optional crossrefDOI = new CrossRef().findIdentifier(entry); - if (crossrefDOI.isPresent()) { - System.out.println("New DOI found for: " + entry); - doiNew.incrementAndGet(); - } - } catch (FetcherException e) { - e.printStackTrace(); - } - } - countDownLatch.countDown(); - } - }); - } - countDownLatch.await(); - - System.out.println("---------------------------------"); - System.out.println("Total DB size: " + total); - System.out.println("Total DOIs: " + dois); - System.out.println("DOIs found: " + doiFound); - System.out.println("DOIs identical: " + doiIdentical); - System.out.println("New DOIs found: " + doiNew); - - executorService.shutdown(); - } - } -} diff --git a/src/main/java/org/jabref/cli/GenerateCharacterTable.java b/src/main/java/org/jabref/cli/GenerateCharacterTable.java deleted file mode 100644 index 57a1690bb10..00000000000 --- a/src/main/java/org/jabref/cli/GenerateCharacterTable.java +++ /dev/null @@ -1,50 +0,0 @@ -package org.jabref.cli; - -import java.util.Map; -import java.util.TreeMap; - -import org.jabref.logic.util.strings.HTMLUnicodeConversionMaps; - -public class GenerateCharacterTable { - - private GenerateCharacterTable() { - } - - public static void main(String[] args) { - Map characterMap = new TreeMap<>(HTMLUnicodeConversionMaps.NUMERICAL_LATEX_CONVERSION_MAP); - System.out.println("\\documentclass[10pt, a4paper]{article}"); - System.out.println("\\usepackage[T5,T1]{fontenc}"); - System.out.println("\\usepackage{amssymb}"); - System.out.println("\\usepackage{amsmath}"); - System.out.println("\\usepackage{txfonts}"); - System.out.println("\\usepackage{xfrac}"); - System.out.println("\\usepackage{combelow}"); - System.out.println("\\usepackage{textcomp}"); - System.out.println("\\usepackage{mathspec}"); - System.out.println("\\usepackage{fontspec}"); - System.out.println("\\usepackage[a4paper,margin=1cm]{geometry}"); - System.out.println("\\usepackage{supertabular}"); - System.out.println("\\usepackage{mathabx}"); - System.out.println("\\fontspec{Cambria}"); - System.out.println("\\DeclareTextSymbolDefault{\\OHORN}{T5}"); - System.out.println("\\DeclareTextSymbolDefault{\\UHORN}{T5}"); - System.out.println("\\DeclareTextSymbolDefault{\\ohorn}{T5}"); - System.out.println("\\DeclareTextSymbolDefault{\\uhorn}{T5}"); - System.out.println("\\begin{document}"); - System.out.println("\\twocolumn"); - System.out.println("\\begin{supertabular}{c|c|c|c|c}"); - System.out.println("No. & Uni & Symb & \\LaTeX & Code \\\\ \n \\hline"); - - for (Map.Entry character : characterMap.entrySet()) { - System.out - .println( - character.getKey() + " & " - + ((character.getKey() > 128) ? - String.valueOf(Character.toChars(character.getKey())) : "") - + " & \\symbol{" + Integer.toString(character.getKey()) + "} & " - + character.getValue() + " & \\verb¤" + character.getValue() + "¤ \\\\"); - } - System.out.println("\\end{supertabular}"); - System.out.println("\\end{document}"); - } -} diff --git a/src/main/java/org/jabref/gui/maintable/columns/FieldColumn.java b/src/main/java/org/jabref/gui/maintable/columns/FieldColumn.java index aee93952cd7..e1b8b1c1ff9 100644 --- a/src/main/java/org/jabref/gui/maintable/columns/FieldColumn.java +++ b/src/main/java/org/jabref/gui/maintable/columns/FieldColumn.java @@ -11,7 +11,7 @@ import org.jabref.model.entry.field.OrFields; import org.jabref.model.entry.field.UnknownField; -import com.google.common.collect.Iterables; +import com.google.common.collect.MoreCollectors; /** * A column that displays the text-value of the field @@ -33,9 +33,9 @@ public FieldColumn(MainTableColumnModel model) { if (fields.size() == 1) { // comparator can't parse more than one value - Field field = Iterables.getOnlyElement(fields); + Field field = fields.stream().collect(MoreCollectors.onlyElement()); - if (field instanceof UnknownField || field.isNumeric()) { + if ((field instanceof UnknownField) || field.isNumeric()) { this.setComparator(new NumericFieldComparator()); } } diff --git a/src/main/java/org/jabref/gui/preferences/protectedterms/ProtectedTermsTabViewModel.java b/src/main/java/org/jabref/gui/preferences/protectedterms/ProtectedTermsTabViewModel.java index 62c09375df6..ac17b25d736 100644 --- a/src/main/java/org/jabref/gui/preferences/protectedterms/ProtectedTermsTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/protectedterms/ProtectedTermsTabViewModel.java @@ -1,8 +1,7 @@ package org.jabref.gui.preferences.protectedterms; -import java.io.File; -import java.io.FileNotFoundException; import java.io.IOException; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -57,6 +56,7 @@ public void setValues() { termsFilesProperty.addAll(termsLoader.getProtectedTermsLists().stream().map(ProtectedTermsListItemModel::new).toList()); } + @Override public void storeSettings() { List enabledExternalList = new ArrayList<>(); List disabledExternalList = new ArrayList<>(); @@ -97,12 +97,8 @@ public void addFile() { dialogService.showFileOpenDialog(fileDialogConfiguration) .ifPresent(file -> { - String fileName = file.toAbsolutePath().toString(); - try { - termsFilesProperty.add(new ProtectedTermsListItemModel(ProtectedTermsLoader.readProtectedTermsListFromFile(new File(fileName), true))); - } catch (FileNotFoundException e) { - LOGGER.warn("Cannot find protected terms file " + fileName, e); - } + Path fileName = file.toAbsolutePath(); + termsFilesProperty.add(new ProtectedTermsListItemModel(ProtectedTermsLoader.readProtectedTermsListFromFile(fileName, true))); }); } @@ -155,16 +151,12 @@ public void displayContent(ProtectedTermsListItemModel itemModel) { public void reloadList(ProtectedTermsListItemModel oldItemModel) { ProtectedTermsList oldList = oldItemModel.getTermsList(); - try { - ProtectedTermsList newList = ProtectedTermsLoader.readProtectedTermsListFromFile(new File(oldList.getLocation()), oldList.isEnabled()); - int index = termsFilesProperty.indexOf(oldItemModel); - if (index >= 0) { - termsFilesProperty.set(index, new ProtectedTermsListItemModel(newList)); - } else { - LOGGER.warn("Problem reloading protected terms file {}.", oldList.getLocation()); - } - } catch (IOException e) { - LOGGER.warn("Problem reloading protected terms file {}.", oldList.getLocation(), e); + ProtectedTermsList newList = ProtectedTermsLoader.readProtectedTermsListFromFile(Path.of(oldList.getLocation()), oldList.isEnabled()); + int index = termsFilesProperty.indexOf(oldItemModel); + if (index >= 0) { + termsFilesProperty.set(index, new ProtectedTermsListItemModel(newList)); + } else { + LOGGER.warn("Problem reloading protected terms file {}.", oldList.getLocation()); } } diff --git a/src/main/java/org/jabref/logic/cleanup/DoiCleanup.java b/src/main/java/org/jabref/logic/cleanup/DoiCleanup.java index 0b3b335fa94..3657acabb5f 100644 --- a/src/main/java/org/jabref/logic/cleanup/DoiCleanup.java +++ b/src/main/java/org/jabref/logic/cleanup/DoiCleanup.java @@ -1,7 +1,7 @@ package org.jabref.logic.cleanup; -import java.io.UnsupportedEncodingException; import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -34,11 +34,7 @@ public List cleanup(BibEntry entry) { String doiFieldValue = entry.getField(StandardField.DOI).orElse(null); String decodeDoiFieldValue = ""; - try { - decodeDoiFieldValue = URLDecoder.decode(doiFieldValue, "UTF-8"); - } catch (UnsupportedEncodingException e) { - decodeDoiFieldValue = doiFieldValue; - } + decodeDoiFieldValue = URLDecoder.decode(doiFieldValue, StandardCharsets.UTF_8); doiFieldValue = decodeDoiFieldValue; Optional doi = DOI.parse(doiFieldValue); diff --git a/src/main/java/org/jabref/logic/exporter/TemplateExporter.java b/src/main/java/org/jabref/logic/exporter/TemplateExporter.java index 1baf1643d33..a95265c7df0 100644 --- a/src/main/java/org/jabref/logic/exporter/TemplateExporter.java +++ b/src/main/java/org/jabref/logic/exporter/TemplateExporter.java @@ -1,14 +1,13 @@ package org.jabref.logic.exporter; -import java.io.File; import java.io.FileNotFoundException; -import java.io.FileReader; import java.io.IOException; -import java.io.InputStreamReader; import java.io.Reader; +import java.net.URISyntaxException; import java.net.URL; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.HashMap; @@ -156,28 +155,24 @@ private Reader getReader(String filename) throws IOException { } else { dir = LAYOUT_PREFIX + (directory == null ? "" : directory + '/'); } - // Attempt to get a Reader for the file path given, either by // loading it as a resource (from within JAR), or as a normal file. If // unsuccessful (e.g. file not found), an IOException is thrown. String name = dir + filename; - Reader reader; // Try loading as a resource first. This works for files inside the JAR: - URL reso = TemplateExporter.class.getResource(name); - // If that did not work, try loading as a normal file URL: try { - if (reso == null) { - File f = new File(name); - reader = new FileReader(f); + URL res = TemplateExporter.class.getResource(name); + Path reso; + if (res == null) { + reso = Path.of(name); } else { - reader = new InputStreamReader(reso.openStream()); + reso = Path.of(res.toURI()); } - } catch (FileNotFoundException ex) { + return Files.newBufferedReader(reso, StandardCharsets.UTF_8); + } catch (FileNotFoundException | URISyntaxException ex) { throw new IOException("Cannot find layout file: '" + name + "'."); } - - return reader; } @Override @@ -280,7 +275,6 @@ public void export(final BibDatabaseContext databaseContext, final Path file, } // Print footer - // changed section - begin (arudert) Layout endLayout = null; try (Reader reader = getReader(lfFileName + END_INFIX + LAYOUT_EXTENSION)) { @@ -313,9 +307,9 @@ public void export(final BibDatabaseContext databaseContext, final Path file, * If so, read all the name formatters so they can be used by the filter layouts. */ private void readFormatterFile() { - File formatterFile = new File(lfFileName + FORMATTERS_EXTENSION); - if (formatterFile.exists()) { - try (Reader in = new FileReader(formatterFile)) { + Path formatterFile = Path.of(lfFileName + FORMATTERS_EXTENSION); + if (Files.exists(formatterFile)) { + try (Reader in = Files.newBufferedReader(formatterFile, StandardCharsets.UTF_8)) { // Ok, we found and opened the file. Read all contents: StringBuilder sb = new StringBuilder(); int c; diff --git a/src/main/java/org/jabref/logic/importer/fetcher/ArXivFetcher.java b/src/main/java/org/jabref/logic/importer/fetcher/ArXivFetcher.java index 6332dc24854..dc5c6888a3d 100644 --- a/src/main/java/org/jabref/logic/importer/fetcher/ArXivFetcher.java +++ b/src/main/java/org/jabref/logic/importer/fetcher/ArXivFetcher.java @@ -47,7 +47,6 @@ import org.jabref.model.strings.StringUtil; import org.jabref.model.util.OptionalUtil; -import com.google.common.collect.ImmutableMap; import org.apache.http.client.utils.URIBuilder; import org.apache.lucene.queryparser.flexible.core.nodes.QueryNode; import org.slf4j.Logger; @@ -93,9 +92,9 @@ public class ArXivFetcher implements FulltextFetcher, PagedSearchBasedFetcher, I */ private static final Set CHOSEN_MANUAL_DOI_FIELDS = Set.of(StandardField.DOI, StandardField.PUBLISHER, InternalField.KEY_FIELD); - private static final Map ARXIV_KEYWORDS_WITH_COMMA_REPLACEMENTS = ImmutableMap.of( + private static final Map ARXIV_KEYWORDS_WITH_COMMA_REPLACEMENTS = Collections.unmodifiableMap(Map.of( "Computational Engineering, Finance, and Science", "Computational Engineering / Finance / Science", - "Distributed, Parallel, and Cluster Computing", "Distributed / Parallel / Cluster Computing"); + "Distributed, Parallel, and Cluster Computing", "Distributed / Parallel / Cluster Computing")); private final ArXiv arXiv; private final DoiFetcher doiFetcher; @@ -607,8 +606,8 @@ private List filterYears(List searchResult, ArXivQueryTransf return searchResult.stream() .filter(entry -> entry.getField(StandardField.DATE).isPresent()) // Filter the date field for year only - .filter(entry -> transformer.getEndYear().isEmpty() || Integer.parseInt(entry.getField(StandardField.DATE).get().substring(0, 4)) <= transformer.getEndYear().get()) - .filter(entry -> transformer.getStartYear().isEmpty() || Integer.parseInt(entry.getField(StandardField.DATE).get().substring(0, 4)) >= transformer.getStartYear().get()) + .filter(entry -> transformer.getEndYear().isEmpty() || (Integer.parseInt(entry.getField(StandardField.DATE).get().substring(0, 4)) <= transformer.getEndYear().get())) + .filter(entry -> transformer.getStartYear().isEmpty() || (Integer.parseInt(entry.getField(StandardField.DATE).get().substring(0, 4)) >= transformer.getStartYear().get())) .collect(Collectors.toList()); } diff --git a/src/main/java/org/jabref/logic/importer/fileformat/ACMPortalParser.java b/src/main/java/org/jabref/logic/importer/fileformat/ACMPortalParser.java index 71d84a6d12e..b49cc5cf9ae 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/ACMPortalParser.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/ACMPortalParser.java @@ -7,6 +7,7 @@ import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -23,7 +24,6 @@ import org.jabref.model.entry.types.StandardEntryType; import com.google.common.base.CaseFormat; -import com.google.common.base.Charsets; import com.google.common.base.Enums; import com.google.gson.JsonArray; import com.google.gson.JsonElement; @@ -92,7 +92,7 @@ public List getBibEntriesFromDoiList(List doiList) throws Fetc List bibEntries = new ArrayList<>(); CookieHandler.setDefault(new CookieManager()); try (InputStream stream = new URLDownload(getUrlFromDoiList(doiList)).asInputStream()) { - String jsonString = new String((stream.readAllBytes()), Charsets.UTF_8); + String jsonString = new String((stream.readAllBytes()), StandardCharsets.UTF_8); JsonElement jsonElement = JsonParser.parseString(jsonString); if (jsonElement.isJsonObject()) { diff --git a/src/main/java/org/jabref/logic/importer/util/JsonReader.java b/src/main/java/org/jabref/logic/importer/util/JsonReader.java index 080d2994b23..cfa8d352c9e 100644 --- a/src/main/java/org/jabref/logic/importer/util/JsonReader.java +++ b/src/main/java/org/jabref/logic/importer/util/JsonReader.java @@ -2,10 +2,10 @@ import java.io.IOException; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import org.jabref.logic.importer.ParseException; -import com.google.common.base.Charsets; import kong.unirest.json.JSONArray; import kong.unirest.json.JSONException; import kong.unirest.json.JSONObject; @@ -22,7 +22,7 @@ public class JsonReader { */ public static JSONObject toJsonObject(InputStream inputStream) throws ParseException { try { - String inputStr = new String((inputStream.readAllBytes()), Charsets.UTF_8); + String inputStr = new String((inputStream.readAllBytes()), StandardCharsets.UTF_8); // Fallback: in case an empty stream was passed, return an empty JSON object if (inputStr.isBlank()) { return new JSONObject(); @@ -35,7 +35,7 @@ public static JSONObject toJsonObject(InputStream inputStream) throws ParseExcep public static JSONArray toJsonArray(InputStream stream) throws ParseException { try { - String inpStr = new String((stream.readAllBytes()), Charsets.UTF_8); + String inpStr = new String((stream.readAllBytes()), StandardCharsets.UTF_8); if (inpStr.isBlank()) { return new JSONArray(); } diff --git a/src/main/java/org/jabref/logic/protectedterms/ProtectedTermsLoader.java b/src/main/java/org/jabref/logic/protectedterms/ProtectedTermsLoader.java index d64c4a013ac..c57095e26fb 100644 --- a/src/main/java/org/jabref/logic/protectedterms/ProtectedTermsLoader.java +++ b/src/main/java/org/jabref/logic/protectedterms/ProtectedTermsLoader.java @@ -1,8 +1,7 @@ package org.jabref.logic.protectedterms; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -47,7 +46,7 @@ public void update(ProtectedTermsPreferences preferences) { if (INTERNAL_LISTS.containsKey(filename)) { mainList.add(readProtectedTermsListFromResource(filename, INTERNAL_LISTS.get(filename).get(), true)); } else { - LOGGER.warn("Protected terms resource '" + filename + "' is no longer available."); + LOGGER.warn("Protected terms resource '{}' is no longer available.", filename); } } for (String filename : preferences.getDisabledInternalTermLists()) { @@ -56,7 +55,7 @@ public void update(ProtectedTermsPreferences preferences) { mainList.add(readProtectedTermsListFromResource(filename, INTERNAL_LISTS.get(filename).get(), false)); } } else { - LOGGER.warn("Protected terms resource '" + filename + "' is no longer available."); + LOGGER.warn("Protected terms resource '{}' is no longer available.", filename); } } @@ -66,43 +65,34 @@ public void update(ProtectedTermsPreferences preferences) { && !preferences.getDisabledInternalTermLists().contains(filename)) { // New internal list, add it mainList.add(readProtectedTermsListFromResource(filename, INTERNAL_LISTS.get(filename).get(), true)); - LOGGER.warn("New protected terms resource '" + filename + "' is available and enabled by default."); + LOGGER.warn("New protected terms resource '{}' is available and enabled by default.", filename); } } // Read external lists for (String filename : preferences.getEnabledExternalTermLists()) { - try { - mainList.add(readProtectedTermsListFromFile(new File(filename), true)); - } catch (FileNotFoundException e) { - // The file couldn't be found... - LOGGER.warn("Cannot find protected terms file " + filename, e); + Path filePath = Path.of(filename); + if (Files.exists(filePath)) { + mainList.add(readProtectedTermsListFromFile(filePath, true)); + } else { + LOGGER.warn("Cannot find protected terms file {} ", filename); } } for (String filename : preferences.getDisabledExternalTermLists()) { if (!preferences.getEnabledExternalTermLists().contains(filename)) { - try { - mainList.add(readProtectedTermsListFromFile(new File(filename), false)); - } catch (FileNotFoundException e) { - // The file couldn't be found... - LOGGER.warn("Cannot find protected terms file " + filename, e); - } + mainList.add(readProtectedTermsListFromFile(Path.of(filename), false)); } } } public void reloadProtectedTermsList(ProtectedTermsList list) { - try { - ProtectedTermsList newList = readProtectedTermsListFromFile(new File(list.getLocation()), list.isEnabled()); - int index = mainList.indexOf(list); - if (index >= 0) { - mainList.set(index, newList); - } else { - LOGGER.warn("Problem reloading protected terms file"); - } - } catch (IOException e) { - LOGGER.warn("Problem with protected terms file '" + list.getLocation() + "'", e); + ProtectedTermsList newList = readProtectedTermsListFromFile(Path.of(list.getLocation()), list.isEnabled()); + int index = mainList.indexOf(list); + if (index >= 0) { + mainList.set(index, newList); + } else { + LOGGER.warn("Problem reloading protected terms file"); } } @@ -122,12 +112,7 @@ public List getProtectedTerms() { } public void addProtectedTermsListFromFile(String fileName, boolean enabled) { - try { - mainList.add(readProtectedTermsListFromFile(new File(fileName), enabled)); - } catch (FileNotFoundException e) { - // The file couldn't be found... - LOGGER.warn("Cannot find protected terms file " + fileName, e); - } + mainList.add(readProtectedTermsListFromFile(Path.of(fileName), enabled)); } public static ProtectedTermsList readProtectedTermsListFromResource(String resource, String description, boolean enabled) { @@ -136,10 +121,10 @@ public static ProtectedTermsList readProtectedTermsListFromResource(String resou return parser.getProtectTermsList(enabled, true); } - public static ProtectedTermsList readProtectedTermsListFromFile(File file, boolean enabled) throws FileNotFoundException { - LOGGER.debug("Reading term list from file " + file); + public static ProtectedTermsList readProtectedTermsListFromFile(Path path, boolean enabled) { + LOGGER.debug("Reading term list from file {}", path); ProtectedTermsParser parser = new ProtectedTermsParser(); - parser.readTermsFromFile(Objects.requireNonNull(file)); + parser.readTermsFromFile(Objects.requireNonNull(path)); return parser.getProtectTermsList(enabled, false); } diff --git a/src/main/java/org/jabref/logic/protectedterms/ProtectedTermsParser.java b/src/main/java/org/jabref/logic/protectedterms/ProtectedTermsParser.java index 8c0c57a7625..5cd8628f657 100644 --- a/src/main/java/org/jabref/logic/protectedterms/ProtectedTermsParser.java +++ b/src/main/java/org/jabref/logic/protectedterms/ProtectedTermsParser.java @@ -1,19 +1,15 @@ package org.jabref.logic.protectedterms; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileReader; import java.io.IOException; -import java.io.InputStreamReader; -import java.io.Reader; -import java.net.URL; -import java.nio.charset.Charset; +import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.jabref.logic.l10n.Localization; @@ -33,63 +29,45 @@ public class ProtectedTermsParser { private String location; public void readTermsFromResource(String resourceFileName, String descriptionString) { - URL url = Objects - .requireNonNull(ProtectedTermsLoader.class.getResource(Objects.requireNonNull(resourceFileName))); - description = descriptionString; - location = resourceFileName; try { - readTermsList(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8)); - } catch (IOException e) { - LOGGER.info("Could not read protected terms from resource " + resourceFileName, e); + Path path = Path.of(Objects.requireNonNull(ProtectedTermsLoader.class.getResource(Objects.requireNonNull(resourceFileName))).toURI()); + readTermsList(path); + description = descriptionString; + location = resourceFileName; + } catch (URISyntaxException e1) { + LOGGER.error(""); } } - public void readTermsFromFile(File file) throws FileNotFoundException { - location = file.getAbsolutePath(); - try (FileReader reader = new FileReader(Objects.requireNonNull(file))) { - readTermsList(reader); - } catch (FileNotFoundException e) { - throw e; - } catch (IOException e) { - LOGGER.warn("Could not read terms from file " + file.getAbsolutePath(), e); - } + public void readTermsFromFile(Path path) { + location = path.toAbsolutePath().toString(); + readTermsList(path); } - public void readTermsFromFile(File file, Charset encoding) throws FileNotFoundException { - location = file.getAbsolutePath(); - try (FileInputStream stream = new FileInputStream(Objects.requireNonNull(file)); - InputStreamReader reader = new InputStreamReader(stream, Objects.requireNonNull(encoding))) { - readTermsList(reader); - } catch (FileNotFoundException e) { - throw e; + private void readTermsList(Path path) { + if (!Files.exists(path)) { + LOGGER.warn("Could not read terms from file {}", path); + return; + } + try (Stream lines = Files.lines(path, StandardCharsets.UTF_8)) { + this.terms.addAll(lines.map(this::setDescription).filter(line -> line != null).collect(Collectors.toList())); } catch (IOException e) { - LOGGER.warn("Could not read terms from file " + file.getAbsolutePath(), e); + LOGGER.warn("Could not read terms from file {}", path, e); } } /** - * Read the given file, which should contain a list of journal names and their - * abbreviations. Each line should be formatted as: "Full Journal Name=Abbr. Journal Name" + * Parse the description that starts after the # but don't include it in the terms * - * @param in + * @param line + * @return line or null if the line contains the description */ - private void readTermsList(Reader in) { - try (BufferedReader reader = new BufferedReader(in)) { - String line; - while ((line = reader.readLine()) != null) { - addLine(line); - } - } catch (IOException ex) { - LOGGER.info("Could not read journal list from file ", ex); - } - } - - private void addLine(String line) { + private String setDescription(String line) { if (line.startsWith("#")) { description = line.substring(1).trim(); - return; + return null; } - this.terms.add(line); + return line; } public ProtectedTermsList getProtectTermsList(boolean enabled, boolean internal) { diff --git a/src/test/java/org/jabref/logic/protectedterms/ProtectedTermsLoaderTest.java b/src/test/java/org/jabref/logic/protectedterms/ProtectedTermsLoaderTest.java index 2124d7a63ae..2b25939e788 100644 --- a/src/test/java/org/jabref/logic/protectedterms/ProtectedTermsLoaderTest.java +++ b/src/test/java/org/jabref/logic/protectedterms/ProtectedTermsLoaderTest.java @@ -1,11 +1,8 @@ package org.jabref.logic.protectedterms; -import java.io.File; -import java.io.FileNotFoundException; import java.net.URISyntaxException; import java.nio.file.Path; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -37,18 +34,17 @@ void testGetProtectedTerms() throws URISyntaxException { loader.removeProtectedTermsList(list); } assertTrue(loader.getProtectedTermsLists().isEmpty()); - String filename = Path.of( - ProtectedTermsLoader.class.getResource("/org/jabref/logic/protectedterms/namedterms.terms").toURI()) - .toFile().getPath(); + String filename = Path.of(ProtectedTermsLoader.class.getResource("/org/jabref/logic/protectedterms/namedterms.terms") + .toURI()) + .toFile().getPath(); loader.addProtectedTermsListFromFile(filename, true); - assertEquals(Arrays.asList("Einstein"), loader.getProtectedTerms()); + assertEquals(List.of("Einstein"), loader.getProtectedTerms()); } @Test void testAddProtectedTermsListFromFile() throws URISyntaxException { - String filename = Path.of( - ProtectedTermsLoader.class.getResource("/org/jabref/logic/protectedterms/namedterms.terms") - .toURI()) + String filename = Path.of(ProtectedTermsLoader.class.getResource("/org/jabref/logic/protectedterms/namedterms.terms") + .toURI()) .toFile().getPath(); assertEquals(ProtectedTermsLoader.getInternalLists().size(), loader.getProtectedTermsLists().size()); loader.addProtectedTermsListFromFile(filename, false); @@ -56,48 +52,48 @@ void testAddProtectedTermsListFromFile() throws URISyntaxException { } @Test - void testReadProtectedTermsListFromFileReadsDescription() throws URISyntaxException, FileNotFoundException { - File file = Path.of( - ProtectedTermsLoader.class.getResource("/org/jabref/logic/protectedterms/namedterms.terms").toURI()) - .toFile(); + void testReadProtectedTermsListFromFileReadsDescription() throws URISyntaxException { + Path file = Path.of( + ProtectedTermsLoader.class.getResource("/org/jabref/logic/protectedterms/namedterms.terms") + .toURI()); + ProtectedTermsList list = ProtectedTermsLoader.readProtectedTermsListFromFile(file, true); assertEquals("Term list", list.getDescription()); } @Test - void testReadProtectedTermsListFromFileDisabledWorks() throws URISyntaxException, FileNotFoundException { - File file = Path.of( - ProtectedTermsLoader.class.getResource("/org/jabref/logic/protectedterms/namedterms.terms").toURI()) - .toFile(); + void testReadProtectedTermsListFromFileDisabledWorks() throws URISyntaxException { + Path file = Path.of(ProtectedTermsLoader.class.getResource("/org/jabref/logic/protectedterms/namedterms.terms") + .toURI()); + ProtectedTermsList list = ProtectedTermsLoader.readProtectedTermsListFromFile(file, false); assertFalse(list.isEnabled()); } @Test - void testReadProtectedTermsListFromFileEnabledWorks() throws URISyntaxException, FileNotFoundException { - File file = Path.of( - ProtectedTermsLoader.class.getResource("/org/jabref/logic/protectedterms/namedterms.terms").toURI()) - .toFile(); + void testReadProtectedTermsListFromFileEnabledWorks() throws URISyntaxException { + Path file = Path.of(ProtectedTermsLoader.class.getResource("/org/jabref/logic/protectedterms/namedterms.terms") + .toURI()); + ProtectedTermsList list = ProtectedTermsLoader.readProtectedTermsListFromFile(file, true); assertTrue(list.isEnabled()); } @Test - void testReadProtectedTermsListFromFileIsNotInternalList() throws URISyntaxException, FileNotFoundException { - File file = Path.of( - ProtectedTermsLoader.class.getResource("/org/jabref/logic/protectedterms/namedterms.terms").toURI()) - .toFile(); + void testReadProtectedTermsListFromFileIsNotInternalList() throws URISyntaxException { + Path file = Path.of(ProtectedTermsLoader.class.getResource("/org/jabref/logic/protectedterms/namedterms.terms") + .toURI()); + ProtectedTermsList list = ProtectedTermsLoader.readProtectedTermsListFromFile(file, true); assertFalse(list.isInternalList()); } @Test void testReadProtectedTermsListFromFileNoDescriptionGivesDefaultDescription() - throws URISyntaxException, FileNotFoundException { - File file = Path.of( + throws URISyntaxException { + Path file = Path.of( ProtectedTermsLoader.class.getResource("/org/jabref/logic/protectedterms/unnamedterms.terms") - .toURI()) - .toFile(); + .toURI()); ProtectedTermsList list = ProtectedTermsLoader.readProtectedTermsListFromFile(file, true); assertEquals(Localization.lang("The text after the last line starting with # will be used"), list.getDescription()); @@ -105,17 +101,21 @@ void testReadProtectedTermsListFromFileNoDescriptionGivesDefaultDescription() @Test void testNewListsAreIncluded() { - ProtectedTermsLoader localLoader = new ProtectedTermsLoader( - new ProtectedTermsPreferences(Collections.emptyList(), - Collections.emptyList(), Collections.emptyList(), Collections.emptyList())); + ProtectedTermsLoader localLoader = new ProtectedTermsLoader(new ProtectedTermsPreferences( + Collections.emptyList(), + Collections.emptyList(), + Collections.emptyList(), + Collections.emptyList())); assertEquals(ProtectedTermsLoader.getInternalLists().size(), localLoader.getProtectedTermsLists().size()); } @Test void testNewListsAreEnabled() { - ProtectedTermsLoader localLoader = new ProtectedTermsLoader( - new ProtectedTermsPreferences(Collections.emptyList(), - Collections.emptyList(), Collections.emptyList(), Collections.emptyList())); + ProtectedTermsLoader localLoader = new ProtectedTermsLoader(new ProtectedTermsPreferences( + Collections.emptyList(), + Collections.emptyList(), + Collections.emptyList(), + Collections.emptyList())); for (ProtectedTermsList list : localLoader.getProtectedTermsLists()) { assertTrue(list.isEnabled()); } @@ -123,9 +123,11 @@ void testNewListsAreEnabled() { @Test void testInitalizedAllInternalDisabled() { - ProtectedTermsLoader localLoader = new ProtectedTermsLoader( - new ProtectedTermsPreferences(Collections.emptyList(), Collections.emptyList(), - ProtectedTermsLoader.getInternalLists(), Collections.emptyList())); + ProtectedTermsLoader localLoader = new ProtectedTermsLoader(new ProtectedTermsPreferences( + Collections.emptyList(), + Collections.emptyList(), + ProtectedTermsLoader.getInternalLists(), + Collections.emptyList())); for (ProtectedTermsList list : localLoader.getProtectedTermsLists()) { assertFalse(list.isEnabled()); } @@ -133,75 +135,92 @@ void testInitalizedAllInternalDisabled() { @Test void testUnknownExternalFileWillNotLoad() { - ProtectedTermsLoader localLoader = new ProtectedTermsLoader( - new ProtectedTermsPreferences(ProtectedTermsLoader.getInternalLists(), - Collections.singletonList("someUnlikelyNameThatNeverWillExist"), Collections.emptyList(), - Collections.emptyList())); + ProtectedTermsLoader localLoader = new ProtectedTermsLoader(new ProtectedTermsPreferences( + ProtectedTermsLoader.getInternalLists(), + Collections.singletonList("someUnlikelyNameThatNeverWillExist"), + Collections.emptyList(), + Collections.emptyList())); assertEquals(ProtectedTermsLoader.getInternalLists().size(), localLoader.getProtectedTermsLists().size()); } @Test void testAllDisabledNoWords() { - ProtectedTermsLoader localLoader = new ProtectedTermsLoader( - new ProtectedTermsPreferences(Collections.emptyList(), Collections.emptyList(), - ProtectedTermsLoader.getInternalLists(), Collections.emptyList())); + ProtectedTermsLoader localLoader = new ProtectedTermsLoader(new ProtectedTermsPreferences( + Collections.emptyList(), + Collections.emptyList(), + ProtectedTermsLoader.getInternalLists(), + Collections.emptyList())); assertEquals(Collections.emptyList(), localLoader.getProtectedTerms()); } @Test void testDoNotLoadTheSameInternalListTwice() { ProtectedTermsLoader localLoader = new ProtectedTermsLoader( - new ProtectedTermsPreferences(ProtectedTermsLoader.getInternalLists(), Collections.emptyList(), - ProtectedTermsLoader.getInternalLists(), Collections.emptyList())); + new ProtectedTermsPreferences( + ProtectedTermsLoader.getInternalLists(), + Collections.emptyList(), + ProtectedTermsLoader.getInternalLists(), + Collections.emptyList())); assertEquals(ProtectedTermsLoader.getInternalLists().size(), localLoader.getProtectedTermsLists().size()); } @Test void testAddNewTermListAddsList(@TempDir Path tempDir) { - ProtectedTermsLoader localLoader = new ProtectedTermsLoader( - new ProtectedTermsPreferences(Collections.emptyList(), Collections.emptyList(), - ProtectedTermsLoader.getInternalLists(), Collections.emptyList())); - localLoader.addNewProtectedTermsList("My new list", tempDir.toFile().getAbsolutePath()); + ProtectedTermsLoader localLoader = new ProtectedTermsLoader(new ProtectedTermsPreferences( + Collections.emptyList(), + Collections.emptyList(), + ProtectedTermsLoader.getInternalLists(), + Collections.emptyList())); + localLoader.addNewProtectedTermsList("My new list", tempDir.resolve("MyNewList.terms").toAbsolutePath().toString()); assertEquals(ProtectedTermsLoader.getInternalLists().size() + 1, localLoader.getProtectedTermsLists().size()); } @Test void testAddNewTermListNewListInList(@TempDir Path tempDir) { ProtectedTermsLoader localLoader = new ProtectedTermsLoader( - new ProtectedTermsPreferences(Collections.emptyList(), Collections.emptyList(), - ProtectedTermsLoader.getInternalLists(), Collections.emptyList())); - ProtectedTermsList newList = localLoader.addNewProtectedTermsList("My new list", - tempDir.toFile().getAbsolutePath()); + new ProtectedTermsPreferences( + Collections.emptyList(), + Collections.emptyList(), + ProtectedTermsLoader.getInternalLists(), + Collections.emptyList())); + ProtectedTermsList newList = localLoader.addNewProtectedTermsList("My new list", tempDir.resolve("MyNewList.terms") + .toAbsolutePath() + .toString()); assertTrue(localLoader.getProtectedTermsLists().contains(newList)); } @Test void testRemoveTermList(@TempDir Path tempDir) { ProtectedTermsLoader localLoader = new ProtectedTermsLoader( - new ProtectedTermsPreferences(Collections.emptyList(), Collections.emptyList(), - ProtectedTermsLoader.getInternalLists(), Collections.emptyList())); - ProtectedTermsList newList = localLoader.addNewProtectedTermsList("My new list", tempDir.toFile().getAbsolutePath()); + new ProtectedTermsPreferences( + Collections.emptyList(), + Collections.emptyList(), + ProtectedTermsLoader.getInternalLists(), + Collections.emptyList())); + ProtectedTermsList newList = localLoader.addNewProtectedTermsList("My new list", tempDir.resolve("MyNewList.terms").toAbsolutePath().toString()); assertTrue(localLoader.removeProtectedTermsList(newList)); } @Test void testRemoveTermListReduceTheCount(@TempDir Path tempDir) { - ProtectedTermsLoader localLoader = new ProtectedTermsLoader( - new ProtectedTermsPreferences(Collections.emptyList(), Collections.emptyList(), - ProtectedTermsLoader.getInternalLists(), Collections.emptyList())); - ProtectedTermsList newList = localLoader.addNewProtectedTermsList("My new list", - tempDir.toFile().getAbsolutePath()); + ProtectedTermsLoader localLoader = new ProtectedTermsLoader(new ProtectedTermsPreferences( + Collections.emptyList(), + Collections.emptyList(), + ProtectedTermsLoader.getInternalLists(), + Collections.emptyList())); + ProtectedTermsList newList = localLoader.addNewProtectedTermsList("My new list", tempDir.resolve("MyNewList.terms").toAbsolutePath().toString()); localLoader.removeProtectedTermsList(newList); assertEquals(ProtectedTermsLoader.getInternalLists().size(), localLoader.getProtectedTermsLists().size()); } @Test void testAddNewTermListSetsCorrectDescription(@TempDir Path tempDir) { - ProtectedTermsLoader localLoader = new ProtectedTermsLoader( - new ProtectedTermsPreferences(Collections.emptyList(), Collections.emptyList(), - ProtectedTermsLoader.getInternalLists(), Collections.emptyList())); - ProtectedTermsList newList = localLoader.addNewProtectedTermsList("My new list", - tempDir.toFile().getAbsolutePath()); + ProtectedTermsLoader localLoader = new ProtectedTermsLoader(new ProtectedTermsPreferences( + Collections.emptyList(), + Collections.emptyList(), + ProtectedTermsLoader.getInternalLists(), + Collections.emptyList())); + ProtectedTermsList newList = localLoader.addNewProtectedTermsList("My new list", tempDir.resolve("MyNewList.terms").toAbsolutePath().toString()); assertEquals("My new list", newList.getDescription()); } }