use of org.jabref.logic.layout.LayoutHelper in project jabref by JabRef.
the class FileUtil method createFileNameFromPattern.
/**
* Determines filename provided by an entry in a database
*
* @param database the database, where the entry is located
* @param entry the entry to which the file should be linked to
* @param fileNamePattern the filename pattern
* @param prefs the layout preferences
* @return a suggested fileName
*/
public static String createFileNameFromPattern(BibDatabase database, BibEntry entry, String fileNamePattern, LayoutFormatterPreferences prefs) {
String targetName = null;
StringReader sr = new StringReader(fileNamePattern);
Layout layout = null;
try {
layout = new LayoutHelper(sr, prefs).getLayoutFromText();
} catch (IOException e) {
LOGGER.info("Wrong format " + e.getMessage(), e);
}
if (layout != null) {
targetName = layout.doLayout(entry, database);
}
if ((targetName == null) || targetName.isEmpty()) {
targetName = entry.getCiteKeyOptional().orElse("default");
}
//Removes illegal characters from filename
targetName = FileNameCleaner.cleanFileName(targetName);
return targetName;
}
use of org.jabref.logic.layout.LayoutHelper in project jabref by JabRef.
the class BasePanel method copyKeyAndTitle.
private void copyKeyAndTitle() {
List<BibEntry> bes = mainTable.getSelectedEntries();
if (!bes.isEmpty()) {
storeCurrentEdit();
// OK: in a future version, this string should be configurable to allow arbitrary exports
StringReader sr = new StringReader("\\bibtexkey - \\begin{title}\\format[RemoveBrackets]{\\title}\\end{title}\n");
Layout layout;
try {
layout = new LayoutHelper(sr, Globals.prefs.getLayoutFormatterPreferences(Globals.journalAbbreviationLoader)).getLayoutFromText();
} catch (IOException e) {
LOGGER.info("Could not get layout", e);
return;
}
StringBuilder sb = new StringBuilder();
int copied = 0;
// Collect all non-null keys.
for (BibEntry be : bes) {
if (be.hasCiteKey()) {
copied++;
sb.append(layout.doLayout(be, bibDatabaseContext.getDatabase()));
}
}
if (copied == 0) {
output(Localization.lang("None of the selected entries have BibTeX keys."));
return;
}
final StringSelection ss = new StringSelection(sb.toString());
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, BasePanel.this);
if (copied == bes.size()) {
// All entries had keys.
output((bes.size() > 1 ? Localization.lang("Copied keys") : Localization.lang("Copied key")) + '.');
} else {
output(Localization.lang("Warning: %0 out of %1 entries have undefined BibTeX key.", Integer.toString(bes.size() - copied), Integer.toString(bes.size())));
}
}
}
use of org.jabref.logic.layout.LayoutHelper in project jabref by JabRef.
the class CitationStyleToClipboardWorker method doInBackground.
@Override
protected List<String> doInBackground() throws Exception {
if (CitationStyle.isCitationStyleFile(style)) {
return CitationStyleGenerator.generateCitations(selectedEntries, style, outputFormat);
} else {
StringReader sr = new StringReader(previewStyle.replace("__NEWLINE__", "\n"));
LayoutFormatterPreferences layoutFormatterPreferences = Globals.prefs.getLayoutFormatterPreferences(Globals.journalAbbreviationLoader);
Layout layout = new LayoutHelper(sr, layoutFormatterPreferences).getLayoutFromText();
List<String> citations = new ArrayList<>(selectedEntries.size());
for (BibEntry entry : selectedEntries) {
citations.add(layout.doLayout(entry, basePanel.getDatabase()));
}
return citations;
}
}
use of org.jabref.logic.layout.LayoutHelper in project jabref by JabRef.
the class ExportFormat method performExport.
@Override
public void performExport(final BibDatabaseContext databaseContext, final String file, final Charset encoding, List<BibEntry> entries) throws Exception {
Objects.requireNonNull(databaseContext);
Objects.requireNonNull(entries);
if (entries.isEmpty()) {
// Do not export if no entries to export -- avoids exports with only template text
return;
}
Path outFile = Paths.get(file);
SaveSession ss = null;
if (this.encoding != null) {
try {
ss = new FileSaveSession(this.encoding, false);
} catch (SaveException ex) {
// Perhaps the overriding encoding doesn't work?
// We will fall back on the default encoding.
LOGGER.warn("Cannot get save session.", ex);
}
}
if (ss == null) {
ss = new FileSaveSession(encoding, false);
}
try (VerifyingWriter ps = ss.getWriter()) {
Layout beginLayout = null;
// Check if this export filter has bundled name formatters:
// Add these to the preferences, so all layouts have access to the custom name formatters:
readFormatterFile();
List<String> missingFormatters = new ArrayList<>(1);
// Print header
try (Reader reader = getReader(lfFileName + ".begin.layout")) {
LayoutHelper layoutHelper = new LayoutHelper(reader, layoutPreferences);
beginLayout = layoutHelper.getLayoutFromText();
} catch (IOException ex) {
// If an exception was cast, export filter doesn't have a begin
// file.
}
// Write the header
if (beginLayout != null) {
ps.write(beginLayout.doLayout(databaseContext, encoding));
missingFormatters.addAll(beginLayout.getMissingFormatters());
}
/*
* Write database entries; entries will be sorted as they appear on the
* screen, or sorted by author, depending on Preferences. We also supply
* the Set entries - if we are to export only certain entries, it will
* be non-null, and be used to choose entries. Otherwise, it will be
* null, and be ignored.
*/
List<BibEntry> sorted = BibDatabaseWriter.getSortedEntries(databaseContext, entries, savePreferences);
// Load default layout
Layout defLayout;
LayoutHelper layoutHelper;
try (Reader reader = getReader(lfFileName + ".layout")) {
layoutHelper = new LayoutHelper(reader, layoutPreferences);
defLayout = layoutHelper.getLayoutFromText();
}
if (defLayout != null) {
missingFormatters.addAll(defLayout.getMissingFormatters());
if (!missingFormatters.isEmpty()) {
LOGGER.warn(missingFormatters);
}
}
Map<String, Layout> layouts = new HashMap<>();
Layout layout;
ExportFormats.entryNumber = 0;
for (BibEntry entry : sorted) {
// Increment entry counter.
ExportFormats.entryNumber++;
// Get the layout
String type = entry.getType();
if (layouts.containsKey(type)) {
layout = layouts.get(type);
} else {
try (Reader reader = getReader(lfFileName + '.' + type + ".layout")) {
// We try to get a type-specific layout for this entry.
layoutHelper = new LayoutHelper(reader, layoutPreferences);
layout = layoutHelper.getLayoutFromText();
layouts.put(type, layout);
if (layout != null) {
missingFormatters.addAll(layout.getMissingFormatters());
}
} catch (IOException ex) {
// The exception indicates that no type-specific layout
// exists, so we
// go with the default one.
layout = defLayout;
}
}
// Write the entry
if (layout != null) {
ps.write(layout.doLayout(entry, databaseContext.getDatabase()));
}
}
// Print footer
// changed section - begin (arudert)
Layout endLayout = null;
try (Reader reader = getReader(lfFileName + ".end.layout")) {
layoutHelper = new LayoutHelper(reader, layoutPreferences);
endLayout = layoutHelper.getLayoutFromText();
} catch (IOException ex) {
// If an exception was thrown, export filter doesn't have an end
// file.
}
// Write footer
if (endLayout != null) {
ps.write(endLayout.doLayout(databaseContext, this.encoding));
missingFormatters.addAll(endLayout.getMissingFormatters());
}
// Clear custom name formatters:
layoutPreferences.clearCustomExportNameFormatters();
if (!missingFormatters.isEmpty()) {
StringBuilder sb = new StringBuilder("The following formatters could not be found: ");
sb.append(String.join(", ", missingFormatters));
LOGGER.warn(sb);
}
finalizeSaveSession(ss, outFile);
}
}
use of org.jabref.logic.layout.LayoutHelper in project jabref by JabRef.
the class OOBibStyle method handleStructureLine.
/**
* Parse a line providing bibliography structure information for an entry type.
*
* @param line The string containing the structure description.
* @throws IOException
*/
private void handleStructureLine(String line) {
int index = line.indexOf('=');
if ((index > 0) && (index < (line.length() - 1))) {
String formatString = line.substring(index + 1);
boolean setDefault = line.substring(0, index).equals(OOBibStyle.DEFAULT_MARK);
String type = line.substring(0, index);
try {
Layout layout = new LayoutHelper(new StringReader(formatString), this.prefs).getLayoutFromText();
if (setDefault) {
defaultBibLayout = layout;
} else {
bibLayout.put(type.toLowerCase(Locale.ENGLISH), layout);
}
} catch (IOException ex) {
LOGGER.warn("Cannot parse bibliography structure", ex);
}
}
}
Aggregations