use of org.jabref.model.database.BibDatabase in project jabref by JabRef.
the class OpenOfficePanel method exportEntries.
private void exportEntries() {
try {
if (style == null) {
style = loader.getUsedStyle();
} else {
style.ensureUpToDate();
}
ooBase.updateSortedReferenceMarks();
List<BibDatabase> databases = getBaseList();
List<String> unresolvedKeys = ooBase.refreshCiteMarkers(databases, style);
BibDatabase newDatabase = ooBase.generateDatabase(databases);
if (!unresolvedKeys.isEmpty()) {
JOptionPane.showMessageDialog(frame, Localization.lang("Your OpenOffice/LibreOffice document references the BibTeX key '%0', which could not be found in your current library.", unresolvedKeys.get(0)), Localization.lang("Unable to generate new library"), JOptionPane.ERROR_MESSAGE);
}
Defaults defaults = new Defaults(Globals.prefs.getDefaultBibDatabaseMode());
BibDatabaseContext databaseContext = new BibDatabaseContext(newDatabase, defaults);
this.frame.addTab(databaseContext, true);
} catch (BibEntryNotFoundException ex) {
JOptionPane.showMessageDialog(frame, Localization.lang("Your OpenOffice/LibreOffice document references the BibTeX key '%0', which could not be found in your current library.", ex.getBibtexKey()), Localization.lang("Unable to synchronize bibliography"), JOptionPane.ERROR_MESSAGE);
LOGGER.debug("BibEntry not found", ex);
} catch (com.sun.star.lang.IllegalArgumentException | UnknownPropertyException | PropertyVetoException | UndefinedCharacterFormatException | NoSuchElementException | WrappedTargetException | IOException | CreationException e) {
LOGGER.warn("Problem generating new database.", e);
}
}
use of org.jabref.model.database.BibDatabase in project jabref by JabRef.
the class OOBibBase method insertEntry.
/**
* This method inserts a cite marker in the text for the given BibEntry,
* and may refresh the bibliography.
* @param entries The entries to cite.
* @param database The database the entry belongs to.
* @param style The bibliography style we are using.
* @param inParenthesis Indicates whether it is an in-text citation or a citation in parenthesis.
* This is not relevant if numbered citations are used.
* @param withText Indicates whether this should be a normal citation (true) or an empty
* (invisible) citation (false).
* @param sync Indicates whether the reference list should be refreshed.
* @throws IllegalTypeException
* @throws PropertyExistException
* @throws NotRemoveableException
* @throws UnknownPropertyException
* @throws UndefinedCharacterFormatException
* @throws NoSuchElementException
* @throws WrappedTargetException
* @throws IOException
* @throws PropertyVetoException
* @throws CreationException
* @throws BibEntryNotFoundException
* @throws UndefinedParagraphFormatException
*/
public void insertEntry(List<BibEntry> entries, BibDatabase database, List<BibDatabase> allBases, OOBibStyle style, boolean inParenthesis, boolean withText, String pageInfo, boolean sync) throws IllegalArgumentException, UnknownPropertyException, NotRemoveableException, PropertyExistException, IllegalTypeException, UndefinedCharacterFormatException, WrappedTargetException, NoSuchElementException, PropertyVetoException, IOException, CreationException, BibEntryNotFoundException, UndefinedParagraphFormatException {
try {
XTextViewCursor xViewCursor = xViewCursorSupplier.getViewCursor();
if (entries.size() > 1) {
if (style.getBooleanCitProperty(OOBibStyle.MULTI_CITE_CHRONOLOGICAL)) {
entries.sort(yearAuthorTitleComparator);
} else {
entries.sort(entryComparator);
}
}
String keyString = String.join(",", entries.stream().map(entry -> entry.getCiteKeyOptional().orElse("")).collect(Collectors.toList()));
// Insert bookmark:
String bName = getUniqueReferenceMarkName(keyString, withText ? inParenthesis ? OOBibBase.AUTHORYEAR_PAR : OOBibBase.AUTHORYEAR_INTEXT : OOBibBase.INVISIBLE_CIT);
// If we should store metadata for page info, do that now:
if (pageInfo != null) {
LOGGER.info("Storing page info: " + pageInfo);
setCustomProperty(bName, pageInfo);
}
xViewCursor.getText().insertString(xViewCursor, " ", false);
if (style.isFormatCitations()) {
XPropertySet xCursorProps = UnoRuntime.queryInterface(XPropertySet.class, xViewCursor);
String charStyle = style.getCitationCharacterFormat();
try {
xCursorProps.setPropertyValue(CHAR_STYLE_NAME, charStyle);
} catch (UnknownPropertyException | PropertyVetoException | IllegalArgumentException | WrappedTargetException ex) {
// Setting the character format failed, so we throw an exception that
// will result in an error message for the user. Before that,
// delete the space we inserted:
xViewCursor.goLeft((short) 1, true);
xViewCursor.setString("");
throw new UndefinedCharacterFormatException(charStyle);
}
}
xViewCursor.goLeft((short) 1, false);
Map<BibEntry, BibDatabase> databaseMap = new HashMap<>();
for (BibEntry entry : entries) {
databaseMap.put(entry, database);
}
String citeText = style.isNumberEntries() ? "-" : style.getCitationMarker(entries, databaseMap, inParenthesis, null, null);
insertReferenceMark(bName, citeText, xViewCursor, withText, style);
xViewCursor.collapseToEnd();
xViewCursor.goRight((short) 1, false);
XTextRange position = xViewCursor.getEnd();
if (sync) {
// To account for numbering and for uniqiefiers, we must refresh the cite markers:
updateSortedReferenceMarks();
refreshCiteMarkers(allBases, style);
// Insert it at the current position:
rebuildBibTextSection(allBases, style);
}
// Go back to the relevant position:
xViewCursor.gotoRange(position, false);
} catch (DisposedException ex) {
// or catch a DisposedException (which is in a OO JAR file).
throw new ConnectionLostException(ex.getMessage());
}
}
use of org.jabref.model.database.BibDatabase in project jabref by JabRef.
the class OOBibBase method insertFullReferenceAtCursor.
private void insertFullReferenceAtCursor(XTextCursor cursor, Map<BibEntry, BibDatabase> entries, OOBibStyle style, String parFormat) throws UndefinedParagraphFormatException, IllegalArgumentException, UnknownPropertyException, PropertyVetoException, WrappedTargetException {
Map<BibEntry, BibDatabase> correctEntries;
// If we don't have numbered entries, we need to sort the entries before adding them:
if (style.isSortByPosition()) {
// Use the received map directly
correctEntries = entries;
} else {
// Sort map
Map<BibEntry, BibDatabase> newMap = new TreeMap<>(entryComparator);
newMap.putAll(entries);
correctEntries = newMap;
}
int number = 1;
for (Map.Entry<BibEntry, BibDatabase> entry : correctEntries.entrySet()) {
if (entry.getKey() instanceof UndefinedBibtexEntry) {
continue;
}
OOUtil.insertParagraphBreak(text, cursor);
if (style.isNumberEntries()) {
int minGroupingCount = style.getIntCitProperty(OOBibStyle.MINIMUM_GROUPING_COUNT);
OOUtil.insertTextAtCurrentLocation(text, cursor, style.getNumCitationMarker(Collections.singletonList(number++), minGroupingCount, true), Collections.emptyList());
}
Layout layout = style.getReferenceFormat(entry.getKey().getType());
layout.setPostFormatter(POSTFORMATTER);
OOUtil.insertFullReferenceAtCurrentLocation(text, cursor, layout, parFormat, entry.getKey(), entry.getValue(), uniquefiers.get(entry.getKey().getCiteKeyOptional().orElse(null)));
}
}
use of org.jabref.model.database.BibDatabase in project jabref by JabRef.
the class OOBibBase method generateDatabase.
public BibDatabase generateDatabase(List<BibDatabase> databases) throws NoSuchElementException, WrappedTargetException {
BibDatabase resultDatabase = new BibDatabase();
List<String> cited = findCitedKeys();
// For each cited key
for (String key : cited) {
// Loop through the available databases
for (BibDatabase loopDatabase : databases) {
Optional<BibEntry> entry = loopDatabase.getEntryByKey(key);
// If entry found
if (entry.isPresent()) {
BibEntry clonedEntry = (BibEntry) entry.get().clone();
// Insert a copy of the entry
resultDatabase.insertEntry(clonedEntry);
// Check if the cloned entry has a crossref field
clonedEntry.getField(FieldName.CROSSREF).ifPresent(crossref -> {
if (!resultDatabase.getEntryByKey(crossref).isPresent()) {
loopDatabase.getEntryByKey(crossref).ifPresent(resultDatabase::insertEntry);
}
});
// Be happy with the first found BibEntry and move on to next key
break;
}
}
}
return resultDatabase;
}
use of org.jabref.model.database.BibDatabase in project jabref by JabRef.
the class OOBibBase method getSortedEntriesFromSortedRefMarks.
private Map<BibEntry, BibDatabase> getSortedEntriesFromSortedRefMarks(List<String> names, Map<String, BibDatabase> linkSourceBase) {
Map<BibEntry, BibDatabase> newList = new LinkedHashMap<>();
for (String name : names) {
Matcher citeMatcher = CITE_PATTERN.matcher(name);
if (citeMatcher.find()) {
String[] keys = citeMatcher.group(2).split(",");
for (String key : keys) {
BibDatabase database = linkSourceBase.get(key);
Optional<BibEntry> origEntry = Optional.empty();
if (database != null) {
origEntry = database.getEntryByKey(key);
}
if (origEntry.isPresent()) {
if (!newList.containsKey(origEntry.get())) {
newList.put(origEntry.get(), database);
}
} else {
LOGGER.info("BibTeX key not found: '" + key + "'");
LOGGER.info("Problem with reference mark: '" + name + "'");
newList.put(new UndefinedBibtexEntry(key), null);
}
}
}
}
return newList;
}
Aggregations