use of org.jabref.model.entry.BibEntry in project jabref by JabRef.
the class DroppedFileHandler method tryXmpImport.
// Done by MrDlib
private boolean tryXmpImport(String fileName, ExternalFileType fileType, NamedCompound edits) {
if (!"pdf".equals(fileType.getExtension())) {
return false;
}
List<BibEntry> xmpEntriesInFile;
try {
xmpEntriesInFile = XMPUtil.readXMP(fileName, Globals.prefs.getXMPPreferences());
} catch (IOException e) {
LOGGER.warn("Problem reading XMP", e);
return false;
}
if ((xmpEntriesInFile == null) || xmpEntriesInFile.isEmpty()) {
return false;
}
JLabel confirmationMessage = new JLabel(Localization.lang("The PDF contains one or several BibTeX-records.") + "\n" + Localization.lang("Do you want to import these as new entries into the current library?"));
JPanel entriesPanel = new JPanel();
entriesPanel.setLayout(new BoxLayout(entriesPanel, BoxLayout.Y_AXIS));
xmpEntriesInFile.forEach(entry -> {
JTextArea entryArea = new JTextArea(entry.toString());
entryArea.setEditable(false);
entriesPanel.add(entryArea);
});
JPanel contentPanel = new JPanel(new BorderLayout());
contentPanel.add(confirmationMessage, BorderLayout.NORTH);
contentPanel.add(entriesPanel, BorderLayout.CENTER);
int reply = JOptionPane.showConfirmDialog(frame, contentPanel, Localization.lang("XMP-metadata found in PDF: %0", fileName), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
if (reply == JOptionPane.CANCEL_OPTION) {
// The user canceled thus that we are done.
return true;
}
if (reply == JOptionPane.NO_OPTION) {
return false;
}
// reply == JOptionPane.YES_OPTION)
/*
* TODO Extract Import functionality from ImportMenuItem then we could
* do:
*
* ImportMenuItem importer = new ImportMenuItem(frame, (mainTable ==
* null), new PdfXmpImporter());
*
* importer.automatedImport(new String[] { fileName });
*/
boolean isSingle = xmpEntriesInFile.size() == 1;
BibEntry single = isSingle ? xmpEntriesInFile.get(0) : null;
boolean success = true;
String destFilename;
if (linkInPlace.isSelected()) {
destFilename = FileUtil.shortenFileName(Paths.get(fileName), panel.getBibDatabaseContext().getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences())).toString();
} else {
if (renameCheckBox.isSelected() || (single == null)) {
destFilename = fileName;
} else {
destFilename = single.getCiteKey() + "." + fileType.getExtension();
}
if (copyRadioButton.isSelected()) {
success = doCopy(fileName, destFilename, edits);
} else if (moveRadioButton.isSelected()) {
success = doMove(fileName, destFilename, edits);
}
}
if (success) {
for (BibEntry aXmpEntriesInFile : xmpEntriesInFile) {
aXmpEntriesInFile.setId(IdGenerator.next());
edits.addEdit(new UndoableInsertEntry(panel.getDatabase(), aXmpEntriesInFile, panel));
panel.getDatabase().insertEntry(aXmpEntriesInFile);
doLink(aXmpEntriesInFile, fileType, destFilename, true, edits);
}
panel.markBaseChanged();
panel.updateEntryEditorIfShowing();
}
return true;
}
use of org.jabref.model.entry.BibEntry in project jabref by JabRef.
the class FindFullTextAction method update.
@Override
public void update() {
List<Optional<URL>> remove = new ArrayList<>();
for (Entry<Optional<URL>, BibEntry> download : downloads.entrySet()) {
BibEntry entry = download.getValue();
Optional<URL> result = download.getKey();
if (result.isPresent()) {
List<String> dirs = basePanel.getBibDatabaseContext().getFileDirectories(Globals.prefs.getFileDirectoryPreferences());
if (dirs.isEmpty()) {
JOptionPane.showMessageDialog(basePanel.frame(), Localization.lang("Main file directory not set!") + " " + Localization.lang("Preferences") + " -> " + Localization.lang("File"), Localization.lang("Directory not found"), JOptionPane.ERROR_MESSAGE);
return;
}
DownloadExternalFile def = new DownloadExternalFile(basePanel.frame(), basePanel.getBibDatabaseContext(), entry);
try {
def.download(result.get(), file -> {
FileListTableModel fileLinkModel = new FileListTableModel();
entry.getField(FieldName.FILE).ifPresent(fileLinkModel::setContent);
fileLinkModel.addEntry(0, file);
String newValue = fileLinkModel.getStringRepresentation();
UndoableFieldChange edit = new UndoableFieldChange(entry, FieldName.FILE, entry.getField(FieldName.FILE).orElse(null), newValue);
entry.setField(FieldName.FILE, newValue);
basePanel.getUndoManager().addEdit(edit);
basePanel.markBaseChanged();
});
} catch (IOException e) {
LOGGER.warn("Problem downloading file", e);
}
basePanel.output(Localization.lang("Finished downloading full text document for entry %0.", entry.getCiteKeyOptional().orElse(Localization.lang("undefined"))));
} else {
String title = Localization.lang("Full text document download failed");
String message = Localization.lang("Full text document download failed for entry %0.", entry.getCiteKeyOptional().orElse(Localization.lang("undefined")));
basePanel.output(message);
JOptionPane.showMessageDialog(basePanel.frame(), message, title, JOptionPane.ERROR_MESSAGE);
}
remove.add(result);
}
for (Optional<URL> result : remove) {
downloads.remove(result);
}
}
use of org.jabref.model.entry.BibEntry in project jabref by JabRef.
the class ACMPortalFetcher method downloadEntryBibTeX.
private static Optional<BibEntry> downloadEntryBibTeX(String id, boolean downloadAbstract) {
try {
URL url = new URL(ACMPortalFetcher.START_URL + ACMPortalFetcher.BIBTEX_URL + id + ACMPortalFetcher.BIBTEX_URL_END);
URLConnection connection = url.openConnection();
// set user-agent to avoid being blocked as a crawler
connection.addRequestProperty("User-Agent", URLDownload.USER_AGENT);
Collection<BibEntry> items = null;
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
String htmlCode = in.lines().filter(s -> !s.isEmpty()).collect(Collectors.joining());
String bibtexString = htmlCode.substring(htmlCode.indexOf(START_BIBTEX_ENTRY), htmlCode.indexOf(END_BIBTEX_ENTRY_HTML));
items = new BibtexParser(Globals.prefs.getImportFormatPreferences()).parseEntries(bibtexString);
} catch (IOException | ParseException e) {
LOGGER.info("Download of BibTeX information from ACM Portal failed.", e);
}
if ((items == null) || items.isEmpty()) {
return Optional.empty();
}
BibEntry entry = items.iterator().next();
//wait between requests or you will be blocked by ACM
Thread.sleep(ACMPortalFetcher.WAIT_TIME);
// get abstract
if (downloadAbstract) {
URLDownload dl = new URLDownload(ACMPortalFetcher.START_URL + ACMPortalFetcher.ABSTRACT_URL + id);
String page = dl.asString(Globals.prefs.getDefaultEncoding());
Matcher absM = ACMPortalFetcher.ABSTRACT_PATTERN.matcher(page);
if (absM.find()) {
entry.setField(FieldName.ABSTRACT, absM.group(1).trim());
}
//wait between requests or you will be blocked by ACM
Thread.sleep(ACMPortalFetcher.WAIT_TIME);
}
return Optional.of(entry);
} catch (NoSuchElementException e) {
LOGGER.info("Bad BibTeX record read at: " + ACMPortalFetcher.BIBTEX_URL + id + ACMPortalFetcher.BIBTEX_URL_END, e);
} catch (MalformedURLException e) {
LOGGER.info("Malformed URL.", e);
} catch (IOException e) {
LOGGER.info("Cannot connect.", e);
} catch (InterruptedException ignored) {
// Ignored
}
return Optional.empty();
}
use of org.jabref.model.entry.BibEntry in project jabref by JabRef.
the class AppendDatabaseAction method mergeFromBibtex.
private static void mergeFromBibtex(BasePanel panel, ParserResult parserResult, boolean importEntries, boolean importStrings, boolean importGroups, boolean importSelectorWords) throws KeyCollisionException {
BibDatabase fromDatabase = parserResult.getDatabase();
List<BibEntry> appendedEntries = new ArrayList<>();
List<BibEntry> originalEntries = new ArrayList<>();
BibDatabase database = panel.getDatabase();
NamedCompound ce = new NamedCompound(Localization.lang("Append library"));
MetaData meta = parserResult.getMetaData();
if (importEntries) {
// Add entries
boolean overwriteOwner = Globals.prefs.getBoolean(JabRefPreferences.OVERWRITE_OWNER);
boolean overwriteTimeStamp = Globals.prefs.getBoolean(JabRefPreferences.OVERWRITE_TIME_STAMP);
for (BibEntry originalEntry : fromDatabase.getEntries()) {
BibEntry entry = (BibEntry) originalEntry.clone();
UpdateField.setAutomaticFields(entry, overwriteOwner, overwriteTimeStamp, Globals.prefs.getUpdateFieldPreferences());
database.insertEntry(entry);
appendedEntries.add(entry);
originalEntries.add(originalEntry);
ce.addEdit(new UndoableInsertEntry(database, entry, panel));
}
}
if (importStrings) {
for (BibtexString bs : fromDatabase.getStringValues()) {
if (!database.hasStringLabel(bs.getName())) {
database.addString(bs);
ce.addEdit(new UndoableInsertString(panel, database, bs));
}
}
}
if (importGroups) {
meta.getGroups().ifPresent(newGroups -> {
if (newGroups.getGroup() instanceof AllEntriesGroup) {
try {
ExplicitGroup group = new ExplicitGroup("Imported", GroupHierarchyType.INDEPENDENT, Globals.prefs.getKeywordDelimiter());
newGroups.setGroup(group);
group.add(appendedEntries);
} catch (IllegalArgumentException e) {
LOGGER.error(e);
}
}
addGroups(newGroups, ce);
});
}
if (importSelectorWords) {
for (ContentSelector selector : meta.getContentSelectorList()) {
panel.getBibDatabaseContext().getMetaData().addContentSelector(selector);
}
}
ce.end();
panel.getUndoManager().addEdit(ce);
panel.markBaseChanged();
}
use of org.jabref.model.entry.BibEntry in project jabref by JabRef.
the class EntryFromFileCreatorManager method addEntriesFromFiles.
/**
* Tries to add a entry for each file in the List.
*
* @param files
* @param database
* @param panel
* @param entryType
* @param generateKeywordsFromPathToFile
* @param changeListener
* @param importGUIMessages list of unexpected import event - Messages including
* failures
* @return Returns The number of entries added
*/
public int addEntriesFromFiles(List<File> files, BibDatabase database, BasePanel panel, EntryType entryType, boolean generateKeywordsFromPathToFile, ChangeListener changeListener, List<String> importGUIMessages) {
int count = 0;
CompoundEdit ce = new CompoundEdit();
for (File f : files) {
EntryFromFileCreator creator = getEntryCreator(f);
if (creator == null) {
importGUIMessages.add("Problem importing " + f.getPath() + ": Unknown filetype.");
} else {
Optional<BibEntry> entry = creator.createEntry(f, generateKeywordsFromPathToFile);
if (!entry.isPresent()) {
importGUIMessages.add("Problem importing " + f.getPath() + ": Entry could not be created.");
continue;
}
if (entryType != null) {
entry.get().setType(entryType);
}
if (entry.get().getId() == null) {
entry.get().setId(IdGenerator.next());
}
/*
* TODO: database.insertEntry(BibEntry) is not sensible. Why
* does 'true' mean "There were duplicates", while 'false' means
* "Everything alright"?
*/
if (!database.containsEntryWithId(entry.get().getId())) {
// Therefore, we only insert the entry if it is not already present
if (database.insertEntry(entry.get())) {
importGUIMessages.add("Problem importing " + f.getPath() + ": Insert into BibDatabase failed.");
} else {
count++;
if (panel != null) {
ce.addEdit(new UndoableInsertEntry(database, entry.get(), panel));
}
}
}
}
if (changeListener != null) {
changeListener.stateChanged(new ChangeEvent(this));
}
}
if ((count > 0) && (panel != null)) {
ce.end();
panel.getUndoManager().addEdit(ce);
}
return count;
}
Aggregations