Esempio n. 1
0
  /**
   * Import an entry from an OAI2 archive. The BibtexEntry provided has to have the field
   * OAI2_IDENTIFIER_FIELD set to the search string.
   *
   * @param key The OAI2 key to fetch from ArXiv.
   * @return The imnported BibtexEntry or null if none.
   */
  private BibtexDatabase importInspireEntries(String key, OutputPrinter frame) {
    String url = constructUrl(key);
    try {
      HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
      conn.setRequestProperty("User-Agent", "Jabref");
      InputStream inputStream = conn.getInputStream();

      INSPIREBibtexFilterReader reader =
          new INSPIREBibtexFilterReader(new InputStreamReader(inputStream));

      ParserResult pr = BibtexParser.parse(reader);

      return pr.getDatabase();
    } catch (IOException e) {
      frame.showMessage(
          Localization.lang("An Exception ocurred while accessing '%0'", url) + "\n\n" + e,
          Localization.lang(getKeyName()),
          JOptionPane.ERROR_MESSAGE);
    } catch (RuntimeException e) {
      frame.showMessage(
          Localization.lang(
                  "An Error occurred while fetching from INSPIRE source (%0):", new String[] {url})
              + "\n\n"
              + e.getMessage(),
          Localization.lang(getKeyName()),
          JOptionPane.ERROR_MESSAGE);
    }
    return null;
  }
  @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);
  }
  /* (non-Javadoc)
   * @see junit.framework.TestCase#setUp()
   */
  @Before
  public void setUp() throws Exception {
    ParserResult result =
        BibtexParser.parse(new FileReader(ImportDataTest.UNLINKED_FILES_TEST_BIB));
    database = result.getDatabase();
    entries = database.getEntries();

    entry1 = database.getEntryByKey("entry1");
    entry2 = database.getEntryByKey("entry2");
  }
Esempio n. 4
0
  private BibtexDatabase parseBibtexDatabase(List<String> id, boolean abs) throws IOException {
    if (id.isEmpty()) {
      return null;
    }
    URLConnection conn;
    try {
      conn = new URL(IEEEXploreFetcher.IMPORT_URL).openConnection();
    } catch (MalformedURLException e) {
      e.printStackTrace();
      return null;
    }
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setRequestProperty("Referer", searchUrl);
    PrintWriter out = new PrintWriter(conn.getOutputStream());

    String recordIds = "";
    for (String anId : id) {
      recordIds += anId + " ";
    }
    recordIds = recordIds.trim();
    String citation = abs ? "citation-abstract" : "citation-only";

    String content =
        "recordIds="
            + recordIds.replaceAll(" ", "%20")
            + "&fromPageName=&citations-format="
            + citation
            + "&download-format=download-bibtex";
    System.out.println(content);
    out.write(content);
    out.flush();
    out.close();

    BufferedReader bufr = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    StringBuilder sb = new StringBuilder();
    char[] buffer = new char[256];
    while (true) {
      int bytesRead = bufr.read(buffer);
      if (bytesRead == -1) {
        break;
      }
      for (int i = 0; i < bytesRead; i++) {
        sb.append(buffer[i]);
      }
    }
    System.out.println(sb);

    ParserResult results = new BibtexParser(bufr).parse();
    bufr.close();
    return results.getDatabase();
  }