-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Export OO/LO citations to new database #1630
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,7 @@ | |
import net.sf.jabref.model.database.BibDatabase; | ||
import net.sf.jabref.model.entry.BibEntry; | ||
import net.sf.jabref.model.entry.FieldName; | ||
import net.sf.jabref.model.entry.IdGenerator; | ||
|
||
import com.sun.star.awt.Point; | ||
import com.sun.star.beans.IllegalTypeException; | ||
|
@@ -1296,4 +1297,31 @@ public int hashCode() { | |
|
||
} | ||
|
||
|
||
public BibDatabase generateDatabase(List<BibDatabase> databases, OOBibStyle style) | ||
throws NoSuchElementException, WrappedTargetException { | ||
BibDatabase resultDatabase = new BibDatabase(); | ||
List<String> cited = findCitedKeys(); | ||
for (String key : cited) { | ||
for (BibDatabase loopDatabase : databases) { | ||
Optional<BibEntry> entry = loopDatabase.getEntryByKey(key); | ||
if (entry.isPresent()) { | ||
BibEntry clonedEntry = (BibEntry) entry.get().clone(); | ||
clonedEntry.setId(IdGenerator.next()); | ||
resultDatabase.insertEntry(clonedEntry); | ||
entry.get().getFieldOptional(FieldName.CROSSREF).ifPresent(crossref -> { | ||
if (!resultDatabase.getEntryByKey(crossref).isPresent()) { | ||
Optional<BibEntry> refEntry = loopDatabase.getEntryByKey(crossref); | ||
if (refEntry.isPresent()) { | ||
resultDatabase.insertEntry(refEntry.get()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I would use ifPresent(resultDatabase.insertEntry) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense. Copied the code from the AUX parser where more things were done in the corresponding place and didn't reflect on it. |
||
} | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
|
||
return resultDatabase; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,8 @@ | |
import javax.swing.JRadioButtonMenuItem; | ||
import javax.swing.JTextField; | ||
|
||
import net.sf.jabref.BibDatabaseContext; | ||
import net.sf.jabref.Defaults; | ||
import net.sf.jabref.Globals; | ||
import net.sf.jabref.gui.BasePanel; | ||
import net.sf.jabref.gui.IconTheme; | ||
|
@@ -64,7 +66,9 @@ | |
import net.sf.jabref.logic.openoffice.UndefinedParagraphFormatException; | ||
import net.sf.jabref.logic.util.OS; | ||
import net.sf.jabref.model.database.BibDatabase; | ||
import net.sf.jabref.model.database.BibDatabaseMode; | ||
import net.sf.jabref.model.entry.BibEntry; | ||
import net.sf.jabref.preferences.JabRefPreferences; | ||
|
||
import com.jgoodies.forms.builder.ButtonBarBuilder; | ||
import com.jgoodies.forms.builder.FormBuilder; | ||
|
@@ -97,6 +101,7 @@ public class OpenOfficePanel extends AbstractWorker { | |
private final JButton update; | ||
private final JButton merge = new JButton(Localization.lang("Merge citations")); | ||
private final JButton manageCitations = new JButton(Localization.lang("Manage citations")); | ||
private final JButton exportCitations = new JButton(Localization.lang("Generate new BIB database")); | ||
private final JButton settingsB = new JButton(Localization.lang("Settings")); | ||
private final JButton help = new HelpAction(Localization.lang("OpenOffice/LibreOffice integration"), | ||
HelpFile.OPENOFFICE_LIBREOFFICE).getHelpButton(); | ||
|
@@ -281,6 +286,8 @@ public void actionPerformed(ActionEvent e) { | |
} | ||
}); | ||
|
||
exportCitations.addActionListener(event -> exportEntries()); | ||
|
||
selectDocument.setEnabled(false); | ||
pushEntries.setEnabled(false); | ||
pushEntriesInt.setEnabled(false); | ||
|
@@ -289,9 +296,11 @@ public void actionPerformed(ActionEvent e) { | |
update.setEnabled(false); | ||
merge.setEnabled(false); | ||
manageCitations.setEnabled(false); | ||
exportCitations.setEnabled(false); | ||
diag = new JDialog((JFrame) null, "OpenOffice/LibreOffice panel", false); | ||
|
||
FormBuilder mainBuilder = FormBuilder.create().layout(new FormLayout("fill:pref:grow", "p,p,p,p,p,p,p,p,p,p")); | ||
FormBuilder mainBuilder = FormBuilder.create() | ||
.layout(new FormLayout("fill:pref:grow", "p,p,p,p,p,p,p,p,p,p,p")); | ||
|
||
FormBuilder topRowBuilder = FormBuilder.create() | ||
.layout(new FormLayout("fill:pref:grow, 1dlu, fill:pref:grow, 1dlu, fill:pref:grow, " | ||
|
@@ -309,7 +318,8 @@ public void actionPerformed(ActionEvent e) { | |
mainBuilder.add(pushEntriesEmpty).xy(1, 6); | ||
mainBuilder.add(merge).xy(1, 7); | ||
mainBuilder.add(manageCitations).xy(1, 8); | ||
mainBuilder.add(settingsB).xy(1, 9); | ||
mainBuilder.add(exportCitations).xy(1, 9); | ||
mainBuilder.add(settingsB).xy(1, 10); | ||
|
||
JPanel content = new JPanel(); | ||
comp.setContentContainer(content); | ||
|
@@ -322,6 +332,39 @@ public void actionPerformed(ActionEvent e) { | |
|
||
} | ||
|
||
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, style); | ||
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 database.", | ||
unresolvedKeys.get(0)), | ||
Localization.lang("Unable to generate new database"), JOptionPane.ERROR_MESSAGE); | ||
} | ||
|
||
Defaults defaults = new Defaults( | ||
BibDatabaseMode.fromPreference(Globals.prefs.getBoolean(JabRefPreferences.BIBLATEX_DEFAULT_MODE))); | ||
|
||
BibDatabaseContext databaseContext = new BibDatabaseContext(newDatabase, defaults); | ||
this.frame.addTab(databaseContext, true); | ||
|
||
} catch (Exception e) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you specify a more concrete type of exception here. Catching There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem is that I have earlier tried to specify the exceptions in OO/LO where possible, but there are still some methods that only throw } catch (UndefinedCharacterFormatException ex) {
reportUndefinedCharacterFormat(ex);
} catch (UndefinedParagraphFormatException ex) {
reportUndefinedParagraphFormat(ex);
} catch (ConnectionLostException ex) {
showConnectionLostErrorMessage();
} catch (IOException ex) {
JOptionPane.showMessageDialog(frame,
Localization
.lang("You must select either a valid style file, or use one of the default styles."),
Localization.lang("No valid style file defined"), JOptionPane.ERROR_MESSAGE);
LOGGER.warn("Problem with style file", ex);
} 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 database.",
ex.getBibtexKey()),
Localization.lang("Unable to synchronize bibliography"), JOptionPane.ERROR_MESSAGE);
LOGGER.debug("BibEntry not found", ex);
} catch (Exception ex) {
LOGGER.warn("Could not update bibliography", ex);
} which probably is slightly better. The main "problem" is that there is very limited exception handling locally in the OO/LO part. Everything is propagated to the calling function. Which is good from some perspectives but makes it hard to change some things as you do not know what is actually happening and how it affect things. (Unless you really understand the code...) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another option would be to convert the
Then you can only deal with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, there are quite a few Exception types already defined in OO/LO so defining one more (in case we want JabRefException to be used later for something else) shouldn't be a problem. It may be a problem naming it since I'm not really sure what the errors are, maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, there are indeed quite some customn exceptions. But I don't understand why you want to define yet another exception? Why not just use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can of course use that, but I'm still tempted to try to figure out
something more clever... ;-)
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I introduced There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, then this is good enough! I'll merge now. |
||
LOGGER.warn("Problem generating new database.", e); | ||
} | ||
|
||
} | ||
|
||
private List<BibDatabase> getBaseList() { | ||
List<BibDatabase> databases = new ArrayList<>(); | ||
if (preferences.useAllDatabases()) { | ||
|
@@ -418,6 +461,7 @@ private void connect(boolean auto) { | |
update.setEnabled(true); | ||
merge.setEnabled(true); | ||
manageCitations.setEnabled(true); | ||
exportCitations.setEnabled(true); | ||
|
||
} catch (UnsatisfiedLinkError e) { | ||
LOGGER.warn("Could not connect to running OpenOffice/LibreOffice", e); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'Avoid checking every optional for isPresent, use a flatMap for chained optionals:
(Optional FlatMap example)
http://codingjunkie.net/working-with-java8-optionals/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had some plans to break the loop once the key was found. In that case one is stuck with
isPresent
as far as I know.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The break solved the problem with the multiple entries. I was really confused for a while, but since the function looks through all tabs, it found the first generated database and included those entries once more and so on... :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. This whole loop with all the presents looked very complex and confusing at the first moment. 😕
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added some comments. Most of the time I like Optional (even though I'm at the best is using
ifPresent
), but the lack ofif ... else ...
and the fact that the lambda becomes its own function (which is very natural, but still prohibits writing some things, like return or break) is a bit annoying.