PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/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
  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. package org.gjt.sp.jedit.search;
  23. //{{{ Imports
  24. import bsh.NameSpace;
  25. import gnu.regexp.*;
  26. import javax.swing.text.Segment;
  27. import org.gjt.sp.jedit.BeanShell;
  28. import org.gjt.sp.jedit.MiscUtilities;
  29. //}}}
  30. /**
  31. * A regular expression string matcher.
  32. * @author Slava Pestov
  33. * @version $Id: RESearchMatcher.java 3933 2001-12-03 10:52:27Z spestov $
  34. */
  35. public class RESearchMatcher implements SearchMatcher
  36. {
  37. /**
  38. * Perl5 syntax with character classes enabled.
  39. * @since jEdit 3.0pre5
  40. */
  41. public static final RESyntax RE_SYNTAX_JEDIT
  42. = new RESyntax(RESyntax.RE_SYNTAX_PERL5)
  43. .set(RESyntax.RE_CHAR_CLASSES)
  44. .setLineSeparator("\n");
  45. //{{{ RESearchMatcher constructor
  46. /**
  47. * Creates a new regular expression string matcher.
  48. */
  49. public RESearchMatcher(String search, String replace,
  50. boolean ignoreCase, boolean beanshell,
  51. String replaceMethod) throws Exception
  52. {
  53. if(!beanshell)
  54. {
  55. // gnu.regexp doesn't seem to support \n and \t in the replace
  56. // string, so implement it here
  57. this.replace = MiscUtilities.escapesToChars(replace);
  58. }
  59. this.beanshell = beanshell;
  60. this.replaceMethod = replaceMethod;
  61. replaceNS = new NameSpace(BeanShell.getNameSpace(),"search and replace");
  62. re = new RE(search,(ignoreCase ? RE.REG_ICASE : 0)
  63. | RE.REG_MULTILINE,RE_SYNTAX_JEDIT);
  64. } //}}}
  65. //{{{ nextMatch() method
  66. /**
  67. * Returns the offset of the first match of the specified text
  68. * within this matcher.
  69. * @param text The text to search in
  70. * @param start True if the start of the segment is the beginning of the
  71. * buffer
  72. * @param end True if the end of the segment is the end of the buffer
  73. * @return an array where the first element is the start offset
  74. * of the match, and the second element is the end offset of
  75. * the match
  76. * @since jEdit 4.0pre3
  77. */
  78. public int[] nextMatch(CharIndexed text, boolean start, boolean end)
  79. {
  80. int flags = 0;
  81. // unless we are matching from the start of the buffer,
  82. // ^ should not match on the beginning of the substring
  83. if(!start)
  84. flags |= RE.REG_NOTBOL;
  85. // unless we are matching to the end of the buffer,
  86. // $ should not match on the end of the substring
  87. if(!end)
  88. flags |= RE.REG_NOTEOL;
  89. REMatch match = re.getMatch(text,0,flags);
  90. if(match == null)
  91. return null;
  92. int[] result = { match.getStartIndex(), match.getEndIndex() };
  93. return result;
  94. } //}}}
  95. //{{{ substitute() method
  96. /**
  97. * Returns the specified text, with any substitution specified
  98. * within this matcher performed.
  99. * @param text The text
  100. */
  101. public String substitute(String text) throws Exception
  102. {
  103. REMatch match = re.getMatch(text);
  104. if(match == null)
  105. return null;
  106. if(beanshell)
  107. {
  108. int count = re.getNumSubs();
  109. for(int i = 0; i < count; i++)
  110. replaceNS.setVariable("_" + i,match.toString(i));
  111. Object obj = BeanShell.runCachedBlock(replaceMethod,
  112. null,replaceNS);
  113. if(obj == null)
  114. return null;
  115. else
  116. return obj.toString();
  117. }
  118. else
  119. return match.substituteInto(replace);
  120. } //}}}
  121. //{{{ Private members
  122. private String replace;
  123. private RE re;
  124. private boolean beanshell;
  125. private String replaceMethod;
  126. private NameSpace replaceNS;
  127. //}}}
  128. }