/** * Search a given file for all patterns: maxIsPerPattern == true up to maxMatches of any single * pattern (then search the same file for the next pattern) otherwise up to a maxMatches for all * patterns in a given file(then end search of that file and start on the next one) */ public void find(int maxMatches, boolean maxIsPerPattern) { for (Iterator<File> iter = files.iterator(); iter.hasNext(); ) { // for each file File file = iter.next(); CharSequence buf = null; try { buf = converter.fileToCharSequence(file); } catch (Exception ex) { System.out.println("In File " + file + ": " + ex); continue; } int numMatches = 0; for (int i = 0; i < matchers.length; i++) { matchers[i].reset(buf); if (maxIsPerPattern) { numMatches = 0; // new pattern matcher so reset the match count } if (numMatches == maxMatches) { // can only be true if maxIsPerPattern is false break; // don't look for any more matches in this file } // find each pattern at most maxMatches per file while (matchers[i].find() && numMatches++ < maxMatches) { matcherUsed[i] = true; // System.out.println(files[f].getName() + " " + matchers[i].group()); String region = null; if (matchers[i].groupCount() == 0) { if (precedingCount == 0 && trailingCount == 0) { region = matchers[i].group(); } else { int start = matchers[i].start() - precedingCount; if (start < 0) { start = 0; } int end = matchers[i].end() + trailingCount; if (end > buf.length()) { end = buf.length(); } region = buf.subSequence(start, end).toString(); } } else { region = matchers[i].group(1); } results.addMatch(matchers[i].pattern().pattern(), file, region); } } } }
/** * Search a given file list for all patterns: maxIsPerPattern == true up to maxMatches of any * single pattern (then search the same file for the next pattern) otherwise up to a maxMatches * for all patterns in a given file(then end search of that file and start on the next one) */ public void findMeNot() { for (Iterator<File> iter = files.iterator(); iter.hasNext(); ) { // for each file File file = iter.next(); CharSequence buf = null; try { buf = converter.fileToCharSequence(file); } catch (ParseException ex) { System.out.println(ex); continue; } catch (IOException ex) { System.out.println(ex); continue; } for (int i = 0; i < matchers.length; i++) { matchers[i].reset(buf); if (!matchers[i].find()) { matcherUsed[i] = true; results.addMatch(matchers[i].pattern().pattern(), file, "findMeNot"); } } } }