示例#1
0
    public boolean compare(BibtexEntry entry) {
      // specification of fields to search is done in the search expression itself
      String[] searchKeys = entry.getAllFields().toArray(new String[entry.getAllFields().size()]);

      boolean noSuchField = true;
      // this loop iterates over all regular keys, then over pseudo keys like "type"
      for (int i = 0; i < searchKeys.length + 1; i++) {
        String content;
        if (i - searchKeys.length == 0) {
          // PSEUDOFIELD_TYPE
          if (!fieldPattern.matcher("entrytype").matches()) continue;
          content = entry.getType().getName();
        } else {
          String searchKey = searchKeys[i];
          if (!fieldPattern.matcher(searchKey).matches()) continue;
          content = entry.getField(searchKey);
        }
        noSuchField = false;
        if (content == null) continue; // paranoia

        if (matchInField(content)) {
          return true;
        }
      }

      return noSuchField && operator == ComparisonOperator.DOES_NOT_CONTAIN;
    }
  @Override
  public boolean applyRule(String query, BibtexEntry bibtexEntry) {

    String searchString = query;
    if (!caseSensitive) {
      searchString = searchString.toLowerCase();
    }

    List<String> words = new SentenceAnalyzer(searchString).getWords();

    // We need match for all words:
    boolean[] matchFound = new boolean[words.size()];

    for (String field : bibtexEntry.getAllFields()) {
      Object fieldContentAsObject = bibtexEntry.getField(field);
      if (fieldContentAsObject != null) {
        String fieldContent =
            ContainBasedSearchRule.REMOVE_LATEX_COMMANDS.format(fieldContentAsObject.toString());
        if (!caseSensitive) {
          fieldContent = fieldContent.toLowerCase();
        }

        int index = 0;
        // Check if we have a match for each of the query words, ignoring
        // those words for which we already have a match:
        for (String word : words) {
          matchFound[index] = matchFound[index] || fieldContent.contains(word);

          index++;
        }
      }
    }
    for (boolean aMatchFound : matchFound) {
      if (!aMatchFound) {
        return false; // Didn't match all words.
      }
    }
    return true; // Matched all words.
  }