Пример #1
0
  @Override
  public List<FieldChange> cleanup(BibEntry entry) {

    ArrayList<FieldChange> changes = new ArrayList<>();

    // First check if the Doi Field is empty
    if (entry.getField("doi") != null) {
      String doiFieldValue = entry.getField("doi");

      Optional<DOI> doi = DOI.build(doiFieldValue);

      if (doi.isPresent()) {
        String newValue = doi.get().getDOI();
        if (!doiFieldValue.equals(newValue)) {
          entry.setField("doi", newValue);

          FieldChange change = new FieldChange(entry, "doi", doiFieldValue, newValue);
          changes.add(change);
        }

        // Doi field seems to contain Doi -> cleanup note, url, ee field
        for (String field : fields) {
          DOI.build(entry.getField((field)))
              .ifPresent(unused -> removeFieldValue(entry, field, changes));
        }
      }
    } else {
      // As the Doi field is empty we now check if note, url, or ee field contains a Doi
      for (String field : fields) {
        Optional<DOI> doi = DOI.build(entry.getField(field));

        if (doi.isPresent()) {
          // update Doi
          String oldValue = entry.getField("doi");
          String newValue = doi.get().getDOI();

          entry.setField("doi", newValue);

          FieldChange change = new FieldChange(entry, "doi", oldValue, newValue);
          changes.add(change);

          removeFieldValue(entry, field, changes);
        }
      }
    }

    return changes;
  }
Пример #2
0
  /**
   * Removes the http://... for each DOI Moves DOIs from URL and NOTE filed to DOI field
   *
   * @param ce
   */
  private static void doCleanUpDOI(BibtexEntry bes, NamedCompound ce) {
    // fields to check
    String[] fields = {"note", "url", "ee"};

    // First check if the Doi Field is empty
    if (bes.getField("doi") != null) {
      String doiFieldValue = bes.getField("doi");

      Optional<DOI> doi = DOI.build(doiFieldValue);

      if (doi.isPresent()) {
        String newValue = doi.get().getDOI();
        if (!doiFieldValue.equals(newValue)) {
          ce.addEdit(new UndoableFieldChange(bes, "doi", doiFieldValue, newValue));
          bes.setField("doi", newValue);
        }

        // Doi field seems to contain Doi
        // -> cleanup note, url, ee field
        for (String field : fields) {
          DOI.build(bes.getField((field))).ifPresent(unused -> removeFieldValue(bes, field, ce));
        }
      }
    } else {
      // As the Doi field is empty we now check if note, url, or ee field contains a Doi
      for (String field : fields) {
        Optional<DOI> doi = DOI.build(bes.getField(field));

        if (doi.isPresent()) {
          // update Doi
          String oldValue = bes.getField("doi");
          String newValue = doi.get().getDOI();
          ce.addEdit(new UndoableFieldChange(bes, "doi", oldValue, newValue));
          bes.setField("doi", newValue);

          removeFieldValue(bes, field, ce);
        }
      }
    }
  }
Пример #3
0
  /**
   * Tries to find a fulltext URL for a given BibTex entry.
   *
   * <p>Currently only uses the DOI if found.
   *
   * @param entry The Bibtex entry
   * @return The fulltext PDF URL Optional, if found, or an empty Optional if not found.
   * @throws NullPointerException if no BibTex entry is given
   * @throws java.io.IOException
   */
  public Optional<URL> findFullText(BibtexEntry entry) throws IOException {
    Objects.requireNonNull(entry);
    Optional<URL> pdfLink = Optional.empty();

    // DOI search
    Optional<DOI> doi = DOI.build(entry.getField("doi"));

    if (doi.isPresent()) {
      String source = String.format(SOURCE, doi.get().getDOI());
      // Retrieve PDF link
      Document html = Jsoup.connect(source).ignoreHttpErrors(true).get();
      Element link = html.select(".pdf-high-res a").first();

      if (link != null) {
        LOGGER.info("Fulltext PDF found @ ACS.");
        pdfLink = Optional.of(new URL(source.replaceFirst("/abs/", "/pdf/")));
      }
    }
    return pdfLink;
  }
Пример #4
0
  /**
   * Set up a mouse listener for opening an external viewer and fetching by DOI
   *
   * @param fieldEditor
   * @param panel
   * @return
   */
  public static Optional<JComponent> getDoiExtraComponent(
      BasePanel panel, EntryEditor entryEditor, FieldEditor fieldEditor) {
    JPanel controls = new JPanel();
    controls.setLayout(new BorderLayout());
    // open doi link
    JButton button = new JButton(Localization.lang("Open"));
    button.setEnabled(false);
    button.addActionListener(
        actionEvent -> {
          try {
            JabRefDesktop.openExternalViewer(
                panel.getBibDatabaseContext(), fieldEditor.getText(), fieldEditor.getFieldName());
          } catch (IOException ex) {
            panel.output(Localization.lang("Unable to open link."));
          }
        });
    // lookup doi
    JButton doiButton = new JButton(Localization.lang("Lookup DOI"));
    doiButton.addActionListener(
        actionEvent -> {
          Optional<DOI> doi = DOI.fromBibEntry(entryEditor.getEntry());
          if (doi.isPresent()) {
            entryEditor.getEntry().setField(FieldName.DOI, doi.get().getDOI());
          } else {
            panel
                .frame()
                .setStatus(
                    Localization.lang("No %0 found", FieldName.getDisplayName(FieldName.DOI)));
          }
        });
    // fetch bibtex data
    JButton fetchButton =
        new JButton(
            Localization.lang("Get BibTeX data from %0", FieldName.getDisplayName(FieldName.DOI)));
    fetchButton.setEnabled(false);
    fetchButton.addActionListener(
        actionEvent -> {
          BibEntry entry = entryEditor.getEntry();
          new FetchAndMergeEntry(entry, panel, FieldName.DOI);
        });

    controls.add(button, BorderLayout.NORTH);
    controls.add(doiButton, BorderLayout.CENTER);
    controls.add(fetchButton, BorderLayout.SOUTH);

    // enable/disable button
    JTextComponent doi = (JTextComponent) fieldEditor;

    doi.getDocument()
        .addDocumentListener(
            new DocumentListener() {
              @Override
              public void changedUpdate(DocumentEvent documentEvent) {
                checkDoi();
              }

              @Override
              public void insertUpdate(DocumentEvent documentEvent) {
                checkDoi();
              }

              @Override
              public void removeUpdate(DocumentEvent documentEvent) {
                checkDoi();
              }

              private void checkDoi() {
                Optional<DOI> doiUrl = DOI.build(doi.getText());
                if (doiUrl.isPresent()) {
                  button.setEnabled(true);
                  fetchButton.setEnabled(true);
                } else {
                  button.setEnabled(false);
                  fetchButton.setEnabled(false);
                }
              }
            });

    return Optional.of(controls);
  }