use of org.jabref.logic.bibtex.LatexFieldFormatter in project jabref by JabRef.
the class EntryEditor method getSourceString.
private static String getSourceString(BibEntry entry, BibDatabaseMode type) throws IOException {
StringWriter stringWriter = new StringWriter(200);
LatexFieldFormatter formatter = LatexFieldFormatter.buildIgnoreHashes(Globals.prefs.getLatexFieldFormatterPreferences());
new BibEntryWriter(formatter, false).writeWithoutPrependedNewlines(entry, stringWriter, type);
return stringWriter.getBuffer().toString();
}
use of org.jabref.logic.bibtex.LatexFieldFormatter in project jabref by JabRef.
the class SendAsEMailAction method run.
@Override
public void run() {
if (!Desktop.isDesktopSupported()) {
message = Localization.lang("Error creating email");
return;
}
BasePanel panel = frame.getCurrentBasePanel();
if (panel == null) {
return;
}
if (panel.getSelectedEntries().isEmpty()) {
message = Localization.lang("This operation requires one or more entries to be selected.");
return;
}
StringWriter sw = new StringWriter();
List<BibEntry> bes = panel.getSelectedEntries();
// write the entries using sw, which is used later to form the email content
BibEntryWriter bibtexEntryWriter = new BibEntryWriter(new LatexFieldFormatter(Globals.prefs.getLatexFieldFormatterPreferences()), true);
for (BibEntry entry : bes) {
try {
bibtexEntryWriter.write(entry, sw, panel.getBibDatabaseContext().getMode());
} catch (IOException e) {
LOGGER.warn("Problem creating BibTeX file for mailing.", e);
}
}
List<String> attachments = new ArrayList<>();
// open folders is needed to indirectly support email programs, which cannot handle
// the unofficial "mailto:attachment" property
boolean openFolders = JabRefPreferences.getInstance().getBoolean(JabRefPreferences.OPEN_FOLDERS_OF_ATTACHED_FILES);
List<Path> fileList = FileUtil.getListOfLinkedFiles(bes, frame.getCurrentBasePanel().getBibDatabaseContext().getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences()));
for (Path f : fileList) {
attachments.add(f.toAbsolutePath().toString());
if (openFolders) {
try {
JabRefDesktop.openFolderAndSelectFile(f.toAbsolutePath());
} catch (IOException e) {
LOGGER.debug("Cannot open file", e);
}
}
}
String mailTo = "?Body=".concat(sw.getBuffer().toString());
mailTo = mailTo.concat("&Subject=");
mailTo = mailTo.concat(JabRefPreferences.getInstance().get(JabRefPreferences.EMAIL_SUBJECT));
for (String path : attachments) {
mailTo = mailTo.concat("&Attachment=\"").concat(path);
mailTo = mailTo.concat("\"");
}
URI uriMailTo;
try {
uriMailTo = new URI("mailto", mailTo, null);
} catch (URISyntaxException e1) {
message = Localization.lang("Error creating email");
LOGGER.warn(message, e1);
return;
}
Desktop desktop = Desktop.getDesktop();
try {
desktop.mail(uriMailTo);
} catch (IOException e) {
message = Localization.lang("Error creating email");
LOGGER.warn(message, e);
return;
}
message = String.format("%s: %d", Localization.lang("Entries added to an email"), bes.size());
}
use of org.jabref.logic.bibtex.LatexFieldFormatter in project jabref by JabRef.
the class XMPUtilTest method bibtexEntry2BibtexString.
private static String bibtexEntry2BibtexString(BibEntry e) throws IOException {
StringWriter sw = new StringWriter();
new BibEntryWriter(new LatexFieldFormatter(mock(LatexFieldFormatterPreferences.class, Answers.RETURNS_DEEP_STUBS)), false).write(e, sw, BibDatabaseMode.BIBTEX);
return sw.getBuffer().toString();
}
use of org.jabref.logic.bibtex.LatexFieldFormatter in project jabref by JabRef.
the class BibtexDatabaseWriter method writeString.
@Override
protected void writeString(BibtexString bibtexString, boolean isFirstString, int maxKeyLength, Boolean reformatFile, LatexFieldFormatterPreferences latexFieldFormatterPreferences) throws SaveException {
try {
// If the string has not been modified, write it back as it was
if (!reformatFile && !bibtexString.hasChanged()) {
getWriter().write(bibtexString.getParsedSerialization());
return;
}
// Write user comments
String userComments = bibtexString.getUserComments();
if (!userComments.isEmpty()) {
getWriter().write(userComments + OS.NEWLINE);
}
if (isFirstString) {
getWriter().write(OS.NEWLINE);
}
getWriter().write(STRING_PREFIX + "{" + bibtexString.getName() + StringUtil.repeatSpaces(maxKeyLength - bibtexString.getName().length()) + " = ");
if (bibtexString.getContent().isEmpty()) {
getWriter().write("{}");
} else {
try {
String formatted = new LatexFieldFormatter(latexFieldFormatterPreferences).format(bibtexString.getContent(), LatexFieldFormatter.BIBTEX_STRING);
getWriter().write(formatted);
} catch (InvalidFieldValueException ex) {
throw new SaveException(ex);
}
}
getWriter().write("}" + OS.NEWLINE);
} catch (IOException e) {
throw new SaveException(e);
}
}
use of org.jabref.logic.bibtex.LatexFieldFormatter in project jabref by JabRef.
the class XMPUtilMain method main.
/**
* Command-line tool for working with XMP-data.
*
* Read or write XMP-metadata from or to pdf file.
*
* Usage:
* <dl>
* <dd>Read from PDF and print as bibtex:</dd>
* <dt>xmpUtil PDF</dt>
* <dd>Read from PDF and print raw XMP:</dd>
* <dt>xmpUtil -x PDF</dt>
* <dd>Write the entry in BIB given by KEY to the PDF:</dd>
* <dt>xmpUtil KEY BIB PDF</dt>
* <dd>Write all entries in BIB to the PDF:</dd>
* <dt>xmpUtil BIB PDF</dt>
* </dl>
*
* @param args
* Command line strings passed to utility.
* @throws IOException
* If any of the given files could not be read or written.
* @throws TransformerException
* If the given BibEntry is malformed.
*/
public static void main(String[] args) throws IOException, TransformerException {
// Don't forget to initialize the preferences
if (Globals.prefs == null) {
Globals.prefs = JabRefPreferences.getInstance();
}
XMPPreferences xmpPreferences = Globals.prefs.getXMPPreferences();
ImportFormatPreferences importFormatPreferences = Globals.prefs.getImportFormatPreferences();
switch(args.length) {
case 0:
usage();
break;
case 1:
if (args[0].endsWith(".pdf")) {
// Read from pdf and write as BibTex
List<BibEntry> l = XMPUtil.readXMP(new File(args[0]), xmpPreferences);
BibEntryWriter bibtexEntryWriter = new BibEntryWriter(new LatexFieldFormatter(Globals.prefs.getLatexFieldFormatterPreferences()), false);
for (BibEntry entry : l) {
StringWriter sw = new StringWriter();
bibtexEntryWriter.write(entry, sw, BibDatabaseMode.BIBTEX);
System.out.println(sw.getBuffer());
}
} else if (args[0].endsWith(".bib")) {
// Read from BIB and write as XMP
try (FileReader fr = new FileReader(args[0])) {
ParserResult result = new BibtexParser(importFormatPreferences).parse(fr);
Collection<BibEntry> entries = result.getDatabase().getEntries();
if (entries.isEmpty()) {
System.err.println("Could not find BibEntry in " + args[0]);
} else {
System.out.println(XMPUtil.toXMP(entries, result.getDatabase(), xmpPreferences));
}
}
} else {
usage();
}
break;
case 2:
if ("-x".equals(args[0]) && args[1].endsWith(".pdf")) {
// Read from pdf and write as BibTex
Optional<XMPMetadata> meta = XMPUtil.readRawXMP(new File(args[1]));
if (meta.isPresent()) {
XMLUtil.save(meta.get().getXMPDocument(), System.out, StandardCharsets.UTF_8.name());
} else {
System.err.println("The given pdf does not contain any XMP-metadata.");
}
break;
}
if (args[0].endsWith(".bib") && args[1].endsWith(".pdf")) {
ParserResult result = new BibtexParser(importFormatPreferences).parse(new FileReader(args[0]));
Collection<BibEntry> entries = result.getDatabase().getEntries();
if (entries.isEmpty()) {
System.err.println("Could not find BibEntry in " + args[0]);
} else {
XMPUtil.writeXMP(new File(args[1]), entries, result.getDatabase(), false, xmpPreferences);
System.out.println("XMP written.");
}
break;
}
usage();
break;
case 3:
if (!args[1].endsWith(".bib") && !args[2].endsWith(".pdf")) {
usage();
break;
}
ParserResult result = new BibtexParser(importFormatPreferences).parse(new FileReader(args[1]));
Optional<BibEntry> bibEntry = result.getDatabase().getEntryByKey(args[0]);
if (bibEntry.isPresent()) {
XMPUtil.writeXMP(new File(args[2]), bibEntry.get(), result.getDatabase(), xmpPreferences);
System.out.println("XMP written.");
} else {
System.err.println("Could not find BibEntry " + args[0] + " in " + args[0]);
}
break;
default:
usage();
}
}
Aggregations