예제 #1
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();
    }
  }
예제 #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);
  }
예제 #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);
  }
예제 #4
0
 /**
  * Parses BibtexEntries from the given string and returns one entry found (or null if none found)
  *
  * <p>It is undetermined which entry is returned, so use this in case you know there is only one
  * entry in the string.
  *
  * @param bibtexString
  * @return The BibEntry or null if non was found or an error occurred.
  */
 public static BibEntry singleFromString(String bibtexString) {
   Collection<BibEntry> entries = BibtexParser.fromString(bibtexString);
   if ((entries == null) || entries.isEmpty()) {
     return null;
   }
   return entries.iterator().next();
 }
  @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);
  }
예제 #6
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);
  }
예제 #7
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;
    }
  }
예제 #8
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);
  }
예제 #9
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);
  }
예제 #10
0
  @Test
  public void testGetPublicationDate() {

    Assert.assertEquals(
        "2003-02",
        (BibtexParser.singleFromString("@ARTICLE{HipKro03, year = {2003}, month = #FEB# }"))
            .getPublicationDate());

    Assert.assertEquals(
        "2003-03",
        (BibtexParser.singleFromString("@ARTICLE{HipKro03, year = {2003}, month = 3 }"))
            .getPublicationDate());

    Assert.assertEquals(
        "2003",
        (BibtexParser.singleFromString("@ARTICLE{HipKro03, year = {2003}}")).getPublicationDate());

    Assert.assertEquals(
        null,
        (BibtexParser.singleFromString("@ARTICLE{HipKro03, month = 3 }")).getPublicationDate());

    Assert.assertEquals(
        null,
        (BibtexParser.singleFromString("@ARTICLE{HipKro03, author={bla}}")).getPublicationDate());

    Assert.assertEquals(
        "2003-12",
        (BibtexParser.singleFromString("@ARTICLE{HipKro03, year = {03}, month = #DEC# }"))
            .getPublicationDate());
  }
예제 #11
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);
  }
예제 #12
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;
  }
예제 #13
0
  @Test
  public void testKeywordMethods() {
    BibEntry be = BibtexParser.singleFromString("@ARTICLE{Key15, keywords = {Foo, Bar}}");

    String[] expected = {"Foo", "Bar"};
    Assert.assertArrayEquals(expected, be.getSeparatedKeywords().toArray());

    List<String> kw = be.getSeparatedKeywords();

    be.addKeyword("FooBar");
    String[] expected2 = {"Foo", "Bar", "FooBar"};
    Assert.assertArrayEquals(expected2, be.getSeparatedKeywords().toArray());

    be.addKeyword("FooBar");
    Assert.assertArrayEquals(expected2, be.getSeparatedKeywords().toArray());

    be.addKeyword("FOO");
    Assert.assertArrayEquals(expected2, be.getSeparatedKeywords().toArray());

    be.addKeyword("");
    Assert.assertArrayEquals(expected2, be.getSeparatedKeywords().toArray());

    try {
      be.addKeyword(null);
      Assert.fail();
    } catch (NullPointerException asExpected) {

    }

    BibEntry be2 = new BibEntry();
    Assert.assertTrue(be2.getSeparatedKeywords().isEmpty());
    be2.addKeyword("");
    Assert.assertTrue(be2.getSeparatedKeywords().isEmpty());
    be2.addKeywords(be.getSeparatedKeywords());
    Assert.assertArrayEquals(expected2, be2.getSeparatedKeywords().toArray());
    be2.putKeywords(kw);
    Assert.assertArrayEquals(expected, be2.getSeparatedKeywords().toArray());
  }
예제 #14
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();
 }
예제 #15
0
  @Override
  public boolean processQuery(String query, ImportInspector inspector, OutputPrinter status) {
    String q;
    try {
      q = URLEncoder.encode(query, StandardCharsets.UTF_8.name());
    } catch (UnsupportedEncodingException e) {
      // this should never happen
      status.setStatus(Localization.lang("Error"));
      LOGGER.warn("Encoding issues", e);
      return false;
    }

    String urlString = String.format(DiVAtoBibTeXFetcher.URL_PATTERN, q);

    // Send the request
    URL url;
    try {
      url = new URL(urlString);
    } catch (MalformedURLException e) {
      LOGGER.warn("Bad URL", e);
      return false;
    }

    String bibtexString;
    try {
      URLDownload dl = new URLDownload(url);

      bibtexString = dl.downloadToString(StandardCharsets.UTF_8);
    } catch (FileNotFoundException e) {
      status.showMessage(
          Localization.lang("Unknown DiVA entry: '%0'.", query),
          Localization.lang("Get BibTeX entry from DiVA"),
          JOptionPane.INFORMATION_MESSAGE);
      return false;
    } catch (IOException e) {
      LOGGER.warn("Communication problems", e);
      return false;
    }

    BibEntry entry = BibtexParser.singleFromString(bibtexString);
    if (entry != null) {
      // Optionally add curly brackets around key words to keep the case
      entry
          .getFieldOptional(FieldName.TITLE)
          .ifPresent(
              title -> {
                // Unit formatting
                if (Globals.prefs.getBoolean(JabRefPreferences.USE_UNIT_FORMATTER_ON_SEARCH)) {
                  title = unitsToLatexFormatter.format(title);
                }

                // Case keeping
                if (Globals.prefs.getBoolean(JabRefPreferences.USE_CASE_KEEPER_ON_SEARCH)) {
                  title = protectTermsFormatter.format(title);
                }
                entry.setField(FieldName.TITLE, title);
              });

      entry
          .getFieldOptional("institution")
          .ifPresent(
              institution ->
                  entry.setField("institution", new UnicodeToLatexFormatter().format(institution)));
      // Do not use the provided key
      // entry.clearField(InternalBibtexFields.KEY_FIELD);
      inspector.addEntry(entry);

      return true;
    }
    return false;
  }
예제 #16
0
  @Override
  public boolean processQuery(String query, ImportInspector inspector, OutputPrinter status) {
    String q;
    try {
      q = URLEncoder.encode(query, StandardCharsets.UTF_8.name());
    } catch (UnsupportedEncodingException e) {
      // this should never happen
      status.setStatus(Localization.lang("Error"));
      e.printStackTrace();
      return false;
    }

    String urlString = String.format(ISBNtoBibTeXFetcher.URL_PATTERN, q);

    // Send the request
    URL url;
    try {
      url = new URL(urlString);
    } catch (MalformedURLException e) {
      e.printStackTrace();
      return false;
    }

    try (InputStream source = url.openStream()) {
      String bibtexString;
      try (Scanner scan = new Scanner(source)) {
        bibtexString = scan.useDelimiter("\\A").next();
      }

      BibEntry entry = BibtexParser.singleFromString(bibtexString);
      if (entry != null) {
        // Optionally add curly brackets around key words to keep the case
        String title = entry.getField("title");
        if (title != null) {
          // Unit formatting
          if (Globals.prefs.getBoolean(JabRefPreferences.USE_UNIT_FORMATTER_ON_SEARCH)) {
            title = unitFormatter.format(title);
          }

          // Case keeping
          if (Globals.prefs.getBoolean(JabRefPreferences.USE_CASE_KEEPER_ON_SEARCH)) {
            title = caseKeeper.format(title);
          }
          entry.setField("title", title);
        }

        inspector.addEntry(entry);
        return true;
      }
      return false;
    } catch (FileNotFoundException e) {
      // invalid ISBN --> 404--> FileNotFoundException
      status.showMessage(Localization.lang("Invalid ISBN"));
      return false;
    } catch (java.net.UnknownHostException e) {
      // It is very unlikely that ebook.de is an unknown host
      // It is more likely that we don't have an internet connection
      status.showMessage(Localization.lang("No_Internet_Connection."));
      return false;
    } catch (Exception e) {
      status.showMessage(e.toString());
      return false;
    }
  }
예제 #17
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();
 }