KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > search > PatternSearchMatcher


1 /*
2  * PatternSearchMatcher.java - Regular expression matcher
3  * :noTabs=false:
4  *
5  * Copyright (C) 2006 Marcelo Vanzin
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.gjt.sp.jedit.search;
23
24 import java.util.regex.Matcher JavaDoc;
25 import java.util.regex.Pattern JavaDoc;
26
27 /**
28  * A regular expression string matcher using java.util.regex.
29  * @see java.util.regex.Pattern
30  *
31  * @author Marcelo Vanzin
32  * @version $Id: PatternSearchMatcher.java 8168 2006-12-02 21:43:00Z kpouer $
33  * @since jEdit 4.3pre5
34  */

35 public class PatternSearchMatcher extends SearchMatcher
36 {
37
38     /**
39      * Creates a new regular expression string matcher.
40      * @see java.util.regex.Pattern
41      * @param search the search pattern
42      * @param ignoreCase <code>true</code> if you want to ignore case
43      * @since jEdit 4.3pre5
44      */

45     public PatternSearchMatcher(String JavaDoc search, boolean ignoreCase)
46     {
47         pattern = search;
48         flags = Pattern.MULTILINE;
49         if (ignoreCase)
50             flags |= Pattern.CASE_INSENSITIVE;
51     }
52
53     /**
54      * Returns the offset of the first match of the specified text
55      * within this matcher.
56      *
57      * @param text The text to search in
58      * @param start True if the start of the segment is the beginning
59      * of the buffer
60      * @param end True if the end of the segment is the end of the
61      * buffer
62      * @param firstTime If false and the search string matched at the
63      * start offset with length zero, automatically
64      * find next match
65      * @param reverse Unsupported for PatternSearchMatcher. Should
66      * always be "false".
67      *
68      * @return A {@link SearchMatcher.Match} object.
69      * @since jEdit 4.3pre5
70      */

71     public SearchMatcher.Match nextMatch(CharSequence JavaDoc text, boolean start,
72         boolean end, boolean firstTime, boolean reverse)
73     {
74         if (re == null)
75             re = Pattern.compile(pattern, flags);
76
77         Matcher JavaDoc match = re.matcher(text);
78         if (!match.find())
79             return null;
80
81         // if we're not at the start of the buffer, and the pattern
82
// begins with "^" and matched the beginning of the region
83
// being matched, ignore the match and try the next one.
84
if (!start && match.start() == 0
85             && re.pattern().charAt(0) == '^' && !match.find())
86             return null;
87
88         // similarly, if we're not at the end of the buffer and we
89
// match the end of the text, and the pattern ends with a "$",
90
// return null.
91
if (!end && match.end() == (text.length() - 1)
92             && pattern.charAt(pattern.length() - 1) == '$')
93             return null;
94
95         returnValue.substitutions = new String JavaDoc[match.groupCount() + 1];
96         for(int i = 0; i < returnValue.substitutions.length; i++)
97         {
98             returnValue.substitutions[i] = match.group(i);
99         }
100
101         int _start = match.start();
102         int _end = match.end();
103
104         returnValue.start = _start;
105         returnValue.end = _end;
106         return returnValue;
107     }
108
109     public boolean isMatchingEOL()
110     {
111         return pattern.charAt(pattern.length() - 1) == '$';
112     }
113
114     //{{{ toString() method
115
public String JavaDoc toString()
116     {
117         return "PatternSearchMatcher[" + pattern + ']';
118     } //}}}
119

120         private int flags;
121     private Pattern JavaDoc re;
122     private String JavaDoc pattern;
123
124 }
125
126
Popular Tags