Esempio n. 1
0
  @Test
  public void roundTripTest() throws IOException {
    // @formatter:off
    String bibtexEntry =
        "@Article{test,"
            + OS.NEWLINE
            + "  Author                   = {Foo Bar},"
            + OS.NEWLINE
            + "  Journal                  = {International Journal of Something},"
            + OS.NEWLINE
            + "  Note                     = {some note},"
            + OS.NEWLINE
            + "  Number                   = {1}"
            + OS.NEWLINE
            + "}";
    // @formatter:on

    // read in bibtex string
    ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry));
    Collection<BibEntry> entries = result.getDatabase().getEntries();
    BibEntry entry = entries.iterator().next();

    // write out bibtex string
    StringWriter stringWriter = new StringWriter();
    writer.write(entry, stringWriter, BibDatabaseMode.BIBTEX);
    String actual = stringWriter.toString();

    assertEquals(bibtexEntry, actual);
  }
Esempio n. 2
0
  @Test
  public void monthFieldSpecialSyntax() throws IOException {
    // @formatter:off
    String bibtexEntry =
        "@Article{test,"
            + OS.NEWLINE
            + "  Author                   = {Foo Bar},"
            + OS.NEWLINE
            + "  Month                    = mar,"
            + OS.NEWLINE
            + "  Number                   = {1}"
            + OS.NEWLINE
            + "}";
    // @formatter:on

    // read in bibtex string
    ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry));
    Collection<BibEntry> entries = result.getDatabase().getEntries();
    BibEntry entry = entries.iterator().next();

    // modify month field
    Set<String> fields = entry.getFieldNames();
    assertTrue(fields.contains("month"));
    assertEquals("#mar#", entry.getFieldOptional("month").get());

    // write out bibtex string
    StringWriter stringWriter = new StringWriter();
    writer.write(entry, stringWriter, BibDatabaseMode.BIBTEX);
    String actual = stringWriter.toString();

    assertEquals(bibtexEntry, actual);
  }
Esempio n. 3
0
  @Test
  public void roundTripWithAppendedNewlines() throws IOException {
    // @formatter:off
    String bibtexEntry =
        "@Article{test,"
            + OS.NEWLINE
            + "  Author                   = {Foo Bar},"
            + OS.NEWLINE
            + "  Journal                  = {International Journal of Something},"
            + OS.NEWLINE
            + "  Note                     = {some note},"
            + OS.NEWLINE
            + "  Number                   = {1}"
            + OS.NEWLINE
            + "}\n\n";
    // @formatter:on

    // read in bibtex string
    ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry));
    Collection<BibEntry> entries = result.getDatabase().getEntries();
    BibEntry entry = entries.iterator().next();

    // write out bibtex string
    StringWriter stringWriter = new StringWriter();
    writer.write(entry, stringWriter, BibDatabaseMode.BIBTEX);
    String actual = stringWriter.toString();

    // Only one appending newline is written by the writer, the rest by FileActions. So, these
    // should be removed here.
    assertEquals(bibtexEntry.substring(0, bibtexEntry.length() - 1), actual);
  }
  @Test
  @Ignore
  public void testAddEntrysFromFiles() throws Exception {
    ParserResult result =
        BibtexParser.parse(new FileReader(ImportDataTest.UNLINKED_FILES_TEST_BIB));
    BibtexDatabase database = result.getDatabase();

    List<File> files = new ArrayList<File>();

    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 (BibtexEntry entry : database.getEntries()) {
      String filesInfo = entry.getField("file");
      if (filesInfo.contains(files.get(0).getName())) {
        file1Found = true;
      }
      if (filesInfo.contains(files.get(1).getName())) {
        file2Found = true;
      }
    }

    Assert.assertTrue(file1Found);
    Assert.assertFalse(file2Found);
  }
Esempio n. 5
0
  private static BibEntry downloadEntryBibTeX(String id, boolean downloadAbstract) {
    try {
      URL url =
          new URL(
              ACMPortalFetcher.START_URL
                  + ACMPortalFetcher.BIBTEX_URL
                  + id
                  + ACMPortalFetcher.BIBTEX_URL_END);
      URLConnection connection = url.openConnection();

      // set user-agent to avoid being blocked as a crawler
      connection.addRequestProperty(
          "User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0");
      Collection<BibEntry> items = null;
      try (BufferedReader in =
          new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
        items = BibtexParser.parse(in).getDatabase().getEntries();
      } catch (IOException e) {
        LOGGER.info("Download of BibTeX information from ACM Portal failed.", e);
      }
      if ((items == null) || items.isEmpty()) {
        return null;
      }
      BibEntry entry = items.iterator().next();
      Thread.sleep(
          ACMPortalFetcher.WAIT_TIME); // wait between requests or you will be blocked by ACM

      // get abstract
      if (downloadAbstract) {
        url = new URL(ACMPortalFetcher.START_URL + ACMPortalFetcher.ABSTRACT_URL + id);
        String page = Util.getResults(url);
        Matcher absM = ACMPortalFetcher.ABSTRACT_PATTERN.matcher(page);
        if (absM.find()) {
          entry.setField("abstract", absM.group(1).trim());
        }
        Thread.sleep(
            ACMPortalFetcher.WAIT_TIME); // wait between requests or you will be blocked by ACM
      }

      return entry;
    } catch (NoSuchElementException e) {
      LOGGER.info(
          "Bad Bibtex record read at: "
              + ACMPortalFetcher.BIBTEX_URL
              + id
              + ACMPortalFetcher.BIBTEX_URL_END,
          e);
      return null;
    } catch (MalformedURLException e) {
      LOGGER.info("Malformed URL.", e);
      return null;
    } catch (IOException e) {
      LOGGER.info("Cannot connect.", e);
      return null;
    } catch (InterruptedException ignored) {
      return null;
    }
  }
Esempio n. 6
0
  @Test
  public void testEntryTypeChange() throws IOException {
    // @formatter:off
    String expected =
        OS.NEWLINE
            + "@Article{test,"
            + OS.NEWLINE
            + "  author       = {BlaBla},"
            + OS.NEWLINE
            + "  journal      = {International Journal of Something},"
            + OS.NEWLINE
            + "  number       = {1},"
            + OS.NEWLINE
            + "  note         = {some note},"
            + OS.NEWLINE
            + "  howpublished = {asdf},"
            + OS.NEWLINE
            + "}"
            + OS.NEWLINE;
    // @formatter:on

    // read in bibtex string
    ParserResult result = BibtexParser.parse(new StringReader(expected));
    Collection<BibEntry> entries = result.getDatabase().getEntries();
    BibEntry entry = entries.iterator().next();

    // modify entry
    entry.setType("inproceedings");

    // write out bibtex string
    StringWriter stringWriter = new StringWriter();
    writer.write(entry, stringWriter, BibDatabaseMode.BIBTEX);
    String actual = stringWriter.toString();

    // @formatter:off
    String expectedNewEntry =
        OS.NEWLINE
            + "@InProceedings{test,"
            + OS.NEWLINE
            + "  author       = {BlaBla},"
            + OS.NEWLINE
            + "  number       = {1},"
            + OS.NEWLINE
            + "  note         = {some note},"
            + OS.NEWLINE
            + "  howpublished = {asdf},"
            + OS.NEWLINE
            + "  journal      = {International Journal of Something},"
            + OS.NEWLINE
            + "}"
            + OS.NEWLINE;
    // @formatter:on
    assertEquals(expectedNewEntry, actual);
  }
Esempio n. 7
0
  /**
   * Parses BibtexEntries from the given string and returns the collection of all entries found.
   *
   * @param bibtexString
   * @return Returns returns an empty collection if no entries where found or if an error occurred.
   */
  public static List<BibEntry> fromString(String bibtexString) {
    StringReader reader = new StringReader(bibtexString);
    BibtexParser parser = new BibtexParser(reader);

    try {
      return parser.parse().getDatabase().getEntries();
    } catch (Exception e) {
      LOGGER.warn("BibtexParser.fromString(String): " + e.getMessage(), e);
      return Collections.emptyList();
    }
  }
Esempio n. 8
0
  @Test
  public void roundTripWithCamelCasingInTheOriginalEntryAndResultInLowerCase() throws IOException {
    // @formatter:off
    String bibtexEntry =
        OS.NEWLINE
            + "@Article{test,"
            + OS.NEWLINE
            + "  Author                   = {Foo Bar},"
            + OS.NEWLINE
            + "  Journal                  = {International Journal of Something},"
            + OS.NEWLINE
            + "  Note                     = {some note},"
            + OS.NEWLINE
            + "  Number                   = {1},"
            + OS.NEWLINE
            + "  HowPublished             = {asdf},"
            + OS.NEWLINE
            + "}";
    // @formatter:on

    // read in bibtex string
    ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry));
    Collection<BibEntry> entries = result.getDatabase().getEntries();
    BibEntry entry = entries.iterator().next();

    // modify entry
    entry.setField("author", "BlaBla");

    // write out bibtex string
    StringWriter stringWriter = new StringWriter();
    writer.write(entry, stringWriter, BibDatabaseMode.BIBTEX);
    String actual = stringWriter.toString();

    // @formatter:off
    String expected =
        OS.NEWLINE
            + "@Article{test,"
            + OS.NEWLINE
            + "  author       = {BlaBla},"
            + OS.NEWLINE
            + "  journal      = {International Journal of Something},"
            + OS.NEWLINE
            + "  number       = {1},"
            + OS.NEWLINE
            + "  note         = {some note},"
            + OS.NEWLINE
            + "  howpublished = {asdf},"
            + OS.NEWLINE
            + "}"
            + OS.NEWLINE;
    // @formatter:on
    assertEquals(expected, actual);
  }
Esempio n. 9
0
  @Test
  public void roundTripWithPrecedingCommentAndModificationTest() throws IOException {
    // @formatter:off
    String bibtexEntry =
        "% Some random comment that should stay here"
            + OS.NEWLINE
            + "@Article{test,"
            + OS.NEWLINE
            + "  Author                   = {Foo Bar},"
            + OS.NEWLINE
            + "  Journal                  = {International Journal of Something},"
            + OS.NEWLINE
            + "  Note                     = {some note},"
            + OS.NEWLINE
            + "  Number                   = {1}"
            + OS.NEWLINE
            + "}";
    // @formatter:on

    // read in bibtex string
    ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry));
    Collection<BibEntry> entries = result.getDatabase().getEntries();
    BibEntry entry = entries.iterator().next();

    // change the entry
    entry.setField("author", "John Doe");

    // write out bibtex string
    StringWriter stringWriter = new StringWriter();
    writer.write(entry, stringWriter, BibDatabaseMode.BIBTEX);
    String actual = stringWriter.toString();
    // @formatter:off
    String expected =
        "% Some random comment that should stay here"
            + OS.NEWLINE
            + OS.NEWLINE
            + "@Article{test,"
            + OS.NEWLINE
            + "  author  = {John Doe},"
            + OS.NEWLINE
            + "  journal = {International Journal of Something},"
            + OS.NEWLINE
            + "  number  = {1},"
            + OS.NEWLINE
            + "  note    = {some note},"
            + OS.NEWLINE
            + "}"
            + OS.NEWLINE;
    // @formatter:on

    assertEquals(expected, actual);
  }
Esempio n. 10
0
  private String testSingleWrite(String bibtexEntry) throws IOException {
    // read in bibtex string
    ParserResult result = BibtexParser.parse(new StringReader(bibtexEntry));
    Collection<BibEntry> entries = result.getDatabase().getEntries();
    BibEntry entry = entries.iterator().next();

    // write out bibtex string
    StringWriter stringWriter = new StringWriter();
    writer.write(entry, stringWriter, BibDatabaseMode.BIBTEX);
    String actual = stringWriter.toString();

    assertEquals(bibtexEntry, actual);
    return actual;
  }
Esempio n. 11
0
 private static BibtexEntry bibtexString2BibtexEntry(String s) throws IOException {
   ParserResult result = BibtexParser.parse(new StringReader(s));
   Collection<BibtexEntry> c = result.getDatabase().getEntries();
   Assert.assertEquals(1, c.size());
   return c.iterator().next();
 }
Esempio n. 12
0
 /**
  * Shortcut usage to create a Parser and read the input.
  *
  * @param in the Reader to read from
  * @throws IOException
  */
 public static ParserResult parse(Reader in) throws IOException {
   BibtexParser parser = new BibtexParser(in);
   return parser.parse();
 }