/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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
- /*
- * RESearchMatcher.java - Regular expression matcher
- * :tabSize=8:indentSize=8:noTabs=false:
- * :folding=explicit:collapseFolds=1:
- *
- * Copyright (C) 1999, 2000, 2001 Slava Pestov
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
- package org.gjt.sp.jedit.search;
- //{{{ Imports
- import bsh.NameSpace;
- import gnu.regexp.*;
- import javax.swing.text.Segment;
- import org.gjt.sp.jedit.BeanShell;
- import org.gjt.sp.jedit.MiscUtilities;
- //}}}
- /**
- * A regular expression string matcher.
- * @author Slava Pestov
- * @version $Id: RESearchMatcher.java 3933 2001-12-03 10:52:27Z spestov $
- */
- public class RESearchMatcher implements SearchMatcher
- {
- /**
- * Perl5 syntax with character classes enabled.
- * @since jEdit 3.0pre5
- */
- public static final RESyntax RE_SYNTAX_JEDIT
- = new RESyntax(RESyntax.RE_SYNTAX_PERL5)
- .set(RESyntax.RE_CHAR_CLASSES)
- .setLineSeparator("\n");
- //{{{ RESearchMatcher constructor
- /**
- * Creates a new regular expression string matcher.
- */
- public RESearchMatcher(String search, String replace,
- boolean ignoreCase, boolean beanshell,
- String replaceMethod) throws Exception
- {
- if(!beanshell)
- {
- // gnu.regexp doesn't seem to support \n and \t in the replace
- // string, so implement it here
- this.replace = MiscUtilities.escapesToChars(replace);
- }
- this.beanshell = beanshell;
- this.replaceMethod = replaceMethod;
- replaceNS = new NameSpace(BeanShell.getNameSpace(),"search and replace");
- re = new RE(search,(ignoreCase ? RE.REG_ICASE : 0)
- | RE.REG_MULTILINE,RE_SYNTAX_JEDIT);
- } //}}}
- //{{{ nextMatch() method
- /**
- * Returns the offset of the first match of the specified text
- * within this matcher.
- * @param text The text to search in
- * @param start True if the start of the segment is the beginning of the
- * buffer
- * @param end True if the end of the segment is the end of the buffer
- * @return an array where the first element is the start offset
- * of the match, and the second element is the end offset of
- * the match
- * @since jEdit 4.0pre3
- */
- public int[] nextMatch(CharIndexed text, boolean start, boolean end)
- {
- int flags = 0;
- // unless we are matching from the start of the buffer,
- // ^ should not match on the beginning of the substring
- if(!start)
- flags |= RE.REG_NOTBOL;
- // unless we are matching to the end of the buffer,
- // $ should not match on the end of the substring
- if(!end)
- flags |= RE.REG_NOTEOL;
- REMatch match = re.getMatch(text,0,flags);
- if(match == null)
- return null;
- int[] result = { match.getStartIndex(), match.getEndIndex() };
- return result;
- } //}}}
- //{{{ substitute() method
- /**
- * Returns the specified text, with any substitution specified
- * within this matcher performed.
- * @param text The text
- */
- public String substitute(String text) throws Exception
- {
- REMatch match = re.getMatch(text);
- if(match == null)
- return null;
- if(beanshell)
- {
- int count = re.getNumSubs();
- for(int i = 0; i < count; i++)
- replaceNS.setVariable("_" + i,match.toString(i));
- Object obj = BeanShell.runCachedBlock(replaceMethod,
- null,replaceNS);
- if(obj == null)
- return null;
- else
- return obj.toString();
- }
- else
- return match.substituteInto(replace);
- } //}}}
- //{{{ Private members
- private String replace;
- private RE re;
- private boolean beanshell;
- private String replaceMethod;
- private NameSpace replaceNS;
- //}}}
- }