Esempio n. 1
0
  private static String[] isiAuthorsConvert(String[] authors) {

    String[] result = new String[authors.length];
    for (int i = 0; i < result.length; i++) {
      result[i] = IsiImporter.isiAuthorConvert(authors[i]);
    }
    return result;
  }
Esempio n. 2
0
 public static String isiAuthorsConvert(String authors) {
   String[] s = IsiImporter.isiAuthorsConvert(authors.split(" and |;"));
   return StringUtil.join(s, " and ");
 }
Esempio n. 3
0
  /** Parse the entries in the source, and return a List of BibEntry objects. */
  @Override
  public List<BibEntry> importEntries(InputStream stream, OutputPrinter status) throws IOException {
    if (stream == null) {
      throw new IOException("No stream given.");
    }

    ArrayList<BibEntry> bibitems = new ArrayList<>();
    StringBuilder sb = new StringBuilder();

    BufferedReader in = new BufferedReader(ImportFormatReader.getReaderDefaultEncoding(stream));

    // Pattern fieldPattern = Pattern.compile("^AU |^TI |^SO |^DT |^C1 |^AB
    // |^ID |^BP |^PY |^SE |^PY |^VL |^IS ");
    String str;

    while ((str = in.readLine()) != null) {
      if (str.length() < 3) {
        continue;
      }

      // begining of a new item
      if ("PT ".equals(str.substring(0, 3))) {
        sb.append("::").append(str);
      } else {
        String beg = str.substring(0, 3).trim();

        // I could have used the fieldPattern regular expression instead
        // however this seems to be
        // quick and dirty and it works!
        if (beg.length() == 2) {
          sb.append(" ## "); // mark the begining of each field
          sb.append(str);
        } else {
          sb.append("EOLEOL"); // mark the end of each line
          sb.append(str.trim()); // remove the initial spaces
        }
      }
    }

    String[] entries = sb.toString().split("::");

    HashMap<String, String> hm = new HashMap<>();

    // skip the first entry as it is either empty or has document header
    for (String entry : entries) {
      String[] fields = entry.split(" ## ");

      if (fields.length == 0) {
        fields = entry.split("\n");
      }

      String Type = "";
      String PT = "";
      String pages = "";
      hm.clear();

      for (String field : fields) {
        // empty field don't do anything
        if (field.length() <= 2) {
          continue;
        }

        String beg = field.substring(0, 2);
        String value = field.substring(3);
        if (value.startsWith(" - ")) {
          value = value.substring(3);
        }
        value = value.trim();

        if ("PT".equals(beg)) {
          if (value.startsWith("J")) {
            PT = "article";
          } else {
            PT = value;
          }
          Type = "article"; // make all of them PT?
        } else if ("TY".equals(beg)) {
          if ("JOUR".equals(value)) {
            Type = "article";
          } else if ("CONF".equals(value)) {
            Type = "inproceedings";
          }
        } else if ("JO".equals(beg)) {
          hm.put("booktitle", value);
        } else if ("AU".equals(beg)) {
          String author = IsiImporter.isiAuthorsConvert(value.replaceAll("EOLEOL", " and "));

          // if there is already someone there then append with "and"
          if (hm.get("author") != null) {
            author = hm.get("author") + " and " + author;
          }

          hm.put("author", author);
        } else if ("TI".equals(beg)) {
          hm.put("title", value.replaceAll("EOLEOL", " "));
        } else if ("SO".equals(beg) || "JA".equals(beg)) {
          hm.put("journal", value.replaceAll("EOLEOL", " "));
        } else if ("ID".equals(beg) || "KW".equals(beg)) {

          value = value.replaceAll("EOLEOL", " ");
          String existingKeywords = hm.get("keywords");
          if ((existingKeywords == null) || existingKeywords.contains(value)) {
            existingKeywords = value;
          } else {
            existingKeywords += ", " + value;
          }
          hm.put("keywords", existingKeywords);

        } else if ("AB".equals(beg)) {
          hm.put("abstract", value.replaceAll("EOLEOL", " "));
        } else if ("BP".equals(beg) || "BR".equals(beg) || "SP".equals(beg)) {
          pages = value;
        } else if ("EP".equals(beg)) {
          int detpos = value.indexOf(' ');

          // tweak for IEEE Explore
          if ((detpos != -1) && !value.substring(0, detpos).trim().isEmpty()) {
            value = value.substring(0, detpos);
          }

          pages = pages + "--" + value;
        } else if ("PS".equals(beg)) {
          pages = IsiImporter.parsePages(value);
        } else if ("AR".equals(beg)) {
          pages = value;
        } else if ("IS".equals(beg)) {
          hm.put("number", value);
        } else if ("PY".equals(beg)) {
          hm.put("year", value);
        } else if ("VL".equals(beg)) {
          hm.put("volume", value);
        } else if ("PU".equals(beg)) {
          hm.put("publisher", value);
        } else if ("DI".equals(beg)) {
          hm.put("doi", value);
        } else if ("PD".equals(beg)) {

          String month = IsiImporter.parseMonth(value);
          if (month != null) {
            hm.put("month", month);
          }

        } else if ("DT".equals(beg)) {
          Type = value;
          if ("Review".equals(Type)) {
            Type = "article"; // set "Review" in Note/Comment?
          } else if (Type.startsWith("Article")
              || Type.startsWith("Journal")
              || "article".equals(PT)) {
            Type = "article";
          } else {
            Type = "misc";
          }
        } else if ("CR".equals(beg)) {
          hm.put("CitedReferences", value.replaceAll("EOLEOL", " ; ").trim());
        } else {
          // Preserve all other entries except
          if ("ER".equals(beg) || "EF".equals(beg) || "VR".equals(beg) || "FN".equals(beg)) {
            continue;
          }
          hm.put(beg.toLowerCase(), value);
        }
      }

      if (!"".equals(pages)) {
        hm.put("pages", pages);
      }

      // Skip empty entries
      if (hm.isEmpty()) {
        continue;
      }

      BibEntry b = new BibEntry(DEFAULT_BIBTEXENTRY_ID, EntryTypes.getTypeOrDefault(Type));
      // id assumes an existing database so don't

      // Remove empty fields:
      List<Object> toRemove = new ArrayList<>();
      for (Map.Entry<String, String> field : hm.entrySet()) {
        String content = field.getValue();
        if ((content == null) || content.trim().isEmpty()) {
          toRemove.add(field.getKey());
        }
      }
      for (Object aToRemove : toRemove) {
        hm.remove(aToRemove);
      }

      // Polish entries
      IsiImporter.processSubSup(hm);
      IsiImporter.processCapitalization(hm);

      b.setField(hm);

      bibitems.add(b);
    }

    return bibitems;
  }