use of org.jabref.model.entry.BibtexString in project jabref by JabRef.
the class ChangeScanner method scanStrings.
private void scanStrings(BibDatabase inMem1, BibDatabase inTmp, BibDatabase onDisk) {
if (inTmp.hasNoStrings() && onDisk.hasNoStrings()) {
return;
}
Set<Object> used = new HashSet<>();
Set<Object> usedInMem = new HashSet<>();
Set<String> notMatched = new HashSet<>(inTmp.getStringCount());
// First try to match by string names.
mainLoop: for (String key : inTmp.getStringKeySet()) {
BibtexString tmp = inTmp.getString(key);
for (String diskId : onDisk.getStringKeySet()) {
if (!used.contains(diskId)) {
BibtexString disk = onDisk.getString(diskId);
if (disk.getName().equals(tmp.getName())) {
// We have found a string with a matching name.
if (!Objects.equals(tmp.getContent(), disk.getContent())) {
// But they have nonmatching contents, so we've found a change.
Optional<BibtexString> mem = findString(inMem1, tmp.getName(), usedInMem);
if (mem.isPresent()) {
changes.add(new StringChange(mem.get(), tmp, tmp.getName(), mem.get().getContent(), disk.getContent()));
} else {
changes.add(new StringChange(null, tmp, tmp.getName(), null, disk.getContent()));
}
}
used.add(diskId);
continue mainLoop;
}
}
}
// If we get here, there was no match for this string.
notMatched.add(tmp.getId());
}
// See if we can detect a name change for those entries that we couldn't match.
if (!notMatched.isEmpty()) {
for (Iterator<String> i = notMatched.iterator(); i.hasNext(); ) {
BibtexString tmp = inTmp.getString(i.next());
// can find one with matching content.
for (String diskId : onDisk.getStringKeySet()) {
if (!used.contains(diskId)) {
BibtexString disk = onDisk.getString(diskId);
if (disk.getContent().equals(tmp.getContent())) {
// We have found a string with the same content. It cannot have the same
// name, or we would have found it above.
// Try to find the matching one in memory:
BibtexString bsMem = null;
for (String memId : inMem1.getStringKeySet()) {
BibtexString bsMemCandidate = inMem1.getString(memId);
if (bsMemCandidate.getContent().equals(disk.getContent()) && !usedInMem.contains(memId)) {
usedInMem.add(memId);
bsMem = bsMemCandidate;
break;
}
}
if (bsMem != null) {
changes.add(new StringNameChange(bsMem, tmp, bsMem.getName(), tmp.getName(), disk.getName(), tmp.getContent()));
i.remove();
used.add(diskId);
}
}
}
}
}
}
if (!notMatched.isEmpty()) {
// Still one or more non-matched strings. So they must have been removed.
for (String notMatchedId : notMatched) {
BibtexString tmp = inTmp.getString(notMatchedId);
// The removed string is not removed from the mem version.
findString(inMem1, tmp.getName(), usedInMem).ifPresent(x -> changes.add(new StringRemoveChange(tmp, tmp, x)));
}
}
// must have been added.
for (String diskId : onDisk.getStringKeySet()) {
if (!used.contains(diskId)) {
BibtexString disk = onDisk.getString(diskId);
used.add(diskId);
changes.add(new StringAddChange(disk));
}
}
}
use of org.jabref.model.entry.BibtexString in project jabref by JabRef.
the class StringAddChange method makeChange.
@Override
public boolean makeChange(BasePanel panel, BibDatabase secondary, NamedCompound undoEdit) {
if (panel.getDatabase().hasStringLabel(string.getName())) {
// The name to change to is already in the database, so we can't comply.
LOGGER.info("Cannot add string '" + string.getName() + "' because the name " + "is already in use.");
}
try {
panel.getDatabase().addString(string);
undoEdit.addEdit(new UndoableInsertString(panel, panel.getDatabase(), string));
} catch (KeyCollisionException ex) {
LOGGER.info("Error: could not add string '" + string.getName() + "': " + ex.getMessage(), ex);
}
try {
secondary.addString(new BibtexString(string.getName(), string.getContent()));
} catch (KeyCollisionException ex) {
LOGGER.info("Error: could not add string '" + string.getName() + "' to tmp database: " + ex.getMessage(), ex);
}
return true;
}
use of org.jabref.model.entry.BibtexString 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.BibtexString in project jabref by JabRef.
the class BibDatabaseWriter method writeString.
protected void writeString(BibtexString bibtexString, boolean isFirstString, Map<String, BibtexString> remaining, int maxKeyLength, Boolean reformatFile, LatexFieldFormatterPreferences latexFieldFormatterPreferences) throws SaveException {
// First remove this from the "remaining" list so it can't cause problem with circular refs:
remaining.remove(bibtexString.getName());
// Then we go through the string looking for references to other strings. If we find references
// to strings that we will write, but still haven't, we write those before proceeding. This ensures
// that the string order will be acceptable for BibTeX.
String content = bibtexString.getContent();
Matcher m;
while ((m = REFERENCE_PATTERN.matcher(content)).find()) {
String foundLabel = m.group(1);
int restIndex = content.indexOf(foundLabel) + foundLabel.length();
content = content.substring(restIndex);
String label = foundLabel.substring(1, foundLabel.length() - 1);
// If the label we found exists as a key in the "remaining" Map, we go on and write it now:
if (remaining.containsKey(label)) {
BibtexString referred = remaining.get(label);
writeString(referred, isFirstString, remaining, maxKeyLength, reformatFile, latexFieldFormatterPreferences);
}
}
writeString(bibtexString, isFirstString, maxKeyLength, reformatFile, latexFieldFormatterPreferences);
}
use of org.jabref.model.entry.BibtexString in project jabref by JabRef.
the class BibDatabaseWriter method writeStrings.
/**
* Write all strings in alphabetical order, modified to produce a safe (for
* BibTeX) order of the strings if they reference each other.
*
* @param database The database whose strings we should write.
*/
private void writeStrings(BibDatabase database, Boolean reformatFile, LatexFieldFormatterPreferences latexFieldFormatterPreferences) throws SaveException {
List<BibtexString> strings = database.getStringKeySet().stream().map(database::getString).collect(Collectors.toList());
strings.sort(new BibtexStringComparator(true));
// First, make a Map of all entries:
Map<String, BibtexString> remaining = new HashMap<>();
int maxKeyLength = 0;
for (BibtexString string : strings) {
remaining.put(string.getName(), string);
maxKeyLength = Math.max(maxKeyLength, string.getName().length());
}
for (BibtexString.Type t : BibtexString.Type.values()) {
boolean isFirstStringInType = true;
for (BibtexString bs : strings) {
if (remaining.containsKey(bs.getName()) && (bs.getType() == t)) {
writeString(bs, isFirstStringInType, remaining, maxKeyLength, reformatFile, latexFieldFormatterPreferences);
isFirstStringInType = false;
}
}
}
}
Aggregations