use of org.jabref.logic.importer.fileformat.BibtexParser in project jabref by JabRef.
the class OOBibStyleTest method testLayout.
@Test
public void testLayout() throws IOException {
Path testBibtexFile = Paths.get("src/test/resources/testbib/complex.bib");
ParserResult result = new BibtexParser(importFormatPreferences).parse(Importer.getReader(testBibtexFile, StandardCharsets.UTF_8));
OOBibStyle style = new OOBibStyle(StyleLoader.DEFAULT_NUMERICAL_STYLE_PATH, layoutFormatterPreferences);
BibDatabase db = result.getDatabase();
Layout l = style.getReferenceFormat("default");
l.setPostFormatter(new OOPreFormatter());
BibEntry entry = db.getEntryByKey("1137631").get();
assertEquals("Boström, G.; Wäyrynen, J.; Bodén, M.; Beznosov, K. and Kruchten, P. (<b>2006</b>). <i>Extending XP practices to support security requirements engineering</i>, : 11-18.", l.doLayout(entry, db));
l = style.getReferenceFormat("incollection");
l.setPostFormatter(new OOPreFormatter());
assertEquals("Boström, G.; Wäyrynen, J.; Bodén, M.; Beznosov, K. and Kruchten, P. (<b>2006</b>). <i>Extending XP practices to support security requirements engineering</i>. In: (Ed.), <i>SESS '06: Proceedings of the 2006 international workshop on Software engineering for secure systems</i>, ACM.", l.doLayout(entry, db));
}
use of org.jabref.logic.importer.fileformat.BibtexParser in project jabref by JabRef.
the class XMPUtilTest method testCommandLineSinglePdf.
/**
* @throws IOException
* @throws TransformerException
* @throws COSVisitorException
* @depends XMPUtil.writeXMP
*/
@Test
public void testCommandLineSinglePdf() throws IOException, TransformerException, COSVisitorException {
{
// Write XMP to file
BibEntry e = t1BibtexEntry();
XMPUtil.writeXMP(pdfFile, e, null, xmpPreferences);
try (ByteArrayOutputStream s = new ByteArrayOutputStream()) {
PrintStream oldOut = System.out;
System.setOut(new PrintStream(s));
XMPUtilMain.main(new String[] { pdfFile.getAbsolutePath() });
System.setOut(oldOut);
String bibtex = s.toString();
ParserResult result = new BibtexParser(importFormatPreferences).parse(new StringReader(bibtex));
Collection<BibEntry> c = result.getDatabase().getEntries();
Assert.assertEquals(1, c.size());
BibEntry x = c.iterator().next();
assertEqualsBibtexEntry(e, x);
}
}
// Write XMP to file
BibEntry e = t1BibtexEntry();
XMPUtil.writeXMP(pdfFile, e, null, xmpPreferences);
try (ByteArrayOutputStream s = new ByteArrayOutputStream()) {
PrintStream oldOut = System.out;
System.setOut(new PrintStream(s));
XMPUtilMain.main(new String[] { "-x", pdfFile.getAbsolutePath() });
System.setOut(oldOut);
s.close();
String xmp = s.toString();
/* Test minimal syntaxical completeness */
Assert.assertTrue(xmp.indexOf("xpacket") > 0);
Assert.assertTrue(xmp.indexOf("adobe:ns:meta") > 0);
Assert.assertTrue((xmp.indexOf("<bibtex:bibtexkey>canh05</bibtex:bibtexkey>") > 0) || (xmp.indexOf("bibtex:bibtexkey=") > 0));
Assert.assertTrue(xmp.indexOf("<rdf:li>K. Crowston</rdf:li>") > 0);
Assert.assertTrue((xmp.indexOf("id='W5M0MpCehiHzreSzNTczkc9d'?>") > 0) || (xmp.indexOf("id=\"W5M0MpCehiHzreSzNTczkc9d\"?>") > 0));
Assert.assertTrue((xmp.indexOf("xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'") > 0) || (xmp.indexOf("xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"") > 0));
Assert.assertTrue(xmp.indexOf("<rdf:Description") > 0);
Assert.assertTrue((xmp.indexOf("<?xpacket end='w'?>") > 0) || (xmp.indexOf("<?xpacket end=\"w\"?>") > 0));
/* Test contents of string */
writeManually(pdfFile, xmp);
List<BibEntry> l = XMPUtil.readXMP(pdfFile, xmpPreferences);
Assert.assertEquals(1, l.size());
assertEqualsBibtexEntry(t1BibtexEntry(), l.get(0));
}
}
use of org.jabref.logic.importer.fileformat.BibtexParser in project jabref by JabRef.
the class XMPUtilTest method testResolveStrings2.
/**
* A better testcase for resolveStrings. Makes sure that also the document information and dublin core are written
* correctly.
* <p/>
* Data was contributed by Philip K.F. Hölzenspies (p.k.f.holzenspies [at] utwente.nl).
*
* @throws IOException
* @throws TransformerException
*/
@Test
public void testResolveStrings2() throws IOException, TransformerException {
try (BufferedReader fr = Files.newBufferedReader(Paths.get("src/test/resources/org/jabref/util/twente.bib"), StandardCharsets.UTF_8)) {
ParserResult result = new BibtexParser(importFormatPreferences).parse(fr);
Assert.assertEquals("Arvind", result.getDatabase().resolveForStrings("#Arvind#"));
AuthorList originalAuthors = AuthorList.parse("Patterson, David and Arvind and Asanov\\'\\i{}c, Krste and Chiou, Derek and Hoe, James and Kozyrakis, Christos and Lu, S{hih-Lien} and Oskin, Mark and Rabaey, Jan and Wawrzynek, John");
try {
XMPUtil.writeXMP(pdfFile, result.getDatabase().getEntryByKey("Patterson06").get(), result.getDatabase(), xmpPreferences);
// Test whether we the main function can load the bibtex correctly
BibEntry b = XMPUtil.readXMP(pdfFile, xmpPreferences).get(0);
Assert.assertNotNull(b);
Assert.assertEquals(originalAuthors, AuthorList.parse(b.getField("author").get()));
// Next check from Document Information
try (PDDocument document = PDDocument.load(pdfFile.getAbsoluteFile())) {
Assert.assertEquals(originalAuthors, AuthorList.parse(document.getDocumentInformation().getAuthor()));
b = XMPUtil.getBibtexEntryFromDocumentInformation(document.getDocumentInformation()).get();
Assert.assertEquals(originalAuthors, AuthorList.parse(b.getField("author").get()));
// Now check from Dublin Core
PDDocumentCatalog catalog = document.getDocumentCatalog();
PDMetadata metaRaw = catalog.getMetadata();
if (metaRaw == null) {
Assert.fail();
// To avoid warnings
return;
}
XMPMetadata meta = new XMPMetadata(XMLUtil.parse(metaRaw.createInputStream()));
meta.addXMLNSMapping(XMPSchemaBibtex.NAMESPACE, XMPSchemaBibtex.class);
List<XMPSchema> schemas = meta.getSchemasByNamespaceURI("http://purl.org/dc/elements/1.1/");
Assert.assertEquals(1, schemas.size());
XMPSchemaDublinCore dcSchema = (XMPSchemaDublinCore) schemas.iterator().next();
Assert.assertNotNull(dcSchema);
Assert.assertEquals("David Patterson", dcSchema.getCreators().get(0));
Assert.assertEquals("Arvind", dcSchema.getCreators().get(1));
Assert.assertEquals("Krste Asanov\\'\\i{}c", dcSchema.getCreators().get(2));
b = XMPUtil.getBibtexEntryFromDublinCore(dcSchema, xmpPreferences).get();
Assert.assertNotNull(b);
Assert.assertEquals(originalAuthors, AuthorList.parse(b.getField("author").get()));
}
} finally {
if (!pdfFile.delete()) {
System.err.println("Cannot delete temporary file");
}
}
}
}
use of org.jabref.logic.importer.fileformat.BibtexParser in project jabref by JabRef.
the class XMPUtilTest method bibtexString2BibtexEntry.
private static BibEntry bibtexString2BibtexEntry(String s, ImportFormatPreferences importFormatPreferences) throws IOException {
ParserResult result = new BibtexParser(importFormatPreferences).parse(new StringReader(s));
Collection<BibEntry> c = result.getDatabase().getEntries();
Assert.assertEquals(1, c.size());
return c.iterator().next();
}
use of org.jabref.logic.importer.fileformat.BibtexParser 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