001: /*
002: * PatternSearchMatcher.java - Regular expression matcher
003: * :noTabs=false:
004: *
005: * Copyright (C) 2006 Marcelo Vanzin
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.gjt.sp.jedit.search;
023:
024: import java.util.regex.Matcher;
025: import java.util.regex.Pattern;
026:
027: /**
028: * A regular expression string matcher using java.util.regex.
029: * @see java.util.regex.Pattern
030: *
031: * @author Marcelo Vanzin
032: * @version $Id: PatternSearchMatcher.java 10798 2007-10-04 09:04:25Z kpouer $
033: * @since jEdit 4.3pre5
034: */
035: public class PatternSearchMatcher extends SearchMatcher {
036:
037: /**
038: * Creates a new regular expression string matcher.
039: * @see java.util.regex.Pattern
040: * @param search the search pattern
041: * @param ignoreCase <code>true</code> if you want to ignore case
042: * @since jEdit 4.3pre5
043: */
044: public PatternSearchMatcher(String search, boolean ignoreCase) {
045: pattern = search;
046: flags = Pattern.MULTILINE;
047: if (ignoreCase)
048: flags |= Pattern.CASE_INSENSITIVE;
049: }
050:
051: /**
052: * Returns the offset of the first match of the specified text
053: * within this matcher.
054: *
055: * @param text The text to search in
056: * @param start True if the start of the segment is the beginning
057: * of the buffer
058: * @param end True if the end of the segment is the end of the
059: * buffer
060: * @param firstTime If false and the search string matched at the
061: * start offset with length zero, automatically
062: * find next match
063: * @param reverse Unsupported for PatternSearchMatcher. Should
064: * always be "false".
065: *
066: * @return A {@link SearchMatcher.Match} object.
067: * @since jEdit 4.3pre5
068: */
069: public SearchMatcher.Match nextMatch(CharSequence text,
070: boolean start, boolean end, boolean firstTime,
071: boolean reverse) {
072: if (re == null)
073: re = Pattern.compile(pattern, flags);
074:
075: Matcher match = re.matcher(text);
076: if (!match.find())
077: return null;
078:
079: // if we're not at the start of the buffer, and the pattern
080: // begins with "^" and matched the beginning of the region
081: // being matched, ignore the match and try the next one.
082: if (!start && match.start() == 0
083: && re.pattern().charAt(0) == '^' && !match.find())
084: return null;
085:
086: // similarly, if we're not at the end of the buffer and we
087: // match the end of the text, and the pattern ends with a "$",
088: // return null.
089: if (!end && match.end() == (text.length() - 1)
090: && pattern.charAt(pattern.length() - 1) == '$')
091: return null;
092:
093: returnValue.substitutions = new String[match.groupCount() + 1];
094: for (int i = 0; i < returnValue.substitutions.length; i++) {
095: returnValue.substitutions[i] = match.group(i);
096: }
097:
098: int _start = match.start();
099: int _end = match.end();
100:
101: returnValue.start = _start;
102: returnValue.end = _end;
103: return returnValue;
104: }
105:
106: public boolean isMatchingEOL() {
107: return pattern.charAt(pattern.length() - 1) == '$';
108: }
109:
110: //{{{ toString() method
111: public String toString() {
112: return "PatternSearchMatcher[" + pattern + ']';
113: } //}}}
114:
115: private int flags;
116: private Pattern re;
117: private String pattern;
118:
119: }
|