use of org.jabref.model.database.BibDatabase in project jabref by JabRef.
the class BibtexParser method initializeParserResult.
private void initializeParserResult() {
database = new BibDatabase();
// To store custom entry types parsed.
entryTypes = new HashMap<>();
parserResult = new ParserResult(database, new MetaData(), entryTypes);
}
use of org.jabref.model.database.BibDatabase in project jabref by JabRef.
the class MrDLibImporter method parse.
/**
* Parses the input from the server to a ParserResult
* @param input A BufferedReader with a reference to a string with the servers response
* @throws IOException
*/
private void parse(BufferedReader input) throws IOException {
// The Bibdatabase that gets returned in the ParserResult.
BibDatabase bibDatabase = new BibDatabase();
// The document to parse
String recommendations = convertToString(input);
// The sorted BibEntries gets stored here later
List<BibEntry> bibEntries = new ArrayList<>();
//Parsing the response with a SAX parser
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
MrDlibImporterHandler handler = new MrDlibImporterHandler();
try (InputStream stream = new ByteArrayInputStream(recommendations.getBytes())) {
saxParser.parse(stream, handler);
} catch (SAXException e) {
LOGGER.error(e.getMessage(), e);
}
List<RankedBibEntry> rankedBibEntries = handler.getRankedBibEntries();
rankedBibEntries.sort((RankedBibEntry rankedBibEntry1, RankedBibEntry rankedBibEntry2) -> rankedBibEntry1.rank.compareTo(rankedBibEntry2.rank));
bibEntries = rankedBibEntries.stream().map(e -> e.entry).collect(Collectors.toList());
} catch (ParserConfigurationException | SAXException e) {
LOGGER.error(e.getMessage(), e);
}
for (BibEntry bibentry : bibEntries) {
bibDatabase.insertEntry(bibentry);
}
parserResult = new ParserResult(bibDatabase);
}
use of org.jabref.model.database.BibDatabase in project jabref by JabRef.
the class AuxCommandLineTest method test.
@Test
public void test() throws URISyntaxException, IOException {
InputStream originalStream = AuxCommandLineTest.class.getResourceAsStream("origin.bib");
File auxFile = Paths.get(AuxCommandLineTest.class.getResource("paper.aux").toURI()).toFile();
try (InputStreamReader originalReader = new InputStreamReader(originalStream, StandardCharsets.UTF_8)) {
ParserResult result = new BibtexParser(importFormatPreferences).parse(originalReader);
AuxCommandLine auxCommandLine = new AuxCommandLine(auxFile.getAbsolutePath(), result.getDatabase());
BibDatabase newDB = auxCommandLine.perform();
Assert.assertNotNull(newDB);
Assert.assertEquals(2, newDB.getEntries().size());
}
}
use of org.jabref.model.database.BibDatabase in project jabref by JabRef.
the class EntryFromFileCreatorManagerTest method testAddEntrysFromFiles.
@Test
@Ignore
public void testAddEntrysFromFiles() throws FileNotFoundException, IOException {
try (FileInputStream stream = new FileInputStream(ImportDataTest.UNLINKED_FILES_TEST_BIB);
InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
ParserResult result = new BibtexParser(mock(ImportFormatPreferences.class)).parse(reader);
BibDatabase database = result.getDatabase();
List<File> files = new ArrayList<>();
files.add(ImportDataTest.FILE_NOT_IN_DATABASE);
files.add(ImportDataTest.NOT_EXISTING_PDF);
EntryFromFileCreatorManager manager = new EntryFromFileCreatorManager();
List<String> errors = manager.addEntrysFromFiles(files, database, null, true);
/**
* One file doesn't exist, so adding it as an entry should lead to an error message.
*/
Assert.assertEquals(1, errors.size());
boolean file1Found = false;
boolean file2Found = false;
for (BibEntry entry : database.getEntries()) {
String filesInfo = entry.getField("file").get();
if (filesInfo.contains(files.get(0).getName())) {
file1Found = true;
}
if (filesInfo.contains(files.get(1).getName())) {
file2Found = true;
}
}
Assert.assertTrue(file1Found);
Assert.assertFalse(file2Found);
}
}
use of org.jabref.model.database.BibDatabase in project jabref by JabRef.
the class AuxParserTest method testNormal.
@Test
public void testNormal() throws URISyntaxException, IOException {
InputStream originalStream = AuxParserTest.class.getResourceAsStream("origin.bib");
File auxFile = Paths.get(AuxParserTest.class.getResource("paper.aux").toURI()).toFile();
try (InputStreamReader originalReader = new InputStreamReader(originalStream, StandardCharsets.UTF_8)) {
ParserResult result = new BibtexParser(importFormatPreferences).parse(originalReader);
AuxParser auxParser = new AuxParser(auxFile.getAbsolutePath(), result.getDatabase());
AuxParserResult auxResult = auxParser.parse();
assertTrue(auxResult.getGeneratedBibDatabase().hasEntries());
assertEquals(0, auxResult.getUnresolvedKeysCount());
BibDatabase newDB = auxResult.getGeneratedBibDatabase();
assertEquals(2, newDB.getEntries().size());
assertEquals(2, auxResult.getResolvedKeysCount());
assertEquals(2, auxResult.getFoundKeysInAux());
assertEquals(auxResult.getFoundKeysInAux() + auxResult.getCrossRefEntriesCount(), auxResult.getResolvedKeysCount() + auxResult.getUnresolvedKeysCount());
assertEquals(0, auxResult.getCrossRefEntriesCount());
}
}
Aggregations