示例#1
0
  /**
   * Convert a JSONObject containing a bibJSON entry to a BibEntry
   *
   * @param bibJsonEntry The JSONObject to convert
   * @return the converted BibEntry
   */
  public BibEntry parseBibJSONtoBibtex(JSONObject bibJsonEntry, String keywordSeparator) {
    // Fields that are directly accessible at the top level BibJson object
    String[] singleFieldStrings = {
      FieldName.YEAR, FieldName.TITLE, FieldName.ABSTRACT, FieldName.MONTH
    };

    // Fields that are accessible in the journal part of the BibJson object
    String[] journalSingleFieldStrings = {FieldName.PUBLISHER, FieldName.NUMBER, FieldName.VOLUME};

    BibEntry entry = new BibEntry();
    entry.setType("article");

    // Authors
    if (bibJsonEntry.has("author")) {
      JSONArray authors = bibJsonEntry.getJSONArray("author");
      List<String> authorList = new ArrayList<>();
      for (int i = 0; i < authors.length(); i++) {
        if (authors.getJSONObject(i).has("name")) {
          authorList.add(authors.getJSONObject(i).getString("name"));
        } else {
          LOGGER.info("Empty author name.");
        }
      }
      entry.setField(FieldName.AUTHOR, String.join(" and ", authorList));
    } else {
      LOGGER.info("No author found.");
    }

    // Direct accessible fields
    for (String field : singleFieldStrings) {
      if (bibJsonEntry.has(field)) {
        entry.setField(field, bibJsonEntry.getString(field));
      }
    }

    // Page numbers
    if (bibJsonEntry.has("start_page")) {
      if (bibJsonEntry.has("end_page")) {
        entry.setField(
            FieldName.PAGES,
            bibJsonEntry.getString("start_page") + "--" + bibJsonEntry.getString("end_page"));
      } else {
        entry.setField(FieldName.PAGES, bibJsonEntry.getString("start_page"));
      }
    }

    // Journal
    if (bibJsonEntry.has("journal")) {
      JSONObject journal = bibJsonEntry.getJSONObject("journal");
      // Journal title
      if (journal.has("title")) {
        entry.setField(FieldName.JOURNAL, journal.getString("title"));
      } else {
        LOGGER.info("No journal title found.");
      }
      // Other journal related fields
      for (String field : journalSingleFieldStrings) {
        if (journal.has(field)) {
          entry.setField(field, journal.getString(field));
        }
      }
    } else {
      LOGGER.info("No journal information found.");
    }

    // Keywords
    if (bibJsonEntry.has("keywords")) {
      JSONArray keywords = bibJsonEntry.getJSONArray("keywords");
      LinkedHashSet<String> keywordList = new LinkedHashSet<>();
      for (int i = 0; i < keywords.length(); i++) {
        if (!keywords.isNull(i)) {
          keywordList.add(keywords.getString(i));
        }
      }
      entry.putKeywords(keywordList, keywordSeparator);
    }

    // Identifiers
    if (bibJsonEntry.has("identifier")) {
      JSONArray identifiers = bibJsonEntry.getJSONArray("identifier");
      for (int i = 0; i < identifiers.length(); i++) {
        String type = identifiers.getJSONObject(i).getString("type");
        if ("doi".equals(type)) {
          entry.setField(FieldName.DOI, identifiers.getJSONObject(i).getString("id"));
        } else if ("pissn".equals(type)) {
          entry.setField(FieldName.ISSN, identifiers.getJSONObject(i).getString("id"));
        } else if ("eissn".equals(type)) {
          entry.setField(FieldName.ISSN, identifiers.getJSONObject(i).getString("id"));
        }
      }
    }

    // Links
    if (bibJsonEntry.has("link")) {
      JSONArray links = bibJsonEntry.getJSONArray("link");
      for (int i = 0; i < links.length(); i++) {
        if (links.getJSONObject(i).has("type")) {
          String type = links.getJSONObject(i).getString("type");
          if ("fulltext".equals(type) && links.getJSONObject(i).has("url")) {
            entry.setField(FieldName.URL, links.getJSONObject(i).getString("url"));
          }
        }
      }
    }

    return entry;
  }