PageRenderTime 140ms CodeModel.GetById 122ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/org/gjt/sp/jedit/search/RESearchMatcher.java

#
Java | 142 lines | 65 code | 14 blank | 63 comment | 11 complexity | 9a110adbc6222a604d99a5c022596e77 MD5 | raw file
  1/*
  2 * RESearchMatcher.java - Regular expression matcher
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 1999, 2000, 2001 Slava Pestov
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22
 23package org.gjt.sp.jedit.search;
 24
 25//{{{ Imports
 26import bsh.NameSpace;
 27import gnu.regexp.*;
 28import javax.swing.text.Segment;
 29import org.gjt.sp.jedit.BeanShell;
 30import org.gjt.sp.jedit.MiscUtilities;
 31//}}}
 32
 33/**
 34 * A regular expression string matcher.
 35 * @author Slava Pestov
 36 * @version $Id: RESearchMatcher.java 3933 2001-12-03 10:52:27Z spestov $
 37 */
 38public class RESearchMatcher implements SearchMatcher
 39{
 40	/**
 41	 * Perl5 syntax with character classes enabled.
 42	 * @since jEdit 3.0pre5
 43	 */
 44	public static final RESyntax RE_SYNTAX_JEDIT
 45		= new RESyntax(RESyntax.RE_SYNTAX_PERL5)
 46		.set(RESyntax.RE_CHAR_CLASSES)
 47		.setLineSeparator("\n");
 48
 49	//{{{ RESearchMatcher constructor
 50	/**
 51	 * Creates a new regular expression string matcher.
 52	 */
 53	public RESearchMatcher(String search, String replace,
 54		boolean ignoreCase, boolean beanshell,
 55		String replaceMethod) throws Exception
 56	{
 57		if(!beanshell)
 58		{
 59			// gnu.regexp doesn't seem to support \n and \t in the replace
 60			// string, so implement it here
 61			this.replace = MiscUtilities.escapesToChars(replace);
 62		}
 63
 64		this.beanshell = beanshell;
 65		this.replaceMethod = replaceMethod;
 66		replaceNS = new NameSpace(BeanShell.getNameSpace(),"search and replace");
 67
 68		re = new RE(search,(ignoreCase ? RE.REG_ICASE : 0)
 69			| RE.REG_MULTILINE,RE_SYNTAX_JEDIT);
 70	} //}}}
 71
 72	//{{{ nextMatch() method
 73	/**
 74	 * Returns the offset of the first match of the specified text
 75	 * within this matcher.
 76	 * @param text The text to search in
 77	 * @param start True if the start of the segment is the beginning of the
 78	 * buffer
 79	 * @param end True if the end of the segment is the end of the buffer
 80	 * @return an array where the first element is the start offset
 81	 * of the match, and the second element is the end offset of
 82	 * the match
 83	 * @since jEdit 4.0pre3
 84	 */
 85	public int[] nextMatch(CharIndexed text, boolean start, boolean end)
 86	{
 87		int flags = 0;
 88
 89		// unless we are matching from the start of the buffer,
 90		// ^ should not match on the beginning of the substring
 91		if(!start)
 92			flags |= RE.REG_NOTBOL;
 93		// unless we are matching to the end of the buffer,
 94		// $ should not match on the end of the substring
 95		if(!end)
 96			flags |= RE.REG_NOTEOL;
 97
 98		REMatch match = re.getMatch(text,0,flags);
 99		if(match == null)
100			return null;
101
102		int[] result = { match.getStartIndex(), match.getEndIndex() };
103		return result;
104	} //}}}
105
106	//{{{ substitute() method
107	/**
108	 * Returns the specified text, with any substitution specified
109	 * within this matcher performed.
110	 * @param text The text
111	 */
112	public String substitute(String text) throws Exception
113	{
114		REMatch match = re.getMatch(text);
115		if(match == null)
116			return null;
117
118		if(beanshell)
119		{
120			int count = re.getNumSubs();
121			for(int i = 0; i < count; i++)
122				replaceNS.setVariable("_" + i,match.toString(i));
123
124			Object obj = BeanShell.runCachedBlock(replaceMethod,
125				null,replaceNS);
126			if(obj == null)
127				return null;
128			else
129				return obj.toString();
130		}
131		else
132			return match.substituteInto(replace);
133	} //}}}
134
135	//{{{ Private members
136	private String replace;
137	private RE re;
138	private boolean beanshell;
139	private String replaceMethod;
140	private NameSpace replaceNS;
141	//}}}
142}