diff --git a/src/main/java/net/sf/jabref/cli/ArgumentProcessor.java b/src/main/java/net/sf/jabref/cli/ArgumentProcessor.java index 873d7b09cb7..32c7c29a413 100644 --- a/src/main/java/net/sf/jabref/cli/ArgumentProcessor.java +++ b/src/main/java/net/sf/jabref/cli/ArgumentProcessor.java @@ -2,6 +2,8 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -515,14 +517,19 @@ private static Optional importFile(String argument) { if ((data.length > 1) && !"*".equals(data[1])) { System.out.println(Localization.lang("Importing") + ": " + data[0]); try { - List entries; + Path file; if (OS.WINDOWS) { - entries = Globals.IMPORT_FORMAT_READER.importFromFile(data[1], data[0], JabRefGUI.getMainFrame()); + file = Paths.get(data[0]); } else { - entries = Globals.IMPORT_FORMAT_READER.importFromFile(data[1], - data[0].replace("~", System.getProperty("user.home")), JabRefGUI.getMainFrame()); + file = Paths.get(data[0].replace("~", System.getProperty("user.home"))); } - return Optional.of(new ParserResult(entries)); + ParserResult result = Globals.IMPORT_FORMAT_READER.importFromFile(data[1], file); + + if(result.hasWarnings()) { + JabRefGUI.getMainFrame().showMessage(result.getErrorMessage()); + } + + return Optional.of(result); } catch (IllegalArgumentException ex) { System.err.println(Localization.lang("Unknown import format") + ": " + data[1]); return Optional.empty(); diff --git a/src/main/java/net/sf/jabref/exporter/CustomExportDialog.java b/src/main/java/net/sf/jabref/exporter/CustomExportDialog.java index 04704edb901..489a69af8c8 100644 --- a/src/main/java/net/sf/jabref/exporter/CustomExportDialog.java +++ b/src/main/java/net/sf/jabref/exporter/CustomExportDialog.java @@ -22,6 +22,7 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; +import java.util.Collections; import javax.swing.AbstractAction; import javax.swing.ActionMap; @@ -103,7 +104,7 @@ public void actionPerformed(ActionEvent e) { JButton browse = new JButton(Localization.lang("Browse")); browse.addActionListener(e -> { File directory = new File(Globals.prefs.get(JabRefPreferences.EXPORT_WORKING_DIRECTORY)); - String chosenStr = FileDialogs.getNewFile(parent, directory, ".layout", + String chosenStr = FileDialogs.getNewFile(parent, directory, Collections.singletonList(".layout"), JFileChooser.OPEN_DIALOG, false); if (chosenStr == null) { return; diff --git a/src/main/java/net/sf/jabref/exporter/SaveDatabaseAction.java b/src/main/java/net/sf/jabref/exporter/SaveDatabaseAction.java index 8577e75d45c..f5ec6b6f6af 100644 --- a/src/main/java/net/sf/jabref/exporter/SaveDatabaseAction.java +++ b/src/main/java/net/sf/jabref/exporter/SaveDatabaseAction.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.nio.charset.Charset; import java.nio.charset.UnsupportedCharsetException; +import java.util.Collections; import javax.swing.JFileChooser; import javax.swing.JOptionPane; @@ -303,8 +304,8 @@ public void saveAs() throws Throwable { String chosenFile; File f = null; while (f == null) { - chosenFile = FileDialogs.getNewFile(frame, new File(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)), ".bib", - JFileChooser.SAVE_DIALOG, false, null); + chosenFile = FileDialogs.getNewFile(frame, new File(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)), + Collections.singletonList(".bib"), JFileChooser.SAVE_DIALOG, false, null); if (chosenFile == null) { canceled = true; return; // canceled diff --git a/src/main/java/net/sf/jabref/external/ExternalFileTypeEntryEditor.java b/src/main/java/net/sf/jabref/external/ExternalFileTypeEntryEditor.java index d7f0f93e3c0..99ceaf30c46 100644 --- a/src/main/java/net/sf/jabref/external/ExternalFileTypeEntryEditor.java +++ b/src/main/java/net/sf/jabref/external/ExternalFileTypeEntryEditor.java @@ -20,6 +20,7 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; +import java.util.Collections; import javax.swing.BorderFactory; import javax.swing.ButtonGroup; @@ -263,7 +264,7 @@ public void actionPerformed(ActionEvent e) { // Nothing in the field. Go to the last file dir used: initial = new File(Globals.prefs.get(JabRefPreferences.FILE_WORKING_DIRECTORY)); } - String chosen = FileDialogs.getNewFile(null, initial, "", + String chosen = FileDialogs.getNewFile(null, initial, Collections.emptyList(), JFileChooser.OPEN_DIALOG, false); if (chosen != null) { File newFile = new File(chosen); diff --git a/src/main/java/net/sf/jabref/external/MoveFileAction.java b/src/main/java/net/sf/jabref/external/MoveFileAction.java index 23caa58e7ef..9533006abf3 100644 --- a/src/main/java/net/sf/jabref/external/MoveFileAction.java +++ b/src/main/java/net/sf/jabref/external/MoveFileAction.java @@ -18,6 +18,7 @@ import java.awt.event.ActionEvent; import java.io.File; import java.io.IOException; +import java.util.Collections; import java.util.List; import java.util.Locale; @@ -142,7 +143,8 @@ public void actionPerformed(ActionEvent event) { } chosenFile = sb.toString(); } else { - chosenFile = FileDialogs.getNewFile(frame, file, extension, JFileChooser.SAVE_DIALOG, false); + chosenFile = FileDialogs.getNewFile(frame, file, Collections.singletonList(extension), + JFileChooser.SAVE_DIALOG, false); } if (chosenFile == null) { return; // canceled diff --git a/src/main/java/net/sf/jabref/gui/BasePanel.java b/src/main/java/net/sf/jabref/gui/BasePanel.java index 7b628b74099..3eae0ea90e1 100644 --- a/src/main/java/net/sf/jabref/gui/BasePanel.java +++ b/src/main/java/net/sf/jabref/gui/BasePanel.java @@ -2328,8 +2328,8 @@ public SaveSelectedAction(SavePreferences.DatabaseSaveType saveType) { public void action() throws SaveException { String chosenFile = FileDialogs.getNewFile(frame, - new File(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)), ".bib", JFileChooser.SAVE_DIALOG, - false); + new File(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)), Collections.singletonList(".bib"), + JFileChooser.SAVE_DIALOG, false); if (chosenFile != null) { File expFile = new File(chosenFile); if (!expFile.exists() || (JOptionPane.showConfirmDialog(frame, diff --git a/src/main/java/net/sf/jabref/gui/FileDialogs.java b/src/main/java/net/sf/jabref/gui/FileDialogs.java index 087b6eca5ac..de93d9657ce 100644 --- a/src/main/java/net/sf/jabref/gui/FileDialogs.java +++ b/src/main/java/net/sf/jabref/gui/FileDialogs.java @@ -19,6 +19,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Objects; import javax.swing.JComponent; import javax.swing.JFileChooser; @@ -28,13 +29,6 @@ import net.sf.jabref.JabRefPreferences; import net.sf.jabref.logic.util.OS; -/** - * Created by IntelliJ IDEA. - * User: alver - * Date: Apr 14, 2009 - * Time: 7:24:07 PM - * To change this template use File | Settings | File Templates. - */ public class FileDialogs { /** @@ -45,21 +39,24 @@ public class FileDialogs { * * @param owner * @param directory - * @param extension + * @param extensions * @param updateWorkingdirectory * @return an array of selected file paths, or an empty array if no selection is made. */ - public static List getMultipleFiles(JFrame owner, File directory, String extension, + public static List getMultipleFiles(JFrame owner, File directory, List extensions, boolean updateWorkingdirectory) { - OpenFileFilter off = null; - if (extension == null) { + Objects.requireNonNull(extensions); + + OpenFileFilter off; + if (extensions.isEmpty()) { off = new OpenFileFilter(); - } else if (!extension.isEmpty()) { - off = new OpenFileFilter(extension); + } else { + off = new OpenFileFilter(extensions); } - Object files = FileDialogs.getNewFileImpl(owner, directory, extension, null, off, JFileChooser.OPEN_DIALOG, updateWorkingdirectory, false, true, null); + Object files = FileDialogs.getNewFileImpl(owner, directory, extensions, null, off, JFileChooser.OPEN_DIALOG, + updateWorkingdirectory, false, true, null); if (files instanceof String[]) { return Arrays.asList((String[]) files); @@ -72,40 +69,42 @@ public static List getMultipleFiles(JFrame owner, File directory, String return Collections.emptyList(); } - public static String getNewFile(JFrame owner, File directory, String extension, int dialogType, boolean updateWorkingDirectory) { - return FileDialogs.getNewFile(owner, directory, extension, null, dialogType, updateWorkingDirectory, false, null); + public static String getNewFile(JFrame owner, File directory, List extensions, int dialogType, boolean updateWorkingDirectory) { + return FileDialogs.getNewFile(owner, directory, extensions, null, dialogType, updateWorkingDirectory, false, null); } - public static String getNewFile(JFrame owner, File directory, String extension, int dialogType, boolean updateWorkingDirectory, JComponent accessory) { - return FileDialogs.getNewFile(owner, directory, extension, null, dialogType, updateWorkingDirectory, false, accessory); + public static String getNewFile(JFrame owner, File directory, List extensions, int dialogType, boolean updateWorkingDirectory, JComponent accessory) { + return FileDialogs.getNewFile(owner, directory, extensions, null, dialogType, updateWorkingDirectory, false, accessory); } - public static String getNewFile(JFrame owner, File directory, String extension, String description, int dialogType, boolean updateWorkingDirectory) { - return FileDialogs.getNewFile(owner, directory, extension, description, dialogType, updateWorkingDirectory, false, null); + public static String getNewFile(JFrame owner, File directory, List extensions, String description, int dialogType, boolean updateWorkingDirectory) { + return FileDialogs.getNewFile(owner, directory, extensions, description, dialogType, updateWorkingDirectory, false, null); } - public static String getNewDir(JFrame owner, File directory, String extension, int dialogType, boolean updateWorkingDirectory) { - return FileDialogs.getNewFile(owner, directory, extension, null, dialogType, updateWorkingDirectory, true, null); + public static String getNewDir(JFrame owner, File directory, List extensions, int dialogType, boolean updateWorkingDirectory) { + return FileDialogs.getNewFile(owner, directory, extensions, null, dialogType, updateWorkingDirectory, true, null); } - public static String getNewDir(JFrame owner, File directory, String extension, String description, int dialogType, boolean updateWorkingDirectory) { - return FileDialogs.getNewFile(owner, directory, extension, description, dialogType, updateWorkingDirectory, true, null); + public static String getNewDir(JFrame owner, File directory, List extensions, String description, int dialogType, boolean updateWorkingDirectory) { + return FileDialogs.getNewFile(owner, directory, extensions, description, dialogType, updateWorkingDirectory, true, null); } - private static String getNewFile(JFrame owner, File directory, String extension, String description, int dialogType, boolean updateWorkingDirectory, boolean dirOnly, JComponent accessory) { + private static String getNewFile(JFrame owner, File directory, List extensions, String description, int dialogType, boolean updateWorkingDirectory, boolean dirOnly, JComponent accessory) { - OpenFileFilter off = null; + OpenFileFilter off; - if (extension == null) { + if (extensions.isEmpty()) { off = new OpenFileFilter(); - } else if (!extension.isEmpty()) { - off = new OpenFileFilter(extension); + } else { + off = new OpenFileFilter(extensions); } - return (String) FileDialogs.getNewFileImpl(owner, directory, extension, description, off, dialogType, updateWorkingDirectory, dirOnly, false, accessory); + return (String) FileDialogs.getNewFileImpl(owner, directory, extensions, description, off, dialogType, updateWorkingDirectory, dirOnly, false, accessory); } - private static Object getNewFileImpl(JFrame owner, File directory, String extension, String description, OpenFileFilter off, int dialogType, boolean updateWorkingDirectory, boolean dirOnly, boolean multipleSelection, JComponent accessory) { + private static Object getNewFileImpl(JFrame owner, File directory, List extensions, String description, + OpenFileFilter off, int dialogType, boolean updateWorkingDirectory, boolean dirOnly, + boolean multipleSelection, JComponent accessory) { // Added the !dirOnly condition below as a workaround to the native file dialog // not supporting directory selection: @@ -160,10 +159,10 @@ private static Object getNewFileImpl(JFrame owner, File directory, String extens // If this is a save dialog, and the user has not chosen "All files" as // filter // we enforce the given extension. But only if extension is not null. - if ((extension != null) && (dialogType == JFileChooser.SAVE_DIALOG) && (fc.getFileFilter() == off) && !off.accept(selectedFile)) { + if ((!extensions.isEmpty()) && (dialogType == JFileChooser.SAVE_DIALOG) && (fc.getFileFilter() == off) && !off.accept(selectedFile)) { // add the first extension if there are multiple extensions - selectedFile = new File(selectedFile.getPath() + extension.split("[, ]+", 0)[0]); + selectedFile = new File(selectedFile.getPath() + extensions.get(0)); } if (updateWorkingDirectory) { diff --git a/src/main/java/net/sf/jabref/gui/FileListEntryEditor.java b/src/main/java/net/sf/jabref/gui/FileListEntryEditor.java index e238934e40b..2858bffadf3 100644 --- a/src/main/java/net/sf/jabref/gui/FileListEntryEditor.java +++ b/src/main/java/net/sf/jabref/gui/FileListEntryEditor.java @@ -23,6 +23,7 @@ import java.io.File; import java.io.IOException; import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.Optional; import java.util.regex.Pattern; @@ -364,8 +365,8 @@ public void actionPerformed(ActionEvent e) { } else { workingDir = new File(Globals.prefs.get(JabRefPreferences.FILE_WORKING_DIRECTORY)); } - - String selection = FileDialogs.getNewFile(parent, workingDir, "", JFileChooser.OPEN_DIALOG, false); + String selection = FileDialogs.getNewFile(parent, workingDir, Collections.emptyList(), + JFileChooser.OPEN_DIALOG, false); if (selection != null) { File newFile = new File(selection); // Store the directory for next time: diff --git a/src/main/java/net/sf/jabref/gui/OpenFileFilter.java b/src/main/java/net/sf/jabref/gui/OpenFileFilter.java index e499969fff1..c5a8fbb9150 100644 --- a/src/main/java/net/sf/jabref/gui/OpenFileFilter.java +++ b/src/main/java/net/sf/jabref/gui/OpenFileFilter.java @@ -17,7 +17,9 @@ import java.io.File; import java.io.FilenameFilter; +import java.util.Arrays; import java.util.HashSet; +import java.util.List; import java.util.Set; public class OpenFileFilter extends javax.swing.filechooser.FileFilter implements FilenameFilter { @@ -26,42 +28,38 @@ public class OpenFileFilter extends javax.swing.filechooser.FileFilter implement private final String desc; - public OpenFileFilter(String[] extensions) { + public OpenFileFilter(List extensions) { StringBuilder buf = new StringBuilder(); - int numExt = extensions.length; + int numExt = extensions.size(); if (numExt > 0) { buf.append('*'); - buf.append(extensions[0]); + buf.append(extensions.get(0)); - extSet.add(extensions[0]); + extSet.add(extensions.get(0)); } for (int curExt = 1; curExt < numExt; curExt++) { buf.append(", *"); - buf.append(extensions[curExt]); + buf.append(extensions.get(curExt)); - extSet.add(extensions[curExt]); + extSet.add(extensions.get(curExt)); } desc = buf.toString(); } public OpenFileFilter() { - this(new String[] { + this(Arrays.asList( ".bib", ".dat", // silverplatter ending - ".txt", // windows puts ".txt" extentions and for scifinder + ".txt", // windows puts ".txt" extensions and for scifinder ".ris", ".ref", // refer/endnote format ".fcgi", // default for pubmed ".bibx", // default for BibTeXML ".xml" - }); - } - - public OpenFileFilter(String s) { - this(s.split("[, ]+", 0)); + )); } @Override diff --git a/src/main/java/net/sf/jabref/gui/actions/BrowseAction.java b/src/main/java/net/sf/jabref/gui/actions/BrowseAction.java index 2dc4e73a02a..88f5757e662 100644 --- a/src/main/java/net/sf/jabref/gui/actions/BrowseAction.java +++ b/src/main/java/net/sf/jabref/gui/actions/BrowseAction.java @@ -17,6 +17,8 @@ import java.awt.event.ActionEvent; import java.io.File; +import java.util.Collections; +import java.util.List; import javax.swing.AbstractAction; import javax.swing.JComponent; @@ -37,21 +39,21 @@ public final class BrowseAction extends AbstractAction { private final JTextField comp; private final boolean dir; private final JComponent focusTarget; - private final String extension; + private final List extension; public static BrowseAction buildForDir(JFrame frame, JTextField tc) { - return new BrowseAction(frame, tc, true, null, ""); + return new BrowseAction(frame, tc, true, null, Collections.emptyList()); } public static BrowseAction buildForDir(JTextField tc) { - return new BrowseAction(null, tc, true, null, ""); + return new BrowseAction(null, tc, true, null, Collections.emptyList()); } public static BrowseAction buildForFile(JTextField tc) { - return new BrowseAction(null, tc, false, null, ""); + return new BrowseAction(null, tc, false, null, Collections.emptyList()); } - public static BrowseAction buildForFile(JTextField tc, JComponent focusTarget, String extension) { + public static BrowseAction buildForFile(JTextField tc, JComponent focusTarget, List extension) { return new BrowseAction(null, tc, false, focusTarget, extension); } @@ -59,7 +61,7 @@ public static BrowseAction buildForDir(JTextField tc, JComponent focusTarget) { return new BrowseAction(null, tc, true, focusTarget, null); } - private BrowseAction(JFrame frame, JTextField tc, boolean dir, JComponent focusTarget, String extension) { + private BrowseAction(JFrame frame, JTextField tc, boolean dir, JComponent focusTarget, List extension) { super(Localization.lang("Browse")); this.frame = frame; this.dir = dir; @@ -90,5 +92,4 @@ private String askUser() { JFileChooser.OPEN_DIALOG, false); } } - } diff --git a/src/main/java/net/sf/jabref/gui/auximport/FromAuxDialog.java b/src/main/java/net/sf/jabref/gui/auximport/FromAuxDialog.java index 33ebb4a8352..27f2731d5bb 100644 --- a/src/main/java/net/sf/jabref/gui/auximport/FromAuxDialog.java +++ b/src/main/java/net/sf/jabref/gui/auximport/FromAuxDialog.java @@ -39,6 +39,7 @@ import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.io.File; +import java.util.Collections; import javax.swing.AbstractAction; import javax.swing.ActionMap; @@ -264,9 +265,7 @@ public BrowseAction(JTextField tc, JabRefFrame frame) { @Override public void actionPerformed(ActionEvent e) { - String chosen = FileDialogs.getNewFile(frame, - new File(comp.getText()), - ".aux", + String chosen = FileDialogs.getNewFile(frame, new File(comp.getText()), Collections.singletonList(".aux"), JFileChooser.OPEN_DIALOG, false); if (chosen != null) { File newFile = new File(chosen); diff --git a/src/main/java/net/sf/jabref/gui/journals/ManageJournalsPanel.java b/src/main/java/net/sf/jabref/gui/journals/ManageJournalsPanel.java index aed710d96d7..cdcea17c256 100644 --- a/src/main/java/net/sf/jabref/gui/journals/ManageJournalsPanel.java +++ b/src/main/java/net/sf/jabref/gui/journals/ManageJournalsPanel.java @@ -454,9 +454,11 @@ public BrowseAction(JTextField tc, boolean dir) { public void actionPerformed(ActionEvent e) { String chosen; if (dir) { - chosen = FileDialogs.getNewDir(frame, new File(comp.getText()), "", JFileChooser.OPEN_DIALOG, false); + chosen = FileDialogs.getNewDir(frame, new File(comp.getText()), Collections.emptyList(), + JFileChooser.OPEN_DIALOG, false); } else { - chosen = FileDialogs.getNewFile(frame, new File(comp.getText()), "", JFileChooser.OPEN_DIALOG, false); + chosen = FileDialogs.getNewFile(frame, new File(comp.getText()), Collections.emptyList(), + JFileChooser.OPEN_DIALOG, false); } if (chosen != null) { comp.setText(Paths.get(chosen).toString()); diff --git a/src/main/java/net/sf/jabref/gui/openoffice/StyleSelectDialog.java b/src/main/java/net/sf/jabref/gui/openoffice/StyleSelectDialog.java index 667597e85a7..522b4baaf91 100644 --- a/src/main/java/net/sf/jabref/gui/openoffice/StyleSelectDialog.java +++ b/src/main/java/net/sf/jabref/gui/openoffice/StyleSelectDialog.java @@ -23,6 +23,7 @@ import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.IOException; +import java.util.Collections; import java.util.Objects; import java.util.Optional; @@ -497,7 +498,7 @@ public AddFileDialog() { super(diag, Localization.lang("Add style file"), true); JButton browse = new JButton(Localization.lang("Browse")); - browse.addActionListener(BrowseAction.buildForFile(newFile, null, ".jstyle")); + browse.addActionListener(BrowseAction.buildForFile(newFile, null, Collections.singletonList(".jstyle"))); // Build content panel FormBuilder builder = FormBuilder.create(); diff --git a/src/main/java/net/sf/jabref/gui/plaintextimport/TextInputDialog.java b/src/main/java/net/sf/jabref/gui/plaintextimport/TextInputDialog.java index 72bba9bd312..73eb5c7a180 100644 --- a/src/main/java/net/sf/jabref/gui/plaintextimport/TextInputDialog.java +++ b/src/main/java/net/sf/jabref/gui/plaintextimport/TextInputDialog.java @@ -120,6 +120,7 @@ import net.sf.jabref.gui.keyboard.KeyBinding; import net.sf.jabref.gui.undo.NamedCompound; import net.sf.jabref.gui.util.component.OverlayPanel; +import net.sf.jabref.importer.ParserResult; import net.sf.jabref.importer.fileformat.FreeCiteImporter; import net.sf.jabref.logic.bibtex.BibEntryWriter; import net.sf.jabref.logic.bibtex.LatexFieldFormatter; @@ -510,8 +511,12 @@ private boolean parseWithFreeCiteAndAddEntries() { text = text.replace(Globals.NEWLINE, " "); text = text.replace("##NEWLINE##", Globals.NEWLINE); - List importedEntries = fimp.importEntries(text, frame); - if (importedEntries == null) { + ParserResult importerResult = fimp.importEntries(text); + if(importerResult.hasWarnings()) { + frame.showMessage(importerResult.getErrorMessage()); + } + List importedEntries = importerResult.getDatabase().getEntries(); + if (importedEntries.isEmpty()) { return false; } else { UpdateField.setAutomaticFields(importedEntries, false, false); diff --git a/src/main/java/net/sf/jabref/gui/preftabs/PreferencesDialog.java b/src/main/java/net/sf/jabref/gui/preftabs/PreferencesDialog.java index be238861aec..84d9f5bd31f 100644 --- a/src/main/java/net/sf/jabref/gui/preftabs/PreferencesDialog.java +++ b/src/main/java/net/sf/jabref/gui/preftabs/PreferencesDialog.java @@ -22,6 +22,7 @@ import java.awt.event.ActionEvent; import java.io.File; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.prefs.BackingStoreException; @@ -166,8 +167,8 @@ public PreferencesDialog(JabRefFrame parent) { // Import and export actions: exportPreferences.setToolTipText(Localization.lang("Export preferences to file")); exportPreferences.addActionListener(e -> { - String filename = FileDialogs.getNewFile(frame, new File(System.getProperty("user.home")), ".xml", - JFileChooser.SAVE_DIALOG, false); + String filename = FileDialogs.getNewFile(frame, new File(System.getProperty("user.home")), + Collections.singletonList(".xml"), JFileChooser.SAVE_DIALOG, false); if (filename == null) { return; } @@ -188,8 +189,8 @@ public PreferencesDialog(JabRefFrame parent) { importPreferences.setToolTipText(Localization.lang("Import preferences from file")); importPreferences.addActionListener(e -> { - String filename = FileDialogs.getNewFile(frame, new File(System.getProperty("user.home")), ".xml", - JFileChooser.OPEN_DIALOG, false); + String filename = FileDialogs.getNewFile(frame, new File(System.getProperty("user.home")), + Collections.singletonList(".xml"), JFileChooser.OPEN_DIALOG, false); if (filename != null) { try { prefs.importPreferences(filename); diff --git a/src/main/java/net/sf/jabref/importer/CustomImporter.java b/src/main/java/net/sf/jabref/importer/CustomImporter.java index 44394dbb91d..e4668770216 100644 --- a/src/main/java/net/sf/jabref/importer/CustomImporter.java +++ b/src/main/java/net/sf/jabref/importer/CustomImporter.java @@ -136,7 +136,6 @@ public ImportFormat getInstance() throws IOException, ClassNotFoundException, try (URLClassLoader cl = new URLClassLoader(new URL[] {getBasePathUrl()})) { Class clazz = Class.forName(className, true, cl); ImportFormat importFormat = (ImportFormat) clazz.newInstance(); - importFormat.setIsCustomImporter(true); return importFormat; } } diff --git a/src/main/java/net/sf/jabref/importer/ImportCustomizationDialog.java b/src/main/java/net/sf/jabref/importer/ImportCustomizationDialog.java index aa0fe301dd8..579c7e662ff 100644 --- a/src/main/java/net/sf/jabref/importer/ImportCustomizationDialog.java +++ b/src/main/java/net/sf/jabref/importer/ImportCustomizationDialog.java @@ -22,6 +22,8 @@ import java.awt.event.ActionEvent; import java.io.File; import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; import java.util.zip.ZipFile; import javax.swing.AbstractAction; @@ -95,20 +97,22 @@ public ImportCustomizationDialog(final JabRefFrame frame) { JButton addFromFolderButton = new JButton(Localization.lang("Add from folder")); addFromFolderButton.addActionListener(e -> { - String chosenFileStr = null; CustomImporter importer = new CustomImporter(); - importer.setBasePath( - FileDialogs.getNewDir(frame, new File(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)), - "", Localization.lang("Select Classpath of New Importer"), JFileChooser.CUSTOM_DIALOG, false)); + importer.setBasePath(FileDialogs + .getNewDir(frame, new File(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)), + Collections.emptyList(), Localization.lang("Select Classpath of New Importer"), + JFileChooser.CUSTOM_DIALOG, false)); + String chosenFileStr = null; if (importer.getBasePath() != null) { - chosenFileStr = FileDialogs.getNewFile(frame, importer.getFileFromBasePath(), ".class", - Localization.lang("Select new ImportFormat Subclass"), JFileChooser.CUSTOM_DIALOG, false); + chosenFileStr = FileDialogs.getNewFile(frame, importer.getFileFromBasePath(), + Collections.singletonList(".class"), Localization.lang("Select new ImportFormat Subclass"), + JFileChooser.CUSTOM_DIALOG, false); } if (chosenFileStr != null) { try { importer.setClassName(pathToClass(importer.getFileFromBasePath(), new File(chosenFileStr))); importer.setName(importer.getInstance().getFormatName()); - importer.setCliId(importer.getInstance().getCLIId()); + importer.setCliId(importer.getInstance().getId()); addOrReplaceImporter(importer); customImporterTable.revalidate(); customImporterTable.repaint(); @@ -126,8 +130,8 @@ public ImportCustomizationDialog(final JabRefFrame frame) { JButton addFromJarButton = new JButton(Localization.lang("Add from jar")); addFromJarButton.addActionListener(e -> { String basePath = FileDialogs.getNewFile(frame, - new File(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)), - ".zip,.jar", Localization.lang("Select a Zip-archive"), JFileChooser.CUSTOM_DIALOG, false); + new File(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)), Arrays.asList(".zip", ".jar"), + Localization.lang("Select a Zip-archive"), JFileChooser.CUSTOM_DIALOG, false); if (basePath != null) { try (ZipFile zipFile = new ZipFile(new File(basePath), ZipFile.OPEN_READ)) { diff --git a/src/main/java/net/sf/jabref/importer/ImportFormatReader.java b/src/main/java/net/sf/jabref/importer/ImportFormatReader.java index 8b9ec7aea92..a5d099234b4 100644 --- a/src/main/java/net/sf/jabref/importer/ImportFormatReader.java +++ b/src/main/java/net/sf/jabref/importer/ImportFormatReader.java @@ -15,15 +15,9 @@ */ package net.sf.jabref.importer; -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.List; import java.util.Objects; import java.util.Optional; @@ -61,7 +55,8 @@ public class ImportFormatReader { public static final String BIBTEX_FORMAT = "BibTeX"; /** - * all import formats, in the default order of import formats + * All import formats. + * Sorted accordingly to {@link ImportFormat#compareTo}, which defaults to alphabetically by the name */ private final SortedSet formats = new TreeSet<>(); @@ -114,14 +109,14 @@ public void resetImportFormats() { */ private Optional getByCliId(String cliId) { for (ImportFormat format : formats) { - if (format.getCLIId().equals(cliId)) { + if (format.getId().equals(cliId)) { return Optional.of(format); } } return Optional.empty(); } - public List importFromFile(String format, String filename, OutputPrinter status) + public ParserResult importFromFile(String format, Path file) throws IOException { Optional importer = getByCliId(format); @@ -129,28 +124,7 @@ public List importFromFile(String format, String filename, OutputPrint throw new IllegalArgumentException("Unknown import format: " + format); } - return importFromFile(importer.get(), filename, status); - } - - public List importFromFile(ImportFormat importer, String filename, OutputPrinter status) throws IOException { - Objects.requireNonNull(importer); - Objects.requireNonNull(filename); - File file = new File(filename); - - try (InputStream stream = new FileInputStream(file); - BufferedInputStream bis = new BufferedInputStream(stream)) { - - bis.mark(Integer.MAX_VALUE); - - if (!importer.isRecognizedFormat(bis)) { - throw new IOException("Wrong file format"); - } - } - - try (InputStream stream = new FileInputStream(file); - BufferedInputStream bis = new BufferedInputStream(stream)) { - return importer.importEntries(bis, status); - } + return importer.get().importDatabase(file, Globals.prefs.getDefaultEncoding()); } /** @@ -184,35 +158,13 @@ public String getImportFormatList() { sb.append(StringUtil.repeatSpaces(pad)); sb.append(" : "); - sb.append(imFo.getCLIId()); + sb.append(imFo.getId()); sb.append('\n'); } return sb.toString(); } - - - public static InputStreamReader getUTF8Reader(File f) throws IOException { - return getReader(f, StandardCharsets.UTF_8); - } - - public static InputStreamReader getUTF16Reader(File f) throws IOException { - return getReader(f, StandardCharsets.UTF_16); - } - - public static InputStreamReader getReader(File f, Charset charset) - throws IOException { - return new InputStreamReader(new FileInputStream(f), charset); - } - - public static Reader getReaderDefaultEncoding(InputStream in) { - InputStreamReader reader; - reader = new InputStreamReader(in, Globals.prefs.getDefaultEncoding()); - - return reader; - } - public static class UnknownFormatImport { public final String format; @@ -225,6 +177,9 @@ public UnknownFormatImport(String format, ParserResult parserResult) { } } + public UnknownFormatImport importUnknownFormat(String filename) { + return importUnknownFormat(Paths.get(filename)); + } /** * Tries to import a file by iterating through the available import filters, @@ -234,26 +189,21 @@ public UnknownFormatImport(String format, ParserResult parserResult) { * * @throws IOException */ - public UnknownFormatImport importUnknownFormat(String filename) { - Objects.requireNonNull(filename); + public UnknownFormatImport importUnknownFormat(Path file) { + Objects.requireNonNull(file); // First, see if it is a BibTeX file: try { - ParserResult pr = OpenDatabaseAction.loadDatabase(new File(filename), + ParserResult pr = OpenDatabaseAction.loadDatabase(file.toFile(), Globals.prefs.getDefaultEncoding()); if (pr.getDatabase().hasEntries() || !pr.getDatabase().hasNoStrings()) { - pr.setFile(new File(filename)); + pr.setFile(file.toFile()); return new UnknownFormatImport(ImportFormatReader.BIBTEX_FORMAT, pr); } } catch (IOException ignore) { // Ignored } - // we don't use a provided OutputPrinter (such as the JabRef frame), - // as we don't want to see any outputs from failed importers: - // we expect failures and do not want to report them to the user - OutputPrinterToNull nullOutput = new OutputPrinterToNull(); - // stores ref to best result, gets updated at the next loop List bestResult = null; int bestResultCount = 0; @@ -261,14 +211,16 @@ public UnknownFormatImport importUnknownFormat(String filename) { // Cycle through all importers: for (ImportFormat imFo : getImportFormats()) { - try { + if(!imFo.isRecognizedFormat(file, Globals.prefs.getDefaultEncoding())) { + continue; + } - List entries = importFromFile(imFo, filename, nullOutput); + ParserResult parserResult = imFo.importDatabase(file, Globals.prefs.getDefaultEncoding()); + List entries = parserResult.getDatabase().getEntries(); - int entryCount; BibDatabases.purgeEmptyEntries(entries); - entryCount = entries.size(); + int entryCount = entries.size(); if (entryCount > bestResultCount) { bestResult = entries; diff --git a/src/main/java/net/sf/jabref/importer/ImportMenuItem.java b/src/main/java/net/sf/jabref/importer/ImportMenuItem.java index 0b6e036f79f..3956f8d67b9 100644 --- a/src/main/java/net/sf/jabref/importer/ImportMenuItem.java +++ b/src/main/java/net/sf/jabref/importer/ImportMenuItem.java @@ -19,8 +19,11 @@ import java.awt.event.ActionListener; import java.io.File; import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -105,7 +108,7 @@ public void init() { importError = null; filenames = FileDialogs.getMultipleFiles(frame, new File(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)), - importer == null ? null : importer.getExtensions(), true); + importer == null ? Collections.emptyList() : importer.getExtensions(), true); if (!filenames.isEmpty()) { frame.block(); @@ -125,6 +128,7 @@ public void run() { // We import all files and collect their results: List imports = new ArrayList<>(); for (String filename : filenames) { + Path file = Paths.get(filename); try { if (importer == null) { // Unknown format: @@ -134,9 +138,10 @@ public void run() { } else { frame.output(Localization.lang("Importing in %0 format", importer.getFormatName()) + "..."); // Specific importer: - ParserResult pr = new ParserResult( - Globals.IMPORT_FORMAT_READER.importFromFile(importer, - filename, frame)); + ParserResult pr = importer.importDatabase(file, Globals.prefs.getDefaultEncoding()); + if (pr.hasWarnings()) { + frame.showMessage(pr.getErrorMessage()); + } imports.add(new ImportFormatReader.UnknownFormatImport(importer .getFormatName(), pr)); diff --git a/src/main/java/net/sf/jabref/importer/OpenDatabaseAction.java b/src/main/java/net/sf/jabref/importer/OpenDatabaseAction.java index ab92590d65b..b485c9832c7 100644 --- a/src/main/java/net/sf/jabref/importer/OpenDatabaseAction.java +++ b/src/main/java/net/sf/jabref/importer/OpenDatabaseAction.java @@ -16,17 +16,13 @@ package net.sf.jabref.importer; import java.awt.event.ActionEvent; -import java.io.BufferedReader; import java.io.File; import java.io.IOException; -import java.io.InputStreamReader; -import java.io.Reader; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; -import java.util.Optional; import javax.swing.Action; import javax.swing.JOptionPane; @@ -48,7 +44,7 @@ import net.sf.jabref.gui.actions.MnemonicAwareAction; import net.sf.jabref.gui.keyboard.KeyBinding; import net.sf.jabref.gui.undo.NamedCompound; -import net.sf.jabref.importer.fileformat.BibtexParser; +import net.sf.jabref.importer.fileformat.BibtexImporter; import net.sf.jabref.logic.l10n.Localization; import net.sf.jabref.logic.util.io.FileBasedLock; import net.sf.jabref.logic.util.strings.StringUtil; @@ -100,7 +96,8 @@ public void actionPerformed(ActionEvent e) { if (showDialog) { List chosenStrings = FileDialogs.getMultipleFiles(frame, - new File(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)), ".bib", true); + new File(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY)), Collections.singletonList(".bib"), + true); for (String chosen : chosenStrings) { if (chosen != null) { filesToOpen.add(new File(chosen)); @@ -114,23 +111,6 @@ public void actionPerformed(ActionEvent e) { openFiles(filesToOpen, true); } - class OpenItSwingHelper implements Runnable { - - private final BasePanel basePanel; - private final boolean raisePanel; - - OpenItSwingHelper(BasePanel basePanel, boolean raisePanel) { - this.basePanel = basePanel; - this.raisePanel = raisePanel; - } - - @Override - public void run() { - frame.addTab(basePanel, raisePanel); - - } - } - /** * Opens the given file. If null or 404, nothing happens * @@ -305,7 +285,8 @@ private void openTheFile(File file, boolean raisePanel) { // if the database contents should be modified due to new features // in this version of JabRef: final ParserResult finalReferenceToResult = result; - SwingUtilities.invokeLater(() -> OpenDatabaseAction.performPostOpenActions(panel, finalReferenceToResult, true)); + SwingUtilities.invokeLater( + () -> OpenDatabaseAction.performPostOpenActions(panel, finalReferenceToResult, true)); } } @@ -342,7 +323,7 @@ public BasePanel addNewDatabase(ParserResult result, final File file, boolean ra BasePanel basePanel = new BasePanel(frame, new BibDatabaseContext(database, meta, file, defaults), result.getEncoding()); // file is set to null inside the EventDispatcherThread - SwingUtilities.invokeLater(new OpenItSwingHelper(basePanel, raisePanel)); + SwingUtilities.invokeLater(() -> frame.addTab(basePanel, raisePanel)); frame.output(Localization.lang("Opened database") + " '" + fileName + "' " + Localization.lang("with") + " " + database.getEntryCount() + " " + Localization.lang("entries") + "."); @@ -354,30 +335,8 @@ public BasePanel addNewDatabase(ParserResult result, final File file, boolean ra * Opens a new database. */ public static ParserResult loadDatabase(File fileToOpen, Charset defaultEncoding) throws IOException { - - // We want to check if there is a JabRef signature in the file, because that would tell us - // which character encoding is used. However, to read the signature we must be using a compatible - // encoding in the first place. Since the signature doesn't contain any fancy characters, we can - // read it regardless of encoding, with either UTF-8 or UTF-16. That's the hypothesis, at any rate. - // 8 bit is most likely, so we try that first: - Optional suppliedEncoding = Optional.empty(); - try (Reader utf8Reader = ImportFormatReader.getUTF8Reader(fileToOpen)) { - suppliedEncoding = OpenDatabaseAction.getSuppliedEncoding(utf8Reader); - } - // Now if that didn't get us anywhere, we check with the 16 bit encoding: - if (!suppliedEncoding.isPresent()) { - try (Reader utf16Reader = ImportFormatReader.getUTF16Reader(fileToOpen)) { - suppliedEncoding = OpenDatabaseAction.getSuppliedEncoding(utf16Reader); - } - } - // Open and parse file - try (InputStreamReader reader = openFile(fileToOpen, suppliedEncoding, defaultEncoding)) { - BibtexParser parser = new BibtexParser(reader); - - ParserResult result = parser.parse(); - result.setEncoding(Charset.forName(reader.getEncoding())); - result.setFile(fileToOpen); + ParserResult result = new BibtexImporter().importDatabase(fileToOpen.toPath(), defaultEncoding); if (SpecialFieldsUtils.keywordSyncEnabled()) { NamedCompound compound = new NamedCompound("SpecialFieldSync"); @@ -387,81 +346,16 @@ public static ParserResult loadDatabase(File fileToOpen, Charset defaultEncoding LOGGER.debug("Synchronized special fields based on keywords"); } - return result; - } - } - - /** - * Opens the file with the provided encoding. If this fails (or no encoding is provided), then the fallback encoding - * will be used. - */ - private static InputStreamReader openFile(File fileToOpen, Optional encoding, Charset defaultEncoding) - throws IOException { - if (encoding.isPresent()) { - try { - return ImportFormatReader.getReader(fileToOpen, encoding.get()); - } catch (IOException ex) { - LOGGER.warn("Problem getting reader", ex); - // The supplied encoding didn't work out, so we use the fallback. - return ImportFormatReader.getReader(fileToOpen, defaultEncoding); - } - } else { - // We couldn't find a header with info about encoding. Use fallback: - return ImportFormatReader.getReader(fileToOpen, defaultEncoding); - - } - } - - /** - * Searches the file for "Encoding: myEncoding" and returns the found supplied encoding. - */ - private static Optional getSuppliedEncoding(Reader reader) { - try { - BufferedReader bufferedReader = new BufferedReader(reader); - String line; - while ((line = bufferedReader.readLine()) != null) { - line = line.trim(); - - // Line does not start with %, so there are no comment lines for us and we can stop parsing - if (!line.startsWith("%")) { - return Optional.empty(); - } - - // Only keep the part after % - line = line.substring(1).trim(); - - if (line.startsWith(Globals.SIGNATURE)) { - // Signature line, so keep reading and skip to next line - } else if (line.startsWith(Globals.ENCODING_PREFIX)) { - // Line starts with "Encoding: ", so the rest of the line should contain the name of the encoding - // Except if there is already a @ symbol signaling the starting of a BibEntry - Integer atSymbolIndex = line.indexOf('@'); - String encoding; - if (atSymbolIndex > 0) { - encoding = line.substring(Globals.ENCODING_PREFIX.length(), atSymbolIndex); - } else { - encoding = line.substring(Globals.ENCODING_PREFIX.length()); - } - - return Optional.of(Charset.forName(encoding)); - } else { - // Line not recognized so stop parsing - return Optional.empty(); - } - } - } catch (IOException ignored) { - // Ignored - } - return Optional.empty(); + return result; } /** * Load database (bib-file) or, if there exists, a newer autosave version, unless the flag is set to ignore the autosave - * - * @param name Name of the bib-file to open - * @param ignoreAutosave true if autosave version of the file should be ignored - * @return ParserResult which never is null - */ + * + * @param name Name of the bib-file to open + * @param ignoreAutosave true if autosave version of the file should be ignored + * @return ParserResult which never is null + */ public static ParserResult loadDatabaseOrAutoSave(String name, boolean ignoreAutosave) { // String in OpenDatabaseAction.java @@ -514,5 +408,4 @@ public static ParserResult loadDatabaseOrAutoSave(String name, boolean ignoreAut } } - } diff --git a/src/main/java/net/sf/jabref/importer/OutputPrinterToNull.java b/src/main/java/net/sf/jabref/importer/OutputPrinterToNull.java deleted file mode 100644 index f9b2ab40b14..00000000000 --- a/src/main/java/net/sf/jabref/importer/OutputPrinterToNull.java +++ /dev/null @@ -1,41 +0,0 @@ -/* Copyright (C) 2011, 2015 JabRef contributors. - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along - with this program; if not, write to the Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -*/ -package net.sf.jabref.importer; - -/** - * Outputs nothing - * - * Used where really no output is desired - * - */ -public class OutputPrinterToNull implements OutputPrinter { - - @Override - public void setStatus(String s) { - // Do nothing - } - - @Override - public void showMessage(Object message, String title, int msgType) { - // Do nothing - } - - @Override - public void showMessage(String string) { - // Do nothing - } - -} diff --git a/src/main/java/net/sf/jabref/importer/ParserResult.java b/src/main/java/net/sf/jabref/importer/ParserResult.java index bd93ab57655..f0b165a0d6d 100644 --- a/src/main/java/net/sf/jabref/importer/ParserResult.java +++ b/src/main/java/net/sf/jabref/importer/ParserResult.java @@ -19,6 +19,7 @@ import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -49,6 +50,9 @@ public class ParserResult { private boolean invalid; private boolean toOpenTab; + public ParserResult() { + this(Collections.emptyList()); + } public ParserResult(Collection entries) { this(BibDatabases.createDatabase(BibDatabases.purgeEmptyEntries(entries)), new MetaData(), new HashMap<>()); @@ -60,6 +64,12 @@ public ParserResult(BibDatabase base, MetaData metaData, Map this.entryTypes = entryTypes; } + public static ParserResult fromErrorMessage(String message) { + ParserResult parserResult = new ParserResult(); + parserResult.addWarning(message); + return parserResult; + } + /** * Check if this base is marked to be added to the currently open tab. Default is false. * diff --git a/src/main/java/net/sf/jabref/importer/ZipFileChooser.java b/src/main/java/net/sf/jabref/importer/ZipFileChooser.java index 3f81a97a38d..6266291f11d 100644 --- a/src/main/java/net/sf/jabref/importer/ZipFileChooser.java +++ b/src/main/java/net/sf/jabref/importer/ZipFileChooser.java @@ -110,7 +110,7 @@ public ZipFileChooser(ImportCustomizationDialog importCustomizationDialog, ZipFi try { ImportFormat importFormat = importer.getInstance(); importer.setName(importFormat.getFormatName()); - importer.setCliId(importFormat.getCLIId()); + importer.setCliId(importFormat.getId()); importCustomizationDialog.addOrReplaceImporter(importer); dispose(); } catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException exc) { diff --git a/src/main/java/net/sf/jabref/importer/fetcher/DOItoBibTeXFetcher.java b/src/main/java/net/sf/jabref/importer/fetcher/DOItoBibTeXFetcher.java index 24599577497..d83c1c77194 100644 --- a/src/main/java/net/sf/jabref/importer/fetcher/DOItoBibTeXFetcher.java +++ b/src/main/java/net/sf/jabref/importer/fetcher/DOItoBibTeXFetcher.java @@ -22,9 +22,9 @@ import java.net.URI; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.util.Objects; import java.util.Optional; -import javax.swing.JOptionPane; import javax.swing.JPanel; import net.sf.jabref.Globals; @@ -32,6 +32,7 @@ import net.sf.jabref.gui.help.HelpFiles; import net.sf.jabref.importer.ImportInspector; import net.sf.jabref.importer.OutputPrinter; +import net.sf.jabref.importer.ParserResult; import net.sf.jabref.importer.fileformat.BibtexParser; import net.sf.jabref.logic.formatter.bibtexfields.UnitsToLatexFormatter; import net.sf.jabref.logic.formatter.casechanger.ProtectTermsFormatter; @@ -57,7 +58,11 @@ public void stopFetching() { @Override public boolean processQuery(String query, ImportInspector inspector, OutputPrinter status) { - BibEntry entry = getEntryFromDOI(query, status); + ParserResult parserResult = new ParserResult(); + BibEntry entry = getEntryFromDOI(query, parserResult); + if(parserResult.hasWarnings()) { + status.showMessage(parserResult.getErrorMessage()); + } if (entry != null) { inspector.addEntry(entry); return true; @@ -81,15 +86,14 @@ public JPanel getOptionsPanel() { return null; } - public BibEntry getEntryFromDOI(String doiStr, OutputPrinter status) { + public BibEntry getEntryFromDOI(String doiStr, ParserResult parserResult) { + Objects.requireNonNull(parserResult); + DOI doi; try { doi = new DOI(doiStr); } catch (IllegalArgumentException e) { - status.showMessage(Localization.lang("Invalid DOI: '%0'.", doiStr), - Localization.lang("Get BibTeX entry from DOI"), - JOptionPane.INFORMATION_MESSAGE); - LOGGER.warn("Invalid DOI", e); + parserResult.addWarning(Localization.lang("Invalid DOI: '%0'.", doiStr)); return null; } @@ -115,11 +119,7 @@ public BibEntry getEntryFromDOI(String doiStr, OutputPrinter status) { dl.addParameters("Accept", "application/x-bibtex"); bibtexString = dl.downloadToString(StandardCharsets.UTF_8); } catch (FileNotFoundException e) { - if (status != null) { - status.showMessage(Localization.lang("Unknown DOI: '%0'.", doi.getDOI()), - Localization.lang("Get BibTeX entry from DOI"), - JOptionPane.INFORMATION_MESSAGE); - } + parserResult.addWarning(Localization.lang("Unknown DOI: '%0'.", doi.getDOI())); LOGGER.debug("Unknown DOI", e); return null; } catch (IOException e) { diff --git a/src/main/java/net/sf/jabref/importer/fetcher/MedlineFetcher.java b/src/main/java/net/sf/jabref/importer/fetcher/MedlineFetcher.java index 74d1436f8a9..586870b691a 100644 --- a/src/main/java/net/sf/jabref/importer/fetcher/MedlineFetcher.java +++ b/src/main/java/net/sf/jabref/importer/fetcher/MedlineFetcher.java @@ -20,6 +20,8 @@ import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -30,6 +32,7 @@ import net.sf.jabref.gui.help.HelpFiles; import net.sf.jabref.importer.ImportInspector; import net.sf.jabref.importer.OutputPrinter; +import net.sf.jabref.importer.ParserResult; import net.sf.jabref.importer.fileformat.MedlineImporter; import net.sf.jabref.logic.l10n.Localization; import net.sf.jabref.model.entry.BibEntry; @@ -150,7 +153,7 @@ public boolean processQuery(String query, ImportInspector iIDialog, OutputPrinte if (cleanQuery.matches("\\d+[,\\d+]*")) { frameOP.setStatus(Localization.lang("Fetching Medline by id...")); - List bibs = MedlineImporter.fetchMedline(cleanQuery, frameOP); + List bibs = fetchMedline(cleanQuery, frameOP); if (bibs.isEmpty()) { frameOP.showMessage(Localization.lang("No references found")); @@ -208,7 +211,7 @@ public boolean processQuery(String query, ImportInspector iIDialog, OutputPrinte // get the ids from entrez result = getIds(searchTerm, i, noToFetch); - List bibs = MedlineImporter.fetchMedline(result.ids, frameOP); + List bibs = fetchMedline(result.ids, frameOP); for (BibEntry entry : bibs) { iIDialog.addEntry(entry); } @@ -222,6 +225,29 @@ public boolean processQuery(String query, ImportInspector iIDialog, OutputPrinte return false; } + /** + * Fetch and parse an medline item from eutils.ncbi.nlm.nih.gov. + * + * @param id One or several ids, separated by "," + * + * @return Will return an empty list on error. + */ + private static List fetchMedline(String id, OutputPrinter status) { + String baseUrl = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&retmode=xml&rettype=citation&id=" + + id; + try { + URL url = new URL(baseUrl); + URLConnection data = url.openConnection(); + ParserResult result = new MedlineImporter().importDatabase( + new BufferedReader(new InputStreamReader(data.getInputStream()))); + if (result.hasWarnings()) { + status.showMessage(result.getErrorMessage()); + } + return result.getDatabase().getEntries(); + } catch (IOException e) { + return new ArrayList<>(); + } + } static class SearchResult { diff --git a/src/main/java/net/sf/jabref/importer/fileformat/BibTeXMLImporter.java b/src/main/java/net/sf/jabref/importer/fileformat/BibTeXMLImporter.java index 36c8ae5ca07..725b72274a8 100644 --- a/src/main/java/net/sf/jabref/importer/fileformat/BibTeXMLImporter.java +++ b/src/main/java/net/sf/jabref/importer/fileformat/BibTeXMLImporter.java @@ -17,20 +17,20 @@ import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.regex.Pattern; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; -import net.sf.jabref.importer.ImportFormatReader; -import net.sf.jabref.importer.OutputPrinter; +import net.sf.jabref.importer.ParserResult; import net.sf.jabref.model.entry.BibEntry; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.xml.sax.InputSource; /** * Importer for the Refer/Endnote format. @@ -44,49 +44,36 @@ public class BibTeXMLImporter extends ImportFormat { private static final Pattern START_PATTERN = Pattern.compile("<(bibtex:)?file .*"); - - /** - * Return the name of this import format. - */ - @Override public String getFormatName() { return "BibTeXML"; } - /* - * (non-Javadoc) - * @see net.sf.jabref.imports.ImportFormat#getCLIId() - */ @Override - public String getCLIId() { - return "bibtexml"; + public List getExtensions() { + return null; } - /** - * Check whether the source is in the correct format for this importer. - */ @Override - public boolean isRecognizedFormat(InputStream stream) throws IOException { + public String getDescription() { + return null; + } + @Override + public boolean isRecognizedFormat(BufferedReader reader) throws IOException { // Our strategy is to look for the " importEntries(InputStream stream, OutputPrinter status) throws IOException { + public ParserResult importDatabase(BufferedReader reader) throws IOException { + Objects.requireNonNull(reader); List bibItems = new ArrayList<>(); @@ -102,22 +89,21 @@ public List importEntries(InputStream stream, OutputPrinter status) th SAXParser parser = parserFactory.newSAXParser(); //May throw exceptions BibTeXMLHandler handler = new BibTeXMLHandler(); // Start the parser. It reads the file and calls methods of the handler. - parser.parse(stream, handler); + parser.parse(new InputSource(reader), handler); // When you're done, report the results stored by your handler object bibItems.addAll(handler.getItems()); } catch (javax.xml.parsers.ParserConfigurationException e) { LOGGER.error("Error with XML parser configuration", e); - status.showMessage(e.getLocalizedMessage()); + return ParserResult.fromErrorMessage(e.getLocalizedMessage()); } catch (org.xml.sax.SAXException e) { LOGGER.error("Error during XML parsing", e); - status.showMessage(e.getLocalizedMessage()); + return ParserResult.fromErrorMessage(e.getLocalizedMessage()); } catch (IOException e) { LOGGER.error("Error during file import", e); - status.showMessage(e.getLocalizedMessage()); + return ParserResult.fromErrorMessage(e.getLocalizedMessage()); } - return bibItems; - + return new ParserResult(bibItems); } } diff --git a/src/main/java/net/sf/jabref/importer/fileformat/BiblioscapeImporter.java b/src/main/java/net/sf/jabref/importer/fileformat/BiblioscapeImporter.java index 08c54ab3619..29b8d4290c6 100644 --- a/src/main/java/net/sf/jabref/importer/fileformat/BiblioscapeImporter.java +++ b/src/main/java/net/sf/jabref/importer/fileformat/BiblioscapeImporter.java @@ -17,15 +17,13 @@ import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; -import net.sf.jabref.importer.ImportFormatReader; -import net.sf.jabref.importer.OutputPrinter; +import net.sf.jabref.importer.ParserResult; import net.sf.jabref.model.entry.BibEntry; /** @@ -36,45 +34,36 @@ */ public class BiblioscapeImporter extends ImportFormat { - /** - * Return the name of this import format. - */ @Override public String getFormatName() { return "Biblioscape"; } - /* - * (non-Javadoc) - * @see net.sf.jabref.imports.ImportFormat#getCLIId() - */ @Override - public String getCLIId() { - return "biblioscape"; + public List getExtensions() { + return null; } - /** - * Check whether the source is in the correct format for this importer. - */ @Override - public boolean isRecognizedFormat(InputStream in) throws IOException { + public String getDescription() { + return null; + } + + @Override + public boolean isRecognizedFormat(BufferedReader reader) { + Objects.requireNonNull(reader); return true; } - /** - * Parse the entries in the source, and return a List of BibEntry - * objects. - */ @Override - public List importEntries(InputStream stream, OutputPrinter status) throws IOException { + public ParserResult importDatabase(BufferedReader reader) throws IOException { List bibItems = new ArrayList<>(); - BufferedReader in = new BufferedReader(ImportFormatReader.getReaderDefaultEncoding(stream)); String line; Map hm = new HashMap<>(); Map lines = new HashMap<>(); StringBuilder previousLine = null; - while ((line = in.readLine()) != null) { + while ((line = reader.readLine()) != null) { if (line.isEmpty()) { continue; // ignore empty lines, e.g. at file } @@ -295,12 +284,12 @@ public List importEntries(InputStream stream, OutputPrinter status) th } // continuation (folding) of previous line if (previousLine == null) { - return Collections.emptyList(); + return new ParserResult(); } previousLine.append(line.trim()); } - return bibItems; + return new ParserResult(bibItems); } } diff --git a/src/main/java/net/sf/jabref/importer/fileformat/BibtexImporter.java b/src/main/java/net/sf/jabref/importer/fileformat/BibtexImporter.java index 5eaf75eee5e..f84f9e8df25 100644 --- a/src/main/java/net/sf/jabref/importer/fileformat/BibtexImporter.java +++ b/src/main/java/net/sf/jabref/importer/fileformat/BibtexImporter.java @@ -15,15 +15,17 @@ */ package net.sf.jabref.importer.fileformat; +import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; +import java.nio.charset.Charset; +import java.nio.file.Path; +import java.util.Collections; import java.util.List; +import java.util.Objects; +import java.util.Optional; -import net.sf.jabref.importer.ImportFormatReader; -import net.sf.jabref.importer.OutputPrinter; +import net.sf.jabref.Globals; import net.sf.jabref.importer.ParserResult; -import net.sf.jabref.model.entry.BibEntry; /** * This importer exists only to enable `--importToOpen someEntry.bib` @@ -38,24 +40,39 @@ public class BibtexImporter extends ImportFormat { * https://github.com/JabRef/jabref/pull/379#issuecomment-158685726 for more details. */ @Override - public boolean isRecognizedFormat(InputStream in) throws IOException { + public boolean isRecognizedFormat(BufferedReader reader) { + Objects.requireNonNull(reader); return true; } - /** - * Parses the given input stream. - * Only plain bibtex entries are returned. - * That especially means that metadata is ignored. - * - * @param in the inputStream to read from - * @param status the OutputPrinter to put status to - * @return a list of BibTeX entries contained in the given inputStream - */ @Override - public List importEntries(InputStream in, OutputPrinter status) - throws IOException { - ParserResult pr = BibtexParser.parse(ImportFormatReader.getReaderDefaultEncoding(in)); - return new ArrayList<>(pr.getDatabase().getEntries()); + public ParserResult importDatabase(Path filePath, Charset defaultEncoding) throws IOException { + // We want to check if there is a JabRef signature in the file, because that would tell us + // which character encoding is used. However, to read the signature we must be using a compatible + // encoding in the first place. Since the signature doesn't contain any fancy characters, we can + // read it regardless of encoding, with either UTF-8 or UTF-16. That's the hypothesis, at any rate. + // 8 bit is most likely, so we try that first: + Optional suppliedEncoding; + try (BufferedReader utf8Reader = getUTF8Reader(filePath)) { + suppliedEncoding = getSuppliedEncoding(utf8Reader); + } + // Now if that didn't get us anywhere, we check with the 16 bit encoding: + if (!suppliedEncoding.isPresent()) { + try (BufferedReader utf16Reader = getUTF16Reader(filePath)) { + suppliedEncoding = getSuppliedEncoding(utf16Reader); + } + } + + if(suppliedEncoding.isPresent()) { + return super.importDatabase(filePath, suppliedEncoding.get()); + } else { + return super.importDatabase(filePath, defaultEncoding); + } + } + + @Override + public ParserResult importDatabase(BufferedReader reader) throws IOException { + return BibtexParser.parse(reader); } @Override @@ -64,8 +81,54 @@ public String getFormatName() { } @Override - public String getExtensions() { - return "bib"; + public List getExtensions() { + return Collections.singletonList("bib"); + } + + @Override + public String getDescription() { + return null; } + /** + * Searches the file for "Encoding: myEncoding" and returns the found supplied encoding. + */ + private static Optional getSuppliedEncoding(BufferedReader reader) { + try { + String line; + while ((line = reader.readLine()) != null) { + line = line.trim(); + + // Line does not start with %, so there are no comment lines for us and we can stop parsing + if (!line.startsWith("%")) { + return Optional.empty(); + } + + // Only keep the part after % + line = line.substring(1).trim(); + + if (line.startsWith(Globals.SIGNATURE)) { + // Signature line, so keep reading and skip to next line + } else if (line.startsWith(Globals.ENCODING_PREFIX)) { + // Line starts with "Encoding: ", so the rest of the line should contain the name of the encoding + // Except if there is already a @ symbol signaling the starting of a BibEntry + Integer atSymbolIndex = line.indexOf('@'); + String encoding; + if (atSymbolIndex > 0) { + encoding = line.substring(Globals.ENCODING_PREFIX.length(), atSymbolIndex); + } else { + encoding = line.substring(Globals.ENCODING_PREFIX.length()); + } + + return Optional.of(Charset.forName(encoding)); + } else { + // Line not recognized so stop parsing + return Optional.empty(); + } + } + } catch (IOException ignored) { + // Ignored + } + return Optional.empty(); + } } diff --git a/src/main/java/net/sf/jabref/importer/fileformat/CopacImporter.java b/src/main/java/net/sf/jabref/importer/fileformat/CopacImporter.java index ab43d01fdc0..6a058417fc8 100644 --- a/src/main/java/net/sf/jabref/importer/fileformat/CopacImporter.java +++ b/src/main/java/net/sf/jabref/importer/fileformat/CopacImporter.java @@ -17,13 +17,12 @@ import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; import java.util.LinkedList; import java.util.List; +import java.util.Objects; import java.util.regex.Pattern; -import net.sf.jabref.importer.ImportFormatReader; -import net.sf.jabref.importer.OutputPrinter; +import net.sf.jabref.importer.ParserResult; import net.sf.jabref.model.entry.BibEntry; /** @@ -37,84 +36,67 @@ public class CopacImporter extends ImportFormat { private static final Pattern COPAC_PATTERN = Pattern.compile("^\\s*TI- "); - - /** - * Return the name of this import format. - */ @Override public String getFormatName() { return "Copac"; } - /* - * (non-Javadoc) - * - * @see net.sf.jabref.imports.ImportFormat#getCLIId() - */ @Override - public String getCLIId() { - return "cpc"; + public List getExtensions() { + return null; } - - - /** - * Check whether the source is in the correct format for this importer. - */ @Override - public boolean isRecognizedFormat(InputStream stream) throws IOException { + public String getId() { + return "cpc"; + } - BufferedReader in = new BufferedReader(ImportFormatReader.getReaderDefaultEncoding(stream)); + @Override + public String getDescription() { + return null; + } + @Override + public boolean isRecognizedFormat(BufferedReader reader) throws IOException { String str; - - while ((str = in.readLine()) != null) { + while ((str = reader.readLine()) != null) { if (CopacImporter.COPAC_PATTERN.matcher(str).find()) { return true; } } - return false; } - /** - * Parse the entries in the source, and return a List of BibEntry - * objects. - */ @Override - public List importEntries(InputStream stream, OutputPrinter status) throws IOException { - if (stream == null) { - throw new IOException("No stream given."); - } + public ParserResult importDatabase(BufferedReader reader) throws IOException { + Objects.requireNonNull(reader); List entries = new LinkedList<>(); StringBuilder sb = new StringBuilder(); - try (BufferedReader in = new BufferedReader(ImportFormatReader.getReaderDefaultEncoding(stream))) { - // Preprocess entries - String str; + // Preprocess entries + String str; - while ((str = in.readLine()) != null) { + while ((str = reader.readLine()) != null) { - if (str.length() < 4) { - continue; - } + if (str.length() < 4) { + continue; + } - String code = str.substring(0, 4); + String code = str.substring(0, 4); - if (" ".equals(code)) { - sb.append(' ').append(str.trim()); - } else { + if (" ".equals(code)) { + sb.append(' ').append(str.trim()); + } else { - // begining of a new item - if ("TI- ".equals(str.substring(0, 4))) { - if (sb.length() > 0) { - entries.add(sb.toString()); - } - sb = new StringBuilder(); + // begining of a new item + if ("TI- ".equals(str.substring(0, 4))) { + if (sb.length() > 0) { + entries.add(sb.toString()); } - sb.append('\n').append(str); + sb = new StringBuilder(); } + sb.append('\n').append(str); } } @@ -166,7 +148,7 @@ public List importEntries(InputStream stream, OutputPrinter status) th results.add(b); } - return results; + return new ParserResult(results); } private static void setOrAppend(BibEntry b, String field, String value, String separator) { diff --git a/src/main/java/net/sf/jabref/importer/fileformat/EndnoteImporter.java b/src/main/java/net/sf/jabref/importer/fileformat/EndnoteImporter.java index dbd5b0cc8ad..d597a352a77 100644 --- a/src/main/java/net/sf/jabref/importer/fileformat/EndnoteImporter.java +++ b/src/main/java/net/sf/jabref/importer/fileformat/EndnoteImporter.java @@ -17,15 +17,13 @@ import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Pattern; -import net.sf.jabref.importer.ImportFormatReader; -import net.sf.jabref.importer.OutputPrinter; +import net.sf.jabref.importer.ParserResult; import net.sf.jabref.logic.labelpattern.LabelPatternUtil; import net.sf.jabref.model.entry.AuthorList; import net.sf.jabref.model.entry.BibEntry; @@ -45,68 +43,57 @@ public class EndnoteImporter extends ImportFormat { private static final Pattern A_PATTERN = Pattern.compile("%A .*"); private static final Pattern E_PATTERN = Pattern.compile("%E .*"); - - /** - * Return the name of this import format. - */ @Override public String getFormatName() { return "Refer/Endnote"; } - /* - * (non-Javadoc) - * @see net.sf.jabref.imports.ImportFormat#getCLIId() - */ @Override - public String getCLIId() { + public List getExtensions() { + return null; + } + + @Override + public String getId() { return "refer"; } - /** - * Check whether the source is in the correct format for this importer. - */ @Override - public boolean isRecognizedFormat(InputStream stream) throws IOException { + public String getDescription() { + return null; + } + @Override + public boolean isRecognizedFormat(BufferedReader reader) throws IOException { // Our strategy is to look for the "%A *" line. - try (BufferedReader in = new BufferedReader(ImportFormatReader.getReaderDefaultEncoding(stream))) { - String str; - while ((str = in.readLine()) != null) { - if (A_PATTERN.matcher(str).matches() || E_PATTERN.matcher(str).matches()) { - return true; - } + String str; + while ((str = reader.readLine()) != null) { + if (A_PATTERN.matcher(str).matches() || E_PATTERN.matcher(str).matches()) { + return true; } } return false; } - /** - * Parse the entries in the source, and return a List of BibEntry - * objects. - */ @Override - public List importEntries(InputStream stream, OutputPrinter status) throws IOException { + public ParserResult importDatabase(BufferedReader reader) throws IOException { List bibitems = new ArrayList<>(); StringBuilder sb = new StringBuilder(); - try (BufferedReader in = new BufferedReader(ImportFormatReader.getReaderDefaultEncoding(stream))) { - - String str; - boolean first = true; - while ((str = in.readLine()) != null) { - str = str.trim(); - if (str.indexOf("%0") == 0) { - if (first) { - first = false; - } else { - sb.append(ENDOFRECORD); - } - sb.append(str); + String str; + boolean first = true; + while ((str = reader.readLine()) != null) { + str = str.trim(); + if (str.indexOf("%0") == 0) { + if (first) { + first = false; } else { - sb.append(str); + sb.append(ENDOFRECORD); } - sb.append('\n'); + sb.append(str); + } else { + sb.append(str); } + sb.append('\n'); } String[] entries = sb.toString().split(ENDOFRECORD); @@ -279,7 +266,7 @@ else if ("P".equals(prefix)) { } - return bibitems; + return new ParserResult(bibitems); } diff --git a/src/main/java/net/sf/jabref/importer/fileformat/FreeCiteImporter.java b/src/main/java/net/sf/jabref/importer/fileformat/FreeCiteImporter.java index 6a870db6026..5f654facda3 100644 --- a/src/main/java/net/sf/jabref/importer/fileformat/FreeCiteImporter.java +++ b/src/main/java/net/sf/jabref/importer/fileformat/FreeCiteImporter.java @@ -15,8 +15,8 @@ */ package net.sf.jabref.importer.fileformat; +import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; @@ -25,8 +25,8 @@ import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.ArrayList; -import java.util.Collections; import java.util.List; +import java.util.Objects; import java.util.Scanner; import javax.xml.stream.XMLInputFactory; @@ -36,7 +36,7 @@ import net.sf.jabref.Globals; import net.sf.jabref.JabRefGUI; -import net.sf.jabref.importer.OutputPrinter; +import net.sf.jabref.importer.ParserResult; import net.sf.jabref.logic.l10n.Localization; import net.sf.jabref.logic.labelpattern.LabelPatternUtil; import net.sf.jabref.model.entry.BibEntry; @@ -55,22 +55,22 @@ public class FreeCiteImporter extends ImportFormat { private static final Log LOGGER = LogFactory.getLog(FreeCiteImporter.class); @Override - public boolean isRecognizedFormat(InputStream in) throws IOException { - // TODO: We don't know how to recognize text files, therefore we return - // "false" + public boolean isRecognizedFormat(BufferedReader reader) throws IOException { + Objects.requireNonNull(reader); + // TODO: We don't know how to recognize text files, therefore we return "false" return false; } @Override - public List importEntries(InputStream in, OutputPrinter status) + public ParserResult importDatabase(BufferedReader reader) throws IOException { - try (Scanner scan = new Scanner(in)) { + try (Scanner scan = new Scanner(reader)) { String text = scan.useDelimiter("\\A").next(); - return importEntries(text, status); + return importEntries(text); } } - public List importEntries(String text, OutputPrinter status) { + public ParserResult importEntries(String text) { // URLencode the string for transmission String urlencodedCitation = null; try { @@ -87,10 +87,10 @@ public List importEntries(String text, OutputPrinter status) { conn = url.openConnection(); } catch (MalformedURLException e) { LOGGER.warn("Bad URL", e); - return Collections.emptyList(); + return new ParserResult(); } catch (IOException e) { LOGGER.warn("Could not download", e); - return Collections.emptyList(); + return new ParserResult(); } try { conn.setRequestProperty("accept", "text/xml"); @@ -104,13 +104,11 @@ public List importEntries(String text, OutputPrinter status) { } catch (IllegalStateException e) { LOGGER.warn("Already connected.", e); } catch (IOException e) { - status.showMessage(Localization.lang("Unable to connect to FreeCite online service.")); LOGGER.warn("Unable to connect to FreeCite online service.", e); - return Collections.emptyList(); + return ParserResult.fromErrorMessage(Localization.lang("Unable to connect to FreeCite online service.")); } // output is in conn.getInputStream(); // new InputStreamReader(conn.getInputStream()) - List res = new ArrayList<>(); XMLInputFactory factory = XMLInputFactory.newInstance(); @@ -223,10 +221,10 @@ public List importEntries(String text, OutputPrinter status) { parser.close(); } catch (IOException | XMLStreamException ex) { LOGGER.warn("Could not parse", ex); - return Collections.emptyList(); + return new ParserResult(); } - return res; + return new ParserResult(res); } @Override @@ -234,4 +232,14 @@ public String getFormatName() { return "text citations"; } + @Override + public List getExtensions() { + return null; + } + + @Override + public String getDescription() { + return null; + } + } diff --git a/src/main/java/net/sf/jabref/importer/fileformat/ImportFormat.java b/src/main/java/net/sf/jabref/importer/fileformat/ImportFormat.java index bc1baa6229a..0c4887630bf 100644 --- a/src/main/java/net/sf/jabref/importer/fileformat/ImportFormat.java +++ b/src/main/java/net/sf/jabref/importer/fileformat/ImportFormat.java @@ -15,68 +15,101 @@ */ package net.sf.jabref.importer.fileformat; +import java.io.BufferedReader; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; import java.util.List; +import java.util.Objects; -import net.sf.jabref.importer.OutputPrinter; -import net.sf.jabref.model.entry.BibEntry; +import net.sf.jabref.importer.ParserResult; /** * Role of an importer for JabRef. - * - *

Importers are sorted according to following criteria - *

  1. - * custom importers come first, then importers shipped with JabRef - *
  2. - * then importers are sorted by name. - *
- *

*/ public abstract class ImportFormat implements Comparable { /** * Using this when I have no database open or when I read * non bibtex file formats (used by the ImportFormatReader.java) + * + * TODO: Is this field really needed or would calling IdGenerator.next() suffice? */ public static final String DEFAULT_BIBTEXENTRY_ID = "__ID"; - private boolean isCustomImporter; - - - /** - * Constructor for custom importers. - */ - public ImportFormat() { - this.isCustomImporter = false; - } - /** * Check whether the source is in the correct format for this importer. + * + * The effect of this method is primarily to avoid unnecessary processing of + * files when searching for a suitable import format. If this method returns + * false, the import routine will move on to the next import format. + * + * Thus the correct behaviour is to return false if it is certain that the file is + * not of the suitable type, and true otherwise. Returning true is the safe choice if not certain. */ - public abstract boolean isRecognizedFormat(InputStream in) throws IOException; + protected abstract boolean isRecognizedFormat(BufferedReader input) throws IOException; + + public boolean isRecognizedFormat(Path filePath, Charset encoding) throws IOException { + try (BufferedReader bufferedReader = getReader(filePath, encoding)) { + return isRecognizedFormat(bufferedReader); + } + } /** - * Parse the entries in the source, and return a List of BibEntry - * objects. + * Parse the database in the source. * * This method can be called in two different contexts - either when importing in * a specified format, or when importing in unknown format. In the latter case, * JabRef cycles through all available import formats. No error messages or feedback * is displayed from individual import formats in this case. * - * If importing in a specified format, and an empty list is returned, JabRef reports - * that no entries were found. If an IOException is thrown, JabRef displays the exception's - * message in unmodified form. + * If importing in a specified format and an empty database is returned, JabRef reports + * that no entries were found. * - * This method should never return null. Return an empty list instead. + * This method should never return null. * - * TODO the return type should be changed to "ParseResult" as the parser could use a different encoding than the default encoding + * @param input the input to read from */ - public abstract List importEntries(InputStream in, OutputPrinter status) throws IOException; + protected abstract ParserResult importDatabase(BufferedReader input) throws IOException ; /** - * Name of this import format. + * Parse the database in the specified file. + * + * Importer having the facilities to detect the correct encoding of a file should overwrite this method, + * determine the encoding and then call {@link #importDatabase(BufferedReader)}. + * + * @param filePath the path to the file which should be imported + * @param encoding the encoding used to decode the file + */ + public ParserResult importDatabase(Path filePath, Charset encoding) throws IOException { + try (BufferedReader bufferedReader = getReader(filePath, encoding)) { + ParserResult parserResult = importDatabase(bufferedReader); + parserResult.setEncoding(encoding); + parserResult.setFile(filePath.toFile()); + return parserResult; + } + } + + public static BufferedReader getUTF8Reader(Path filePath) throws IOException { + return getReader(filePath, StandardCharsets.UTF_8); + } + + public static BufferedReader getUTF16Reader(Path filePath) throws IOException { + return getReader(filePath, StandardCharsets.UTF_16); + } + + public static BufferedReader getReader(Path filePath, Charset encoding) + throws IOException { + InputStream stream = new FileInputStream(filePath.toFile()); + return new BufferedReader(new InputStreamReader(stream, encoding)); + } + + /** + * Returns the name of this import format. * *

The name must be unique.

* @@ -85,20 +118,20 @@ public ImportFormat() { public abstract String getFormatName(); /** - * Extensions that this importer can read. + * Returns the file extensions that this importer can read. + * The extension should contain the leading dot, so for example ".bib" * - * @return comma separated list of extensions or null for the default + * @return list of supported file extensions (not null but may be empty) */ - public String getExtensions() { - return null; - } + public abstract List getExtensions(); /** - * Short, one token ID to identify the format from the command line. + * Returns a one-word ID which identifies this import format. + * Used for example, to identify the format when used from the command line. * - * @return command line ID + * @return ID, must be unique and not null */ - public String getCLIId() { + public String getId() { String id = getFormatName(); StringBuilder result = new StringBuilder(id.length()); for (int i = 0; i < id.length(); i++) { @@ -111,91 +144,43 @@ public String getCLIId() { } /** - * Description of the ImportFormat. + * Returns the description of the import format. * - *

Implementors of ImportFormats should override this. Ideally, it should specify + * The description should specify *

  • * what kind of entries from what sources and based on what specification it is able to import *
  • - * by what criteria it {@link #isRecognizedFormat(InputStream) recognizes} an import format + * by what criteria it {@link #isRecognizedFormat(BufferedReader) recognizes} an import format *
* * @return description of the import format */ - public String getDescription() { - return "No description available for " + getFormatName() + "."; - } - - /** - * Sets if this is a custom importer. - * - *

For custom importers added dynamically to JabRef, this will be - * set automatically by JabRef.

- * - * @param isCustomImporter if this is a custom importer - */ - public final void setIsCustomImporter(boolean isCustomImporter) { - this.isCustomImporter = isCustomImporter; - } - - /** - * Wether this importer is a custom importer. - * - *

Custom importers will have precedence over built-in importers.

- * - * @return wether this is a custom importer - */ - public final boolean isCustomImporter() { - return this.isCustomImporter; - } + public abstract String getDescription(); - /* - * (non-Javadoc) - * @see java.lang.Object#hashCode() - */ @Override public int hashCode() { return getFormatName().hashCode(); } - /* - * (non-Javadoc) - * @see java.lang.Object#equals(java.lang.Object) - */ @Override - public boolean equals(Object o) { - if (this == o) { - return true; + public boolean equals(Object obj) { + if(obj == null) { + return false; } - - if (o instanceof ImportFormat) { - ImportFormat format = (ImportFormat) o; - return (format.isCustomImporter() == isCustomImporter()) && format.getFormatName().equals(getFormatName()); + if(!(obj instanceof ImportFormat)) { + return false; } - return false; + ImportFormat other = (ImportFormat)obj; + return Objects.equals(this.getFormatName(), other.getFormatName()); } - /* - * (non-Javadoc) - * @see java.lang.Object#toString() - */ @Override public String toString() { return getFormatName(); } - /* - * (non-Javadoc) - * @see java.lang.Comparable#compareTo(java.lang.Object) - */ @Override - public int compareTo(ImportFormat importer) { - int result; - if (isCustomImporter() == importer.isCustomImporter()) { - result = getFormatName().compareTo(importer.getFormatName()); - } else { - result = isCustomImporter() ? 1 : -1; - } - return result; + public int compareTo(ImportFormat o) { + return getFormatName().compareTo(o.getFormatName()); } } diff --git a/src/main/java/net/sf/jabref/importer/fileformat/InspecImporter.java b/src/main/java/net/sf/jabref/importer/fileformat/InspecImporter.java index e2f283d8017..f60bad7fe4e 100644 --- a/src/main/java/net/sf/jabref/importer/fileformat/InspecImporter.java +++ b/src/main/java/net/sf/jabref/importer/fileformat/InspecImporter.java @@ -17,15 +17,13 @@ import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Pattern; -import net.sf.jabref.importer.ImportFormatReader; -import net.sf.jabref.importer.OutputPrinter; +import net.sf.jabref.importer.ParserResult; import net.sf.jabref.model.entry.AuthorList; import net.sf.jabref.model.entry.BibEntry; @@ -36,60 +34,46 @@ public class InspecImporter extends ImportFormat { private static final Pattern INSPEC_PATTERN = Pattern.compile("Record.*INSPEC.*"); - - /** - * Return the name of this import format. - */ @Override public String getFormatName() { return "INSPEC"; } - /* - * (non-Javadoc) - * @see net.sf.jabref.imports.ImportFormat#getCLIId() - */ @Override - public String getCLIId() { - return "inspec"; + public List getExtensions() { + return null; } - /** - * Check whether the source is in the correct format for this importer. - */ @Override - public boolean isRecognizedFormat(InputStream stream) throws IOException { - // Our strategy is to look for the "PY " line. - try (BufferedReader in = new BufferedReader(ImportFormatReader.getReaderDefaultEncoding(stream))) { - String str; + public String getDescription() { + return null; + } - while ((str = in.readLine()) != null) { - if (INSPEC_PATTERN.matcher(str).find()) { - return true; - } + @Override + public boolean isRecognizedFormat(BufferedReader reader) throws IOException { + // Our strategy is to look for the "PY " line. + String str; + while ((str = reader.readLine()) != null) { + if (INSPEC_PATTERN.matcher(str).find()) { + return true; } } return false; } - /** - * Parse the entries in the source, and return a List of BibEntry objects. - */ @Override - public List importEntries(InputStream stream, OutputPrinter status) throws IOException { + public ParserResult importDatabase(BufferedReader reader) throws IOException { List bibitems = new ArrayList<>(); StringBuilder sb = new StringBuilder(); String str; - try (BufferedReader in = new BufferedReader(ImportFormatReader.getReaderDefaultEncoding(stream))) { - while ((str = in.readLine()) != null) { - if (str.length() < 2) { - continue; - } - if (str.indexOf("Record") == 0) { - sb.append("__::__").append(str); - } else { - sb.append("__NEWFIELD__").append(str); - } + while ((str = reader.readLine()) != null) { + if (str.length() < 2) { + continue; + } + if (str.indexOf("Record") == 0) { + sb.append("__::__").append(str); + } else { + sb.append("__NEWFIELD__").append(str); } } String[] entries = sb.toString().split("__::__"); @@ -156,6 +140,6 @@ public List importEntries(InputStream stream, OutputPrinter status) th } - return bibitems; + return new ParserResult(bibitems); } } diff --git a/src/main/java/net/sf/jabref/importer/fileformat/IsiImporter.java b/src/main/java/net/sf/jabref/importer/fileformat/IsiImporter.java index bb1c5931973..5b4db6b8661 100644 --- a/src/main/java/net/sf/jabref/importer/fileformat/IsiImporter.java +++ b/src/main/java/net/sf/jabref/importer/fileformat/IsiImporter.java @@ -17,16 +17,15 @@ import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; -import net.sf.jabref.importer.ImportFormatReader; -import net.sf.jabref.importer.OutputPrinter; +import net.sf.jabref.importer.ParserResult; import net.sf.jabref.logic.formatter.casechanger.TitleCaseFormatter; import net.sf.jabref.model.entry.BibEntry; import net.sf.jabref.model.entry.MonthUtil; @@ -57,57 +56,48 @@ public class IsiImporter extends ImportFormat { private static final Pattern ISI_PATTERN = Pattern.compile("FN ISI Export Format|VR 1.|PY \\d{4}"); - /** - * Return the name of this import format. - */ @Override public String getFormatName() { return "ISI"; } - /* - * (non-Javadoc) - * - * @see net.sf.jabref.imports.ImportFormat#getCLIId() - */ @Override - public String getCLIId() { - return "isi"; + public List getExtensions() { + return null; } + @Override + public String getId() { + return "isi"; + } - - /** - * Check whether the source is in the correct format for this importer. - */ @Override - public boolean isRecognizedFormat(InputStream stream) throws IOException { - - try (BufferedReader in = new BufferedReader(ImportFormatReader.getReaderDefaultEncoding(stream))) { - - String str; - int i = 0; - while (((str = in.readLine()) != null) && (i < 50)) { - - /** - * The following line gives false positives for RIS files, so it - * should not be uncommented. The hypen is a characteristic of the - * RIS format. - * - * str = str.replace(" - ", "") - */ - if (IsiImporter.ISI_PATTERN.matcher(str).find()) { - return true; - } + public String getDescription() { + return null; + } - i++; + @Override + public boolean isRecognizedFormat(BufferedReader reader) throws IOException { + String str; + int i = 0; + while (((str = reader.readLine()) != null) && (i < 50)) { + + /** + * The following line gives false positives for RIS files, so it + * should not be uncommented. The hypen is a characteristic of the + * RIS format. + * + * str = str.replace(" - ", "") + */ + if (IsiImporter.ISI_PATTERN.matcher(str).find()) { + return true; } + + i++; } return false; } - - public static void processSubSup(Map map) { String[] subsup = {"title", "abstract", "review", "notes"}; @@ -156,45 +146,37 @@ private static void processCapitalization(Map map) { } } - /** - * Parse the entries in the source, and return a List of BibEntry - * objects. - */ @Override - public List importEntries(InputStream stream, OutputPrinter status) throws IOException { - if (stream == null) { - throw new IOException("No stream given."); - } + public ParserResult importDatabase(BufferedReader reader) throws IOException { + Objects.requireNonNull(reader); List bibitems = new ArrayList<>(); StringBuilder sb = new StringBuilder(); - try (BufferedReader in = new BufferedReader(ImportFormatReader.getReaderDefaultEncoding(stream))) { - // Pattern fieldPattern = Pattern.compile("^AU |^TI |^SO |^DT |^C1 |^AB - // |^ID |^BP |^PY |^SE |^PY |^VL |^IS "); - String str; + // Pattern fieldPattern = Pattern.compile("^AU |^TI |^SO |^DT |^C1 |^AB + // |^ID |^BP |^PY |^SE |^PY |^VL |^IS "); + String str; - while ((str = in.readLine()) != null) { - if (str.length() < 3) { - continue; - } + while ((str = reader.readLine()) != null) { + if (str.length() < 3) { + continue; + } - // beginning of a new item - if ("PT ".equals(str.substring(0, 3))) { - sb.append("::").append(str); + // beginning of a new item + if ("PT ".equals(str.substring(0, 3))) { + sb.append("::").append(str); + } else { + String beg = str.substring(0, 3).trim(); + + // I could have used the fieldPattern regular expression instead + // however this seems to be + // quick and dirty and it works! + if (beg.length() == 2) { + sb.append(" ## "); // mark the beginning of each field + sb.append(str); } else { - String beg = str.substring(0, 3).trim(); - - // I could have used the fieldPattern regular expression instead - // however this seems to be - // quick and dirty and it works! - if (beg.length() == 2) { - sb.append(" ## "); // mark the beginning of each field - sb.append(str); - } else { - sb.append("EOLEOL"); // mark the end of each line - sb.append(str.trim()); // remove the initial spaces - } + sb.append("EOLEOL"); // mark the end of each line + sb.append(str.trim()); // remove the initial spaces } } } @@ -355,7 +337,7 @@ public List importEntries(InputStream stream, OutputPrinter status) th bibitems.add(b); } - return bibitems; + return new ParserResult(bibitems); } private static String parsePages(String value) { diff --git a/src/main/java/net/sf/jabref/importer/fileformat/MedlineImporter.java b/src/main/java/net/sf/jabref/importer/fileformat/MedlineImporter.java index f4e4b4e5b15..4a27483d3cc 100644 --- a/src/main/java/net/sf/jabref/importer/fileformat/MedlineImporter.java +++ b/src/main/java/net/sf/jabref/importer/fileformat/MedlineImporter.java @@ -19,21 +19,19 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.net.URLConnection; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; -import net.sf.jabref.importer.ImportFormatReader; -import net.sf.jabref.importer.OutputPrinter; +import net.sf.jabref.importer.ParserResult; import net.sf.jabref.model.entry.BibEntry; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.xml.sax.InputSource; /** * Importer for the Refer/Endnote format. @@ -51,62 +49,39 @@ public String getFormatName() { return "Medline"; } - /* - * (non-Javadoc) - * - * @see net.sf.jabref.imports.ImportFormat#getCLIId() - */ @Override - public String getCLIId() { - return "medline"; + public List getExtensions() { + return null; } - /** - * Check whether the source is in the correct format for this importer. - */ @Override - public boolean isRecognizedFormat(InputStream stream) throws IOException { + public String getId() { + return "medline"; + } - try (BufferedReader in = new BufferedReader(ImportFormatReader.getReaderDefaultEncoding(stream))) { - String str; - int i = 0; - while (((str = in.readLine()) != null) && (i < 50)) { + @Override + public String getDescription() { + return null; + } - if (str.toLowerCase().contains("")) { - return true; - } + @Override + public boolean isRecognizedFormat(BufferedReader reader) throws IOException { + String str; + int i = 0; + while (((str = reader.readLine()) != null) && (i < 50)) { - i++; + if (str.toLowerCase().contains("")) { + return true; } - } - return false; - } - /** - * Fetch and parse an medline item from eutils.ncbi.nlm.nih.gov. - * - * @param id One or several ids, separated by "," - * - * @return Will return an empty list on error. - */ - public static List fetchMedline(String id, OutputPrinter status) { - String baseUrl = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&retmode=xml&rettype=citation&id=" + - id; - try { - URL url = new URL(baseUrl); - URLConnection data = url.openConnection(); - return new MedlineImporter().importEntries(data.getInputStream(), status); - } catch (IOException e) { - return new ArrayList<>(); + i++; } + return false; } - /** - * Parse the entries in the source, and return a List of BibEntry - * objects. - */ @Override - public List importEntries(InputStream stream, OutputPrinter status) throws IOException { + public ParserResult importDatabase(BufferedReader reader) throws IOException { + Objects.requireNonNull(reader); // Obtain a factory object for creating SAX parsers SAXParserFactory parserFactory = SAXParserFactory.newInstance(); @@ -124,14 +99,14 @@ public List importEntries(InputStream stream, OutputPrinter status) th MedlineHandler handler = new MedlineHandler(); // Start the parser. It reads the file and calls methods of the // handler. - parser.parse(stream, handler); + parser.parse(new InputSource(reader), handler); // Switch this to true if you want to make a local copy for testing. if (false) { - stream.reset(); + reader.reset(); try (FileOutputStream out = new FileOutputStream(new File("/home/alver/ut.txt"))) { int c; - while ((c = stream.read()) != -1) { + while ((c = reader.read()) != -1) { out.write((char) c); } } @@ -140,18 +115,18 @@ public List importEntries(InputStream stream, OutputPrinter status) th // When you're done, report the results stored by your handler // object bibItems.addAll(handler.getItems()); - } catch (javax.xml.parsers.ParserConfigurationException e1) { - LOGGER.error("Error with XML parser configuration", e1); - status.showMessage(e1.getLocalizedMessage()); - } catch (org.xml.sax.SAXException e2) { - LOGGER.error("Error during XML parsing", e2); - status.showMessage(e2.getLocalizedMessage()); - } catch (IOException e3) { - LOGGER.error("Error during file import", e3); - status.showMessage(e3.getLocalizedMessage()); + } catch (javax.xml.parsers.ParserConfigurationException e) { + LOGGER.error("Error with XML parser configuration", e); + return ParserResult.fromErrorMessage(e.getLocalizedMessage()); + } catch (org.xml.sax.SAXException e) { + LOGGER.error("Error during XML parsing", e); + return ParserResult.fromErrorMessage(e.getLocalizedMessage()); + } catch (IOException e) { + LOGGER.error("Error during file import", e); + return ParserResult.fromErrorMessage(e.getLocalizedMessage()); } - return bibItems; + return new ParserResult(bibItems); } } diff --git a/src/main/java/net/sf/jabref/importer/fileformat/MedlinePlainImporter.java b/src/main/java/net/sf/jabref/importer/fileformat/MedlinePlainImporter.java index 7ef2d4c6923..00215f82b9d 100644 --- a/src/main/java/net/sf/jabref/importer/fileformat/MedlinePlainImporter.java +++ b/src/main/java/net/sf/jabref/importer/fileformat/MedlinePlainImporter.java @@ -19,7 +19,6 @@ import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -27,8 +26,7 @@ import java.util.regex.Pattern; import net.sf.jabref.Globals; -import net.sf.jabref.importer.ImportFormatReader; -import net.sf.jabref.importer.OutputPrinter; +import net.sf.jabref.importer.ParserResult; import net.sf.jabref.model.entry.AuthorList; import net.sf.jabref.model.entry.BibEntry; @@ -46,59 +44,44 @@ public class MedlinePlainImporter extends ImportFormat { private static final Pattern PMC_PATTERN = Pattern.compile("PMC.*-.*"); private static final Pattern PMCR_PATTERN = Pattern.compile("PMCR.*-.*"); - - /** - * Return the name of this import format. - */ @Override public String getFormatName() { return "MedlinePlain"; } - /* - * (non-Javadoc) - * @see net.sf.jabref.imports.ImportFormat#getCLIId() - */ @Override - public String getCLIId() { - return "medlineplain"; + public List getExtensions() { + return null; + } + + @Override + public String getDescription() { + return null; } - /** - * Check whether the source is in the correct format for this importer. - */ @Override - public boolean isRecognizedFormat(InputStream stream) throws IOException { + public boolean isRecognizedFormat(BufferedReader reader) throws IOException { // Our strategy is to look for the "PMID - *", "PMC.*-.*", or "PMCR.*-.*" line // (i.e., PubMed Unique Identifier, PubMed Central Identifier, PubMed Central Release) - try (BufferedReader in = new BufferedReader(ImportFormatReader.getReaderDefaultEncoding(stream))) { - - String str; - while ((str = in.readLine()) != null) { - if (PMID_PATTERN.matcher(str).find() || PMC_PATTERN.matcher(str).find() - || PMCR_PATTERN.matcher(str).find()) { - return true; - } + String str; + while ((str = reader.readLine()) != null) { + if (PMID_PATTERN.matcher(str).find() || PMC_PATTERN.matcher(str).find() || PMCR_PATTERN.matcher(str) + .find()) { + return true; } } return false; } - /** - * Parse the entries in the source, and return a List of BibEntry - * objects. - */ @Override - public List importEntries(InputStream stream, OutputPrinter status) throws IOException { + public ParserResult importDatabase(BufferedReader reader) throws IOException { List bibitems = new ArrayList<>(); StringBuilder sb = new StringBuilder(); - try (BufferedReader in = new BufferedReader(ImportFormatReader.getReaderDefaultEncoding(stream))) { - String str; - while ((str = in.readLine()) != null) { - sb.append(str); - sb.append('\n'); - } + String str; + while ((str = reader.readLine()) != null) { + sb.append(str); + sb.append('\n'); } String[] entries = sb.toString().replace("\u2013", "-").replace("\u2014", "--").replace("\u2015", "--") .split("\\n\\n"); @@ -286,7 +269,7 @@ else if ("AID".equals(lab)) { } - return bibitems; + return new ParserResult(bibitems); } } diff --git a/src/main/java/net/sf/jabref/importer/fileformat/MsBibImporter.java b/src/main/java/net/sf/jabref/importer/fileformat/MsBibImporter.java index 6157ce8fe87..52d11a7a5d8 100644 --- a/src/main/java/net/sf/jabref/importer/fileformat/MsBibImporter.java +++ b/src/main/java/net/sf/jabref/importer/fileformat/MsBibImporter.java @@ -15,18 +15,19 @@ */ package net.sf.jabref.importer.fileformat; +import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; import java.util.List; +import java.util.Objects; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; -import net.sf.jabref.importer.OutputPrinter; +import net.sf.jabref.importer.ParserResult; import net.sf.jabref.logic.msbib.MSBibDatabase; -import net.sf.jabref.model.entry.BibEntry; import org.w3c.dom.Document; +import org.xml.sax.InputSource; /** * Importer for the MS Office 2007 XML bibliography format @@ -37,14 +38,10 @@ public class MsBibImporter extends ImportFormat { @Override - public boolean isRecognizedFormat(InputStream in) throws IOException { + public boolean isRecognizedFormat(BufferedReader reader) throws IOException { + Objects.requireNonNull(reader); /* - This method is available for checking if a file can be of the MSBib type. - The effect of this method is primarily to avoid unnecessary processing of - files when searching for a suitable import format. If this method returns - false, the import routine will move on to the next import format. - The correct behaviour is to return false if it is certain that the file is not of the MsBib type, and true otherwise. Returning true is the safe choice if not certain. @@ -54,33 +51,34 @@ public boolean isRecognizedFormat(InputStream in) throws IOException { DocumentBuilder dbuild = DocumentBuilderFactory. newInstance(). newDocumentBuilder(); - docin = dbuild.parse(in); + docin = dbuild.parse(new InputSource(reader)); } catch (Exception e) { return false; } return (docin == null) || docin.getDocumentElement().getTagName().contains("Sources"); } - /** - * String used to identify this import filter on the command line. - * @return "msbib" - */ - public String getCommandLineId() { - return "msbib"; - } - @Override - public List importEntries(InputStream in, OutputPrinter status) throws IOException { + public ParserResult importDatabase(BufferedReader reader) throws IOException { + Objects.requireNonNull(reader); MSBibDatabase dbase = new MSBibDatabase(); - - return dbase.importEntries(in); + return new ParserResult(dbase.importEntries(reader)); } @Override public String getFormatName() { - // This method should return the name of this import format. return "MSBib"; } + @Override + public List getExtensions() { + return null; + } + + @Override + public String getDescription() { + return null; + } + } diff --git a/src/main/java/net/sf/jabref/importer/fileformat/OvidImporter.java b/src/main/java/net/sf/jabref/importer/fileformat/OvidImporter.java index e7494faf3f0..de8fe3d3201 100644 --- a/src/main/java/net/sf/jabref/importer/fileformat/OvidImporter.java +++ b/src/main/java/net/sf/jabref/importer/fileformat/OvidImporter.java @@ -17,7 +17,6 @@ import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -25,8 +24,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import net.sf.jabref.importer.ImportFormatReader; -import net.sf.jabref.importer.OutputPrinter; +import net.sf.jabref.importer.ParserResult; import net.sf.jabref.model.entry.AuthorList; import net.sf.jabref.model.entry.BibEntry; import net.sf.jabref.model.entry.IdGenerator; @@ -55,63 +53,48 @@ public class OvidImporter extends ImportFormat { private static final Pattern OVID_PATTERN = Pattern.compile(OVID_PATTERN_STRING); private static final int MAX_ITEMS = 50; - /** - * Return the name of this import format. - */ + @Override public String getFormatName() { return "Ovid"; } - /* - * (non-Javadoc) - * @see net.sf.jabref.imports.ImportFormat#getCLIId() - */ @Override - public String getCLIId() { - return "ovid"; + public List getExtensions() { + return null; } - - - /** - * Check whether the source is in the correct format for this importer. - */ @Override - public boolean isRecognizedFormat(InputStream stream) throws IOException { - - try (BufferedReader in = new BufferedReader(ImportFormatReader.getReaderDefaultEncoding(stream))) { - String str; - int i = 0; - while (((str = in.readLine()) != null) && (i < MAX_ITEMS)) { + public String getDescription() { + return null; + } - if (OvidImporter.OVID_PATTERN.matcher(str).find()) { - return true; - } + @Override + public boolean isRecognizedFormat(BufferedReader reader) throws IOException { + String str; + int i = 0; + while (((str = reader.readLine()) != null) && (i < MAX_ITEMS)) { - i++; + if (OvidImporter.OVID_PATTERN.matcher(str).find()) { + return true; } + + i++; } return false; } - /** - * Parse the entries in the source, and return a List of BibEntry - * objects. - */ @Override - public List importEntries(InputStream stream, OutputPrinter status) throws IOException { + public ParserResult importDatabase(BufferedReader reader) throws IOException { List bibitems = new ArrayList<>(); StringBuilder sb = new StringBuilder(); - try (BufferedReader in = new BufferedReader(ImportFormatReader.getReaderDefaultEncoding(stream))) { - String line; - while ((line = in.readLine()) != null) { - if (!line.isEmpty() && (line.charAt(0) != ' ')) { - sb.append("__NEWFIELD__"); - } - sb.append(line); - sb.append('\n'); + String line; + while ((line = reader.readLine()) != null) { + if (!line.isEmpty() && (line.charAt(0) != ' ')) { + sb.append("__NEWFIELD__"); } + sb.append(line); + sb.append('\n'); } String[] items = sb.toString().split(OVID_PATTERN_STRING); @@ -244,7 +227,7 @@ public List importEntries(InputStream stream, OutputPrinter status) th } - return bibitems; + return new ParserResult(bibitems); } /** diff --git a/src/main/java/net/sf/jabref/importer/fileformat/PdfContentImporter.java b/src/main/java/net/sf/jabref/importer/fileformat/PdfContentImporter.java index 4393c57e8ba..a9a412ee975 100644 --- a/src/main/java/net/sf/jabref/importer/fileformat/PdfContentImporter.java +++ b/src/main/java/net/sf/jabref/importer/fileformat/PdfContentImporter.java @@ -1,18 +1,20 @@ package net.sf.jabref.importer.fileformat; +import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; import java.io.StringWriter; +import java.nio.charset.Charset; +import java.nio.file.Path; import java.util.ArrayList; -import java.util.Collections; import java.util.List; +import java.util.Objects; import java.util.Optional; import java.util.regex.Matcher; import java.util.regex.Pattern; -import net.sf.jabref.importer.ImportInspector; -import net.sf.jabref.importer.OutputPrinter; +import net.sf.jabref.importer.ParserResult; import net.sf.jabref.importer.fetcher.DOItoBibTeXFetcher; +import net.sf.jabref.logic.l10n.Localization; import net.sf.jabref.logic.util.DOI; import net.sf.jabref.logic.xmp.EncryptedPdfsNotSupportedException; import net.sf.jabref.logic.xmp.XMPUtil; @@ -21,8 +23,6 @@ import net.sf.jabref.model.entry.EntryType; import com.google.common.base.Strings; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.util.PDFTextStripper; @@ -34,7 +34,6 @@ * Integrating XMP support is future work */ public class PdfContentImporter extends ImportFormat { - private static final Log LOGGER = LogFactory.getLog(PdfContentImporter.class); private static final Pattern YEAR_EXTRACT_PATTERN = Pattern.compile("\\d{4}"); // we can store the DOItoBibTeXFetcher as single reference as the fetcher doesn't hold internal state @@ -178,36 +177,31 @@ private static String streamlineTitle(String title) { } @Override - public boolean isRecognizedFormat(InputStream in) throws IOException { + public boolean isRecognizedFormat(BufferedReader reader) throws IOException { + Objects.requireNonNull(reader); return false; } @Override - public List importEntries(InputStream in, OutputPrinter status) throws IOException { - final ArrayList result = new ArrayList<>(1); + public ParserResult importDatabase(BufferedReader reader) throws IOException { + Objects.requireNonNull(reader); + throw new UnsupportedOperationException( + "PdfContentImporter does not support importDatabase(BufferedReader reader)." + + "Instead use importDatabase(Path filePath, Charset defaultEncoding)."); + } - try (PDDocument document = XMPUtil.loadWithAutomaticDecryption(in)) { + @Override + public ParserResult importDatabase(Path filePath, Charset defaultEncoding) { + final ArrayList result = new ArrayList<>(1); + try (PDDocument document = XMPUtil.loadWithAutomaticDecryption(filePath)) { String firstPageContents = getFirstPageContents(document); Optional doi = DOI.findInText(firstPageContents); if (doi.isPresent()) { - ImportInspector inspector = new ImportInspector() { - @Override - public void setProgress(int current, int max) { - // Do nothing - } - - @Override - public void addEntry(BibEntry entry) { - // add the entry to the result object - result.add(entry); - } - }; - - DOI_TO_BIBTEX_FETCHER.processQuery(doi.get().getDOI(), inspector, status); - if (!result.isEmpty()) { - return result; - } + ParserResult parserResult = new ParserResult(result); + BibEntry entry = DOI_TO_BIBTEX_FETCHER.getEntryFromDOI(doi.get().getDOI(), parserResult); + parserResult.getDatabase().insertEntry(entry); + return parserResult; } // idea: split[] contains the different lines @@ -224,7 +218,7 @@ public void addEntry(BibEntry entry) { if (i >= lines.length) { // PDF could not be parsed or is empty // return empty list - return result; + return new ParserResult(); } // we start at the current line @@ -473,10 +467,12 @@ public void addEntry(BibEntry entry) { result.add(entry); } catch (EncryptedPdfsNotSupportedException e) { - LOGGER.info("Decryption not supported"); - return Collections.emptyList(); + return ParserResult.fromErrorMessage(Localization.lang("Decryption not supported.")); + } catch(IOException exception) { + return ParserResult.fromErrorMessage(exception.getLocalizedMessage()); } - return result; + + return new ParserResult(result); } private String getFirstPageContents(PDDocument document) throws IOException { @@ -582,4 +578,14 @@ public String getFormatName() { return "PDFcontent"; } + @Override + public List getExtensions() { + return null; + } + + @Override + public String getDescription() { + return null; + } + } diff --git a/src/main/java/net/sf/jabref/importer/fileformat/PdfXmpImporter.java b/src/main/java/net/sf/jabref/importer/fileformat/PdfXmpImporter.java index 8ba02c31153..14bb88b624c 100644 --- a/src/main/java/net/sf/jabref/importer/fileformat/PdfXmpImporter.java +++ b/src/main/java/net/sf/jabref/importer/fileformat/PdfXmpImporter.java @@ -15,14 +15,16 @@ */ package net.sf.jabref.importer.fileformat; +import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; +import java.nio.charset.Charset; +import java.nio.file.Path; import java.util.List; +import java.util.Objects; -import net.sf.jabref.importer.OutputPrinter; +import net.sf.jabref.importer.ParserResult; import net.sf.jabref.logic.l10n.Localization; import net.sf.jabref.logic.xmp.XMPUtil; -import net.sf.jabref.model.entry.BibEntry; /** * Wraps the XMPUtility function to be used as an ImportFormat. @@ -34,12 +36,35 @@ public String getFormatName() { return Localization.lang("XMP-annotated PDF"); } - /** - * Returns a list of all BibtexEntries found in the inputstream. - */ @Override - public List importEntries(InputStream in, OutputPrinter status) throws IOException { - return XMPUtil.readXMP(in); + public List getExtensions() { + return null; + } + + @Override + public ParserResult importDatabase(BufferedReader reader) throws IOException { + Objects.requireNonNull(reader); + throw new UnsupportedOperationException( + "PdfXmpImporter does not support importDatabase(BufferedReader reader)." + + "Instead use importDatabase(Path filePath, Charset defaultEncoding)."); + } + + @Override + public ParserResult importDatabase(Path filePath, Charset defaultEncoding) { + Objects.requireNonNull(filePath); + try { + return new ParserResult(XMPUtil.readXMP(filePath)); + } catch (IOException exception) { + return ParserResult.fromErrorMessage(exception.getLocalizedMessage()); + } + } + + @Override + protected boolean isRecognizedFormat(BufferedReader reader) throws IOException { + Objects.requireNonNull(reader); + throw new UnsupportedOperationException( + "PdfXmpImporter does not support isRecognizedFormat(BufferedReader reader)." + + "Instead use isRecognizedFormat(Path filePath, Charset defaultEncoding)."); } /** @@ -47,17 +72,19 @@ public List importEntries(InputStream in, OutputPrinter status) throws * contains at least one BibEntry. */ @Override - public boolean isRecognizedFormat(InputStream in) throws IOException { - return XMPUtil.hasMetadata(in); + public boolean isRecognizedFormat(Path filePath, Charset defaultEncoding) throws IOException { + Objects.requireNonNull(filePath); + return XMPUtil.hasMetadata(filePath); } - /** - * String used to identify this import filter on the command line. - * - * @return "xmp" - */ - public String getCommandLineId() { + @Override + public String getId() { return "xmp"; } + @Override + public String getDescription() { + return null; + } + } diff --git a/src/main/java/net/sf/jabref/importer/fileformat/RepecNepImporter.java b/src/main/java/net/sf/jabref/importer/fileformat/RepecNepImporter.java index 4689bbec352..04871c5d41e 100644 --- a/src/main/java/net/sf/jabref/importer/fileformat/RepecNepImporter.java +++ b/src/main/java/net/sf/jabref/importer/fileformat/RepecNepImporter.java @@ -19,19 +19,19 @@ import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; import java.util.Collection; +import java.util.Collections; import java.util.Date; import java.util.GregorianCalendar; import java.util.List; +import java.util.Objects; -import net.sf.jabref.importer.ImportFormatReader; -import net.sf.jabref.importer.OutputPrinter; +import net.sf.jabref.importer.ParserResult; import net.sf.jabref.model.entry.BibEntry; import net.sf.jabref.model.entry.IdGenerator; @@ -169,37 +169,21 @@ public class RepecNepImporter extends ImportFormat { private String preLine = ""; private boolean inOverviewSection; - - /** - * Return the name of this import format. - */ @Override public String getFormatName() { return "REPEC New Economic Papers (NEP)"; } - /* - * (non-Javadoc) - * @see net.sf.jabref.imports.ImportFormat#getCLIId() - */ @Override - public String getCLIId() { + public String getId() { return "repecnep"; } - /* - * (non-Javadoc) - * @see net.sf.jabref.imports.ImportFormat#getExtensions() - */ @Override - public String getExtensions() { - return ".txt"; + public List getExtensions() { + return Collections.singletonList(".txt"); } - /* - * (non-Javadoc) - * @see net.sf.jabref.imports.ImportFormat#getDescription() - */ @Override public String getDescription() { return @@ -210,25 +194,19 @@ public String getDescription() { + "contains the line \"nep.repec.org\"."; } - /* - * (non-Javadoc) - * @see net.sf.jabref.imports.ImportFormat#isRecognizedFormat(java.io.InputStream) - */ @Override - public boolean isRecognizedFormat(InputStream stream) throws IOException { + public boolean isRecognizedFormat(BufferedReader reader) throws IOException { // read the first couple of lines // NEP message usually contain the String 'NEP: New Economics Papers' // or, they are from nep.repec.org - try (BufferedReader inBR = new BufferedReader(ImportFormatReader.getReaderDefaultEncoding(stream))) { - StringBuilder startOfMessage = new StringBuilder(); - String tmpLine = inBR.readLine(); - for (int i = 0; (i < 25) && (tmpLine != null); i++) { - startOfMessage.append(tmpLine); - tmpLine = inBR.readLine(); - } - return startOfMessage.toString().contains("NEP: New Economics Papers") - || startOfMessage.toString().contains("nep.repec.org"); + StringBuilder startOfMessage = new StringBuilder(); + String tmpLine = reader.readLine(); + for (int i = 0; (i < 25) && (tmpLine != null); i++) { + startOfMessage.append(tmpLine); + tmpLine = reader.readLine(); } + return startOfMessage.toString().contains("NEP: New Economics Papers") || startOfMessage.toString().contains( + "nep.repec.org"); } private boolean startsWithKeyword(Collection keywords) { @@ -434,18 +412,15 @@ private boolean isStartOfWorkingPaper() { return this.lastLine.matches("\\d+\\.\\s.*") && !this.inOverviewSection && "".equals(this.preLine.trim()); } - /* - * (non-Javadoc) - * @see net.sf.jabref.imports.ImportFormat#importEntries(java.io.InputStream) - */ @Override - public List importEntries(InputStream stream, OutputPrinter status) throws IOException { + public ParserResult importDatabase(BufferedReader reader) throws IOException { + Objects.requireNonNull(reader); + List bibitems = new ArrayList<>(); String paperNoStr = null; this.line = 0; - - try (BufferedReader in = new BufferedReader(ImportFormatReader.getReaderDefaultEncoding(stream))) { - readLine(in); // skip header and editor information + try { + readLine(reader); // skip header and editor information while (this.lastLine != null) { if (this.lastLine.startsWith("-----------------------------")) { @@ -455,25 +430,25 @@ public List importEntries(InputStream stream, OutputPrinter status) th BibEntry be = new BibEntry(IdGenerator.next()); be.setType("techreport"); paperNoStr = this.lastLine.substring(0, this.lastLine.indexOf('.')); - parseTitleString(be, in); + parseTitleString(be, reader); if (startsWithKeyword(RepecNepImporter.RECOGNIZED_FIELDS)) { - parseAdditionalFields(be, false, in); + parseAdditionalFields(be, false, reader); } else { - readLine(in); // skip empty line - parseAuthors(be, in); - readLine(in); // skip empty line + readLine(reader); // skip empty line + parseAuthors(be, reader); + readLine(reader); // skip empty line } if (!startsWithKeyword(RepecNepImporter.RECOGNIZED_FIELDS)) { - parseAbstract(be, in); + parseAbstract(be, reader); } - parseAdditionalFields(be, true, in); + parseAdditionalFields(be, true, reader); bibitems.add(be); paperNoStr = null; } else { this.preLine = this.lastLine; - readLine(in); + readLine(reader); } } @@ -482,17 +457,11 @@ public List importEntries(InputStream stream, OutputPrinter status) th if (paperNoStr != null) { message += ", paper no. " + paperNoStr + ": "; } - message += e.getMessage(); + message += e.getLocalizedMessage(); LOGGER.error(message, e); - IOException toThrow; - if (e instanceof IOException) { - toThrow = (IOException) e; - } else { - toThrow = new IOException(message); - } - throw toThrow; + return ParserResult.fromErrorMessage(message); } - return bibitems; + return new ParserResult(bibitems); } } diff --git a/src/main/java/net/sf/jabref/importer/fileformat/RisImporter.java b/src/main/java/net/sf/jabref/importer/fileformat/RisImporter.java index 5d4f8403c29..667c2359df9 100644 --- a/src/main/java/net/sf/jabref/importer/fileformat/RisImporter.java +++ b/src/main/java/net/sf/jabref/importer/fileformat/RisImporter.java @@ -17,7 +17,6 @@ import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -25,8 +24,7 @@ import java.util.regex.Pattern; import net.sf.jabref.Globals; -import net.sf.jabref.importer.ImportFormatReader; -import net.sf.jabref.importer.OutputPrinter; +import net.sf.jabref.importer.ParserResult; import net.sf.jabref.model.entry.AuthorList; import net.sf.jabref.model.entry.BibEntry; import net.sf.jabref.model.entry.MonthUtil; @@ -41,56 +39,42 @@ public class RisImporter extends ImportFormat { private static final Pattern RECOGNIZED_FORMAT_PATTERN = Pattern.compile("TY - .*"); - /** - * Return the name of this import format. - */ @Override public String getFormatName() { return "RIS"; } - /* - * (non-Javadoc) - * @see net.sf.jabref.imports.ImportFormat#getCLIId() - */ @Override - public String getCLIId() { - return "ris"; + public List getExtensions() { + return null; } - /** - * Check whether the source is in the correct format for this importer. - */ @Override - public boolean isRecognizedFormat(InputStream stream) throws IOException { + public String getDescription() { + return null; + } + @Override + public boolean isRecognizedFormat(BufferedReader reader) throws IOException { // Our strategy is to look for the "AU - *" line. - try (BufferedReader in = new BufferedReader(ImportFormatReader.getReaderDefaultEncoding(stream))) { - - String str; - while ((str = in.readLine()) != null) { - if (RECOGNIZED_FORMAT_PATTERN.matcher(str).find()) { - return true; - } + String str; + while ((str = reader.readLine()) != null) { + if (RECOGNIZED_FORMAT_PATTERN.matcher(str).find()) { + return true; } } return false; } - /** - * Parse the entries in the source, and return a List of BibEntry - * objects. - */ @Override - public List importEntries(InputStream stream, OutputPrinter status) throws IOException { + public ParserResult importDatabase(BufferedReader reader) throws IOException { List bibitems = new ArrayList<>(); StringBuilder sb = new StringBuilder(); - try (BufferedReader in = new BufferedReader(ImportFormatReader.getReaderDefaultEncoding(stream))) { - String str; - while ((str = in.readLine()) != null) { - sb.append(str); - sb.append('\n'); - } + + String line; + while ((line = reader.readLine()) != null) { + sb.append(line); + sb.append('\n'); } String[] entries = sb.toString().replace("\u2013", "-").replace("\u2014", "--").replace("\u2015", "--") @@ -269,14 +253,14 @@ else if ("ID".equals(lab)) { BibEntry b = new BibEntry(DEFAULT_BIBTEXENTRY_ID, type); // id assumes an existing database so don't // Remove empty fields: - List toRemove = new ArrayList<>(); + List toRemove = new ArrayList<>(); for (Map.Entry key : hm.entrySet()) { String content = key.getValue(); if ((content == null) || content.trim().isEmpty()) { toRemove.add(key.getKey()); } } - for (Object aToRemove : toRemove) { + for (String aToRemove : toRemove) { hm.remove(aToRemove); } @@ -287,7 +271,7 @@ else if ("ID".equals(lab)) { } - return bibitems; + return new ParserResult(bibitems); } } diff --git a/src/main/java/net/sf/jabref/importer/fileformat/SilverPlatterImporter.java b/src/main/java/net/sf/jabref/importer/fileformat/SilverPlatterImporter.java index 0123094f870..1a0b4357399 100644 --- a/src/main/java/net/sf/jabref/importer/fileformat/SilverPlatterImporter.java +++ b/src/main/java/net/sf/jabref/importer/fileformat/SilverPlatterImporter.java @@ -17,15 +17,13 @@ import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Pattern; -import net.sf.jabref.importer.ImportFormatReader; -import net.sf.jabref.importer.OutputPrinter; +import net.sf.jabref.importer.ParserResult; import net.sf.jabref.model.entry.AuthorList; import net.sf.jabref.model.entry.BibEntry; @@ -37,185 +35,170 @@ public class SilverPlatterImporter extends ImportFormat { private static final Pattern START_PATTERN = Pattern.compile("Record.*INSPEC.*"); - - /** - * Return the name of this import format. - */ @Override public String getFormatName() { return "SilverPlatter"; } - /* - * (non-Javadoc) - * @see net.sf.jabref.imports.ImportFormat#getCLIId() - */ @Override - public String getCLIId() { - return "silverplatter"; + public List getExtensions() { + return null; } - /** - * Check whether the source is in the correct format for this importer. - */ @Override - public boolean isRecognizedFormat(InputStream stream) throws IOException { - try (BufferedReader in = new BufferedReader(ImportFormatReader.getReaderDefaultEncoding(stream))) { - - // This format is very similar to Inspec, so we have a two-fold strategy: - // If we see the flag signaling that it is an Inspec file, return false. - // This flag should appear above the first entry and prevent us from - // accepting the Inspec format. Then we look for the title entry. - String str; - while ((str = in.readLine()) != null) { - - if (START_PATTERN.matcher(str).find()) { - return false; // This is an Inspec file, so return false. - } + public String getDescription() { + return null; + } - if ((str.length() >= 5) && "TI: ".equals(str.substring(0, 5))) { - return true; - } + @Override + public boolean isRecognizedFormat(BufferedReader reader) throws IOException { + // This format is very similar to Inspec, so we have a two-fold strategy: + // If we see the flag signaling that it is an Inspec file, return false. + // This flag should appear above the first entry and prevent us from + // accepting the Inspec format. Then we look for the title entry. + String str; + while ((str = reader.readLine()) != null) { + + if (START_PATTERN.matcher(str).find()) { + return false; // This is an Inspec file, so return false. + } + + if ((str.length() >= 5) && "TI: ".equals(str.substring(0, 5))) { + return true; } } return false; } - /** - * Parse the entries in the source, and return a List of BibEntry - * objects. - */ @Override - public List importEntries(InputStream stream, OutputPrinter status) throws IOException { + public ParserResult importDatabase(BufferedReader reader) throws IOException { List bibitems = new ArrayList<>(); - try (BufferedReader in = new BufferedReader(ImportFormatReader.getReaderDefaultEncoding(stream))) { - boolean isChapter = false; - String str; - StringBuilder sb = new StringBuilder(); - while ((str = in.readLine()) != null) { - if (str.length() < 2) { - sb.append("__::__").append(str); - } else { - sb.append("__NEWFIELD__").append(str); - } + boolean isChapter = false; + String str; + StringBuilder sb = new StringBuilder(); + while ((str = reader.readLine()) != null) { + if (str.length() < 2) { + sb.append("__::__").append(str); + } else { + sb.append("__NEWFIELD__").append(str); + } + } + String[] entries = sb.toString().split("__::__"); + String type = ""; + Map h = new HashMap<>(); + for (String entry : entries) { + if (entry.trim().length() < 6) { + continue; } - String[] entries = sb.toString().split("__::__"); - String type = ""; - Map h = new HashMap<>(); - for (String entry : entries) { - if (entry.trim().length() < 6) { + h.clear(); + String[] fields = entry.split("__NEWFIELD__"); + for (String field : fields) { + if (field.length() < 6) { continue; } - h.clear(); - String[] fields = entry.split("__NEWFIELD__"); - for (String field : fields) { - if (field.length() < 6) { - continue; + String f3 = field.substring(0, 2); + String frest = field.substring(5); + if ("TI".equals(f3)) { + h.put("title", frest); + } else if ("AU".equals(f3)) { + if (frest.trim().endsWith("(ed)")) { + String ed = frest.trim(); + ed = ed.substring(0, ed.length() - 4); + h.put("editor", + AuthorList.fixAuthorLastNameFirst(ed.replace(",-", ", ").replace(";", " and "))); + } else { + h.put("author", + AuthorList.fixAuthorLastNameFirst(frest.replace(",-", ", ").replace(";", " and "))); } - String f3 = field.substring(0, 2); - String frest = field.substring(5); - if ("TI".equals(f3)) { - h.put("title", frest); - } else if ("AU".equals(f3)) { - if (frest.trim().endsWith("(ed)")) { - String ed = frest.trim(); - ed = ed.substring(0, ed.length() - 4); - h.put("editor", - AuthorList.fixAuthorLastNameFirst(ed.replace(",-", ", ").replace(";", " and "))); - } else { - h.put("author", - AuthorList.fixAuthorLastNameFirst(frest.replace(",-", ", ").replace(";", " and "))); - } - } else if ("AB".equals(f3)) { - h.put("abstract", frest); - } else if ("DE".equals(f3)) { - String kw = frest.replace("-;", ",").toLowerCase(); - h.put("keywords", kw.substring(0, kw.length() - 1)); - } else if ("SO".equals(f3)) { - int m = frest.indexOf('.'); - if (m >= 0) { - String jr = frest.substring(0, m); - h.put("journal", jr.replace("-", " ")); + } else if ("AB".equals(f3)) { + h.put("abstract", frest); + } else if ("DE".equals(f3)) { + String kw = frest.replace("-;", ",").toLowerCase(); + h.put("keywords", kw.substring(0, kw.length() - 1)); + } else if ("SO".equals(f3)) { + int m = frest.indexOf('.'); + if (m >= 0) { + String jr = frest.substring(0, m); + h.put("journal", jr.replace("-", " ")); + frest = frest.substring(m); + m = frest.indexOf(';'); + if (m >= 5) { + String yr = frest.substring(m - 5, m).trim(); + h.put("year", yr); frest = frest.substring(m); - m = frest.indexOf(';'); - if (m >= 5) { - String yr = frest.substring(m - 5, m).trim(); - h.put("year", yr); - frest = frest.substring(m); - m = frest.indexOf(':'); - int issueIndex = frest.indexOf('('); - int endIssueIndex = frest.indexOf(')'); - if (m >= 0) { - String pg = frest.substring(m + 1).trim(); - h.put("pages", pg); - h.put("volume", frest.substring(1, issueIndex).trim()); - h.put("issue", frest.substring(issueIndex + 1, endIssueIndex).trim()); - } + m = frest.indexOf(':'); + int issueIndex = frest.indexOf('('); + int endIssueIndex = frest.indexOf(')'); + if (m >= 0) { + String pg = frest.substring(m + 1).trim(); + h.put("pages", pg); + h.put("volume", frest.substring(1, issueIndex).trim()); + h.put("issue", frest.substring(issueIndex + 1, endIssueIndex).trim()); } } - } else if ("PB".equals(f3)) { - int m = frest.indexOf(':'); - if (m >= 0) { - String jr = frest.substring(0, m); - h.put("publisher", jr.replace("-", " ").trim()); - frest = frest.substring(m); - m = frest.indexOf(", "); - if ((m + 2) < frest.length()) { - String yr = frest.substring(m + 2).trim(); - try { - Integer.parseInt(yr); - h.put("year", yr); - } catch (NumberFormatException ex) { - // Let's assume that this wasn't a number, since it - // couldn't be parsed as an integer. - } - + } + } else if ("PB".equals(f3)) { + int m = frest.indexOf(':'); + if (m >= 0) { + String jr = frest.substring(0, m); + h.put("publisher", jr.replace("-", " ").trim()); + frest = frest.substring(m); + m = frest.indexOf(", "); + if ((m + 2) < frest.length()) { + String yr = frest.substring(m + 2).trim(); + try { + Integer.parseInt(yr); + h.put("year", yr); + } catch (NumberFormatException ex) { + // Let's assume that this wasn't a number, since it + // couldn't be parsed as an integer. } } - } else if ("AF".equals(f3)) { - h.put("school", frest.trim()); - - } else if ("DT".equals(f3)) { - frest = frest.trim(); - if ("Monograph".equals(frest)) { - type = "book"; - } else if (frest.startsWith("Dissertation")) { - type = "phdthesis"; - } else if (frest.toLowerCase().contains("journal")) { - type = "article"; - } else if ("Contribution".equals(frest) || "Chapter".equals(frest)) { - type = "incollection"; - // This entry type contains page numbers and booktitle in the - // title field. - isChapter = true; - } else { - type = frest.replace(" ", ""); - } + + } + } else if ("AF".equals(f3)) { + h.put("school", frest.trim()); + + } else if ("DT".equals(f3)) { + frest = frest.trim(); + if ("Monograph".equals(frest)) { + type = "book"; + } else if (frest.startsWith("Dissertation")) { + type = "phdthesis"; + } else if (frest.toLowerCase().contains("journal")) { + type = "article"; + } else if ("Contribution".equals(frest) || "Chapter".equals(frest)) { + type = "incollection"; + // This entry type contains page numbers and booktitle in the + // title field. + isChapter = true; + } else { + type = frest.replace(" ", ""); } } + } - if (isChapter) { - Object titleO = h.get("title"); - if (titleO != null) { - String title = ((String) titleO).trim(); - int inPos = title.indexOf("\" in "); - if (inPos > 1) { - h.put("title", title.substring(0, inPos)); - } + if (isChapter) { + Object titleO = h.get("title"); + if (titleO != null) { + String title = ((String) titleO).trim(); + int inPos = title.indexOf("\" in "); + if (inPos > 1) { + h.put("title", title.substring(0, inPos)); } - } - BibEntry b = new BibEntry(DEFAULT_BIBTEXENTRY_ID, type); // id assumes an existing database so don't - // create one here - b.setField(h); + } - bibitems.add(b); + BibEntry b = new BibEntry(DEFAULT_BIBTEXENTRY_ID, type); // id assumes an existing database so don't + // create one here + b.setField(h); + + bibitems.add(b); - } } - return bibitems; + return new ParserResult(bibitems); } } diff --git a/src/main/java/net/sf/jabref/logic/msbib/MSBibDatabase.java b/src/main/java/net/sf/jabref/logic/msbib/MSBibDatabase.java index c873cf8b20e..91bfb3e90a1 100644 --- a/src/main/java/net/sf/jabref/logic/msbib/MSBibDatabase.java +++ b/src/main/java/net/sf/jabref/logic/msbib/MSBibDatabase.java @@ -15,8 +15,8 @@ */ package net.sf.jabref.logic.msbib; +import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; @@ -36,6 +36,7 @@ import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; import org.xml.sax.SAXException; /** @@ -61,14 +62,14 @@ public MSBibDatabase(BibDatabase database, List entries) { } } - public List importEntries(InputStream stream) { + public List importEntries(BufferedReader reader) { entries = new HashSet<>(); Document inputDocument; try { DocumentBuilder documentBuilder = DocumentBuilderFactory. newInstance(). newDocumentBuilder(); - inputDocument = documentBuilder.parse(stream); + inputDocument = documentBuilder.parse(new InputSource(reader)); } catch (ParserConfigurationException | SAXException | IOException e) { LOGGER.warn("Could not parse document", e); return Collections.emptyList(); diff --git a/src/main/java/net/sf/jabref/logic/xmp/XMPUtil.java b/src/main/java/net/sf/jabref/logic/xmp/XMPUtil.java index 1a4d690e020..7c1017d105b 100644 --- a/src/main/java/net/sf/jabref/logic/xmp/XMPUtil.java +++ b/src/main/java/net/sf/jabref/logic/xmp/XMPUtil.java @@ -145,9 +145,13 @@ public static List readXMP(File file) throws IOException { return result; } - public static PDDocument loadWithAutomaticDecryption(InputStream inputStream) - throws IOException { + public static PDDocument loadWithAutomaticDecryption(Path filePath) throws IOException { + return loadWithAutomaticDecryption(new FileInputStream(filePath.toFile())); + } + + public static PDDocument loadWithAutomaticDecryption(InputStream inputStream) throws IOException { PDDocument doc = PDDocument.load(inputStream); + if (doc.isEncrypted()) { // try the empty string as user password StandardDecryptionMaterial sdm = new StandardDecryptionMaterial(""); @@ -230,6 +234,10 @@ public static List readXMP(InputStream inputStream) return result; } + public static Collection readXMP(Path filePath) throws IOException { + return readXMP(filePath.toFile()); + } + /** * Helper function for retrieving a BibEntry from the * PDDocumentInformation in a PDF file. diff --git a/src/main/java/net/sf/jabref/pdfimport/PdfImporter.java b/src/main/java/net/sf/jabref/pdfimport/PdfImporter.java index eaa9c015d79..c11a98357ff 100644 --- a/src/main/java/net/sf/jabref/pdfimport/PdfImporter.java +++ b/src/main/java/net/sf/jabref/pdfimport/PdfImporter.java @@ -16,9 +16,7 @@ package net.sf.jabref.pdfimport; import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; @@ -40,6 +38,7 @@ import net.sf.jabref.gui.entryeditor.EntryEditor; import net.sf.jabref.gui.maintable.MainTable; import net.sf.jabref.gui.undo.UndoableInsertEntry; +import net.sf.jabref.importer.ParserResult; import net.sf.jabref.importer.fileformat.PdfContentImporter; import net.sf.jabref.importer.fileformat.PdfXmpImporter; import net.sf.jabref.logic.l10n.Localization; @@ -185,15 +184,16 @@ private List importPdfFiles(List fileNames) { } private void doXMPImport(String fileName, List res) { - BibEntry entry; List localRes = new ArrayList<>(); PdfXmpImporter importer = new PdfXmpImporter(); - try (InputStream in = new FileInputStream(fileName)) { - localRes.addAll(importer.importEntries(in, frame)); - } catch (IOException ex) { - LOGGER.warn("Cannot import entries", ex); + Path filePath = Paths.get(fileName); + ParserResult result = importer.importDatabase(filePath, Globals.prefs.getDefaultEncoding()); + if (result.hasWarnings()) { + frame.showMessage(result.getErrorMessage()); } + localRes.addAll(result.getDatabase().getEntries()); + BibEntry entry; if (localRes.isEmpty()) { // import failed -> generate default entry LOGGER.info("Import failed"); @@ -229,29 +229,24 @@ private BibEntry createNewBlankEntry(String fileName) { } private void doContentImport(String fileName, List res) { - File file = new File(fileName); - BibEntry entry; - try (InputStream in = new FileInputStream(file)) { - PdfContentImporter contentImporter = new PdfContentImporter(); - List localRes = contentImporter.importEntries(in, frame); - - if (localRes.isEmpty()) { - // import failed -> generate default entry - entry = createNewBlankEntry(fileName); - res.add(entry); - return; - } - // only one entry is imported - entry = localRes.get(0); - } catch (IOException e) { + PdfContentImporter contentImporter = new PdfContentImporter(); + Path filePath = Paths.get(fileName); + ParserResult result = contentImporter.importDatabase(filePath, Globals.prefs.getDefaultEncoding()); + if (result.hasWarnings()) { + frame.showMessage(result.getErrorMessage()); + } + + if (!result.getDatabase().hasEntries()) { // import failed -> generate default entry - LOGGER.info("Import failed", e); - entry = createNewBlankEntry(fileName); + BibEntry entry = createNewBlankEntry(fileName); res.add(entry); return; } + // only one entry is imported + BibEntry entry = result.getDatabase().getEntries().get(0); + // insert entry to database and link file panel.getDatabase().insertEntry(entry); panel.markBaseChanged(); @@ -311,22 +306,4 @@ private BibEntry createNewEntry() { } return null; } - - public MainTable getEntryTable() { - return entryTable; - } - - public void setEntryTable(MainTable entryTable) { - this.entryTable = entryTable; - } - - public int getDropRow() { - return dropRow; - } - - public void setDropRow(int dropRow) { - this.dropRow = dropRow; - } - - } diff --git a/src/main/resources/l10n/JabRef_da.properties b/src/main/resources/l10n/JabRef_da.properties index e6fa85d381b..3803774f1fa 100644 --- a/src/main/resources/l10n/JabRef_da.properties +++ b/src/main/resources/l10n/JabRef_da.properties @@ -1228,7 +1228,6 @@ Searching...= You_have_selected_more_than_%0_entries_for_download._Some_web_sites_might_block_you_if_you_make_too_many_rapid_downloads._Do_you_want_to_continue?= Confirm_selection= Unknown_DOI\:_'%0'.= -Get_BibTeX_entry_from_DOI= Prefer_converting_subscripts_and_superscripts_to_equations_rather_than_text= Add_{}_to_specified_title_words_on_search_to_keep_the_correct_case= Import_conversions= @@ -1686,3 +1685,5 @@ Message= Get_fulltext= Download_from_URL= + +Decryption_not_supported.= diff --git a/src/main/resources/l10n/JabRef_de.properties b/src/main/resources/l10n/JabRef_de.properties index e3db4c5d1f8..b4d9f818700 100644 --- a/src/main/resources/l10n/JabRef_de.properties +++ b/src/main/resources/l10n/JabRef_de.properties @@ -1936,7 +1936,6 @@ Searching...=Suche_läuft... You_have_selected_more_than_%0_entries_for_download._Some_web_sites_might_block_you_if_you_make_too_many_rapid_downloads._Do_you_want_to_continue?=Sie_haben_mehr_als_%0_Einträge_zum_Download_ausgewählt._Einige_Webseiten_könnten_zu_viele_Downloads_blockieren._Möchten_Sie_fortfahren? Confirm_selection=Auswahl_bestätigen Unknown_DOI\:_'%0'.=Unbekannte_DOI\:_'0%'. -Get_BibTeX_entry_from_DOI=BibTeX-Eintrag_aus_DOI_erstellen Prefer_converting_subscripts_and_superscripts_to_equations_rather_than_text=Hoch-_und_tiefgestellte_Zeichen_in_eine_Gleichung_konvertieren_und_nicht_als_Text_darstellen Add_{}_to_specified_title_words_on_search_to_keep_the_correct_case=Nach_der_Suche_{}_zu_festgesetzten_Titelworten_hinzufügen,_um_Groß-/Kleinschreibung_beizubehalten Import_conversions=Konvertierungen_importieren @@ -2402,3 +2401,5 @@ Message= Get_fulltext= Download_from_URL= + +Decryption_not_supported.= diff --git a/src/main/resources/l10n/JabRef_en.properties b/src/main/resources/l10n/JabRef_en.properties index a12aa039044..af02f181a46 100644 --- a/src/main/resources/l10n/JabRef_en.properties +++ b/src/main/resources/l10n/JabRef_en.properties @@ -1835,7 +1835,6 @@ Searching...=Searching... You_have_selected_more_than_%0_entries_for_download._Some_web_sites_might_block_you_if_you_make_too_many_rapid_downloads._Do_you_want_to_continue?=You_have_selected_more_than_%0_entries_for_download._Some_web_sites_might_block_you_if_you_make_too_many_rapid_downloads._Do_you_want_to_continue? Confirm_selection=Confirm_selection Unknown_DOI\:_'%0'.=Unknown_DOI\:_'%0'. -Get_BibTeX_entry_from_DOI=Get_BibTeX_entry_from_DOI Prefer_converting_subscripts_and_superscripts_to_equations_rather_than_text=Prefer_converting_subscripts_and_superscripts_to_equations_rather_than_text Add_{}_to_specified_title_words_on_search_to_keep_the_correct_case=Add_{}_to_specified_title_words_on_search_to_keep_the_correct_case Import_conversions=Import_conversions @@ -2258,3 +2257,5 @@ Copied_version_to_clipboard=Copied_version_to_clipboard BibTeX_key=BibTeX_key Message=Message + +Decryption_not_supported.=Decryption_not_supported. diff --git a/src/main/resources/l10n/JabRef_es.properties b/src/main/resources/l10n/JabRef_es.properties index a0d9f6d049e..02d240878c7 100644 --- a/src/main/resources/l10n/JabRef_es.properties +++ b/src/main/resources/l10n/JabRef_es.properties @@ -1136,7 +1136,6 @@ Searching...=Buscando... You_have_selected_more_than_%0_entries_for_download._Some_web_sites_might_block_you_if_you_make_too_many_rapid_downloads._Do_you_want_to_continue?=Ha_seleccionado_más_de_%0_entradas_para_descargar._Algunos_sitios_web_podrían_bloquearle_si_hace_demasiadas_descargas._¿Desea_continuar? Confirm_selection=Confirmar_selección Unknown_DOI\:_'%0'.=DOI_desconocido\:_'%0'. -Get_BibTeX_entry_from_DOI=Obtener_entrada_BibTeX_desde_DOI Prefer_converting_subscripts_and_superscripts_to_equations_rather_than_text=Preferir_convertir_subíndices_y_superíndices_a_ecuaciónantes_que_a_texto Add_{}_to_specified_title_words_on_search_to_keep_the_correct_case=Añadir_{}_para_especificar_las_palabras_del_título_para_mantener_mayúsculas/minúsculas_correctamente Import_conversions=Importar_conversiones @@ -1587,3 +1586,5 @@ Message= Get_fulltext= Download_from_URL= + +Decryption_not_supported.= diff --git a/src/main/resources/l10n/JabRef_fa.properties b/src/main/resources/l10n/JabRef_fa.properties index cbec249fc1b..7a87a29200c 100644 --- a/src/main/resources/l10n/JabRef_fa.properties +++ b/src/main/resources/l10n/JabRef_fa.properties @@ -1890,7 +1890,6 @@ Searching...= You_have_selected_more_than_%0_entries_for_download._Some_web_sites_might_block_you_if_you_make_too_many_rapid_downloads._Do_you_want_to_continue?= Confirm_selection= Unknown_DOI\:_'%0'.= -Get_BibTeX_entry_from_DOI= Prefer_converting_subscripts_and_superscripts_to_equations_rather_than_text= Add_{}_to_specified_title_words_on_search_to_keep_the_correct_case= Import_conversions= @@ -2373,3 +2372,5 @@ Message= Get_fulltext= Download_from_URL= + +Decryption_not_supported.= diff --git a/src/main/resources/l10n/JabRef_fr.properties b/src/main/resources/l10n/JabRef_fr.properties index 161780ace8c..7fb66544943 100644 --- a/src/main/resources/l10n/JabRef_fr.properties +++ b/src/main/resources/l10n/JabRef_fr.properties @@ -1168,7 +1168,6 @@ Searching...=Recherche... You_have_selected_more_than_%0_entries_for_download._Some_web_sites_might_block_you_if_you_make_too_many_rapid_downloads._Do_you_want_to_continue?=Vous_avez_sélectionné_plus_de_%0_entrées_à_télécharger._Certains_sites_web_pourraient_vous_bloquer_si_vous_effectuez_de_trop_nombreux_et_rapides_téléchargements._Voulez-vous_continuer? Confirm_selection=Confirmez_la_sélection Unknown_DOI\:_'%0'.=DOI_inconnu_\:_'%0'. -Get_BibTeX_entry_from_DOI=Obtenir_l'entrée_BibTeX_à_partir_du_DOI Prefer_converting_subscripts_and_superscripts_to_equations_rather_than_text=Conversion_des_indices_et_exposants_en_équations_plutôt_qu'en_texte Add_{}_to_specified_title_words_on_search_to_keep_the_correct_case=Ajouter_{}_aux_mots_du_titre_spécifiés_lors_d'une_recherche_pour_préserver_la_casse_correcte Import_conversions=Importer_les_conversions @@ -1631,3 +1630,5 @@ Message=Message Get_fulltext=Obtenir_le_document Download_from_URL=Télécharger_depuis_l'URL + +Decryption_not_supported.= diff --git a/src/main/resources/l10n/JabRef_in.properties b/src/main/resources/l10n/JabRef_in.properties index 815cd8b7c09..dd570bfda3e 100644 --- a/src/main/resources/l10n/JabRef_in.properties +++ b/src/main/resources/l10n/JabRef_in.properties @@ -1145,7 +1145,6 @@ Searching...=Sedang_mencari... You_have_selected_more_than_%0_entries_for_download._Some_web_sites_might_block_you_if_you_make_too_many_rapid_downloads._Do_you_want_to_continue?=Anda_memilih_lebih_dari_%0_entri._Sejumlah_situs_web_akan_memblokir_anda_kalau_melakukan_terlalu_banyak_pemuatturunan_dengan_cepat._Apa_mau_teruskan? Confirm_selection=Mengkonfirmasi_pilihan Unknown_DOI\:_'%0'.=DOI_tidak_dikenal\:_'%0'. -Get_BibTeX_entry_from_DOI=Dapatkan_entri_BibTeX_dari_DOI Prefer_converting_subscripts_and_superscripts_to_equations_rather_than_text= Add_{}_to_specified_title_words_on_search_to_keep_the_correct_case= Import_conversions=Impor_konversi @@ -1606,3 +1605,5 @@ Message= Get_fulltext= Download_from_URL= + +Decryption_not_supported.= diff --git a/src/main/resources/l10n/JabRef_it.properties b/src/main/resources/l10n/JabRef_it.properties index ab79f6d7876..4a968c509d1 100644 --- a/src/main/resources/l10n/JabRef_it.properties +++ b/src/main/resources/l10n/JabRef_it.properties @@ -1246,7 +1246,6 @@ Searching...=Ricerca_in_corso... You_have_selected_more_than_%0_entries_for_download._Some_web_sites_might_block_you_if_you_make_too_many_rapid_downloads._Do_you_want_to_continue?=Sono_state_selezionate_pi\u00f9_di_%0_voci_da_scaricare._Alcuni_siti_potrebbero_bloccare_la_connessione_se_si_eseguono_scaricamenti_troppo_numerosi_e_rapidi._Continuare? Confirm_selection=Conferma_la_selezione Unknown_DOI\:_'%0'.=DOI_sconosciuto\:_'%0' -Get_BibTeX_entry_from_DOI=Recupera_la_voce_BibTeX_dal_DOI Prefer_converting_subscripts_and_superscripts_to_equations_rather_than_text=Conversione_di_apici_e_pedici_in_equazioni_piuttosto_che_in_testo. Add_{}_to_specified_title_words_on_search_to_keep_the_correct_case=Aggiungere_{}_alle_parole_del_titolo_specificate_per_mantenere_la_corretta_capitalizzazione_nella_ricerca. Import_conversions=Importare_le_conversioni @@ -1706,3 +1705,5 @@ Message= Get_fulltext= Download_from_URL= + +Decryption_not_supported.= diff --git a/src/main/resources/l10n/JabRef_ja.properties b/src/main/resources/l10n/JabRef_ja.properties index d266fbb4c57..57f75f7881a 100644 --- a/src/main/resources/l10n/JabRef_ja.properties +++ b/src/main/resources/l10n/JabRef_ja.properties @@ -1917,7 +1917,6 @@ Searching...=検索中... You_have_selected_more_than_%0_entries_for_download._Some_web_sites_might_block_you_if_you_make_too_many_rapid_downloads._Do_you_want_to_continue?=ダウンロードする項目を%0個以上選択しました。あまりに多くのダウンロードを急に行うと、其れをブロックするウェブサイトもあります。続けますか? Confirm_selection=選択範囲を確認 Unknown_DOI\:_'%0'.=「%0」は既知のDOIではありません -Get_BibTeX_entry_from_DOI=DOIからBibTeX項目を取得 Prefer_converting_subscripts_and_superscripts_to_equations_rather_than_text=上添字及び下添字をテキストではなく数式に変換する Add_{}_to_specified_title_words_on_search_to_keep_the_correct_case=大小文字を正しく維持するため、検索中に指定したタイトル語に{}を付け加える Import_conversions=読み込み時変換 @@ -2351,3 +2350,5 @@ Message=メッセージ Get_fulltext=フルテキストを得る Download_from_URL=URLからダウンロード + +Decryption_not_supported.= diff --git a/src/main/resources/l10n/JabRef_nl.properties b/src/main/resources/l10n/JabRef_nl.properties index 8493ea10330..1f60cf878dc 100644 --- a/src/main/resources/l10n/JabRef_nl.properties +++ b/src/main/resources/l10n/JabRef_nl.properties @@ -1919,7 +1919,6 @@ Searching...= You_have_selected_more_than_%0_entries_for_download._Some_web_sites_might_block_you_if_you_make_too_many_rapid_downloads._Do_you_want_to_continue?= Confirm_selection= Unknown_DOI\:_'%0'.=Unbekende_DOI\:_'%0'. -Get_BibTeX_entry_from_DOI= Prefer_converting_subscripts_and_superscripts_to_equations_rather_than_text= Add_{}_to_specified_title_words_on_search_to_keep_the_correct_case= Import_conversions= @@ -2382,3 +2381,5 @@ Message= Get_fulltext= Download_from_URL= + +Decryption_not_supported.= diff --git a/src/main/resources/l10n/JabRef_no.properties b/src/main/resources/l10n/JabRef_no.properties index 08ee7d9c061..c71a623efe3 100644 --- a/src/main/resources/l10n/JabRef_no.properties +++ b/src/main/resources/l10n/JabRef_no.properties @@ -2314,7 +2314,6 @@ Searching...= You_have_selected_more_than_%0_entries_for_download._Some_web_sites_might_block_you_if_you_make_too_many_rapid_downloads._Do_you_want_to_continue?= Confirm_selection= Unknown_DOI\:_'%0'.= -Get_BibTeX_entry_from_DOI= Prefer_converting_subscripts_and_superscripts_to_equations_rather_than_text= Add_{}_to_specified_title_words_on_search_to_keep_the_correct_case= Import_conversions= @@ -2778,3 +2777,5 @@ Message= Get_fulltext= Download_from_URL= + +Decryption_not_supported.= diff --git a/src/main/resources/l10n/JabRef_pt_BR.properties b/src/main/resources/l10n/JabRef_pt_BR.properties index fe35567b5eb..39cf57b780c 100644 --- a/src/main/resources/l10n/JabRef_pt_BR.properties +++ b/src/main/resources/l10n/JabRef_pt_BR.properties @@ -1143,7 +1143,6 @@ Searching...=Buscando... You_have_selected_more_than_%0_entries_for_download._Some_web_sites_might_block_you_if_you_make_too_many_rapid_downloads._Do_you_want_to_continue?=Você_selecionou_mais_de_%0_entradas_para_download._Alguns_sites_podem_bloquear_seu_acesso_se_você_fizer_muitos_downloads_num_período_curto_de_tempo._Deseja_continuar_mesmo_assim? Confirm_selection=Confirmar_seleção Unknown_DOI\:_'%0'.=DOI_desconhecido\:_'%0'. -Get_BibTeX_entry_from_DOI=Obter_referência_a_partir_do_DOI. Prefer_converting_subscripts_and_superscripts_to_equations_rather_than_text=Dar_preferência_à_conversão_de_subscritos_sobrescritos_para_equações_ao_invés_de_textos Add_{}_to_specified_title_words_on_search_to_keep_the_correct_case=Adicione_{}_às_palavras_do_título_na_busca_para_manter_maiúsculas_minúsculas Import_conversions=Importar_conversões @@ -1600,3 +1599,5 @@ Message= Get_fulltext= Download_from_URL= + +Decryption_not_supported.= diff --git a/src/main/resources/l10n/JabRef_ru.properties b/src/main/resources/l10n/JabRef_ru.properties index 3fc1069a842..153a9f54630 100644 --- a/src/main/resources/l10n/JabRef_ru.properties +++ b/src/main/resources/l10n/JabRef_ru.properties @@ -1887,7 +1887,6 @@ Searching...=Выполняется_поиск... You_have_selected_more_than_%0_entries_for_download._Some_web_sites_might_block_you_if_you_make_too_many_rapid_downloads._Do_you_want_to_continue?=Выбрано_более_%0_записей_для_загрузки._Возможна_блокировка_пользователя_некоторыми_веб-сайтами_при_большом_числе_быстрых_загрузок._Продолжить? Confirm_selection=Подтвердить_выбор Unknown_DOI\:_'%0'.=Неизвестный_DOI\:_'%0'. -Get_BibTeX_entry_from_DOI=Получить_запись_BibTeX_из_DOI Prefer_converting_subscripts_and_superscripts_to_equations_rather_than_text=Преобразовывать_надстрочный_и_подстрочный_индекс_в_формулу_вместо_текста Add_{}_to_specified_title_words_on_search_to_keep_the_correct_case=Добавлять_{}_к_указанным_заглавным_словам_при_поиске_для_соблюдения_правильности_регистра Import_conversions=Импорт_преобразований @@ -2350,3 +2349,5 @@ Message= Get_fulltext= Download_from_URL= + +Decryption_not_supported.= diff --git a/src/main/resources/l10n/JabRef_sv.properties b/src/main/resources/l10n/JabRef_sv.properties index 9c1b7e41d43..1563e3352d4 100644 --- a/src/main/resources/l10n/JabRef_sv.properties +++ b/src/main/resources/l10n/JabRef_sv.properties @@ -533,7 +533,6 @@ Generate_now=Generera_nu Generated_BibTeX_key_for=Genererade_BibTeX-nycklar_för Generating_BibTeX_key_for=Genererar_BibTeX-nycklar_för Get_BibTeX_data_from_DOI=Hämta_BibTeX-data_från_DOI -Get_BibTeX_entry_from_DOI=Hämta_BibTeX-post_från_DOI Get_BibTeX_entry_from_DiVA=Hämta_BibTeX-post_från_DIVA Grab= Gray_out_entries_not_in_group_selection=Skugga_poster_som_inte_är_med_i_en_grupp @@ -1545,3 +1544,5 @@ Message= Get_fulltext= Download_from_URL= + +Decryption_not_supported.= diff --git a/src/main/resources/l10n/JabRef_tr.properties b/src/main/resources/l10n/JabRef_tr.properties index 0fdeac046d5..63b7b4fde80 100644 --- a/src/main/resources/l10n/JabRef_tr.properties +++ b/src/main/resources/l10n/JabRef_tr.properties @@ -1165,7 +1165,6 @@ Searching...=Arıyor... You_have_selected_more_than_%0_entries_for_download._Some_web_sites_might_block_you_if_you_make_too_many_rapid_downloads._Do_you_want_to_continue?=İndirmek_için_%0'dan_fazla_girdi_seçtiniz._Çok_sayıda_hızlı_indirme_yaparsanız_bazı_web_siteleri_sizi_bloke_edebilir._Devam_etmek_istiyor_musunuz? Confirm_selection=Seçimi_onayla Unknown_DOI\:_'%0'.=Bilinmeyen_DOI\:_'%0'. -Get_BibTeX_entry_from_DOI=BibTeX_girdisini_DOI'den_al Prefer_converting_subscripts_and_superscripts_to_equations_rather_than_text=Alt_ve_üstsimgeleri_metinden_ziyade_denklemlere_dönüştürmeyi_tercih_et Add_{}_to_specified_title_words_on_search_to_keep_the_correct_case=Aramada_doğru_küçük_büyük_harf_seçimi_için_belirli_başlık_sözcüklerine_{}_ekleyin Import_conversions=Dönüşümleri_içe_aktar @@ -1619,3 +1618,5 @@ Message=Mesaj Get_fulltext= Download_from_URL= + +Decryption_not_supported.= diff --git a/src/main/resources/l10n/JabRef_vi.properties b/src/main/resources/l10n/JabRef_vi.properties index 1644fb3422e..1dad633d990 100644 --- a/src/main/resources/l10n/JabRef_vi.properties +++ b/src/main/resources/l10n/JabRef_vi.properties @@ -1913,7 +1913,6 @@ Searching...= You_have_selected_more_than_%0_entries_for_download._Some_web_sites_might_block_you_if_you_make_too_many_rapid_downloads._Do_you_want_to_continue?= Confirm_selection= Unknown_DOI\:_'%0'.= -Get_BibTeX_entry_from_DOI= Prefer_converting_subscripts_and_superscripts_to_equations_rather_than_text= Add_{}_to_specified_title_words_on_search_to_keep_the_correct_case= Import_conversions= @@ -2374,3 +2373,5 @@ Message= Get_fulltext= Download_from_URL= + +Decryption_not_supported.= diff --git a/src/main/resources/l10n/JabRef_zh.properties b/src/main/resources/l10n/JabRef_zh.properties index 5982ba1afbc..30c6ac86d07 100644 --- a/src/main/resources/l10n/JabRef_zh.properties +++ b/src/main/resources/l10n/JabRef_zh.properties @@ -1219,7 +1219,6 @@ Searching...=正在搜索... You_have_selected_more_than_%0_entries_for_download._Some_web_sites_might_block_you_if_you_make_too_many_rapid_downloads._Do_you_want_to_continue?= Confirm_selection=确认选择 Unknown_DOI\:_'%0'.= -Get_BibTeX_entry_from_DOI=从_DOI_中获取_BibTeX_记录 Prefer_converting_subscripts_and_superscripts_to_equations_rather_than_text=将上下标转换成公式而不是文本 Add_{}_to_specified_title_words_on_search_to_keep_the_correct_case=搜索时为特殊的标题单词添加_{}_以保证大小写正确 Import_conversions=导入约定 @@ -1613,3 +1612,5 @@ Message= Get_fulltext= Download_from_URL= + +Decryption_not_supported.= diff --git a/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java b/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java index df2f79f5023..bfaa1c0535b 100644 --- a/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java +++ b/src/test/java/net/sf/jabref/exporter/BibDatabaseWriterTest.java @@ -1,11 +1,11 @@ package net.sf.jabref.exporter; -import java.io.File; import java.io.IOException; import java.io.StringWriter; import java.io.Writer; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; import java.util.Collections; @@ -16,9 +16,9 @@ import net.sf.jabref.Globals; import net.sf.jabref.JabRefPreferences; import net.sf.jabref.MetaData; -import net.sf.jabref.importer.ImportFormatReader; import net.sf.jabref.importer.ParserResult; import net.sf.jabref.importer.fileformat.BibtexParser; +import net.sf.jabref.importer.fileformat.ImportFormat; import net.sf.jabref.importer.fileformat.ParseException; import net.sf.jabref.logic.cleanup.FieldFormatterCleanup; import net.sf.jabref.logic.config.SaveOrderConfig; @@ -277,9 +277,9 @@ public void writeEntryWithCustomizedTypeAlsoWritesTypeDeclaration() throws IOExc @Test public void roundtrip() throws IOException { - File testBibtexFile = Paths.get("src/test/resources/testbib/complex.bib").toFile(); + Path testBibtexFile = Paths.get("src/test/resources/testbib/complex.bib"); Charset encoding = StandardCharsets.UTF_8; - ParserResult result = BibtexParser.parse(ImportFormatReader.getReader(testBibtexFile, encoding)); + ParserResult result = BibtexParser.parse(ImportFormat.getReader(testBibtexFile, encoding)); SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true); BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), diff --git a/src/test/java/net/sf/jabref/exporter/MSBibExportFormatTestFiles.java b/src/test/java/net/sf/jabref/exporter/MSBibExportFormatTestFiles.java index 3e3e5287a26..3c140f8bc55 100644 --- a/src/test/java/net/sf/jabref/exporter/MSBibExportFormatTestFiles.java +++ b/src/test/java/net/sf/jabref/exporter/MSBibExportFormatTestFiles.java @@ -2,7 +2,7 @@ import java.io.File; import java.io.IOException; -import java.io.InputStream; +import java.net.URISyntaxException; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; @@ -16,7 +16,6 @@ import net.sf.jabref.Globals; import net.sf.jabref.JabRefPreferences; import net.sf.jabref.MetaData; -import net.sf.jabref.importer.OutputPrinterToNull; import net.sf.jabref.importer.fileformat.BibtexImporter; import net.sf.jabref.model.database.BibDatabase; import net.sf.jabref.model.entry.BibEntry; @@ -32,7 +31,6 @@ import org.junit.runners.Parameterized.Parameters; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; @RunWith(Parameterized.class) public class MSBibExportFormatTestFiles { @@ -71,14 +69,15 @@ public void setUp() throws Exception { } @Test - public final void testPerformExport() throws IOException { + public final void testPerformExport() throws IOException, URISyntaxException { String xmlFileName = filename.replace(".bib", ".xml"); + Path importFile = Paths.get(MSBibExportFormatTestFiles.class.getResource(filename).toURI()); String tempFilename = tempFile.getCanonicalPath(); - try (InputStream bibIn = MSBibExportFormat.class.getResourceAsStream(filename)) { - List entries = testImporter.importEntries(bibIn, new OutputPrinterToNull()); - assertNotNull(entries); - msBibExportFormat.performExport(databaseContext, tempFile.getPath(), charset, entries); - } + List entries = testImporter.importDatabase(importFile, Charset.defaultCharset()).getDatabase() + .getEntries(); + + msBibExportFormat.performExport(databaseContext, tempFile.getPath(), charset, entries); + List expected = Files.readAllLines(Paths.get(PATH_TO_FILE + xmlFileName)); List exported = Files.readAllLines(Paths.get(tempFilename)); assertEquals(expected, exported); diff --git a/src/test/java/net/sf/jabref/importer/ImportFormatReaderTest.java b/src/test/java/net/sf/jabref/importer/ImportFormatReaderIntegrationTest.java similarity index 78% rename from src/test/java/net/sf/jabref/importer/ImportFormatReaderTest.java rename to src/test/java/net/sf/jabref/importer/ImportFormatReaderIntegrationTest.java index 46f689546ad..58729fcfea0 100644 --- a/src/test/java/net/sf/jabref/importer/ImportFormatReaderTest.java +++ b/src/test/java/net/sf/jabref/importer/ImportFormatReaderIntegrationTest.java @@ -2,6 +2,7 @@ import java.io.IOException; import java.net.URISyntaxException; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collection; @@ -17,21 +18,19 @@ import static org.junit.Assert.assertEquals; @RunWith(Parameterized.class) -public class ImportFormatReaderTest { +public class ImportFormatReaderIntegrationTest { private ImportFormatReader reader; - private final String resourceName; private final int count; public final String format; - private final String fileName; + private final Path file; - public ImportFormatReaderTest(String resource, String format, int count) throws URISyntaxException { - this.resourceName = resource; + public ImportFormatReaderIntegrationTest(String resource, String format, int count) throws URISyntaxException { this.format = format; this.count = count; - this.fileName = Paths.get(ImportFormatReaderTest.class.getResource(resourceName).toURI()).toString(); + this.file = Paths.get(ImportFormatReaderIntegrationTest.class.getResource(resource).toURI()); } @@ -44,14 +43,13 @@ public void setUp() { @Test public void testImportUnknownFormat() { - ImportFormatReader.UnknownFormatImport unknownFormat = reader.importUnknownFormat(fileName); + ImportFormatReader.UnknownFormatImport unknownFormat = reader.importUnknownFormat(file); assertEquals(count, unknownFormat.parserResult.getDatabase().getEntryCount()); } @Test public void testImportFormatFromFile() throws IOException { - OutputPrinter nullPrinter = new OutputPrinterToNull(); - assertEquals(count, reader.importFromFile(format, fileName, nullPrinter).size()); + assertEquals(count, reader.importFromFile(format, file).getDatabase().getEntries().size()); } @Parameterized.Parameters(name = "{index}: {1}") diff --git a/src/test/java/net/sf/jabref/importer/ImportFormatReaderTestParameterless.java b/src/test/java/net/sf/jabref/importer/ImportFormatReaderTestParameterless.java index 95ee61c254b..9c8c8e85972 100644 --- a/src/test/java/net/sf/jabref/importer/ImportFormatReaderTestParameterless.java +++ b/src/test/java/net/sf/jabref/importer/ImportFormatReaderTestParameterless.java @@ -1,11 +1,12 @@ package net.sf.jabref.importer; import java.io.IOException; -import java.util.List; +import java.net.URISyntaxException; +import java.nio.file.Path; +import java.nio.file.Paths; import net.sf.jabref.Globals; import net.sf.jabref.JabRefPreferences; -import net.sf.jabref.model.entry.BibEntry; import org.junit.Before; import org.junit.Test; @@ -16,8 +17,7 @@ public class ImportFormatReaderTestParameterless { private ImportFormatReader reader; - private ImportFormatReader.UnknownFormatImport unknownFormat; - public List result; + @Before public void setUp() { Globals.prefs = JabRefPreferences.getInstance(); @@ -26,21 +26,21 @@ public void setUp() { } @Test - public void testImportUnknownFormatNotWorking() { - String fileName = ImportFormatReaderTestParameterless.class.getResource("fileformat/emptyFile.xml").getFile(); - unknownFormat = reader.importUnknownFormat(fileName); + public void testImportUnknownFormatNotWorking() throws URISyntaxException { + Path file = Paths.get(ImportFormatReaderTestParameterless.class.getResource("fileformat/emptyFile.xml").toURI()); + ImportFormatReader.UnknownFormatImport unknownFormat = reader.importUnknownFormat(file); assertNull(unknownFormat); } @Test(expected = NullPointerException.class) public void testNullImportUnknownFormat() { - unknownFormat = reader.importUnknownFormat(null); + reader.importUnknownFormat((Path)null); fail(); } @Test(expected = IllegalArgumentException.class) public void testImportFromFileUnknownFormat() throws IOException { - result = reader.importFromFile("someunknownformat", "doesn't matter", new OutputPrinterToNull()); + reader.importFromFile("someunknownformat", Paths.get("somepath")); fail(); } } diff --git a/src/test/java/net/sf/jabref/importer/fileformat/BibTeXMLImporterTest.java b/src/test/java/net/sf/jabref/importer/fileformat/BibTeXMLImporterTest.java index 66783ffafde..5219196fed4 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/BibTeXMLImporterTest.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/BibTeXMLImporterTest.java @@ -1,7 +1,7 @@ package net.sf.jabref.importer.fileformat; import java.io.IOException; -import java.io.InputStream; +import java.nio.charset.Charset; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; @@ -13,14 +13,11 @@ import net.sf.jabref.Globals; import net.sf.jabref.JabRefPreferences; -import net.sf.jabref.importer.OutputPrinterToNull; -import net.sf.jabref.model.entry.BibEntry; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; @RunWith(MockitoJUnitRunner.class) @@ -34,10 +31,10 @@ public class BibTeXMLImporterTest { * @return A list of Names * @throws IOException */ - public List getTestFiles() throws IOException { - List files = new ArrayList<>(); + public List getTestFiles() throws IOException { + List files = new ArrayList<>(); try (DirectoryStream stream = Files.newDirectoryStream(Paths.get(FILEFORMAT_PATH))) { - stream.forEach(n -> files.add(n.getFileName().toString())); + stream.forEach(files::add); } return files; @@ -48,17 +45,6 @@ public static void setUp() { Globals.prefs = JabRefPreferences.getInstance(); } - @Test - public void testExceptionOnInputStream() throws IOException { - try (InputStream is = Mockito.mock(InputStream.class)) { - Mockito.doThrow(new IOException()).when(is).read(); - - BibTeXMLImporter importer = new BibTeXMLImporter(); - List entry = importer.importEntries(is, new OutputPrinterToNull()); - Assert.assertTrue(entry.isEmpty()); - } - } - @Test public void testGetItemsEmpty() { BibTeXMLHandler handler = new BibTeXMLHandler(); @@ -74,20 +60,18 @@ public void testGetFormatName() { @Test public void testGetCLIId() { BibTeXMLImporter importer = new BibTeXMLImporter(); - Assert.assertEquals("bibtexml", importer.getCLIId()); + Assert.assertEquals("bibtexml", importer.getId()); } @Test public void testIsRecognizedFormatReject() throws IOException { BibTeXMLImporter importer = new BibTeXMLImporter(); - List list = getTestFiles().stream().filter(n -> !n.startsWith("BibTeXMLImporterTest")) + List list = getTestFiles().stream().filter(n -> !n.getFileName().toString().startsWith("BibTeXMLImporterTest")) .collect(Collectors.toList()); - for (String str : list) { - try (InputStream is = BibTeXMLImporter.class.getResourceAsStream(str)) { - Assert.assertFalse(importer.isRecognizedFormat(is)); - } + for (Path file : list) { + Assert.assertFalse(file.toString(), importer.isRecognizedFormat(file, Charset.defaultCharset())); } } } diff --git a/src/test/java/net/sf/jabref/importer/fileformat/BibTeXMLImporterTestFiles.java b/src/test/java/net/sf/jabref/importer/fileformat/BibTeXMLImporterTestFiles.java index 16ab0971ac6..fc2c81a704e 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/BibTeXMLImporterTestFiles.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/BibTeXMLImporterTestFiles.java @@ -1,7 +1,7 @@ package net.sf.jabref.importer.fileformat; import java.io.IOException; -import java.io.InputStream; +import java.nio.charset.Charset; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; @@ -15,7 +15,6 @@ import net.sf.jabref.Globals; import net.sf.jabref.JabRefPreferences; -import net.sf.jabref.importer.OutputPrinterToNull; import net.sf.jabref.logic.bibtex.BibEntryAssert; import net.sf.jabref.model.entry.BibEntry; @@ -36,7 +35,7 @@ public class BibTeXMLImporterTestFiles { private BibTeXMLImporter bibtexmlImporter; @Parameter - public String fileName; + public Path importFile; @Before @@ -46,36 +45,32 @@ public void setUp() { } @Parameters(name = "{0}") - public static Collection fileNames() throws IOException { - List files = new ArrayList<>(); + public static Collection files() throws IOException { + List files = new ArrayList<>(); try (DirectoryStream stream = Files.newDirectoryStream(Paths.get(FILEFORMAT_PATH))) { - stream.forEach(n -> files.add(n.getFileName().toString())); + stream.forEach(files::add); } - return files.stream().filter(n -> n.startsWith("BibTeXMLImporterTest")).filter(n -> n.endsWith(".xml")) + return files.stream().filter(n -> n.getFileName().toString().startsWith("BibTeXMLImporterTest") && n.getFileName().toString().endsWith(".xml")) .collect(Collectors.toList()); } @Test public void testIsRecognizedFormat() throws IOException { - try (InputStream stream = BibTeXMLImporterTest.class.getResourceAsStream(fileName)) { - Assert.assertTrue(bibtexmlImporter.isRecognizedFormat(stream)); - } + Assert.assertTrue(bibtexmlImporter.isRecognizedFormat(importFile, Charset.defaultCharset())); } @Test public void testImportEntries() throws IOException { - try (InputStream bitexmlStream = BibTeXMLImporterTest.class.getResourceAsStream(fileName)) { - List bibtexmlEntries = bibtexmlImporter.importEntries(bitexmlStream, new OutputPrinterToNull()); + List bibtexmlEntries = bibtexmlImporter.importDatabase(importFile, Charset.defaultCharset()).getDatabase().getEntries(); - String bibFileName = fileName.replace(".xml", ".bib"); - while (PATTERN.matcher(bibFileName).find()) { - bibFileName = bibFileName.replaceFirst("[0123456789]", ""); - } - if (bibtexmlEntries.isEmpty()) { - Assert.assertEquals(Collections.emptyList(), bibtexmlEntries); - } else { - BibEntryAssert.assertEquals(BibTeXMLImporterTest.class, bibFileName, bibtexmlEntries); - } + String bibFileName = importFile.getFileName().toString().replace(".xml", ".bib"); + while (PATTERN.matcher(bibFileName).find()) { + bibFileName = bibFileName.replaceFirst("[0123456789]", ""); + } + if (bibtexmlEntries.isEmpty()) { + Assert.assertEquals(Collections.emptyList(), bibtexmlEntries); + } else { + BibEntryAssert.assertEquals(BibTeXMLImporterTest.class, bibFileName, bibtexmlEntries); } } -} \ No newline at end of file +} diff --git a/src/test/java/net/sf/jabref/importer/fileformat/BibTeXMLImporterTestTypes.java b/src/test/java/net/sf/jabref/importer/fileformat/BibTeXMLImporterTestTypes.java index 4557c5605b5..e75d2475136 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/BibTeXMLImporterTestTypes.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/BibTeXMLImporterTestTypes.java @@ -1,8 +1,8 @@ package net.sf.jabref.importer.fileformat; -import java.io.ByteArrayInputStream; +import java.io.BufferedReader; import java.io.IOException; -import java.nio.charset.StandardCharsets; +import java.io.StringReader; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -10,7 +10,6 @@ import net.sf.jabref.Globals; import net.sf.jabref.JabRefPreferences; -import net.sf.jabref.importer.OutputPrinterToNull; import net.sf.jabref.model.entry.BibEntry; import org.junit.Assert; @@ -54,8 +53,8 @@ public void importConvertsToCorrectBibType() throws IOException { + "Java tricks\n" + "2016\n" + "\n" + "\n" + ""; - List bibEntries = bibteXMLImporter.importEntries( - new ByteArrayInputStream(bibteXMLInput.getBytes(StandardCharsets.UTF_8)), new OutputPrinterToNull()); + List bibEntries = bibteXMLImporter.importDatabase(new BufferedReader(new StringReader(bibteXMLInput))) + .getDatabase().getEntries(); BibEntry entry = new BibEntry(); entry.setField("author", "Max Mustermann"); diff --git a/src/test/java/net/sf/jabref/importer/fileformat/BiblioscapeImporterTest.java b/src/test/java/net/sf/jabref/importer/fileformat/BiblioscapeImporterTest.java index b826c8ab563..ac7baf87666 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/BiblioscapeImporterTest.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/BiblioscapeImporterTest.java @@ -1,11 +1,12 @@ package net.sf.jabref.importer.fileformat; -import java.io.InputStream; +import java.nio.charset.Charset; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Collections; import net.sf.jabref.Globals; import net.sf.jabref.JabRefPreferences; -import net.sf.jabref.importer.OutputPrinterToNull; import org.junit.Assert; import org.junit.Before; @@ -30,13 +31,13 @@ public void testGetFormatName() { @Test public void testGetCLIID() { BiblioscapeImporter importer = new BiblioscapeImporter(); - Assert.assertEquals(importer.getCLIId(), "biblioscape"); + Assert.assertEquals(importer.getId(), "biblioscape"); } @Test public void testImportEntriesAbortion() throws Throwable { - try (InputStream is = BiblioscapeImporter.class.getResourceAsStream("BiblioscapeImporterTestCorrupt.txt")) { - Assert.assertEquals(Collections.emptyList(), bsImporter.importEntries(is, new OutputPrinterToNull())); - } + Path file = Paths.get(BiblioscapeImporter.class.getResource("BiblioscapeImporterTestCorrupt.txt").toURI()); + Assert.assertEquals(Collections.emptyList(), bsImporter.importDatabase(file, Charset.defaultCharset()) + .getDatabase().getEntries()); } } diff --git a/src/test/java/net/sf/jabref/importer/fileformat/BiblioscapeImporterTestFiles.java b/src/test/java/net/sf/jabref/importer/fileformat/BiblioscapeImporterTestFiles.java index 43d662aab35..afc910ec2f0 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/BiblioscapeImporterTestFiles.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/BiblioscapeImporterTestFiles.java @@ -1,14 +1,16 @@ package net.sf.jabref.importer.fileformat; import java.io.IOException; -import java.io.InputStream; +import java.net.URISyntaxException; +import java.nio.charset.Charset; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.Collection; import java.util.List; import net.sf.jabref.Globals; import net.sf.jabref.JabRefPreferences; -import net.sf.jabref.importer.OutputPrinterToNull; import net.sf.jabref.logic.bibtex.BibEntryAssert; import net.sf.jabref.model.entry.BibEntry; @@ -17,7 +19,6 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameter; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) @@ -25,9 +26,13 @@ public class BiblioscapeImporterTestFiles { private BiblioscapeImporter bsImporter; - @Parameter - public String fileName; + public Path importFile; + public String bibFile; + public BiblioscapeImporterTestFiles(String fileName) throws URISyntaxException { + importFile = Paths.get(BiblioscapeImporterTest.class.getResource(fileName + ".txt").toURI()); + bibFile = fileName + ".bib"; + } @Before public void setUp() throws Exception { @@ -37,7 +42,7 @@ public void setUp() throws Exception { @Parameters(name = "{0}") public static Collection fileNames() { - return Arrays.asList(new String[] { + return Arrays.asList( "BiblioscapeImporterTestOptionalFields", "BiblioscapeImporterTestComments", "BiblioscapeImporterTestUnknownFields", @@ -45,26 +50,19 @@ public static Collection fileNames() { "BiblioscapeImporterTestJournalArticle", "BiblioscapeImporterTestInbook", "BiblioscapeImporterTestUnknownType", - "BiblioscapeImporterTestArticleST", - }); + "BiblioscapeImporterTestArticleST" + ); } @Test public void testIsRecognizedFormat() throws IOException { - try (InputStream stream = BiblioscapeImporterTest.class.getResourceAsStream(fileName + ".txt")) { - Assert.assertTrue(bsImporter.isRecognizedFormat(stream)); - } + Assert.assertTrue(bsImporter.isRecognizedFormat(importFile, Charset.defaultCharset())); } @Test public void testImportEntries() throws IOException { - try (InputStream bsStream = BiblioscapeImporterTest.class.getResourceAsStream(fileName + ".txt")) { - - List bsEntries = bsImporter.importEntries(bsStream, new OutputPrinterToNull()); - Assert.assertEquals(1, bsEntries.size()); - BibEntryAssert.assertEquals(BiblioscapeImporterTest.class, fileName + ".bib", bsEntries); - - - } + List bsEntries = bsImporter.importDatabase(importFile, Charset.defaultCharset()).getDatabase().getEntries(); + Assert.assertEquals(1, bsEntries.size()); + BibEntryAssert.assertEquals(BiblioscapeImporterTest.class, bibFile, bsEntries); } -} \ No newline at end of file +} diff --git a/src/test/java/net/sf/jabref/importer/fileformat/BiblioscapeImporterTestTypes.java b/src/test/java/net/sf/jabref/importer/fileformat/BiblioscapeImporterTestTypes.java index c3bb10268de..95a70c5f804 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/BiblioscapeImporterTestTypes.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/BiblioscapeImporterTestTypes.java @@ -1,8 +1,8 @@ package net.sf.jabref.importer.fileformat; -import java.io.ByteArrayInputStream; +import java.io.BufferedReader; import java.io.IOException; -import java.nio.charset.StandardCharsets; +import java.io.StringReader; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -10,7 +10,6 @@ import net.sf.jabref.Globals; import net.sf.jabref.JabRefPreferences; -import net.sf.jabref.importer.OutputPrinterToNull; import net.sf.jabref.model.entry.BibEntry; import org.junit.Assert; @@ -51,8 +50,8 @@ public void importConvertsToCorrectBibType() throws IOException { String bsInput = "--AU-- Baklouti, F.\n" + "--YP-- 1999\n" + "--KW-- Cells; Rna; Isoforms\n" + "--TI-- Blood\n" + "--RT-- " + biblioscapeType + "\n" + "------"; - List bibEntries = bsImporter.importEntries( - new ByteArrayInputStream(bsInput.getBytes(StandardCharsets.UTF_8)), new OutputPrinterToNull()); + List bibEntries = bsImporter.importDatabase(new BufferedReader(new StringReader(bsInput))) + .getDatabase().getEntries(); BibEntry entry = new BibEntry(); entry.setField("author", "Baklouti, F."); diff --git a/src/test/java/net/sf/jabref/importer/fileformat/BibtexImporterTest.java b/src/test/java/net/sf/jabref/importer/fileformat/BibtexImporterTest.java index 2288968a16f..ebfe572d9e2 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/BibtexImporterTest.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/BibtexImporterTest.java @@ -1,12 +1,14 @@ package net.sf.jabref.importer.fileformat; import java.io.IOException; -import java.io.InputStream; +import java.net.URISyntaxException; +import java.nio.charset.Charset; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.List; import net.sf.jabref.Globals; import net.sf.jabref.JabRefPreferences; -import net.sf.jabref.importer.OutputPrinterToNull; import net.sf.jabref.model.entry.BibEntry; import org.junit.Before; @@ -34,61 +36,59 @@ public void setUp() { } @Test - public void testIsRecognizedFormat() throws IOException { - try (InputStream stream = BibtexImporterTest.class.getResourceAsStream("BibtexImporter.examples.bib")) { - assertTrue(importer.isRecognizedFormat(stream)); - } + public void testIsRecognizedFormat() throws IOException, URISyntaxException { + Path file = Paths.get(BibtexImporterTest.class.getResource("BibtexImporter.examples.bib").toURI()); + assertTrue(importer.isRecognizedFormat(file, Charset.defaultCharset())); } @Test - public void testImportEntries() throws IOException { - try (InputStream stream = BibtexImporterTest.class.getResourceAsStream("BibtexImporter.examples.bib")) { - List bibEntries = importer.importEntries(stream, new OutputPrinterToNull()); + public void testImportEntries() throws IOException, URISyntaxException { + Path file = Paths.get(BibtexImporterTest.class.getResource("BibtexImporter.examples.bib").toURI()); + List bibEntries = importer.importDatabase(file, Charset.defaultCharset()).getDatabase().getEntries(); - assertEquals(4, bibEntries.size()); + assertEquals(4, bibEntries.size()); - for (BibEntry entry : bibEntries) { + for (BibEntry entry : bibEntries) { - if (entry.getCiteKey().equals("aksin")) { - assertEquals("Aks{\\i}n, {\\\"O}zge and T{\\\"u}rkmen, Hayati and Artok, Levent and {\\c{C}}etinkaya, " + - "Bekir and Ni, Chaoying and B{\\\"u}y{\\\"u}kg{\\\"u}ng{\\\"o}r, Orhan and {\\\"O}zkal, Erhan", - entry.getField("author")); - assertEquals("aksin", entry.getField("bibtexkey")); - assertEquals("2006", entry.getField("date")); - assertEquals("Effect of immobilization on catalytic characteristics", entry.getField("indextitle")); - assertEquals("#jomch#", entry.getField("journaltitle")); - assertEquals("13", entry.getField("number")); - assertEquals("3027-3036", entry.getField("pages")); - assertEquals("Effect of immobilization on catalytic characteristics of saturated {Pd-N}-heterocyclic " + - "carbenes in {Mizoroki-Heck} reactions", entry.getField("title")); - assertEquals("691", entry.getField("volume")); - } else if (entry.getCiteKey().equals("stdmodel")) { - assertEquals("A \\texttt{set} with three members discussing the standard model of particle physics. " + - "The \\texttt{crossref} field in the \\texttt{@set} entry and the \\texttt{entryset} field in " + - "each set member entry is needed only when using BibTeX as the backend", - entry.getField("annotation")); - assertEquals("stdmodel", entry.getField("bibtexkey")); - assertEquals("glashow,weinberg,salam", entry.getField("entryset")); - } else if (entry.getCiteKey().equals("set")) { - assertEquals("A \\texttt{set} with three members. The \\texttt{crossref} field in the \\texttt{@set} " + - "entry and the \\texttt{entryset} field in each set member entry is needed only when using " + - "BibTeX as the backend", entry.getField("annotation")); - assertEquals("set", entry.getField("bibtexkey")); - assertEquals("herrmann,aksin,yoon", entry.getField("entryset")); - } else if (entry.getCiteKey().equals("Preissel2016")) { - assertEquals("Heidelberg", entry.getField("address")); - assertEquals("Preißel, René", entry.getField("author")); - assertEquals("Preissel2016", entry.getField("bibtexkey")); - assertEquals("3., aktualisierte und erweiterte Auflage", entry.getField("edition")); - assertEquals("978-3-86490-311-3", entry.getField("isbn")); - assertEquals("Versionsverwaltung", entry.getField("keywords")); - assertEquals("XX, 327 Seiten", entry.getField("pages")); - assertEquals("dpunkt.verlag", entry.getField("publisher")); - assertEquals("Git: dezentrale Versionsverwaltung im Team : Grundlagen und Workflows", - entry.getField("title")); - assertEquals("http://d-nb.info/107601965X", entry.getField("url")); - assertEquals("2016", entry.getField("year")); - } + if (entry.getCiteKey().equals("aksin")) { + assertEquals("Aks{\\i}n, {\\\"O}zge and T{\\\"u}rkmen, Hayati and Artok, Levent and {\\c{C}}etinkaya, " + + "Bekir and Ni, Chaoying and B{\\\"u}y{\\\"u}kg{\\\"u}ng{\\\"o}r, Orhan and {\\\"O}zkal, Erhan", + entry.getField("author")); + assertEquals("aksin", entry.getField("bibtexkey")); + assertEquals("2006", entry.getField("date")); + assertEquals("Effect of immobilization on catalytic characteristics", entry.getField("indextitle")); + assertEquals("#jomch#", entry.getField("journaltitle")); + assertEquals("13", entry.getField("number")); + assertEquals("3027-3036", entry.getField("pages")); + assertEquals("Effect of immobilization on catalytic characteristics of saturated {Pd-N}-heterocyclic " + + "carbenes in {Mizoroki-Heck} reactions", entry.getField("title")); + assertEquals("691", entry.getField("volume")); + } else if (entry.getCiteKey().equals("stdmodel")) { + assertEquals("A \\texttt{set} with three members discussing the standard model of particle physics. " + + "The \\texttt{crossref} field in the \\texttt{@set} entry and the \\texttt{entryset} field in " + + "each set member entry is needed only when using BibTeX as the backend", + entry.getField("annotation")); + assertEquals("stdmodel", entry.getField("bibtexkey")); + assertEquals("glashow,weinberg,salam", entry.getField("entryset")); + } else if (entry.getCiteKey().equals("set")) { + assertEquals("A \\texttt{set} with three members. The \\texttt{crossref} field in the \\texttt{@set} " + + "entry and the \\texttt{entryset} field in each set member entry is needed only when using " + + "BibTeX as the backend", entry.getField("annotation")); + assertEquals("set", entry.getField("bibtexkey")); + assertEquals("herrmann,aksin,yoon", entry.getField("entryset")); + } else if (entry.getCiteKey().equals("Preissel2016")) { + assertEquals("Heidelberg", entry.getField("address")); + assertEquals("Preißel, René", entry.getField("author")); + assertEquals("Preissel2016", entry.getField("bibtexkey")); + assertEquals("3., aktualisierte und erweiterte Auflage", entry.getField("edition")); + assertEquals("978-3-86490-311-3", entry.getField("isbn")); + assertEquals("Versionsverwaltung", entry.getField("keywords")); + assertEquals("XX, 327 Seiten", entry.getField("pages")); + assertEquals("dpunkt.verlag", entry.getField("publisher")); + assertEquals("Git: dezentrale Versionsverwaltung im Team : Grundlagen und Workflows", + entry.getField("title")); + assertEquals("http://d-nb.info/107601965X", entry.getField("url")); + assertEquals("2016", entry.getField("year")); } } } @@ -97,9 +97,4 @@ public void testImportEntries() throws IOException { public void testGetFormatName() { assertEquals("BibTeX", importer.getFormatName()); } - - @Test - public void testGetExtensions() { - assertEquals("bib", importer.getExtensions()); - } } diff --git a/src/test/java/net/sf/jabref/importer/fileformat/CopacImporterTest.java b/src/test/java/net/sf/jabref/importer/fileformat/CopacImporterTest.java index b6741a347ba..8ced65c0830 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/CopacImporterTest.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/CopacImporterTest.java @@ -1,7 +1,8 @@ package net.sf.jabref.importer.fileformat; import java.io.IOException; -import java.io.InputStream; +import java.net.URISyntaxException; +import java.nio.charset.Charset; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; @@ -13,7 +14,6 @@ import net.sf.jabref.Globals; import net.sf.jabref.JabRefPreferences; -import net.sf.jabref.importer.OutputPrinterToNull; import net.sf.jabref.model.entry.BibEntry; import org.junit.Assert; @@ -43,46 +43,22 @@ public static void setUp() { Globals.prefs = JabRefPreferences.getInstance(); } - @Test(expected = IOException.class) - public void testImportEntriesException() throws IOException { - CopacImporter importer = new CopacImporter(); - importer.importEntries(null, new OutputPrinterToNull()); - } - - @Test - public void testGetFormatName() { - CopacImporter importer = new CopacImporter(); - Assert.assertEquals("Copac", importer.getFormatName()); - } - @Test - public void testGetCLIId() { + public void testIsNotRecognizedFormat() throws IOException, URISyntaxException { CopacImporter importer = new CopacImporter(); - Assert.assertEquals("cpc", importer.getCLIId()); - } - - @Test - public void testIsRecognizedFormatReject() throws IOException { - CopacImporter importer = new CopacImporter(); - List list = getTestFiles().stream().filter(n -> !n.startsWith("CopacImporterTest")) .collect(Collectors.toList()); - for (String str : list) { - try (InputStream is = CopacImporterTest.class.getResourceAsStream(str)) { - Assert.assertFalse(importer.isRecognizedFormat(is)); - } + Path file = Paths.get(CopacImporterTest.class.getResource(str).toURI()); + Assert.assertFalse(importer.isRecognizedFormat(file, Charset.defaultCharset())); } } @Test - public void testImportEmptyEntries() throws IOException { + public void testImportEmptyEntries() throws IOException, URISyntaxException { CopacImporter importer = new CopacImporter(); - - try (InputStream is = CopacImporterTest.class.getResourceAsStream("Empty.txt")) { - List entries = importer.importEntries(is, new OutputPrinterToNull()); - Assert.assertEquals(0, entries.size()); - Assert.assertEquals(Collections.emptyList(), entries); - } + Path path = Paths.get(CopacImporterTest.class.getResource("Empty.txt").toURI()); + List entries = importer.importDatabase(path, Charset.defaultCharset()).getDatabase().getEntries(); + Assert.assertEquals(Collections.emptyList(), entries); } -} \ No newline at end of file +} diff --git a/src/test/java/net/sf/jabref/importer/fileformat/CopacImporterTestFiles.java b/src/test/java/net/sf/jabref/importer/fileformat/CopacImporterTestFiles.java index 0ef18788af1..4c054737809 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/CopacImporterTestFiles.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/CopacImporterTestFiles.java @@ -2,6 +2,8 @@ import java.io.IOException; import java.io.InputStream; +import java.net.URISyntaxException; +import java.nio.charset.Charset; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; @@ -50,21 +52,17 @@ public static Collection fileNames() throws IOException { } @Test - public void testIsRecognizedFormat() throws IOException { - try (InputStream stream = CopacImporterTest.class.getResourceAsStream(fileName)) { - Assert.assertTrue(copacImporter.isRecognizedFormat(stream)); - } + public void testIsRecognizedFormat() throws IOException, URISyntaxException { + Path file = Paths.get(CopacImporterTest.class.getResource(fileName).toURI()); + Assert.assertTrue(copacImporter.isRecognizedFormat(file, Charset.defaultCharset())); } @Test - public void testImportEntries() throws IOException { + public void testImportEntries() throws IOException, URISyntaxException { String bibFileName = fileName.replace(".txt", ".bib"); - try (InputStream copacStream = CopacImporterTest.class.getResourceAsStream(fileName); - InputStream bibStream = BibtexImporterTest.class.getResourceAsStream(bibFileName)) { - BibEntryAssert.assertEquals(bibStream, copacStream, copacImporter); + try (InputStream bibStream = BibtexImporterTest.class.getResourceAsStream(bibFileName)) { + BibEntryAssert.assertEquals(bibStream, CopacImporterTest.class.getResource(fileName), copacImporter); } - } - -} \ No newline at end of file +} diff --git a/src/test/java/net/sf/jabref/importer/fileformat/EndnoteImporterTest.java b/src/test/java/net/sf/jabref/importer/fileformat/EndnoteImporterTest.java index f11ff74b923..da2bd3b8505 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/EndnoteImporterTest.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/EndnoteImporterTest.java @@ -1,15 +1,17 @@ package net.sf.jabref.importer.fileformat; -import java.io.ByteArrayInputStream; +import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; +import java.io.StringReader; +import java.net.URISyntaxException; +import java.nio.charset.Charset; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.List; import net.sf.jabref.Globals; import net.sf.jabref.JabRefPreferences; -import net.sf.jabref.importer.OutputPrinterToNull; import net.sf.jabref.model.entry.BibEntry; import org.junit.Before; @@ -37,84 +39,80 @@ public void testGetFormatName() { @Test public void testGetCLIId() { - assertEquals("refer", importer.getCLIId()); + assertEquals("refer", importer.getId()); } @Test - public void testIsRecognizedFormat() throws IOException { + public void testIsRecognizedFormat() throws IOException, URISyntaxException { List list = Arrays.asList("Endnote.pattern.A.enw", "Endnote.pattern.E.enw", "Endnote.book.example.enw"); for (String str : list) { - try (InputStream is = EndnoteImporterTest.class.getResourceAsStream(str)) { - assertTrue(importer.isRecognizedFormat(is)); - } + Path file = Paths.get(EndnoteImporterTest.class.getResource(str).toURI()); + assertTrue(importer.isRecognizedFormat(file, Charset.defaultCharset())); } } @Test - public void testIsRecognizedFormatReject() throws IOException { + public void testIsRecognizedFormatReject() throws IOException, URISyntaxException { List list = Arrays.asList("IEEEImport1.txt", "IsiImporterTest1.isi", "IsiImporterTestInspec.isi", "IsiImporterTestWOS.isi", "IsiImporterTestMedline.isi", "RisImporterTest1.ris", "Endnote.pattern.no_enw", "empty.pdf", "annotated.pdf"); for (String str : list) { - try (InputStream is = EndnoteImporterTest.class.getResourceAsStream(str)) { - assertFalse(importer.isRecognizedFormat(is)); - } + Path file = Paths.get(EndnoteImporterTest.class.getResource(str).toURI()); + assertFalse(importer.isRecognizedFormat(file, Charset.defaultCharset())); } } @Test - public void testImportEntries0() throws IOException { - try (InputStream is = EndnoteImporterTest.class.getResourceAsStream("Endnote.entries.enw")) { - List bibEntries = importer.importEntries(is, new OutputPrinterToNull()); - - assertEquals(5, bibEntries.size()); - - BibEntry be0 = bibEntries.get(0); - assertEquals("misc", be0.getType()); - assertEquals("testA0 and testA1", be0.getField("author")); - assertEquals("testE0 and testE1", be0.getField("editor")); - assertEquals("testT", be0.getField("title")); - - BibEntry be1 = bibEntries.get(1); - assertEquals("misc", be1.getType()); - assertEquals("testC", be1.getField("address")); - assertEquals("testB2", be1.getField("booktitle")); - assertEquals("test8", be1.getField("date")); - assertEquals("test7", be1.getField("edition")); - assertEquals("testJ", be1.getField("journal")); - assertEquals("testD", be1.getField("year")); - - BibEntry be2 = bibEntries.get(2); - assertEquals("article", be2.getType()); - assertEquals("testB0", be2.getField("journal")); - - BibEntry be3 = bibEntries.get(3); - assertEquals("book", be3.getType()); - assertEquals("testI0", be3.getField("publisher")); - assertEquals("testB1", be3.getField("series")); - - BibEntry be4 = bibEntries.get(4); - assertEquals("mastersthesis", be4.getType()); - assertEquals("testX", be4.getField("abstract")); - assertEquals("testF", be4.getField("bibtexkey")); - assertEquals("testR", be4.getField("doi")); - assertEquals("testK", be4.getField("keywords")); - assertEquals("testO1", be4.getField("note")); - assertEquals("testN", be4.getField("number")); - assertEquals("testP", be4.getField("pages")); - assertEquals("testI1", be4.getField("school")); - assertEquals("testU", be4.getField("url")); - assertEquals("testV", be4.getField("volume")); - } + public void testImportEntries0() throws IOException, URISyntaxException { + Path file = Paths.get(EndnoteImporterTest.class.getResource("Endnote.entries.enw").toURI()); + List bibEntries = importer.importDatabase(file, Charset.defaultCharset()).getDatabase().getEntries(); + + assertEquals(5, bibEntries.size()); + + BibEntry be0 = bibEntries.get(0); + assertEquals("misc", be0.getType()); + assertEquals("testA0 and testA1", be0.getField("author")); + assertEquals("testE0 and testE1", be0.getField("editor")); + assertEquals("testT", be0.getField("title")); + + BibEntry be1 = bibEntries.get(1); + assertEquals("misc", be1.getType()); + assertEquals("testC", be1.getField("address")); + assertEquals("testB2", be1.getField("booktitle")); + assertEquals("test8", be1.getField("date")); + assertEquals("test7", be1.getField("edition")); + assertEquals("testJ", be1.getField("journal")); + assertEquals("testD", be1.getField("year")); + + BibEntry be2 = bibEntries.get(2); + assertEquals("article", be2.getType()); + assertEquals("testB0", be2.getField("journal")); + + BibEntry be3 = bibEntries.get(3); + assertEquals("book", be3.getType()); + assertEquals("testI0", be3.getField("publisher")); + assertEquals("testB1", be3.getField("series")); + + BibEntry be4 = bibEntries.get(4); + assertEquals("mastersthesis", be4.getType()); + assertEquals("testX", be4.getField("abstract")); + assertEquals("testF", be4.getField("bibtexkey")); + assertEquals("testR", be4.getField("doi")); + assertEquals("testK", be4.getField("keywords")); + assertEquals("testO1", be4.getField("note")); + assertEquals("testN", be4.getField("number")); + assertEquals("testP", be4.getField("pages")); + assertEquals("testI1", be4.getField("school")); + assertEquals("testU", be4.getField("url")); + assertEquals("testV", be4.getField("volume")); } @Test public void testImportEntries1() throws IOException { String s = "%O Artn\\\\s testO\n%A testA,\n%E testE0, testE1"; - InputStream is = new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8)); - List bibEntries = importer.importEntries(is, new OutputPrinterToNull()); + List bibEntries = importer.importDatabase(new BufferedReader(new StringReader(s))).getDatabase().getEntries(); assertEquals(1, bibEntries.size()); @@ -126,23 +124,22 @@ public void testImportEntries1() throws IOException { } @Test - public void testImportEntriesBookExample() throws IOException { - try (InputStream is = EndnoteImporterTest.class.getResourceAsStream("Endnote.book.example.enw")) { - List bibEntries = importer.importEntries(is, new OutputPrinterToNull()); - - assertEquals(1, bibEntries.size()); - - BibEntry be = bibEntries.get(0); - assertEquals("book", be.getType()); - assertEquals("Heidelberg", be.getField("address")); - assertEquals("Preißel, René and Stachmann, Bjørn", be.getField("author")); - assertEquals("3., aktualisierte und erweiterte Auflage", be.getField("edition")); - assertEquals("Versionsverwaltung", be.getField("keywords")); - assertEquals("XX, 327", be.getField("pages")); - assertEquals("dpunkt.verlag", be.getField("publisher")); - assertEquals("Git : dezentrale Versionsverwaltung im Team : Grundlagen und Workflows", be.getField("title")); - assertEquals("http://d-nb.info/107601965X", be.getField("url")); - assertEquals("2016", be.getField("year")); - } + public void testImportEntriesBookExample() throws IOException, URISyntaxException { + Path file = Paths.get(EndnoteImporterTest.class.getResource("Endnote.book.example.enw").toURI()); + List bibEntries = importer.importDatabase(file, Charset.defaultCharset()).getDatabase().getEntries(); + + assertEquals(1, bibEntries.size()); + + BibEntry be = bibEntries.get(0); + assertEquals("book", be.getType()); + assertEquals("Heidelberg", be.getField("address")); + assertEquals("Preißel, René and Stachmann, Bjørn", be.getField("author")); + assertEquals("3., aktualisierte und erweiterte Auflage", be.getField("edition")); + assertEquals("Versionsverwaltung", be.getField("keywords")); + assertEquals("XX, 327", be.getField("pages")); + assertEquals("dpunkt.verlag", be.getField("publisher")); + assertEquals("Git : dezentrale Versionsverwaltung im Team : Grundlagen und Workflows", be.getField("title")); + assertEquals("http://d-nb.info/107601965X", be.getField("url")); + assertEquals("2016", be.getField("year")); } } diff --git a/src/test/java/net/sf/jabref/importer/fileformat/ImportFormatTest.java b/src/test/java/net/sf/jabref/importer/fileformat/ImportFormatTest.java new file mode 100644 index 00000000000..a28fd996e77 --- /dev/null +++ b/src/test/java/net/sf/jabref/importer/fileformat/ImportFormatTest.java @@ -0,0 +1,97 @@ +package net.sf.jabref.importer.fileformat; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; +import java.util.regex.Pattern; + +import org.junit.Assert; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; +import org.mockito.Mockito; + +import static org.mockito.Mockito.when; + +@RunWith(Parameterized.class) +public class ImportFormatTest { + + @Parameter + public ImportFormat format; + + @Test(expected = NullPointerException.class) + public void isRecognizedFormatWithNullThrowsException() throws IOException { + format.isRecognizedFormat(null); + } + + @Test(expected = NullPointerException.class) + public void importDatabaseWithNullThrowsException() throws IOException { + format.importDatabase(null); + } + + @Test + public void getFormatterNameDoesNotReturnNull() { + Assert.assertNotNull(format.getFormatName()); + } + + @Test + @Ignore + public void getExtensionsDoesNotReturnNull() { + Assert.assertNotNull(format.getExtensions()); + } + + @Test + public void getIdDoesNotReturnNull() { + Assert.assertNotNull(format.getId()); + } + + @Test + public void getIdDoesNotContainWhitespace() { + Pattern whitespacePattern = Pattern.compile("\\s"); + Assert.assertFalse(whitespacePattern.matcher(format.getId()).find()); + } + + @Test + public void getIdStripsSpecialCharactersAndConvertsToLowercase() { + ImportFormat importFormat = Mockito.mock(ImportFormat.class, Mockito.CALLS_REAL_METHODS); + when(importFormat.getFormatName()).thenReturn("*Test-Importer"); + Assert.assertEquals("testimporter", importFormat.getId()); + } + + @Test + @Ignore + public void getDescriptionDoesNotReturnNull() { + Assert.assertNotNull(format.getDescription()); + } + + @Parameters(name = "{index}: {0}") + public static Collection instancesToTest() { + // all classes implementing {@link ImportFormat} + // sorted alphabetically + + // @formatter:off + return Arrays.asList( + new Object[]{new BiblioscapeImporter()}, + new Object[]{new BibtexImporter()}, + new Object[]{new BibTeXMLImporter()}, + new Object[]{new CopacImporter()}, + new Object[]{new EndnoteImporter()}, + new Object[]{new FreeCiteImporter()}, + new Object[]{new InspecImporter()}, + new Object[]{new IsiImporter()}, + new Object[]{new MedlineImporter()}, + new Object[]{new MedlinePlainImporter()}, + new Object[]{new MsBibImporter()}, + new Object[]{new OvidImporter()}, + new Object[]{new PdfContentImporter()}, + new Object[]{new PdfXmpImporter()}, + new Object[]{new RepecNepImporter()}, + new Object[]{new RisImporter()}, + new Object[]{new SilverPlatterImporter()} + ); + // @formatter:on + } +} diff --git a/src/test/java/net/sf/jabref/importer/fileformat/InspecImportTest.java b/src/test/java/net/sf/jabref/importer/fileformat/InspecImportTest.java index 8aee52bb74b..a423ec33e70 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/InspecImportTest.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/InspecImportTest.java @@ -1,15 +1,18 @@ package net.sf.jabref.importer.fileformat; -import java.io.ByteArrayInputStream; +import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; +import java.io.StringReader; +import java.net.URISyntaxException; +import java.nio.charset.Charset; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.Collections; import java.util.List; import net.sf.jabref.Globals; import net.sf.jabref.JabRefPreferences; -import net.sf.jabref.importer.OutputPrinterToNull; import net.sf.jabref.logic.bibtex.BibEntryAssert; import net.sf.jabref.model.entry.BibEntry; @@ -31,29 +34,27 @@ public void setUp() throws Exception { } @Test - public void testIsRecognizedFormatAccept() throws IOException { + public void testIsRecognizedFormatAccept() throws IOException, URISyntaxException { List testList = Arrays.asList("InspecImportTest.txt", "InspecImportTest2.txt"); for (String str : testList) { - try (InputStream inStream = InspecImportTest.class.getResourceAsStream(str)) { - assertTrue(inspecImp.isRecognizedFormat(inStream)); - } + Path file = Paths.get(InspecImportTest.class.getResource(str).toURI()); + assertTrue(inspecImp.isRecognizedFormat(file, Charset.defaultCharset())); } } @Test - public void testIsRecognizedFormatReject() throws IOException { + public void testIsRecognizedFormatReject() throws IOException, URISyntaxException { List testList = Arrays.asList("CopacImporterTest1.txt", "CopacImporterTest2.txt", "IEEEImport1.txt", "IsiImporterTest1.isi", "IsiImporterTestInspec.isi", "IsiImporterTestWOS.isi", "IsiImporterTestMedline.isi", "RisImporterTest1.ris", "InspecImportTestFalse.txt"); for (String str : testList) { - try (InputStream inStream = InspecImportTest.class.getResourceAsStream(str)) { - assertFalse(inspecImp.isRecognizedFormat(inStream)); - } + Path file = Paths.get(InspecImportTest.class.getResource(str).toURI()); + assertFalse(inspecImp.isRecognizedFormat(file, Charset.defaultCharset())); } } @Test - public void testCompleteBibtexEntryOnJournalPaperImport() throws IOException { + public void testCompleteBibtexEntryOnJournalPaperImport() throws IOException, URISyntaxException { BibEntry expectedEntry = new BibEntry(); expectedEntry.setType("article"); @@ -67,19 +68,21 @@ public void testCompleteBibtexEntryOnJournalPaperImport() throws IOException { expectedEntry.setField("volume", "19"); BibEntryAssert.assertEquals(Collections.singletonList(expectedEntry), - InspecImportTest.class.getResourceAsStream("InspecImportTest2.txt"), inspecImp); + InspecImportTest.class.getResource("InspecImportTest2.txt"), inspecImp); } @Test public void importConferencePaperGivesInproceedings() throws IOException { String testInput = "Record.*INSPEC.*\n" + "\n" + - "RT ~ Conference-Paper"; + "RT ~ Conference-Paper\n" + + "AU ~ Prechelt, Lutz"; BibEntry expectedEntry = new BibEntry(); expectedEntry.setType("Inproceedings"); + expectedEntry.setField("author", "Prechelt, Lutz"); - try (InputStream inStream = new ByteArrayInputStream(testInput.getBytes())) { - List entries = inspecImp.importEntries(inStream, new OutputPrinterToNull()); + try (BufferedReader reader = new BufferedReader(new StringReader(testInput))) { + List entries = inspecImp.importDatabase(reader).getDatabase().getEntries(); assertEquals(Collections.singletonList(expectedEntry), entries); } } @@ -88,12 +91,14 @@ public void importConferencePaperGivesInproceedings() throws IOException { public void importMiscGivesMisc() throws IOException { String testInput = "Record.*INSPEC.*\n" + "\n" + + "AU ~ Prechelt, Lutz \n" + "RT ~ Misc"; BibEntry expectedEntry = new BibEntry(); expectedEntry.setType("Misc"); + expectedEntry.setField("author", "Prechelt, Lutz"); - try (InputStream inStream = new ByteArrayInputStream(testInput.getBytes())) { - List entries = inspecImp.importEntries(inStream, new OutputPrinterToNull()); + try (BufferedReader reader = new BufferedReader(new StringReader(testInput))) { + List entries = inspecImp.importDatabase(reader).getDatabase().getEntries(); assertEquals(1, entries.size()); BibEntry entry = entries.get(0); assertEquals(expectedEntry, entry); @@ -107,7 +112,7 @@ public void testGetFormatName() { @Test public void testGetCLIId() { - assertEquals("inspec", inspecImp.getCLIId()); + assertEquals("inspec", inspecImp.getId()); } } diff --git a/src/test/java/net/sf/jabref/importer/fileformat/IsiImporterTest.java b/src/test/java/net/sf/jabref/importer/fileformat/IsiImporterTest.java index 3fe25245114..6d7fe7fd59f 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/IsiImporterTest.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/IsiImporterTest.java @@ -1,14 +1,16 @@ package net.sf.jabref.importer.fileformat; import java.io.IOException; -import java.io.InputStream; +import java.net.URISyntaxException; +import java.nio.charset.Charset; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.HashMap; import java.util.List; import net.sf.jabref.Globals; import net.sf.jabref.JabRefPreferences; -import net.sf.jabref.importer.OutputPrinterToNull; import net.sf.jabref.model.entry.BibEntry; import org.junit.Assert; @@ -31,14 +33,6 @@ public static void setUp() { Globals.prefs = JabRefPreferences.getInstance(); } - @Test - public void testImportEntriesException() throws IOException { - thrown.expect(IOException.class); - - IsiImporter importer = new IsiImporter(); - importer.importEntries(null, new OutputPrinterToNull()); - } - @Test public void testParseMonthException() { IsiImporter.parseMonth("20l06 06-07"); @@ -55,11 +49,11 @@ public void testGetFormatName() { public void testGetCLIId() { IsiImporter importer = new IsiImporter(); - Assert.assertEquals(importer.getCLIId(), "isi"); + Assert.assertEquals(importer.getId(), "isi"); } @Test - public void testIsRecognizedFormatAccepted() throws IOException { + public void testIsRecognizedFormatAccepted() throws IOException, URISyntaxException { IsiImporter importer = new IsiImporter(); @@ -67,23 +61,21 @@ public void testIsRecognizedFormatAccepted() throws IOException { "IsiImporterTestWOS.isi", "IsiImporterTestMedline.isi"); for (String str : list) { - try (InputStream is = IsiImporterTest.class.getResourceAsStream(str)) { - Assert.assertTrue(importer.isRecognizedFormat(is)); - } + Path file = Paths.get(IsiImporterTest.class.getResource(str).toURI()); + Assert.assertTrue(importer.isRecognizedFormat(file, Charset.defaultCharset())); } } @Test - public void testIsRecognizedFormatRejected() throws IOException { + public void testIsRecognizedFormatRejected() throws IOException, URISyntaxException { IsiImporter importer = new IsiImporter(); List list = Arrays.asList("IsiImporterTestEmpty.isi"); for (String str : list) { - try (InputStream is = IsiImporterTest.class.getResourceAsStream(str)) { - Assert.assertFalse(importer.isRecognizedFormat(is)); - } + Path file = Paths.get(IsiImporterTest.class.getResource(str).toURI()); + Assert.assertFalse(importer.isRecognizedFormat(file, Charset.defaultCharset())); } } @@ -133,109 +125,97 @@ public void testProcessSubSup() { } @Test - public void testImportEntries1() throws IOException { + public void testImportEntries1() throws IOException, URISyntaxException { IsiImporter importer = new IsiImporter(); - - try (InputStream is = IsiImporterTest.class.getResourceAsStream("IsiImporterTest1.isi")) { - - List entries = importer.importEntries(is, new OutputPrinterToNull()); - Assert.assertEquals(1, entries.size()); - BibEntry entry = entries.get(0); - Assert.assertEquals("Optical properties of MgO doped LiNbO$_3$ single crystals", entry.getField("title")); - Assert.assertEquals( - "James Brown and James Marc Brown and Brown, J. M. and Brown, J. and Brown, J. M. and Brown, J.", - entry.getField("author")); - - Assert.assertEquals("article", entry.getType()); - Assert.assertEquals("Optical Materials", entry.getField("journal")); - Assert.assertEquals("2006", entry.getField("year")); - Assert.assertEquals("28", entry.getField("volume")); - Assert.assertEquals("5", entry.getField("number")); - Assert.assertEquals("467--72", entry.getField("pages")); - } + Path file = Paths.get(IsiImporterTest.class.getResource("IsiImporterTest1.isi").toURI()); + List entries = importer.importDatabase(file, Charset.defaultCharset()).getDatabase().getEntries(); + Assert.assertEquals(1, entries.size()); + BibEntry entry = entries.get(0); + Assert.assertEquals("Optical properties of MgO doped LiNbO$_3$ single crystals", entry.getField("title")); + Assert.assertEquals( + "James Brown and James Marc Brown and Brown, J. M. and Brown, J. and Brown, J. M. and Brown, J.", + entry.getField("author")); + + Assert.assertEquals("article", entry.getType()); + Assert.assertEquals("Optical Materials", entry.getField("journal")); + Assert.assertEquals("2006", entry.getField("year")); + Assert.assertEquals("28", entry.getField("volume")); + Assert.assertEquals("5", entry.getField("number")); + Assert.assertEquals("467--72", entry.getField("pages")); } @Test - public void testImportEntries2() throws IOException { + public void testImportEntries2() throws IOException, URISyntaxException { IsiImporter importer = new IsiImporter(); - - try (InputStream is = IsiImporterTest.class.getResourceAsStream("IsiImporterTest2.isi")) { - - List entries = importer.importEntries(is, new OutputPrinterToNull()); - Assert.assertEquals(3, entries.size()); - BibEntry entry = entries.get(0); - Assert.assertEquals("Optical properties of MgO doped LiNbO$_3$ single crystals", entry.getField("title")); - - Assert.assertEquals("misc", entry.getType()); - Assert.assertEquals("Optical Materials", entry.getField("journal")); - Assert.assertEquals("2006", entry.getField("year")); - Assert.assertEquals("28", entry.getField("volume")); - Assert.assertEquals("5", entry.getField("number")); - Assert.assertEquals("467-72", entry.getField("pages")); - } + Path file = Paths.get(IsiImporterTest.class.getResource("IsiImporterTest2.isi").toURI()); + List entries = importer.importDatabase(file, Charset.defaultCharset()).getDatabase().getEntries(); + Assert.assertEquals(3, entries.size()); + BibEntry entry = entries.get(0); + Assert.assertEquals("Optical properties of MgO doped LiNbO$_3$ single crystals", entry.getField("title")); + + Assert.assertEquals("misc", entry.getType()); + Assert.assertEquals("Optical Materials", entry.getField("journal")); + Assert.assertEquals("2006", entry.getField("year")); + Assert.assertEquals("28", entry.getField("volume")); + Assert.assertEquals("5", entry.getField("number")); + Assert.assertEquals("467-72", entry.getField("pages")); } @Test - public void testImportEntriesINSPEC() throws IOException { + public void testImportEntriesINSPEC() throws IOException, URISyntaxException { IsiImporter importer = new IsiImporter(); - - try (InputStream is = IsiImporterTest.class.getResourceAsStream("IsiImporterTestInspec.isi")) { - - List entries = importer.importEntries(is, new OutputPrinterToNull()); - - Assert.assertEquals(2, entries.size()); - BibEntry a = entries.get(0); - BibEntry b = entries.get(1); - - if (a.getField("title") - .equals("Optical and photoelectric spectroscopy of photorefractive Sn$_2$P$_2$S$_6$ crystals")) { - BibEntry tmp = a; - a = b; - b = tmp; - } - - Assert.assertEquals( - "Second harmonic generation of continuous wave ultraviolet light and production of beta -BaB$_2$O$_4$ optical waveguides", - a.getField("title")); - Assert.assertEquals("article", a.getType()); - - Assert.assertEquals("Degl'Innocenti, R. and Guarino, A. and Poberaj, G. and Gunter, P.", - a.getField("author")); - Assert.assertEquals("Applied Physics Letters", a.getField("journal")); - Assert.assertEquals("2006", a.getField("year")); - Assert.assertEquals("#jul#", a.getField("month")); - Assert.assertEquals("89", a.getField("volume")); - Assert.assertEquals("4", a.getField("number")); - Assert.assertEquals( - "Lorem ipsum abstract", - a.getField("abstract")); - Assert.assertEquals("Aip", a.getField("publisher")); - - Assert.assertEquals("Optical and photoelectric spectroscopy of photorefractive Sn$_2$P$_2$S$_6$ crystals", - b.getField("title")); - Assert.assertEquals("article", b.getType()); + Path file = Paths.get(IsiImporterTest.class.getResource("IsiImporterTestInspec.isi").toURI()); + List entries = importer.importDatabase(file, Charset.defaultCharset()).getDatabase().getEntries(); + + Assert.assertEquals(2, entries.size()); + BibEntry a = entries.get(0); + BibEntry b = entries.get(1); + + if (a.getField("title") + .equals("Optical and photoelectric spectroscopy of photorefractive Sn$_2$P$_2$S$_6$ crystals")) { + BibEntry tmp = a; + a = b; + b = tmp; } + + Assert.assertEquals( + "Second harmonic generation of continuous wave ultraviolet light and production of beta -BaB$_2$O$_4$ optical waveguides", + a.getField("title")); + Assert.assertEquals("article", a.getType()); + + Assert.assertEquals("Degl'Innocenti, R. and Guarino, A. and Poberaj, G. and Gunter, P.", + a.getField("author")); + Assert.assertEquals("Applied Physics Letters", a.getField("journal")); + Assert.assertEquals("2006", a.getField("year")); + Assert.assertEquals("#jul#", a.getField("month")); + Assert.assertEquals("89", a.getField("volume")); + Assert.assertEquals("4", a.getField("number")); + Assert.assertEquals( + "Lorem ipsum abstract", + a.getField("abstract")); + Assert.assertEquals("Aip", a.getField("publisher")); + + Assert.assertEquals("Optical and photoelectric spectroscopy of photorefractive Sn$_2$P$_2$S$_6$ crystals", + b.getField("title")); + Assert.assertEquals("article", b.getType()); } @Test - public void testImportEntriesWOS() throws IOException { + public void testImportEntriesWOS() throws IOException, URISyntaxException { IsiImporter importer = new IsiImporter(); + Path file = Paths.get(IsiImporterTest.class.getResource("IsiImporterTestWOS.isi").toURI()); + List entries = importer.importDatabase(file, Charset.defaultCharset()).getDatabase().getEntries(); - try (InputStream is = IsiImporterTest.class.getResourceAsStream("IsiImporterTestWOS.isi")) { - - List entries = importer.importEntries(is, new OutputPrinterToNull()); - - Assert.assertEquals(2, entries.size()); - BibEntry a = entries.get(0); - BibEntry b = entries.get(1); + Assert.assertEquals(2, entries.size()); + BibEntry a = entries.get(0); + BibEntry b = entries.get(1); - Assert.assertEquals("Optical and photoelectric spectroscopy of photorefractive Sn2P2S6 crystals", - a.getField("title")); - Assert.assertEquals("Optical waveguides in Sn2P2S6 by low fluence MeV He+ ion implantation", - b.getField("title")); + Assert.assertEquals("Optical and photoelectric spectroscopy of photorefractive Sn2P2S6 crystals", + a.getField("title")); + Assert.assertEquals("Optical waveguides in Sn2P2S6 by low fluence MeV He+ ion implantation", + b.getField("title")); - Assert.assertEquals("Journal of Physics-condensed Matter", a.getField("journal")); - } + Assert.assertEquals("Journal of Physics-condensed Matter", a.getField("journal")); } @Test @@ -279,115 +259,96 @@ public void testIsiAuthorConvert() { } @Test - public void testGetIsCustomImporter() { + public void testImportIEEEExport() throws IOException, URISyntaxException { IsiImporter importer = new IsiImporter(); - Assert.assertEquals(false, importer.isCustomImporter()); + Path file = Paths.get(IsiImporterTest.class.getResource("IEEEImport1.txt").toURI()); + List entries = importer.importDatabase(file, Charset.defaultCharset()).getDatabase().getEntries(); + + Assert.assertEquals(1, entries.size()); + BibEntry a = entries.get(0); + Assert.assertEquals("article", a.getType()); + Assert.assertEquals("Geoscience and Remote Sensing Letters, IEEE", a.getField("journal")); + Assert.assertEquals("Improving Urban Road Extraction in High-Resolution " + + "Images Exploiting Directional Filtering, Perceptual " + + "Grouping, and Simple Topological Concepts", a.getField("title")); + Assert.assertEquals("4", a.getField("volume")); + Assert.assertEquals("3", a.getField("number")); + Assert.assertEquals("1545-598X", a.getField("SN")); + Assert.assertEquals("387--391", a.getField("pages")); + Assert.assertEquals("Gamba, P. and Dell'Acqua, F. and Lisini, G.", a.getField("author")); + Assert.assertEquals("2006", a.getField("year")); + Assert.assertEquals("Perceptual grouping, street extraction, urban remote sensing", a.getField("keywords")); + Assert.assertEquals("Lorem ipsum abstract", a.getField("abstract")); } @Test - public void testImportIEEEExport() throws IOException { + public void testIEEEImport() throws IOException, URISyntaxException { IsiImporter importer = new IsiImporter(); + Path file = Paths.get(IsiImporterTest.class.getResource("IEEEImport1.txt").toURI()); + List entries = importer.importDatabase(file, Charset.defaultCharset()).getDatabase().getEntries(); - try (InputStream is = IsiImporterTest.class.getResourceAsStream("IEEEImport1.txt")) { - - List entries = importer.importEntries(is, new OutputPrinterToNull()); - - Assert.assertEquals(1, entries.size()); - BibEntry a = entries.get(0); - Assert.assertEquals("article", a.getType()); - Assert.assertEquals("Geoscience and Remote Sensing Letters, IEEE", a.getField("journal")); - Assert.assertEquals("Improving Urban Road Extraction in High-Resolution " - + "Images Exploiting Directional Filtering, Perceptual " - + "Grouping, and Simple Topological Concepts", a.getField("title")); - Assert.assertEquals("4", a.getField("volume")); - Assert.assertEquals("3", a.getField("number")); - Assert.assertEquals("1545-598X", a.getField("SN")); - Assert.assertEquals("387--391", a.getField("pages")); - Assert.assertEquals("Gamba, P. and Dell'Acqua, F. and Lisini, G.", a.getField("author")); - Assert.assertEquals("2006", a.getField("year")); - Assert.assertEquals("Perceptual grouping, street extraction, urban remote sensing", a.getField("keywords")); - Assert.assertEquals("Lorem ipsum abstract", - a.getField("abstract")); + Assert.assertEquals(1, entries.size()); + BibEntry a = entries.get(0); - } + Assert.assertEquals("article", a.getType()); + Assert.assertEquals("Geoscience and Remote Sensing Letters, IEEE", a.getField("journal")); + Assert.assertEquals( + "Improving Urban Road Extraction in High-Resolution Images Exploiting Directional Filtering, Perceptual Grouping, and Simple Topological Concepts", + a.getField("title")); + Assert.assertEquals("4", a.getField("volume")); + Assert.assertEquals("3", a.getField("number")); + Assert.assertEquals("1545-598X", a.getField("SN")); + Assert.assertEquals("387--391", a.getField("pages")); + Assert.assertEquals("Gamba, P. and Dell'Acqua, F. and Lisini, G.", a.getField("author")); + Assert.assertEquals("2006", a.getField("year")); + Assert.assertEquals("Perceptual grouping, street extraction, urban remote sensing", a.getField("keywords")); + Assert.assertEquals("Lorem ipsum abstract", a.getField("abstract")); } @Test - public void testIEEEImport() throws IOException { + public void testImportEntriesMedline() throws IOException, URISyntaxException { IsiImporter importer = new IsiImporter(); + Path file = Paths.get(IsiImporterTest.class.getResource("IsiImporterTestMedline.isi").toURI()); - try (InputStream is = IsiImporterTest.class.getResourceAsStream("IEEEImport1.txt")) { - - List entries = importer.importEntries(is, new OutputPrinterToNull()); + List entries = importer.importDatabase(file, Charset.defaultCharset()).getDatabase().getEntries(); - Assert.assertEquals(1, entries.size()); - BibEntry a = entries.get(0); + Assert.assertEquals(2, entries.size()); + BibEntry a = entries.get(0); + BibEntry b = entries.get(1); - Assert.assertEquals("article", a.getType()); - Assert.assertEquals("Geoscience and Remote Sensing Letters, IEEE", a.getField("journal")); - Assert.assertEquals( - "Improving Urban Road Extraction in High-Resolution Images Exploiting Directional Filtering, Perceptual Grouping, and Simple Topological Concepts", - a.getField("title")); - Assert.assertEquals("4", a.getField("volume")); - Assert.assertEquals("3", a.getField("number")); - Assert.assertEquals("1545-598X", a.getField("SN")); - Assert.assertEquals("387--391", a.getField("pages")); - Assert.assertEquals("Gamba, P. and Dell'Acqua, F. and Lisini, G.", a.getField("author")); - Assert.assertEquals("2006", a.getField("year")); - Assert.assertEquals("Perceptual grouping, street extraction, urban remote sensing", a.getField("keywords")); - Assert.assertEquals("Lorem ipsum abstract", - a.getField("abstract")); + Assert.assertEquals("Effects of modafinil on cognitive performance and alertness during sleep deprivation.", + a.getField("title")); - } - } + Assert.assertEquals("Wesensten, Nancy J.", a.getField("author")); + Assert.assertEquals("Curr Pharm Des", a.getField("journal")); + Assert.assertEquals("2006", a.getField("year")); + Assert.assertEquals(null, a.getField("month")); + Assert.assertEquals("12", a.getField("volume")); + Assert.assertEquals("20", a.getField("number")); + Assert.assertEquals("2457--71", a.getField("pages")); + Assert.assertEquals("article", a.getType()); - @Test - public void testImportEntriesMedline() throws IOException { - IsiImporter importer = new IsiImporter(); - try (InputStream is = IsiImporterTest.class.getResourceAsStream("IsiImporterTestMedline.isi")) { - - List entries = importer.importEntries(is, new OutputPrinterToNull()); - - Assert.assertEquals(2, entries.size()); - BibEntry a = entries.get(0); - BibEntry b = entries.get(1); - - Assert.assertEquals("Effects of modafinil on cognitive performance and alertness during sleep deprivation.", - a.getField("title")); - - Assert.assertEquals("Wesensten, Nancy J.", a.getField("author")); - Assert.assertEquals("Curr Pharm Des", a.getField("journal")); - Assert.assertEquals("2006", a.getField("year")); - Assert.assertEquals(null, a.getField("month")); - Assert.assertEquals("12", a.getField("volume")); - Assert.assertEquals("20", a.getField("number")); - Assert.assertEquals("2457--71", a.getField("pages")); - Assert.assertEquals("article", a.getType()); - - Assert.assertEquals( - "Estrogen therapy selectively enhances prefrontal cognitive processes: a randomized, double-blind, placebo-controlled study with functional magnetic resonance imaging in perimenopausal and recently postmenopausal women.", - b.getField("title")); - Assert.assertEquals( - "Joffe, Hadine and Hall, Janet E. and Gruber, Staci and Sarmiento, Ingrid A. and Cohen, Lee S. and Yurgelun-Todd, Deborah and Martin, Kathryn A.", - b.getField("author")); - Assert.assertEquals("2006", b.getField("year")); - Assert.assertEquals("#may#", b.getField("month")); - Assert.assertEquals("13", b.getField("volume")); - Assert.assertEquals("3", b.getField("number")); - Assert.assertEquals("411--22", b.getField("pages")); - Assert.assertEquals("article", b.getType()); - } + Assert.assertEquals( + "Estrogen therapy selectively enhances prefrontal cognitive processes: a randomized, double-blind, placebo-controlled study with functional magnetic resonance imaging in perimenopausal and recently postmenopausal women.", + b.getField("title")); + Assert.assertEquals( + "Joffe, Hadine and Hall, Janet E. and Gruber, Staci and Sarmiento, Ingrid A. and Cohen, Lee S. and Yurgelun-Todd, Deborah and Martin, Kathryn A.", + b.getField("author")); + Assert.assertEquals("2006", b.getField("year")); + Assert.assertEquals("#may#", b.getField("month")); + Assert.assertEquals("13", b.getField("volume")); + Assert.assertEquals("3", b.getField("number")); + Assert.assertEquals("411--22", b.getField("pages")); + Assert.assertEquals("article", b.getType()); } @Test - public void testImportEntriesEmpty() throws IOException { + public void testImportEntriesEmpty() throws IOException, URISyntaxException { IsiImporter importer = new IsiImporter(); + Path file = Paths.get(IsiImporterTest.class.getResource("IsiImporterTestEmpty.isi").toURI()); - try (InputStream is = IsiImporterTest.class.getResourceAsStream("IsiImporterTestEmpty.isi")) { + List entries = importer.importDatabase(file, Charset.defaultCharset()).getDatabase().getEntries(); - List entries = importer.importEntries(is, new OutputPrinterToNull()); - - Assert.assertEquals(1, entries.size()); - } + Assert.assertEquals(1, entries.size()); } } diff --git a/src/test/java/net/sf/jabref/importer/fileformat/MedlineImporterTest.java b/src/test/java/net/sf/jabref/importer/fileformat/MedlineImporterTest.java index 5a16d5c1f1b..0dcefbbd835 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/MedlineImporterTest.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/MedlineImporterTest.java @@ -1,7 +1,7 @@ package net.sf.jabref.importer.fileformat; import java.io.IOException; -import java.io.InputStream; +import java.nio.charset.Charset; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; @@ -13,14 +13,11 @@ import net.sf.jabref.Globals; import net.sf.jabref.JabRefPreferences; -import net.sf.jabref.importer.OutputPrinterToNull; -import net.sf.jabref.model.entry.BibEntry; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; import static org.junit.Assert.assertEquals; @@ -47,10 +44,10 @@ public class MedlineImporterTest { * @return A list of Names * @throws IOException */ - public List getTestFiles() throws IOException { - List files = new ArrayList<>(); + public List getTestFiles() throws IOException { + List files = new ArrayList<>(); try (DirectoryStream stream = Files.newDirectoryStream(Paths.get(FILEFORMAT_PATH))) { - stream.forEach(n -> files.add(n.getFileName().toString())); + stream.forEach(files::add); } return files; } @@ -61,15 +58,6 @@ public void setUp() throws Exception { this.medlineImporter = new MedlineImporter(); } - @Test - public void testExceptionOnInputStream() throws IOException { - try (InputStream is = Mockito.mock(InputStream.class)) { - Mockito.doThrow(new IOException()).when(is).read(); - List entry = medlineImporter.importEntries(is, new OutputPrinterToNull()); - Assert.assertTrue(entry.isEmpty()); - } - } - @Test public void testGetItemsEmpty() { MedlineHandler handler = new MedlineHandler(); @@ -83,18 +71,16 @@ public void testGetFormatName() { @Test public void testGetCLIId() { - assertEquals("medline", medlineImporter.getCLIId()); + assertEquals("medline", medlineImporter.getId()); } @Test public void testIsRecognizedFormatReject() throws IOException { - List list = getTestFiles().stream().filter(n -> !n.startsWith("MedlineImporter")) + List list = getTestFiles().stream().filter(n -> !n.getFileName().toString().startsWith("MedlineImporter")) .collect(Collectors.toList()); - for (String str : list) { - try (InputStream is = MedlineImporter.class.getResourceAsStream(str)) { - Assert.assertFalse(medlineImporter.isRecognizedFormat(is)); - } + for (Path file : list) { + Assert.assertFalse(file.toString(), medlineImporter.isRecognizedFormat(file, Charset.defaultCharset())); } } } diff --git a/src/test/java/net/sf/jabref/importer/fileformat/MedlineImporterTestFiles.java b/src/test/java/net/sf/jabref/importer/fileformat/MedlineImporterTestFiles.java index 333bbb6330b..f0ada4dbeb4 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/MedlineImporterTestFiles.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/MedlineImporterTestFiles.java @@ -1,7 +1,7 @@ package net.sf.jabref.importer.fileformat; import java.io.IOException; -import java.io.InputStream; +import java.nio.charset.Charset; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; @@ -14,7 +14,6 @@ import net.sf.jabref.Globals; import net.sf.jabref.JabRefPreferences; -import net.sf.jabref.importer.OutputPrinterToNull; import net.sf.jabref.logic.bibtex.BibEntryAssert; import net.sf.jabref.model.entry.BibEntry; @@ -37,7 +36,7 @@ public class MedlineImporterTestFiles { private MedlineImporter medlineImporter; @Parameter - public String fileName; + public Path importFile; @Before @@ -47,33 +46,29 @@ public void setUp() { } @Parameters(name = "{0}") - public static Collection fileNames() throws IOException { - List files = new ArrayList<>(); + public static Collection files() throws IOException { + List files = new ArrayList<>(); try (DirectoryStream stream = Files.newDirectoryStream(Paths.get(FILEFORMAT_PATH))) { - stream.forEach(n -> files.add(n.getFileName().toString())); + stream.forEach(files::add); } - return files.stream().filter(n -> n.startsWith("MedlineImporterTest")).filter(n -> n.endsWith(".xml")) + return files.stream().filter(n -> n.getFileName().toString().startsWith("MedlineImporterTest") && n.getFileName().toString().endsWith(".xml")) .collect(Collectors.toList()); } @Test public void testIsRecognizedFormat() throws IOException { - try (InputStream stream = MedlineImporterTest.class.getResourceAsStream(fileName)) { - Assert.assertTrue(medlineImporter.isRecognizedFormat(stream)); - } + Assert.assertTrue(medlineImporter.isRecognizedFormat(importFile, Charset.defaultCharset())); } @Test @Ignore public void testImportEntries() throws IOException { - try (InputStream inputStream = MedlineImporterTest.class.getResourceAsStream(fileName)) { - List medlineEntries = medlineImporter.importEntries(inputStream, new OutputPrinterToNull()); - String bibFileName = fileName.replace(".xml", ".bib"); + List medlineEntries = medlineImporter.importDatabase(importFile, Charset.defaultCharset()).getDatabase().getEntries(); + String bibFileName = importFile.getFileName().toString().replace(".xml", ".bib"); if (medlineEntries.isEmpty()) { assertEquals(Collections.emptyList(), medlineEntries); } else { BibEntryAssert.assertEquals(MedlineImporterTest.class, bibFileName, medlineEntries); } - } } -} \ No newline at end of file +} diff --git a/src/test/java/net/sf/jabref/importer/fileformat/MedlinePlainImporterTest.java b/src/test/java/net/sf/jabref/importer/fileformat/MedlinePlainImporterTest.java index 7e8718953db..dc795c78ffa 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/MedlinePlainImporterTest.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/MedlinePlainImporterTest.java @@ -1,16 +1,19 @@ package net.sf.jabref.importer.fileformat; -import java.io.ByteArrayInputStream; +import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; -import java.nio.charset.StandardCharsets; +import java.io.StringReader; +import java.net.URISyntaxException; +import java.nio.charset.Charset; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.Collections; import java.util.List; import net.sf.jabref.Globals; import net.sf.jabref.JabRefPreferences; -import net.sf.jabref.importer.OutputPrinterToNull; import net.sf.jabref.logic.bibtex.BibEntryAssert; import net.sf.jabref.model.entry.BibEntry; @@ -18,14 +21,14 @@ import org.junit.Before; import org.junit.Test; +import static org.junit.Assert.assertEquals; + public class MedlinePlainImporterTest { - private final InputStream emptyFileStream = streamForString(""); private MedlinePlainImporter importer; - - private InputStream streamForString(String string) { - return new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8)); + private BufferedReader readerForString(String string) { + return new BufferedReader(new StringReader(string)); } @Before @@ -35,13 +38,12 @@ public void setUp() { } @Test - public void testIsRecognizedFormat() throws IOException { + public void testIsRecognizedFormat() throws IOException, URISyntaxException { List list = Arrays.asList("CopacImporterTest1.txt", "CopacImporterTest2.txt", "IsiImporterTest1.isi", "IsiImporterTestInspec.isi", "IsiImporterTestWOS.isi", "IsiImporterTestMedline.isi"); for (String str : list) { - try (InputStream is = MedlinePlainImporter.class.getResourceAsStream(str)) { - Assert.assertFalse(importer.isRecognizedFormat(is)); - } + Path file = Paths.get(MedlinePlainImporter.class.getResource(str).toURI()); + Assert.assertFalse(importer.isRecognizedFormat(file, Charset.defaultCharset())); } } @@ -52,70 +54,72 @@ public void testIsNotRecognizedFormat() throws Exception { "MedlinePlainImporterTestMultiTitle.txt", "MedlinePlainImporterTestDOI.txt", "MedlinePlainImporterTestInproceeding.txt"); for (String str : list) { - try (InputStream is = MedlinePlainImporter.class.getResourceAsStream(str)) { - Assert.assertTrue(importer.isRecognizedFormat(is)); - } + Path file = Paths.get(MedlinePlainImporter.class.getResource(str).toURI()); + Assert.assertTrue(importer.isRecognizedFormat(file, Charset.defaultCharset())); } } @Test - public void testIsNotEmptyFileRecognizedFormat() throws IOException { - Assert.assertFalse(importer.isRecognizedFormat(emptyFileStream)); + public void doesNotRecognizeEmptyFiles() throws IOException { + Assert.assertFalse(importer.isRecognizedFormat(readerForString(""))); } @Test - public void testImportMultipleEntriesInSingleFile() throws IOException { - try (InputStream is = MedlinePlainImporter.class - .getResourceAsStream("MedlinePlainImporterTestMultipleEntries.txt")) { - - List entries = importer.importEntries(is, new OutputPrinterToNull()); - Assert.assertEquals(7, entries.size()); - - BibEntry testEntry = entries.get(0); - Assert.assertEquals("article", testEntry.getType()); - Assert.assertNull(testEntry.getField("month")); - Assert.assertEquals("Long, Vicky and Marland, Hilary", testEntry.getField("author")); - Assert.assertEquals( - "From danger and motherhood to health and beauty: health advice for the factory girl in early twentieth-century Britain.", - testEntry.getField("title")); - - testEntry = entries.get(1); - Assert.assertEquals("conference", testEntry.getType()); - Assert.assertEquals("06", testEntry.getField("month")); - Assert.assertNull(testEntry.getField("author")); - Assert.assertNull(testEntry.getField("title")); - - testEntry = entries.get(2); - Assert.assertEquals("book", testEntry.getType()); - Assert.assertEquals( - "This is a Testtitle: This title should be appended: This title should also be appended. Another append to the Title? LastTitle", - testEntry.getField("title")); - - testEntry = entries.get(3); - Assert.assertEquals("techreport", testEntry.getType()); - Assert.assertNotNull(testEntry.getField("doi")); - - testEntry = entries.get(4); - Assert.assertEquals("inproceedings", testEntry.getType()); - Assert.assertEquals("Inproceedings book title", testEntry.getField("booktitle")); - - testEntry = entries.get(5); - Assert.assertEquals("proceedings", testEntry.getType()); - - testEntry = entries.get(6); - Assert.assertEquals("misc", testEntry.getType()); - - } + public void testImportMultipleEntriesInSingleFile() throws IOException, URISyntaxException { + Path inputFile = Paths.get( + MedlinePlainImporter.class.getResource("MedlinePlainImporterTestMultipleEntries.txt").toURI()); + + List entries = importer.importDatabase(inputFile, Charset.defaultCharset()).getDatabase() + .getEntries(); + assertEquals(7, entries.size()); + + BibEntry testEntry = entries.get(0); + assertEquals("article", testEntry.getType()); + Assert.assertNull(testEntry.getField("month")); + assertEquals("Long, Vicky and Marland, Hilary", testEntry.getField("author")); + assertEquals( + "From danger and motherhood to health and beauty: health advice for the factory girl in early twentieth-century Britain.", + testEntry.getField("title")); + + testEntry = entries.get(1); + assertEquals("conference", testEntry.getType()); + assertEquals("06", testEntry.getField("month")); + Assert.assertNull(testEntry.getField("author")); + Assert.assertNull(testEntry.getField("title")); + + testEntry = entries.get(2); + assertEquals("book", testEntry.getType()); + assertEquals( + "This is a Testtitle: This title should be appended: This title should also be appended. Another append to the Title? LastTitle", + testEntry.getField("title")); + + testEntry = entries.get(3); + assertEquals("techreport", testEntry.getType()); + Assert.assertNotNull(testEntry.getField("doi")); + + testEntry = entries.get(4); + assertEquals("inproceedings", testEntry.getType()); + assertEquals("Inproceedings book title", testEntry.getField("booktitle")); + + BibEntry expectedEntry5 = new BibEntry(); + expectedEntry5.setType("proceedings"); + expectedEntry5.setField("keywords", "Female"); + assertEquals(expectedEntry5, entries.get(5)); + + BibEntry expectedEntry6 = new BibEntry(); + expectedEntry6.setType("misc"); + expectedEntry6.setField("keywords", "Female"); + assertEquals(expectedEntry6, entries.get(6)); } @Test public void testEmptyFileImport() throws IOException { - List emptyEntries = importer.importEntries(emptyFileStream, new OutputPrinterToNull()); - Assert.assertEquals(Collections.emptyList(), emptyEntries); + List emptyEntries = importer.importDatabase(readerForString("")).getDatabase().getEntries(); + assertEquals(Collections.emptyList(), emptyEntries); } @Test - public void testImportSingleEntriesInSingleFiles() throws IOException { + public void testImportSingleEntriesInSingleFiles() throws IOException, URISyntaxException { List testFiles = Arrays.asList("MedlinePlainImporterTestCompleteEntry", "MedlinePlainImporterTestMultiAbstract", "MedlinePlainImporterTestMultiTitle", "MedlinePlainImporterTestDOI", "MedlinePlainImporterTestInproceeding"); @@ -126,67 +130,72 @@ public void testImportSingleEntriesInSingleFiles() throws IOException { } } - private void assertImportOfMedlineFileEqualsBibtexFile(String medlineFile, String bibtexFile) throws IOException { - try (InputStream is = MedlinePlainImporter.class.getResourceAsStream(medlineFile); - InputStream nis = MedlinePlainImporter.class.getResourceAsStream(bibtexFile)) { - List entries = importer.importEntries(is, new OutputPrinterToNull()); + private void assertImportOfMedlineFileEqualsBibtexFile(String medlineFile, String bibtexFile) + throws IOException, URISyntaxException { + Path file = Paths.get(MedlinePlainImporter.class.getResource(medlineFile).toURI()); + try (InputStream nis = MedlinePlainImporter.class.getResourceAsStream(bibtexFile)) { + List entries = importer.importDatabase(file, Charset.defaultCharset()).getDatabase().getEntries(); Assert.assertNotNull(entries); - Assert.assertEquals(1, entries.size()); + assertEquals(1, entries.size()); BibEntryAssert.assertEquals(nis, entries.get(0)); } } @Test public void testMultiLineComments() throws IOException { - try (InputStream stream = streamForString("PMID-22664220" + "\n" + "CON - Comment1" + "\n" + "CIN - Comment2" - + "\n" + "EIN - Comment3" + "\n" + "EFR - Comment4" + "\n" + "CRI - Comment5" + "\n" + "CRF - Comment6" - + "\n" + "PRIN- Comment7" + "\n" + "PROF- Comment8" + "\n" + "RPI - Comment9" + "\n" + "RPF - Comment10" - + "\n" + "RIN - Comment11" + "\n" + "ROF - Comment12" + "\n" + "UIN - Comment13" + "\n" - + "UOF - Comment14" + "\n" + "SPIN- Comment15" + "\n" + "ORI - Comment16");) { - List actualEntries = importer.importEntries(stream, new OutputPrinterToNull()); - - BibEntry expectedEntry = new BibEntry(); - expectedEntry.setField("comment", - "Comment1" + "\n" + "Comment2" + "\n" + "Comment3" + "\n" + "Comment4" + "\n" + "Comment5" + "\n" - + "Comment6" + "\n" + "Comment7" + "\n" + "Comment8" + "\n" + "Comment9" + "\n" - + "Comment10" + "\n" + "Comment11" + "\n" + "Comment12" + "\n" + "Comment13" + "\n" - + "Comment14" + "\n" + "Comment15" + "\n" + "Comment16"); - Assert.assertEquals(Collections.singletonList(expectedEntry), actualEntries); - } + BufferedReader reader = readerForString( + "PMID-22664220" + "\n" + "CON - Comment1" + "\n" + "CIN - Comment2" + "\n" + "EIN - Comment3" + "\n" + + "EFR - Comment4" + "\n" + "CRI - Comment5" + "\n" + "CRF - Comment6" + "\n" + "PRIN- Comment7" + + "\n" + "PROF- Comment8" + "\n" + "RPI - Comment9" + "\n" + "RPF - Comment10" + "\n" + + "RIN - Comment11" + "\n" + "ROF - Comment12" + "\n" + "UIN - Comment13" + "\n" + + "UOF - Comment14" + "\n" + "SPIN- Comment15" + "\n" + "ORI - Comment16"); + List actualEntries = importer.importDatabase(reader).getDatabase().getEntries(); + + BibEntry expectedEntry = new BibEntry(); + expectedEntry.setField("comment", + "Comment1" + "\n" + "Comment2" + "\n" + "Comment3" + "\n" + "Comment4" + "\n" + "Comment5" + "\n" + + "Comment6" + "\n" + "Comment7" + "\n" + "Comment8" + "\n" + "Comment9" + "\n" + + "Comment10" + "\n" + "Comment11" + "\n" + "Comment12" + "\n" + "Comment13" + "\n" + + "Comment14" + "\n" + "Comment15" + "\n" + "Comment16"); + assertEquals(Collections.singletonList(expectedEntry), actualEntries); } @Test public void testKeyWords() throws IOException { - try (InputStream stream = streamForString("PMID-22664795" + "\n" + "MH - Female" + "\n" + "OT - Male");) { - List actualEntries = importer.importEntries(stream, new OutputPrinterToNull()); + try (BufferedReader reader = readerForString("PMID-22664795" + "\n" + "MH - Female" + "\n" + "OT - Male")) { + List actualEntries = importer.importDatabase(reader).getDatabase().getEntries(); BibEntry expectedEntry = new BibEntry(); expectedEntry.setField("keywords", "Female, Male"); - Assert.assertEquals(Collections.singletonList(expectedEntry), actualEntries); + assertEquals(Collections.singletonList(expectedEntry), actualEntries); } } @Test public void testAllArticleTypes() throws IOException { - try (InputStream stream = streamForString("PMID-22664795" + "\n" + "PT - journal article" + "\n" - + "PT - classical article" + "\n" + "PT - corrected and republished article" + "\n" - + "PT - introductory journal article" + "\n" + "PT - newspaper article");) { - List actualEntries = importer.importEntries(stream, new OutputPrinterToNull()); + try (BufferedReader reader = readerForString("PMID-22664795" + "\n" + + "MH - Female\n" + + "PT - journal article" + "\n" + + "PT - classical article" + "\n" + + "PT - corrected and republished article" + "\n" + + "PT - introductory journal article" + "\n" + "PT - newspaper article")) { + List actualEntries = importer.importDatabase(reader).getDatabase().getEntries(); BibEntry expectedEntry = new BibEntry(); expectedEntry.setType("article"); - Assert.assertEquals(Collections.singletonList(expectedEntry), actualEntries); + expectedEntry.setField("keywords", "Female"); + assertEquals(Collections.singletonList(expectedEntry), actualEntries); } } @Test public void testGetFormatName() { - Assert.assertEquals("MedlinePlain", importer.getFormatName()); + assertEquals("MedlinePlain", importer.getFormatName()); } @Test public void testGetCLIId() { - Assert.assertEquals("medlineplain", importer.getCLIId()); + assertEquals("medlineplain", importer.getId()); } } diff --git a/src/test/java/net/sf/jabref/importer/fileformat/MsBibImporterTest.java b/src/test/java/net/sf/jabref/importer/fileformat/MsBibImporterTest.java index 287a8d044f5..de4d18d692f 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/MsBibImporterTest.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/MsBibImporterTest.java @@ -1,14 +1,16 @@ package net.sf.jabref.importer.fileformat; import java.io.IOException; -import java.io.InputStream; +import java.net.URISyntaxException; +import java.nio.charset.Charset; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.Collections; import java.util.List; import net.sf.jabref.Globals; import net.sf.jabref.JabRefPreferences; -import net.sf.jabref.importer.OutputPrinterToNull; import net.sf.jabref.model.entry.BibEntry; import org.junit.Assert; @@ -28,28 +30,24 @@ public final void testIsNotRecognizedFormat() throws Exception { List notAccepted = Arrays.asList("CopacImporterTest1.txt", "IsiImporterTest1.isi", "IsiImporterTestInspec.isi", "emptyFile.xml", "IsiImporterTestWOS.isi"); for (String s : notAccepted) { - try (InputStream stream = MsBibImporter.class.getResourceAsStream(s)) { - Assert.assertFalse(testImporter.isRecognizedFormat(stream)); - } + Path file = Paths.get(MsBibImporter.class.getResource(s).toURI()); + Assert.assertFalse(testImporter.isRecognizedFormat(file, Charset.defaultCharset())); } - } @Test - public final void testImportEntriesEmpty() throws IOException { + public final void testImportEntriesEmpty() throws IOException, URISyntaxException { MsBibImporter testImporter = new MsBibImporter(); - - List entries = testImporter.importEntries( - MsBibImporterTest.class.getResourceAsStream("MsBibImporterTest.xml"), new OutputPrinterToNull()); + Path file = Paths.get(MsBibImporter.class.getResource("MsBibImporterTest.xml").toURI()); + List entries = testImporter.importDatabase(file, Charset.defaultCharset()).getDatabase().getEntries(); Assert.assertEquals(Collections.emptyList(), entries); } @Test - public final void testImportEntriesNotRecognizedFormat() throws IOException { + public final void testImportEntriesNotRecognizedFormat() throws IOException, URISyntaxException { MsBibImporter testImporter = new MsBibImporter(); - - List entries = testImporter.importEntries( - MsBibImporterTest.class.getResourceAsStream("CopacImporterTest1.txt"), new OutputPrinterToNull()); + Path file = Paths.get(MsBibImporter.class.getResource("CopacImporterTest1.txt").toURI()); + List entries = testImporter.importDatabase(file, Charset.defaultCharset()).getDatabase().getEntries(); Assert.assertEquals(0, entries.size()); } @@ -62,7 +60,7 @@ public final void testGetFormatName() { @Test public final void testGetCommandLineId() { MsBibImporter testImporter = new MsBibImporter(); - Assert.assertEquals("msbib", testImporter.getCommandLineId()); + Assert.assertEquals("msbib", testImporter.getId()); } } diff --git a/src/test/java/net/sf/jabref/importer/fileformat/MsBibImporterTestfiles.java b/src/test/java/net/sf/jabref/importer/fileformat/MsBibImporterTestfiles.java index c47bf4390bb..3e8d1e6d350 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/MsBibImporterTestfiles.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/MsBibImporterTestfiles.java @@ -1,19 +1,21 @@ package net.sf.jabref.importer.fileformat; import java.io.IOException; -import java.io.InputStream; +import java.net.URISyntaxException; +import java.nio.charset.Charset; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.Collection; import java.util.List; import net.sf.jabref.Globals; import net.sf.jabref.JabRefPreferences; -import net.sf.jabref.importer.OutputPrinterToNull; import net.sf.jabref.logic.bibtex.BibEntryAssert; import net.sf.jabref.model.entry.BibEntry; import org.junit.Assert; -import org.junit.BeforeClass; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; @@ -26,10 +28,12 @@ public class MsBibImporterTestfiles { @Parameter public String fileName; + private Path xmlFile; - @BeforeClass - public static void setUp() { + @Before + public void setUp() throws URISyntaxException { Globals.prefs = JabRefPreferences.getInstance(); + xmlFile = Paths.get(MsBibImporter.class.getResource(fileName + ".xml").toURI()); } @Parameters(name = "{index}: {0}") @@ -42,23 +46,18 @@ public static Collection fileNames() { @Test public final void testIsRecognizedFormat() throws Exception { - String xmlFileName = fileName + ".xml"; MsBibImporter testImporter = new MsBibImporter(); - try (InputStream stream = MsBibImporter.class.getResourceAsStream(xmlFileName)) { - Assert.assertTrue(testImporter.isRecognizedFormat(stream)); - } + Assert.assertTrue(testImporter.isRecognizedFormat(xmlFile, Charset.defaultCharset())); } @Test public void testImportEntries() throws IOException { - String xmlFileName = fileName + ".xml"; + String bibFileName = fileName + ".bib"; MsBibImporter testImporter = new MsBibImporter(); - try (InputStream is = MsBibImporter.class.getResourceAsStream(xmlFileName)) { - List result = testImporter.importEntries(is, new OutputPrinterToNull()); - BibEntryAssert.assertEquals(MsBibImporterTest.class, bibFileName, result); - } + List result = testImporter.importDatabase(xmlFile, Charset.defaultCharset()).getDatabase().getEntries(); + BibEntryAssert.assertEquals(MsBibImporterTest.class, bibFileName, result); } } diff --git a/src/test/java/net/sf/jabref/importer/fileformat/OvidImporterTest.java b/src/test/java/net/sf/jabref/importer/fileformat/OvidImporterTest.java index 81a442e9901..5c0c66f8d57 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/OvidImporterTest.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/OvidImporterTest.java @@ -2,13 +2,16 @@ import java.io.IOException; import java.io.InputStream; +import java.net.URISyntaxException; +import java.nio.charset.Charset; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.Collections; import java.util.List; import net.sf.jabref.Globals; import net.sf.jabref.JabRefPreferences; -import net.sf.jabref.importer.OutputPrinterToNull; import net.sf.jabref.logic.bibtex.BibEntryAssert; import net.sf.jabref.model.entry.BibEntry; @@ -34,118 +37,110 @@ public void testGetFormatName() { @Test public void testGetCLIId() { - Assert.assertEquals("ovid", importer.getCLIId()); + Assert.assertEquals("ovid", importer.getId()); } @Test - public void testIsRecognizedFormatAccept() throws IOException { + public void testIsRecognizedFormatAccept() throws IOException, URISyntaxException { List list = Arrays.asList("OvidImporterTest1.txt", "OvidImporterTest3.txt", "OvidImporterTest4.txt", "OvidImporterTest5.txt", "OvidImporterTest6.txt", "OvidImporterTest7.txt"); for (String str : list) { - try (InputStream is = OvidImporter.class.getResourceAsStream(str)) { - Assert.assertTrue(importer.isRecognizedFormat(is)); - } + Path file = Paths.get(OvidImporter.class.getResource(str).toURI()); + Assert.assertTrue(importer.isRecognizedFormat(file, Charset.defaultCharset())); } } @Test - public void testIsRecognizedFormatRejected() throws IOException { + public void testIsRecognizedFormatRejected() throws IOException, URISyntaxException { List list = Arrays.asList("Empty.txt", "OvidImporterTest2.txt"); for (String str : list) { - try (InputStream is = OvidImporter.class.getResourceAsStream(str)) { - Assert.assertFalse(importer.isRecognizedFormat(is)); - } + Path file = Paths.get(OvidImporter.class.getResource(str).toURI()); + Assert.assertFalse(importer.isRecognizedFormat(file, Charset.defaultCharset())); } } @Test - public void testImportEmpty() throws IOException { - - try (InputStream is = OvidImporter.class.getResourceAsStream("Empty.txt")) { - List entries = importer.importEntries(is, new OutputPrinterToNull()); - Assert.assertEquals(Collections.emptyList(), entries); - } + public void testImportEmpty() throws IOException, URISyntaxException { + Path file = Paths.get(OvidImporter.class.getResource("Empty.txt").toURI()); + List entries = importer.importDatabase(file, Charset.defaultCharset()).getDatabase().getEntries(); + Assert.assertEquals(Collections.emptyList(), entries); } @Test - public void testImportEntries1() throws IOException { - - try (InputStream is = OvidImporter.class.getResourceAsStream("OvidImporterTest1.txt")) { - List entries = importer.importEntries(is, new OutputPrinterToNull()); - Assert.assertEquals(5, entries.size()); - - BibEntry entry = entries.get(0); - Assert.assertEquals("misc", entry.getType()); - Assert.assertEquals("Mustermann and Musterfrau", entry.getField("author")); - Assert.assertEquals("Short abstract", entry.getField("abstract")); - Assert.assertEquals("Musterbuch", entry.getField("title")); - Assert.assertEquals("Einleitung", entry.getField("chaptertitle")); - - entry = entries.get(1); - Assert.assertEquals("inproceedings", entry.getType()); - Assert.assertEquals("Max", entry.getField("editor")); - Assert.assertEquals("Max the Editor", entry.getField("title")); - Assert.assertEquals("Very Long Title", entry.getField("journal")); - Assert.assertEquals("28", entry.getField("volume")); - Assert.assertEquals("2", entry.getField("issue")); - Assert.assertEquals("2015", entry.getField("year")); - Assert.assertEquals("103--106", entry.getField("pages")); - - entry = entries.get(2); - Assert.assertEquals("incollection", entry.getType()); - Assert.assertEquals("Max", entry.getField("author")); - Assert.assertEquals("Test", entry.getField("title")); - Assert.assertEquals("Very Long Title", entry.getField("journal")); - Assert.assertEquals("28", entry.getField("volume")); - Assert.assertEquals("2", entry.getField("issue")); - Assert.assertEquals("April", entry.getField("month")); - Assert.assertEquals("2015", entry.getField("year")); - Assert.assertEquals("103--106", entry.getField("pages")); - - entry = entries.get(3); - Assert.assertEquals("book", entry.getType()); - Assert.assertEquals("Max", entry.getField("author")); - Assert.assertEquals("2015", entry.getField("year")); - Assert.assertEquals("Editor", entry.getField("editor")); - Assert.assertEquals("Very Long Title", entry.getField("booktitle")); - Assert.assertEquals("103--106", entry.getField("pages")); - Assert.assertEquals("Address", entry.getField("address")); - Assert.assertEquals("Publisher", entry.getField("publisher")); - - entry = entries.get(4); - Assert.assertEquals("article", entry.getType()); - Assert.assertEquals("2014", entry.getField("year")); - Assert.assertEquals("58", entry.getField("pages")); - Assert.assertEquals("Test", entry.getField("address")); - Assert.assertNull(entry.getField("title")); - Assert.assertEquals("TestPublisher", entry.getField("publisher")); - } + public void testImportEntries1() throws IOException, URISyntaxException { + Path file = Paths.get(OvidImporter.class.getResource("OvidImporterTest1.txt").toURI()); + List entries = importer.importDatabase(file, Charset.defaultCharset()).getDatabase().getEntries(); + Assert.assertEquals(5, entries.size()); + + BibEntry entry = entries.get(0); + Assert.assertEquals("misc", entry.getType()); + Assert.assertEquals("Mustermann and Musterfrau", entry.getField("author")); + Assert.assertEquals("Short abstract", entry.getField("abstract")); + Assert.assertEquals("Musterbuch", entry.getField("title")); + Assert.assertEquals("Einleitung", entry.getField("chaptertitle")); + + entry = entries.get(1); + Assert.assertEquals("inproceedings", entry.getType()); + Assert.assertEquals("Max", entry.getField("editor")); + Assert.assertEquals("Max the Editor", entry.getField("title")); + Assert.assertEquals("Very Long Title", entry.getField("journal")); + Assert.assertEquals("28", entry.getField("volume")); + Assert.assertEquals("2", entry.getField("issue")); + Assert.assertEquals("2015", entry.getField("year")); + Assert.assertEquals("103--106", entry.getField("pages")); + + entry = entries.get(2); + Assert.assertEquals("incollection", entry.getType()); + Assert.assertEquals("Max", entry.getField("author")); + Assert.assertEquals("Test", entry.getField("title")); + Assert.assertEquals("Very Long Title", entry.getField("journal")); + Assert.assertEquals("28", entry.getField("volume")); + Assert.assertEquals("2", entry.getField("issue")); + Assert.assertEquals("April", entry.getField("month")); + Assert.assertEquals("2015", entry.getField("year")); + Assert.assertEquals("103--106", entry.getField("pages")); + + entry = entries.get(3); + Assert.assertEquals("book", entry.getType()); + Assert.assertEquals("Max", entry.getField("author")); + Assert.assertEquals("2015", entry.getField("year")); + Assert.assertEquals("Editor", entry.getField("editor")); + Assert.assertEquals("Very Long Title", entry.getField("booktitle")); + Assert.assertEquals("103--106", entry.getField("pages")); + Assert.assertEquals("Address", entry.getField("address")); + Assert.assertEquals("Publisher", entry.getField("publisher")); + + entry = entries.get(4); + Assert.assertEquals("article", entry.getType()); + Assert.assertEquals("2014", entry.getField("year")); + Assert.assertEquals("58", entry.getField("pages")); + Assert.assertEquals("Test", entry.getField("address")); + Assert.assertNull(entry.getField("title")); + Assert.assertEquals("TestPublisher", entry.getField("publisher")); } @Test - public void testImportEntries2() throws IOException { - - try (InputStream is = OvidImporter.class.getResourceAsStream("OvidImporterTest2.txt")) { - List entries = importer.importEntries(is, new OutputPrinterToNull()); - Assert.assertEquals(Collections.emptyList(), entries); - } + public void testImportEntries2() throws IOException, URISyntaxException { + Path file = Paths.get(OvidImporter.class.getResource("OvidImporterTest2.txt").toURI()); + List entries = importer.importDatabase(file, Charset.defaultCharset()).getDatabase().getEntries(); + Assert.assertEquals(Collections.emptyList(), entries); } @Test - public void testImportSingleEntries() throws IOException { + public void testImportSingleEntries() throws IOException, URISyntaxException { for (int n = 3; n <= 7; n++) { - try (InputStream is = OvidImporter.class.getResourceAsStream("OvidImporterTest" + n + ".txt"); - InputStream nis = OvidImporter.class.getResourceAsStream("OvidImporterTestBib" + n + ".bib")) { - List entries = importer.importEntries(is, new OutputPrinterToNull()); + Path file = Paths.get(OvidImporter.class.getResource("OvidImporterTest" + n + ".txt").toURI()); + try (InputStream nis = OvidImporter.class.getResourceAsStream("OvidImporterTestBib" + n + ".bib")) { + List entries = importer.importDatabase(file, Charset.defaultCharset()).getDatabase().getEntries(); Assert.assertNotNull(entries); Assert.assertEquals(1, entries.size()); BibEntryAssert.assertEquals(nis, entries.get(0)); } } } -} \ No newline at end of file +} diff --git a/src/test/java/net/sf/jabref/importer/fileformat/PdfContentImporterTest.java b/src/test/java/net/sf/jabref/importer/fileformat/PdfContentImporterTest.java index 7fd630c1520..3e853d66c09 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/PdfContentImporterTest.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/PdfContentImporterTest.java @@ -1,7 +1,10 @@ package net.sf.jabref.importer.fileformat; import java.io.IOException; -import java.io.InputStream; +import java.net.URISyntaxException; +import java.nio.charset.Charset; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Collections; import java.util.List; @@ -14,12 +17,11 @@ public class PdfContentImporterTest { @Test - public void doesNotHandleEncryptedPdfs() throws IOException { + public void doesNotHandleEncryptedPdfs() throws IOException, URISyntaxException { PdfContentImporter importer = new PdfContentImporter(); - try (InputStream is = PdfContentImporter.class.getResourceAsStream("/pdfs/encrypted.pdf")) { - List result = importer.importEntries(is, null); - assertEquals(Collections.emptyList(), result); - } + Path file = Paths.get(PdfContentImporter.class.getResource("/pdfs/encrypted.pdf").toURI()); + List result = importer.importDatabase(file, Charset.defaultCharset()).getDatabase().getEntries(); + assertEquals(Collections.emptyList(), result); } } diff --git a/src/test/java/net/sf/jabref/importer/fileformat/PdfContentImporterTestFiles.java b/src/test/java/net/sf/jabref/importer/fileformat/PdfContentImporterTestFiles.java index f3b6a0d3fc1..305cb611382 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/PdfContentImporterTestFiles.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/PdfContentImporterTestFiles.java @@ -1,7 +1,10 @@ package net.sf.jabref.importer.fileformat; import java.io.IOException; -import java.io.InputStream; +import java.net.URISyntaxException; +import java.nio.charset.Charset; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.Collection; import java.util.List; @@ -43,14 +46,13 @@ public static Collection fileNames() { } @Test - public void correctContent() throws IOException { + public void correctContent() throws IOException, URISyntaxException { String pdfFileName = fileName + ".pdf"; String bibFileName = fileName + ".bib"; PdfContentImporter importer = new PdfContentImporter(); - try (InputStream is = PdfContentImporter.class.getResourceAsStream(pdfFileName)) { - List result = importer.importEntries(is, null); - BibEntryAssert.assertEquals(PdfContentImporterTest.class, bibFileName, result); - } + Path pdfFile = Paths.get(PdfContentImporter.class.getResource(pdfFileName).toURI()); + List result = importer.importDatabase(pdfFile, Charset.defaultCharset()).getDatabase().getEntries(); + BibEntryAssert.assertEquals(PdfContentImporterTest.class, bibFileName, result); } } diff --git a/src/test/java/net/sf/jabref/importer/fileformat/PdfXmpImporterTest.java b/src/test/java/net/sf/jabref/importer/fileformat/PdfXmpImporterTest.java index c9a21a59d73..ae3bfd84081 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/PdfXmpImporterTest.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/PdfXmpImporterTest.java @@ -1,16 +1,19 @@ package net.sf.jabref.importer.fileformat; import java.io.IOException; -import java.io.InputStream; +import java.net.URISyntaxException; +import java.nio.charset.Charset; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.List; import net.sf.jabref.Globals; import net.sf.jabref.JabRefPreferences; -import net.sf.jabref.importer.OutputPrinterToNull; -import net.sf.jabref.logic.xmp.EncryptedPdfsNotSupportedException; +import net.sf.jabref.importer.ParserResult; import net.sf.jabref.model.entry.BibEntry; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -34,49 +37,46 @@ public void testGetFormatName() { assertEquals("XMP-annotated PDF", importer.getFormatName()); } - @Test(expected = EncryptedPdfsNotSupportedException.class) - public void importEncryptedFileThrowsException() throws IOException { - try (InputStream is = PdfXmpImporterTest.class.getResourceAsStream("/pdfs/encrypted.pdf")) { - importer.importEntries(is, new OutputPrinterToNull()); - } + @Test + public void importEncryptedFileReturnsError() throws IOException, URISyntaxException { + Path file = Paths.get(PdfXmpImporterTest.class.getResource("/pdfs/encrypted.pdf").toURI()); + ParserResult result = importer.importDatabase(file, Charset.defaultCharset()); + Assert.assertTrue(result.hasWarnings()); } @Test - public void testImportEntries() throws IOException { - try (InputStream is = PdfXmpImporterTest.class.getResourceAsStream("annotated.pdf")) { - List bibEntries = importer.importEntries(is, new OutputPrinterToNull()); + public void testImportEntries() throws IOException, URISyntaxException { + Path file = Paths.get(PdfXmpImporterTest.class.getResource("annotated.pdf").toURI()); + List bibEntries = importer.importDatabase(file, Charset.defaultCharset()).getDatabase().getEntries(); - assertEquals(1, bibEntries.size()); + assertEquals(1, bibEntries.size()); - BibEntry be0 = bibEntries.get(0); - assertEquals("how to annotate a pdf", be0.getField("abstract")); - assertEquals("Chris", be0.getField("author")); - assertEquals("pdf, annotation", be0.getField("keywords")); - assertEquals("The best Pdf ever", be0.getField("title")); - } + BibEntry be0 = bibEntries.get(0); + assertEquals("how to annotate a pdf", be0.getField("abstract")); + assertEquals("Chris", be0.getField("author")); + assertEquals("pdf, annotation", be0.getField("keywords")); + assertEquals("The best Pdf ever", be0.getField("title")); } @Test - public void testIsRecognizedFormat() throws IOException { - try (InputStream is = PdfXmpImporterTest.class.getResourceAsStream("annotated.pdf")) { - assertTrue(importer.isRecognizedFormat(is)); - } + public void testIsRecognizedFormat() throws IOException, URISyntaxException { + Path file = Paths.get(PdfXmpImporterTest.class.getResource("annotated.pdf").toURI()); + assertTrue(importer.isRecognizedFormat(file, Charset.defaultCharset())); } @Test - public void testIsRecognizedFormatReject() throws IOException { + public void testIsRecognizedFormatReject() throws IOException, URISyntaxException { List list = Arrays.asList("IEEEImport1.txt", "IsiImporterTest1.isi", "IsiImporterTestInspec.isi", "IsiImporterTestWOS.isi", "IsiImporterTestMedline.isi", "RisImporterTest1.ris", "empty.pdf"); for (String str : list) { - try (InputStream is = PdfXmpImporterTest.class.getResourceAsStream(str)) { - assertFalse(importer.isRecognizedFormat(is)); - } + Path file = Paths.get(PdfXmpImporterTest.class.getResource(str).toURI()); + assertFalse(importer.isRecognizedFormat(file, Charset.defaultCharset())); } } @Test public void testGetCommandLineId() { - assertEquals("xmp", importer.getCommandLineId()); + assertEquals("xmp", importer.getId()); } } diff --git a/src/test/java/net/sf/jabref/importer/fileformat/RISImporterTest.java b/src/test/java/net/sf/jabref/importer/fileformat/RISImporterTest.java index 8d9d32b8f6e..75a0f683acc 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/RISImporterTest.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/RISImporterTest.java @@ -1,7 +1,10 @@ package net.sf.jabref.importer.fileformat; import java.io.IOException; -import java.io.InputStream; +import java.net.URISyntaxException; +import java.nio.charset.Charset; +import java.nio.file.Path; +import java.nio.file.Paths; import net.sf.jabref.Globals; import net.sf.jabref.JabRefPreferences; @@ -28,14 +31,13 @@ public void testGetFormatName() { @Test public void testGetCLIId() { - Assert.assertEquals(risImporter.getCLIId(), "ris"); + Assert.assertEquals(risImporter.getId(), "ris"); } @Test - public void testIfNotRecognizedFormat() throws IOException { - try (InputStream stream = RISImporterTest.class.getResourceAsStream("RisImporterCorrupted.ris")) { - Assert.assertFalse(risImporter.isRecognizedFormat(stream)); - } + public void testIfNotRecognizedFormat() throws IOException, URISyntaxException { + Path file = Paths.get(RISImporterTest.class.getResource("RisImporterCorrupted.ris").toURI()); + Assert.assertFalse(risImporter.isRecognizedFormat(file, Charset.defaultCharset())); } } diff --git a/src/test/java/net/sf/jabref/importer/fileformat/RISImporterTestFiles.java b/src/test/java/net/sf/jabref/importer/fileformat/RISImporterTestFiles.java index 5b6ab5d02e7..508711c4bfd 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/RISImporterTestFiles.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/RISImporterTestFiles.java @@ -1,14 +1,16 @@ package net.sf.jabref.importer.fileformat; import java.io.IOException; -import java.io.InputStream; +import java.net.URISyntaxException; +import java.nio.charset.Charset; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.Collection; import java.util.List; import net.sf.jabref.Globals; import net.sf.jabref.JabRefPreferences; -import net.sf.jabref.importer.OutputPrinterToNull; import net.sf.jabref.logic.bibtex.BibEntryAssert; import net.sf.jabref.model.entry.BibEntry; @@ -28,34 +30,29 @@ public class RISImporterTestFiles { @Parameter public String fileName; + private Path risFile; @Before - public void setUp() { + public void setUp() throws URISyntaxException { Globals.prefs = JabRefPreferences.getInstance(); risImporter = new RisImporter(); + risFile = Paths.get(RISImporterTest.class.getResource(fileName + ".ris").toURI()); } @Parameters(name = "{0}") public static Collection fileNames() { - return Arrays - .asList(new String[] {"RisImporterTest1", "RisImporterTest3", "RisImporterTest4a", "RisImporterTest4b", - "RisImporterTest4c", "RisImporterTest5a", "RisImporterTest5b", "RisImporterTest6"}); + return Arrays.asList("RisImporterTest1", "RisImporterTest3", "RisImporterTest4a", "RisImporterTest4b", + "RisImporterTest4c", "RisImporterTest5a", "RisImporterTest5b", "RisImporterTest6"); } @Test public void testIsRecognizedFormat() throws IOException { - try (InputStream stream = RISImporterTest.class.getResourceAsStream(fileName + ".ris")) { - Assert.assertTrue(risImporter.isRecognizedFormat(stream)); - } + Assert.assertTrue(risImporter.isRecognizedFormat(risFile, Charset.defaultCharset())); } @Test public void testImportEntries() throws IOException { - try (InputStream risStream = RISImporterTest.class.getResourceAsStream(fileName + ".ris")) { - - List risEntries = risImporter.importEntries(risStream, new OutputPrinterToNull()); - BibEntryAssert.assertEquals(RISImporterTest.class, fileName + ".bib", risEntries); - - } + List risEntries = risImporter.importDatabase(risFile, Charset.defaultCharset()).getDatabase().getEntries(); + BibEntryAssert.assertEquals(RISImporterTest.class, fileName + ".bib", risEntries); } } diff --git a/src/test/java/net/sf/jabref/importer/fileformat/RepecNepImporterTest.java b/src/test/java/net/sf/jabref/importer/fileformat/RepecNepImporterTest.java index 4b113c79bfb..8d3b8082c03 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/RepecNepImporterTest.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/RepecNepImporterTest.java @@ -2,12 +2,15 @@ import java.io.IOException; import java.io.InputStream; +import java.net.URISyntaxException; +import java.nio.charset.Charset; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.List; import net.sf.jabref.Globals; import net.sf.jabref.JabRefPreferences; -import net.sf.jabref.importer.OutputPrinterToNull; import net.sf.jabref.logic.bibtex.BibEntryAssert; import net.sf.jabref.model.entry.BibEntry; @@ -27,57 +30,50 @@ public void setUp() { } @Test - public final void testIsRecognizedFormat() throws IOException { + public final void testIsRecognizedFormat() throws IOException, URISyntaxException { List accepted = Arrays.asList("RepecNepImporterTest1.txt", "RepecNepImporterTest2.txt", "RepecNepImporterTest3.txt"); for (String s : accepted) { - try (InputStream stream = RepecNepImporter.class.getResourceAsStream(s)) { - Assert.assertTrue(testImporter.isRecognizedFormat(stream)); - } + Path file = Paths.get(RepecNepImporter.class.getResource(s).toURI()); + Assert.assertTrue(testImporter.isRecognizedFormat(file, Charset.defaultCharset())); } } @Test - public final void testIsNotRecognizedFormat() throws IOException { + public final void testIsNotRecognizedFormat() throws IOException, URISyntaxException { List notAccepted = Arrays.asList("RepecNep1.xml", "CopacImporterTest1.txt", "RisImporterTest1.ris", "CopacImporterTest2.txt", "IEEEImport1.txt"); for (String s : notAccepted) { - try (InputStream stream = RepecNepImporter.class.getResourceAsStream(s)) { - Assert.assertFalse(testImporter.isRecognizedFormat(stream)); - } + Path file = Paths.get(RepecNepImporter.class.getResource(s).toURI()); + Assert.assertFalse(testImporter.isRecognizedFormat(file, Charset.defaultCharset())); } } - @Test(expected = IOException.class) - public final void testImportEntriesNull() throws IOException { - testImporter.importEntries(null, new OutputPrinterToNull()); - } - @Test - public final void testImportEntries1() throws IOException { - try (InputStream in = RepecNepImporter.class.getResourceAsStream("RepecNepImporterTest1.txt"); - InputStream bibIn = RepecNepImporter.class.getResourceAsStream("RepecNepImporterTest1.bib")) { - List entries = testImporter.importEntries(in, new OutputPrinterToNull()); + public final void testImportEntries1() throws IOException, URISyntaxException { + Path file = Paths.get(RepecNepImporter.class.getResource("RepecNepImporterTest1.txt").toURI()); + try (InputStream bibIn = RepecNepImporter.class.getResourceAsStream("RepecNepImporterTest1.bib")) { + List entries = testImporter.importDatabase(file, Charset.defaultCharset()).getDatabase().getEntries(); Assert.assertEquals(1, entries.size()); BibEntryAssert.assertEquals(bibIn, entries.get(0)); } } @Test - public final void testImportEntries2() throws IOException { - try (InputStream in = RepecNepImporter.class.getResourceAsStream("RepecNepImporterTest2.txt"); - InputStream bibIn = RepecNepImporter.class.getResourceAsStream("RepecNepImporterTest2.bib")) { - List entries = testImporter.importEntries(in, new OutputPrinterToNull()); + public final void testImportEntries2() throws IOException, URISyntaxException { + Path file = Paths.get(RepecNepImporter.class.getResource("RepecNepImporterTest2.txt").toURI()); + try (InputStream bibIn = RepecNepImporter.class.getResourceAsStream("RepecNepImporterTest2.bib")) { + List entries = testImporter.importDatabase(file, Charset.defaultCharset()).getDatabase().getEntries(); Assert.assertEquals(1, entries.size()); BibEntryAssert.assertEquals(bibIn, entries.get(0)); } } @Test - public final void testImportEntries3() throws IOException { - try (InputStream in = RepecNepImporter.class.getResourceAsStream("RepecNepImporterTest3.txt"); - InputStream bibIn = RepecNepImporter.class.getResourceAsStream("RepecNepImporterTest3.bib")) { - List entries = testImporter.importEntries(in, new OutputPrinterToNull()); + public final void testImportEntries3() throws IOException, URISyntaxException { + Path file = Paths.get(RepecNepImporter.class.getResource("RepecNepImporterTest3.txt").toURI()); + try (InputStream bibIn = RepecNepImporter.class.getResourceAsStream("RepecNepImporterTest3.bib")) { + List entries = testImporter.importDatabase(file, Charset.defaultCharset()).getDatabase().getEntries(); Assert.assertEquals(1, entries.size()); BibEntryAssert.assertEquals(bibIn, entries.get(0)); } @@ -91,7 +87,7 @@ public final void testGetFormatName() { @Test public final void testGetCliId() { - Assert.assertEquals("repecnep", testImporter.getCLIId()); + Assert.assertEquals("repecnep", testImporter.getId()); } @Test @@ -102,9 +98,4 @@ public final void testGetDescription() { + "copy&paste the papers you want to import and make sure, one of the first lines\n" + "contains the line \"nep.repec.org\".", testImporter.getDescription()); } - - @Test - public final void testGetExtensions() { - Assert.assertEquals(".txt", testImporter.getExtensions()); - } } diff --git a/src/test/java/net/sf/jabref/importer/fileformat/SilverPlatterImporterTest.java b/src/test/java/net/sf/jabref/importer/fileformat/SilverPlatterImporterTest.java index e452ca8c649..40950ba2732 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/SilverPlatterImporterTest.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/SilverPlatterImporterTest.java @@ -1,13 +1,15 @@ package net.sf.jabref.importer.fileformat; import java.io.InputStream; +import java.nio.charset.Charset; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.Collection; import java.util.List; import net.sf.jabref.Globals; import net.sf.jabref.JabRefPreferences; -import net.sf.jabref.importer.OutputPrinterToNull; import net.sf.jabref.logic.bibtex.BibEntryAssert; import net.sf.jabref.model.entry.BibEntry; @@ -27,7 +29,7 @@ public class SilverPlatterImporterTest { @Parameter public String filename; - public String txtName; + public Path txtFile; public String bibName; @@ -35,7 +37,7 @@ public class SilverPlatterImporterTest { public void setUp() throws Exception { Globals.prefs = JabRefPreferences.getInstance(); testImporter = new SilverPlatterImporter(); - txtName = filename + ".txt"; + txtFile = Paths.get(SilverPlatterImporterTest.class.getResource(filename + ".txt").toURI()); bibName = filename + ".bib"; } @@ -47,16 +49,13 @@ public static Collection fileNames() { @Test public final void testIsRecognizedFormat() throws Exception { - try (InputStream stream = SilverPlatterImporterTest.class.getResourceAsStream(txtName)) { - Assert.assertTrue(testImporter.isRecognizedFormat(stream)); - } + Assert.assertTrue(testImporter.isRecognizedFormat(txtFile, Charset.defaultCharset())); } @Test public final void testImportEntries() throws Exception { - try (InputStream in = SilverPlatterImporter.class.getResourceAsStream(txtName); - InputStream bibIn = SilverPlatterImporterTest.class.getResourceAsStream(bibName)) { - List entries = testImporter.importEntries(in, new OutputPrinterToNull()); + try (InputStream bibIn = SilverPlatterImporterTest.class.getResourceAsStream(bibName)) { + List entries = testImporter.importDatabase(txtFile, Charset.defaultCharset()).getDatabase().getEntries(); BibEntryAssert.assertEquals(bibIn, entries); } } diff --git a/src/test/java/net/sf/jabref/importer/fileformat/SilverPlatterImporterTestNotRecognized.java b/src/test/java/net/sf/jabref/importer/fileformat/SilverPlatterImporterTestNotRecognized.java index 8e42ae41e84..6e11e7503fb 100644 --- a/src/test/java/net/sf/jabref/importer/fileformat/SilverPlatterImporterTestNotRecognized.java +++ b/src/test/java/net/sf/jabref/importer/fileformat/SilverPlatterImporterTestNotRecognized.java @@ -1,6 +1,8 @@ package net.sf.jabref.importer.fileformat; -import java.io.InputStream; +import java.nio.charset.Charset; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.List; @@ -27,9 +29,8 @@ public final void testIsNotRecognizedFormat() throws Exception { List notAccept = Arrays.asList("emptyFile.xml", "IsiImporterTest1.isi", "oai2.xml", "RisImporterTest1.ris", "InspecImportTest2.txt"); for (String s : notAccept) { - try (InputStream stream = SilverPlatterImporter.class.getResourceAsStream(s)) { - Assert.assertFalse(testImporter.isRecognizedFormat(stream)); - } + Path file = Paths.get(SilverPlatterImporter.class.getResource(s).toURI()); + Assert.assertFalse(testImporter.isRecognizedFormat(file, Charset.defaultCharset())); } } diff --git a/src/test/java/net/sf/jabref/logic/bibtex/BibEntryAssert.java b/src/test/java/net/sf/jabref/logic/bibtex/BibEntryAssert.java index e68e68d9252..219fd087734 100644 --- a/src/test/java/net/sf/jabref/logic/bibtex/BibEntryAssert.java +++ b/src/test/java/net/sf/jabref/logic/bibtex/BibEntryAssert.java @@ -4,12 +4,15 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; -import java.io.UnsupportedEncodingException; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Collections; import java.util.List; -import net.sf.jabref.importer.OutputPrinterToNull; import net.sf.jabref.importer.ParserResult; import net.sf.jabref.importer.fileformat.BibtexParser; import net.sf.jabref.importer.fileformat.ImportFormat; @@ -27,7 +30,7 @@ public class BibEntryAssert { * @param resourceName the resource to read * @param entry the entry to compare with */ - public static void assertEquals(Class clazz, String resourceName, BibEntry entry) + public static void assertEquals(Class clazz, String resourceName, BibEntry entry) throws IOException { Assert.assertNotNull(clazz); Assert.assertNotNull(resourceName); @@ -45,7 +48,7 @@ public static void assertEquals(Class clazz, String resourceNa * @param resourceName the resource to read * @param asIsEntries a list containing a single entry to compare with */ - public static void assertEquals(Class clazz, String resourceName, List asIsEntries) + public static void assertEquals(Class clazz, String resourceName, List asIsEntries) throws IOException { Assert.assertNotNull(clazz); Assert.assertNotNull(resourceName); @@ -73,14 +76,14 @@ private static List getListFromInputStream(InputStream is) throws IOEx * @param actualEntries a list containing a single entry to compare with */ public static void assertEquals(InputStream expectedInputStream, List actualEntries) - throws UnsupportedEncodingException, IOException { + throws IOException { Assert.assertNotNull(expectedInputStream); Assert.assertNotNull(actualEntries); Assert.assertEquals(getListFromInputStream(expectedInputStream), actualEntries); } public static void assertEquals(List expectedEntries, InputStream actualInputStream) - throws UnsupportedEncodingException, IOException { + throws IOException { Assert.assertNotNull(actualInputStream); Assert.assertNotNull(expectedEntries); Assert.assertEquals(expectedEntries, getListFromInputStream(actualInputStream)); @@ -94,34 +97,43 @@ public static void assertEquals(List expectedEntries, InputStream actu * @param actual the entry to compare with */ public static void assertEquals(InputStream expected, BibEntry actual) - throws UnsupportedEncodingException, IOException { + throws IOException { assertEquals(expected, Collections.singletonList(actual)); } /** - * Compares two InputStreams. For each InputStream a list will be created. expectedIs is read directly, actualIs is filtered through importerForActualIs to convert to a list of BibEntries. + * Compares two InputStreams. For each InputStream a list will be created. expectedIs is read directly, actualIs is filtered through importFormat to convert to a list of BibEntries. * @param expectedIs A BibtexImporter InputStream. - * @param actualIs Your ImportFormat InputStream you want to compare with a BibtexImporter ImportStream. - * @param importerForActualIs The fileformat you want to use to convert the actualIs to the list of expected BibEntries + * @param fileToImport The path to the file to be imported. + * @param importFormat The fileformat you want to use to read the passed file to get the list of expected BibEntries * @throws IOException */ - public static void assertEquals(InputStream expectedIs, InputStream actualIs, ImportFormat importerForActualIs) + public static void assertEquals(InputStream expectedIs, Path fileToImport, ImportFormat importFormat) throws IOException { - List actualEntries = importerForActualIs.importEntries(actualIs, new OutputPrinterToNull()); - Assert.assertEquals(getListFromInputStream(expectedIs), actualEntries); + assertEquals(getListFromInputStream(expectedIs), fileToImport, importFormat); + } + + public static void assertEquals(InputStream expectedIs, URL fileToImport, ImportFormat importFormat) + throws URISyntaxException, IOException { + assertEquals(expectedIs, Paths.get(fileToImport.toURI()), importFormat); } /** * Compares a list of BibEntries to an InputStream. actualIs is filtered through importerForActualIs to convert to a list of BibEntries. - * @param expectedIs A BibtexImporter InputStream. - * @param actualIs Your ImportFormat InputStream you want to compare with a BibtexImporter ImportStream. - * @param importerForActualIs The fileformat you want to use to convert the actualIs to the list of expected BibEntries + * @param expected A BibtexImporter InputStream. + * @param fileToImport The path to the file to be imported. + * @param importFormat The fileformat you want to use to read the passed file to get the list of expected BibEntries * @throws IOException */ - public static void assertEquals(List expected, InputStream actualIs, ImportFormat importerForActualIs) + public static void assertEquals(List expected, Path fileToImport, ImportFormat importFormat) throws IOException { - List actualEntries = importerForActualIs.importEntries(actualIs, new OutputPrinterToNull()); + List actualEntries = importFormat.importDatabase(fileToImport, Charset.defaultCharset()) + .getDatabase().getEntries(); Assert.assertEquals(expected, actualEntries); } + public static void assertEquals(List expected, URL fileToImport, ImportFormat importFormat) + throws URISyntaxException, IOException { + assertEquals(expected, Paths.get(fileToImport.toURI()), importFormat); + } } diff --git a/src/test/java/net/sf/jabref/logic/openoffice/OOBibStyleTest.java b/src/test/java/net/sf/jabref/logic/openoffice/OOBibStyleTest.java index 73bdbf148e0..2626ebe43fa 100644 --- a/src/test/java/net/sf/jabref/logic/openoffice/OOBibStyleTest.java +++ b/src/test/java/net/sf/jabref/logic/openoffice/OOBibStyleTest.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; @@ -15,9 +16,9 @@ import net.sf.jabref.Globals; import net.sf.jabref.JabRefMain; import net.sf.jabref.JabRefPreferences; -import net.sf.jabref.importer.ImportFormatReader; import net.sf.jabref.importer.ParserResult; import net.sf.jabref.importer.fileformat.BibtexParser; +import net.sf.jabref.importer.fileformat.ImportFormat; import net.sf.jabref.logic.journals.JournalAbbreviationRepository; import net.sf.jabref.logic.layout.Layout; import net.sf.jabref.model.database.BibDatabase; @@ -142,8 +143,8 @@ public void testGetCitProperty() throws IOException { @Test public void testGetCitationMarker() throws IOException { - File testBibtexFile = new File("src/test/resources/testbib/complex.bib"); - ParserResult result = BibtexParser.parse(ImportFormatReader.getReader(testBibtexFile, StandardCharsets.UTF_8)); + Path testBibtexFile = Paths.get("src/test/resources/testbib/complex.bib"); + ParserResult result = BibtexParser.parse(ImportFormat.getReader(testBibtexFile, StandardCharsets.UTF_8)); OOBibStyle style = new OOBibStyle(StyleLoader.DEFAULT_NUMERICAL_STYLE_PATH, mock(JournalAbbreviationRepository.class)); Map entryDBMap = new HashMap<>(); @@ -163,8 +164,8 @@ public void testGetCitationMarker() throws IOException { @Test public void testLayout() throws IOException { - File testBibtexFile = new File("src/test/resources/testbib/complex.bib"); - ParserResult result = BibtexParser.parse(ImportFormatReader.getReader(testBibtexFile, StandardCharsets.UTF_8)); + Path testBibtexFile = Paths.get("src/test/resources/testbib/complex.bib"); + ParserResult result = BibtexParser.parse(ImportFormat.getReader(testBibtexFile, StandardCharsets.UTF_8)); OOBibStyle style = new OOBibStyle(StyleLoader.DEFAULT_NUMERICAL_STYLE_PATH, mock(JournalAbbreviationRepository.class)); BibDatabase db = result.getDatabase(); @@ -516,4 +517,4 @@ public void testEmptyStringPropertyAndOxfordComma() throws URISyntaxException, I } -} \ No newline at end of file +} diff --git a/src/test/resources/net/sf/jabref/importer/fileformat/MedlinePlainImporterTestMultipleEntries.txt b/src/test/resources/net/sf/jabref/importer/fileformat/MedlinePlainImporterTestMultipleEntries.txt index 0f16addc682..c9c85e07d32 100644 --- a/src/test/resources/net/sf/jabref/importer/fileformat/MedlinePlainImporterTestMultipleEntries.txt +++ b/src/test/resources/net/sf/jabref/importer/fileformat/MedlinePlainImporterTestMultipleEntries.txt @@ -94,6 +94,8 @@ JT - Inproceedings book title PMID-96578310 PT - Overall +MH - Female PMID-45984220 -PT - +PT - +MH - Female