@Test public void roundtripWithContentSelectorsAndUmlauts() throws IOException, SaveException { String fileContent = "% Encoding: UTF-8" + OS.NEWLINE + OS.NEWLINE + "@Comment{jabref-meta: selector_journal:Test {\\\\\"U}mlaut;}" + OS.NEWLINE; Charset encoding = StandardCharsets.UTF_8; ParserResult firstParse = new BibtexParser(importFormatPreferences).parse(new StringReader(fileContent)); SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true); BibDatabaseContext context = new BibDatabaseContext( firstParse.getDatabase(), firstParse.getMetaData(), new Defaults(BibDatabaseMode.BIBTEX)); StringSaveSession session = databaseWriter.savePartOfDatabase( context, firstParse.getDatabase().getEntries(), preferences); assertEquals(fileContent, session.getStringValue()); }
@Test public void roundtripWithUserCommentBeforeStringAndChange() throws Exception { Path testBibtexFile = Paths.get("src/test/resources/testbib/complex.bib"); Charset encoding = StandardCharsets.UTF_8; ParserResult result = new BibtexParser(importFormatPreferences) .parse(Importer.getReader(testBibtexFile, encoding)); for (BibtexString string : result.getDatabase().getStringValues()) { // Mark them as changed string.setContent(string.getContent()); } SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true); BibDatabaseContext context = new BibDatabaseContext( result.getDatabase(), result.getMetaData(), new Defaults(BibDatabaseMode.BIBTEX)); StringSaveSession session = databaseWriter.savePartOfDatabase(context, result.getDatabase().getEntries(), preferences); try (Scanner scanner = new Scanner(testBibtexFile, encoding.name())) { assertEquals(scanner.useDelimiter("\\A").next(), session.getStringValue()); } }
@Test public void roundtripWithUserCommentAndEntryChange() throws Exception { Path testBibtexFile = Paths.get("src/test/resources/testbib/bibWithUserComments.bib"); Charset encoding = StandardCharsets.UTF_8; ParserResult result = new BibtexParser(importFormatPreferences) .parse(Importer.getReader(testBibtexFile, encoding)); BibEntry entry = result.getDatabase().getEntryByKey("1137631").get(); entry.setField("author", "Mr. Author"); SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true); BibDatabaseContext context = new BibDatabaseContext( result.getDatabase(), result.getMetaData(), new Defaults(BibDatabaseMode.BIBTEX)); StringSaveSession session = databaseWriter.savePartOfDatabase(context, result.getDatabase().getEntries(), preferences); try (Scanner scanner = new Scanner( Paths.get("src/test/resources/testbib/bibWithUserCommentAndEntryChange.bib"), encoding.name())) { assertEquals(scanner.useDelimiter("\\A").next(), session.getStringValue()); } }
@Test public void duplicateBibDatabaseConfiguration() throws URISyntaxException, IOException { InputStream originalStream = AuxParserTest.class.getResourceAsStream("config.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(); BibDatabase db = auxResult.getGeneratedBibDatabase(); assertEquals(Optional.of("\"Maintained by \" # maintainer"), db.getPreamble()); assertEquals(1, db.getStringCount()); } }
private void doContentImport(String fileName, List<BibEntry> res) { PdfContentImporter contentImporter = new PdfContentImporter(Globals.prefs.getImportFormatPreferences()); Path filePath = Paths.get(fileName); ParserResult result = contentImporter.importDatabase(filePath, Globals.prefs.getDefaultEncoding()); if (result.hasWarnings()) { frame.showMessage(result.getErrorMessage()); } if (!result.getDatabase().hasEntries()) { // import failed -> generate default entry createNewBlankEntry(fileName).ifPresent(res::add); return; } // only one entry is imported BibEntry entry = result.getDatabase().getEntries().get(0); // insert entry to database and link file panel.getDatabase().insertEntry(entry); panel.markBaseChanged(); BibtexKeyPatternUtil.makeLabel( panel .getBibDatabaseContext() .getMetaData() .getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern()), panel.getDatabase(), entry, Globals.prefs.getBibtexKeyPatternPreferences()); DroppedFileHandler dfh = new DroppedFileHandler(frame, panel); dfh.linkPdfToEntry(fileName, entry); panel.highlightEntry(entry); if (Globals.prefs.getBoolean(JabRefPreferences.AUTO_OPEN_FORM)) { EntryEditor editor = panel.getEntryEditor(entry); panel.showEntryEditor(editor); } res.add(entry); }
private void doXMPImport(String fileName, List<BibEntry> res) { List<BibEntry> localRes = new ArrayList<>(); PdfXmpImporter importer = new PdfXmpImporter(Globals.prefs.getXMPPreferences()); Path filePath = Paths.get(fileName); ParserResult result = importer.importDatabase(filePath, Globals.prefs.getDefaultEncoding()); if (result.hasWarnings()) { frame.showMessage(result.getErrorMessage()); } localRes.addAll(result.getDatabase().getEntries()); BibEntry entry; if (localRes.isEmpty()) { // import failed -> generate default entry LOGGER.info("Import failed"); createNewBlankEntry(fileName).ifPresent(res::add); return; } // only one entry is imported entry = localRes.get(0); // insert entry to database and link file panel.getDatabase().insertEntry(entry); panel.markBaseChanged(); FileListTableModel tm = new FileListTableModel(); File toLink = new File(fileName); // Get a list of file directories: List<String> dirsS = panel.getBibDatabaseContext().getFileDirectory(Globals.prefs.getFileDirectoryPreferences()); tm.addEntry( 0, new FileListEntry( toLink.getName(), FileUtil.shortenFileName(toLink, dirsS).getPath(), ExternalFileTypes.getInstance().getExternalFileTypeByName("PDF"))); entry.setField(FieldName.FILE, tm.getStringRepresentation()); res.add(entry); }
@Override public ParserResult importDatabase(BufferedReader reader) throws IOException { Objects.requireNonNull(reader); List<BibEntry> bibItems = new ArrayList<>(); try { JAXBContext context = JAXBContext.newInstance("net.sf.jabref.logic.importer.fileformat.medline"); XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory(); XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(reader); // go to the root element while (!xmlStreamReader.isStartElement()) { xmlStreamReader.next(); } Unmarshaller unmarshaller = context.createUnmarshaller(); Object unmarshalledObject = unmarshaller.unmarshal(xmlStreamReader); // check whether we have an article set, an article, a book article or a book article set if (unmarshalledObject instanceof PubmedArticleSet) { PubmedArticleSet articleSet = (PubmedArticleSet) unmarshalledObject; for (Object article : articleSet.getPubmedArticleOrPubmedBookArticle()) { if (article instanceof PubmedArticle) { PubmedArticle currentArticle = (PubmedArticle) article; parseArticle(currentArticle, bibItems); } if (article instanceof PubmedBookArticle) { PubmedBookArticle currentArticle = (PubmedBookArticle) article; parseBookArticle(currentArticle, bibItems); } } } else if (unmarshalledObject instanceof PubmedArticle) { PubmedArticle article = (PubmedArticle) unmarshalledObject; parseArticle(article, bibItems); } else if (unmarshalledObject instanceof PubmedBookArticle) { PubmedBookArticle currentArticle = (PubmedBookArticle) unmarshalledObject; parseBookArticle(currentArticle, bibItems); } else { PubmedBookArticleSet bookArticleSet = (PubmedBookArticleSet) unmarshalledObject; for (PubmedBookArticle bookArticle : bookArticleSet.getPubmedBookArticle()) { parseBookArticle(bookArticle, bibItems); } } } catch (JAXBException | XMLStreamException e) { LOGGER.debug("could not parse document", e); return ParserResult.fromErrorMessage(e.getLocalizedMessage()); } return new ParserResult(bibItems); }
@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()); } }