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