Esempio n. 1
0
  @Override
  public void getEntries(Map<String, Boolean> selection, ImportInspector inspector) {
    for (Map.Entry<String, Boolean> selentry : selection.entrySet()) {
      if (!shouldContinue) {
        break;
      }
      boolean sel = selentry.getValue();
      if (sel) {
        BibEntry entry = downloadEntryBibTeX(selentry.getKey(), fetchAbstract);
        if (entry != null) {
          // Convert from HTML and optionally add curly brackets around key words to keep the case
          entry
              .getFieldOptional("title")
              .ifPresent(
                  title -> {
                    title = title.replaceAll("\\\\&", "&").replaceAll("\\\\#", "#");
                    title = convertHTMLChars(title);

                    // 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);
                  });

          entry
              .getFieldOptional("abstract")
              .ifPresent(
                  abstr -> {
                    entry.setField("abstract", convertHTMLChars(abstr));
                  });
          inspector.addEntry(entry);
        }
      }
    }
  }
Esempio n. 2
0
  @Override
  public boolean processQuery(String query, ImportInspector dialog, OutputPrinter status) {
    stopFetching = false;
    try {
      List<String> citations = getCitations(query);
      if (citations == null) {
        return false;
      }
      if (citations.isEmpty()) {
        status.showMessage(
            Localization.lang("No entries found for the search string '%0'", query),
            Localization.lang("Search %0", SCIENCE_DIRECT),
            JOptionPane.INFORMATION_MESSAGE);
        return false;
      }

      int i = 0;
      for (String cit : citations) {
        if (stopFetching) {
          break;
        }
        BibsonomyScraper.getEntry(cit).ifPresent(dialog::addEntry);
        dialog.setProgress(++i, citations.size());
      }

      return true;

    } catch (IOException e) {
      LOGGER.warn("Communcation problems", e);
      status.showMessage(
          Localization.lang("Error while fetching from %0", SCIENCE_DIRECT)
              + ": "
              + e.getMessage());
    }
    return false;
  }
  @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;
    }
  }
Esempio n. 4
0
  @Override
  public boolean processQuery(String query, ImportInspector iIDialog, OutputPrinter frameOP) {

    shouldContinue = true;

    query = query.trim().replace(';', ',');

    if (query.matches("\\d+[,\\d+]*")) {
      frameOP.setStatus(Localization.lang("Fetching Medline by id..."));

      List<BibtexEntry> bibs = MedlineImporter.fetchMedline(query, frameOP);

      if (bibs.isEmpty()) {
        frameOP.showMessage(Localization.lang("No references found"));
      }

      for (BibtexEntry entry : bibs) {
        iIDialog.addEntry(entry);
      }
      return true;
    }

    if (!query.isEmpty()) {
      frameOP.setStatus(Localization.lang("Fetching Medline by term..."));

      String searchTerm = toSearchTerm(query);

      // get the ids from entrez
      SearchResult result = getIds(searchTerm, 0, 1);

      if (result.count == 0) {
        frameOP.showMessage(Localization.lang("No references found"));
        return false;
      }

      int numberToFetch = result.count;
      if (numberToFetch > MedlineFetcher.PACING) {

        while (true) {
          String strCount =
              JOptionPane.showInputDialog(
                  Localization.lang("References found")
                      + ": "
                      + numberToFetch
                      + "  "
                      + Localization.lang("Number of references to fetch?"),
                  Integer.toString(numberToFetch));

          if (strCount == null) {
            frameOP.setStatus(Localization.lang("Medline import canceled"));
            return false;
          }

          try {
            numberToFetch = Integer.parseInt(strCount.trim());
            break;
          } catch (RuntimeException ex) {
            frameOP.showMessage(Localization.lang("Please enter a valid number"));
          }
        }
      }

      for (int i = 0; i < numberToFetch; i += MedlineFetcher.PACING) {
        if (!shouldContinue) {
          break;
        }

        int noToFetch = Math.min(MedlineFetcher.PACING, numberToFetch - i);

        // get the ids from entrez
        result = getIds(searchTerm, i, noToFetch);

        List<BibtexEntry> bibs = MedlineImporter.fetchMedline(result.ids, frameOP);
        for (BibtexEntry entry : bibs) {
          iIDialog.addEntry(entry);
        }
        iIDialog.setProgress(i + noToFetch, numberToFetch);
      }
      return true;
    }
    frameOP.showMessage(
        Localization.lang(
            "Please enter a comma separated list of Medline IDs (numbers) or search terms."),
        Localization.lang("Input error"),
        JOptionPane.ERROR_MESSAGE);
    return false;
  }
Esempio n. 5
0
  @Override
  public boolean processQuery(String query, ImportInspector inspector, OutputPrinter status) {
    shouldContinue = true;
    try {
      status.setStatus(Localization.lang("Searching..."));
      HttpResponse<JsonNode> jsonResponse;
      query = URLEncoder.encode(query, "UTF-8");
      jsonResponse =
          Unirest.get(API_URL + query + "&api_key=" + API_KEY + "&p=1")
              .header("accept", "application/json")
              .asJson();
      JSONObject jo = jsonResponse.getBody().getObject();
      int hits = jo.getJSONArray("result").getJSONObject(0).getInt("total");
      int numberToFetch = 0;
      if (hits > 0) {
        if (hits > maxPerPage) {
          while (true) {
            String strCount =
                JOptionPane.showInputDialog(
                    Localization.lang("References found")
                        + ": "
                        + hits
                        + "  "
                        + Localization.lang("Number of references to fetch?"),
                    Integer.toString(hits));

            if (strCount == null) {
              status.setStatus(Localization.lang("Search canceled"));
              return false;
            }

            try {
              numberToFetch = Integer.parseInt(strCount.trim());
              break;
            } catch (RuntimeException ex) {
              status.showMessage(Localization.lang("Please enter a valid number"));
            }
          }
        } else {
          numberToFetch = hits;
        }

        int fetched = 0; // Keep track of number of items fetched for the progress bar
        for (int startItem = 1; startItem <= numberToFetch; startItem += maxPerPage) {
          if (!shouldContinue) {
            break;
          }

          int noToFetch = Math.min(maxPerPage, numberToFetch - startItem);
          jsonResponse =
              Unirest.get(
                      API_URL
                          + query
                          + "&api_key="
                          + API_KEY
                          + "&p="
                          + noToFetch
                          + "&s="
                          + startItem)
                  .header("accept", "application/json")
                  .asJson();
          jo = jsonResponse.getBody().getObject();
          if (jo.has("records")) {
            JSONArray results = jo.getJSONArray("records");
            for (int i = 0; i < results.length(); i++) {
              JSONObject springerJsonEntry = results.getJSONObject(i);
              BibtexEntry entry = jep.SpringerJSONtoBibtex(springerJsonEntry);
              inspector.addEntry(entry);
              fetched++;
              inspector.setProgress(fetched, numberToFetch);
            }
          }
        }
        return true;
      } else {
        status.showMessage(
            Localization.lang("No entries found for the search string '%0'", query),
            Localization.lang("Search %0", "Springer"),
            JOptionPane.INFORMATION_MESSAGE);
        return false;
      }
    } catch (UnirestException e) {
      LOGGER.warn("Problem searching Springer", e);
    } catch (UnsupportedEncodingException e) {
      LOGGER.warn("Cannot encode query", e);
    }
    return false;
  }
Esempio n. 6
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;
  }