Skip to content

Commit

Permalink
Rewrite CustomImporter
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez committed Sep 24, 2016
1 parent 1fd13fd commit 8cd7d3f
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 243 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
import net.sf.jabref.gui.keyboard.KeyBinding;
import net.sf.jabref.gui.util.GUIUtil;
import net.sf.jabref.logic.help.HelpFile;
import net.sf.jabref.logic.importer.CustomImporter;
import net.sf.jabref.logic.importer.fileformat.Importer;
import net.sf.jabref.logic.importer.fileformat.CustomImporter;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.util.FileExtensions;

Expand Down Expand Up @@ -79,21 +78,19 @@ public ImportCustomizationDialog(final JabRefFrame frame) {

JButton addFromFolderButton = new JButton(Localization.lang("Add from folder"));
addFromFolderButton.addActionListener(e -> {
CustomImporter importer = new CustomImporter();

FileDialog dialog = new FileDialog(frame).withExtension(FileExtensions.CLASS);
dialog.setDefaultExtension(FileExtensions.CLASS);
Optional<Path> selectedFile = dialog.showDialogAndGetSelectedFile();

if (selectedFile.isPresent() && (selectedFile.get().getParent() != null)) {
importer.setBasePath(selectedFile.get().getParent().toString());

String chosenFileStr = selectedFile.toString();
String chosenFileStr = selectedFile.get().toString();

try {
importer.setClassName(pathToClass(importer.getFileFromBasePath(), new File(chosenFileStr)));
importer.setName(importer.getInstance().getFormatName());
importer.setCliId(importer.getInstance().getId());
String basePath = selectedFile.get().getParent().toString();
String className = pathToClass(basePath, new File(chosenFileStr));
CustomImporter importer = new CustomImporter(basePath, className);

addOrReplaceImporter(importer);
customImporterTable.revalidate();
customImporterTable.repaint();
Expand Down Expand Up @@ -146,14 +143,7 @@ public ImportCustomizationDialog(final JabRefFrame frame) {
JOptionPane.showMessageDialog(frame, Localization.lang("Please select an importer."));
} else {
CustomImporter importer = ((ImportTableModel) customImporterTable.getModel()).getImporter(row);
try {
Importer importFormat = importer.getInstance();
JOptionPane.showMessageDialog(frame, importFormat.getDescription());
} catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException exc) {
LOGGER.warn("Could not instantiate importer " + importer.getName(), exc);
JOptionPane.showMessageDialog(frame, Localization.lang("Could not instantiate %0 %1",
importer.getName() + ":\n", exc.getMessage()));
}
JOptionPane.showMessageDialog(frame, importer.getDescription());
}
});

Expand Down Expand Up @@ -232,11 +222,11 @@ public Dimension getSize() {
* @param path path that includes base-path as a prefix
* @return class name
*/
private static String pathToClass(File basePath, File path) {
private static String pathToClass(String basePath, File path) {
String className = null;
File actualPath = path;
// remove leading basepath from path
while (!actualPath.equals(basePath)) {
while (!actualPath.equals(new File(basePath))) {
className = actualPath.getName() + (className == null ? "" : "." + className);
actualPath = actualPath.getParentFile();
}
Expand Down Expand Up @@ -280,11 +270,11 @@ public Object getValueAt(int rowIndex, int columnIndex) {
if (columnIndex == 0) {
value = importer.getName();
} else if (columnIndex == 1) {
value = importer.getClidId();
value = importer.getName();
} else if (columnIndex == 2) {
value = importer.getClassName();
} else if (columnIndex == 3) {
value = importer.getFileFromBasePath();
value = importer.getBasePath();
}
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import net.sf.jabref.gui.undo.NamedCompound;
import net.sf.jabref.gui.worker.AbstractWorker;
import net.sf.jabref.logic.importer.ImportFormatReader;
import net.sf.jabref.logic.importer.ParserResult;
import net.sf.jabref.logic.importer.Importer;
import net.sf.jabref.logic.importer.ParserResult;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.util.UpdateField;
import net.sf.jabref.model.database.BibDatabase;
Expand Down
20 changes: 7 additions & 13 deletions src/main/java/net/sf/jabref/gui/importer/ZipFileChooser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.IOException;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
Expand All @@ -29,7 +28,6 @@

import net.sf.jabref.gui.util.GUIUtil;
import net.sf.jabref.logic.importer.fileformat.CustomImporter;
import net.sf.jabref.logic.importer.Importer;
import net.sf.jabref.logic.l10n.Localization;

import org.apache.commons.logging.Log;
Expand Down Expand Up @@ -82,21 +80,17 @@ public ZipFileChooser(ImportCustomizationDialog importCustomizationDialog, ZipFi
} else {
ZipFileChooserTableModel model = (ZipFileChooserTableModel) table.getModel();
ZipEntry tempZipEntry = model.getZipEntry(row);
CustomImporter importer = new CustomImporter();
importer.setBasePath(model.getZipFile().getName());
String className = tempZipEntry.getName().substring(0, tempZipEntry.getName().lastIndexOf('.'))
.replace("/", ".");
importer.setClassName(className);
String className = tempZipEntry.getName().substring(0, tempZipEntry.getName().lastIndexOf('.')).replace(
"/", ".");

try {
Importer importFormat = importer.getInstance();
importer.setName(importFormat.getFormatName());
importer.setCliId(importFormat.getId());
CustomImporter importer = new CustomImporter(model.getZipFile().getName(), className);
importCustomizationDialog.addOrReplaceImporter(importer);
dispose();
} catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException exc) {
LOGGER.warn("Could not instantiate importer: " + importer.getName(), exc);
} catch (ClassNotFoundException exc) {
LOGGER.warn("Could not instantiate importer: " + className, exc);
JOptionPane.showMessageDialog(this, Localization.lang("Could not instantiate %0 %1",
importer.getName() + ":\n", exc.getMessage()));
className + ":\n", exc.getMessage()));
}
}
});
Expand Down
132 changes: 0 additions & 132 deletions src/main/java/net/sf/jabref/logic/importer/CustomImporter.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import net.sf.jabref.logic.bibtex.FieldContentParserPreferences;
import net.sf.jabref.logic.bibtexkeypattern.BibtexKeyPatternPreferences;
import net.sf.jabref.logic.importer.fileformat.CustomImporter;

public class ImportFormatPreferences {

Expand Down
14 changes: 4 additions & 10 deletions src/main/java/net/sf/jabref/logic/importer/ImportFormatReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,7 @@ public void resetImportFormats(ImportFormatPreferences newImportFormatPreference
* Get custom import formats
*/
for (CustomImporter importer : importFormatPreferences.getCustomImportList()) {
try {
Importer imFo = importer.getInstance();
formats.add(imFo);
} catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
LOGGER.error("Could not instantiate " + importer.getName()
+ " importer, will ignore it. Please check if the class is still available.", e);
}
formats.add(importer);
}
}

Expand Down Expand Up @@ -141,9 +135,9 @@ public String getImportFormatList() {
StringBuilder sb = new StringBuilder();

for (Importer imFo : formats) {
int pad = Math.max(0, 14 - imFo.getFormatName().length());
int pad = Math.max(0, 14 - imFo.getName().length());
sb.append(" ");
sb.append(imFo.getFormatName());
sb.append(imFo.getName());

sb.append(StringUtil.repeatSpaces(pad));

Expand Down Expand Up @@ -214,7 +208,7 @@ public UnknownFormatImport importUnknownFormat(Path filePath) {
if (entryCount > bestResultCount) {
bestResult = entries;
bestResultCount = bestResult.size();
bestFormatName = imFo.getFormatName();
bestFormatName = imFo.getName();
}
} catch (IOException ex) {
// The import did not succeed. Go on.
Expand Down
Loading

0 comments on commit 8cd7d3f

Please sign in to comment.