KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonicsystems > jarjar > regex > GnuRegexEngine


1 /*
2   Jar Jar Links - A utility to repackage and embed Java libraries
3   Copyright (C) 2004 Tonic Systems, Inc.
4
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 2 of the License, or
8   (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program; see the file COPYING. if not, write to
17   the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18   Boston, MA 02111-1307 USA
19 */

20
21 package com.tonicsystems.jarjar.regex;
22
23 import gnu.regexp.RE;
24 import gnu.regexp.REException;
25 import gnu.regexp.REMatch;
26
27 public class GnuRegexEngine implements RegexEngine
28 {
29     public Pattern compile(String JavaDoc pattern) {
30         try {
31             final RE re = new RE(pattern);
32             return new Pattern() {
33                 public String JavaDoc replaceAll(String JavaDoc value, String JavaDoc replace) {
34                     return re.substituteAll(value, replace, 0, RE.REG_NO_INTERPOLATE);
35                 }
36                 public boolean matches(String JavaDoc value) {
37                     return re.isMatch(value);
38                 }
39                 public int groupCount() {
40                     return re.getNumSubs();
41                 }
42                 public Matcher getMatcher(final String JavaDoc value) {
43                     final REMatch match = re.getMatch(value, 0);
44                     return new Matcher() {
45                         public boolean matches() {
46                             return re.isMatch(value); // TODO?
47
}
48                         public int start() {
49                             return match.getStartIndex();
50                         }
51                         public int end() {
52                             return match.getEndIndex();
53                         }
54                         public String JavaDoc group(int index) {
55                             return match.toString(index);
56                         }
57                     };
58                 }
59                 public String JavaDoc toString() {
60                     return re.toString();
61                 }
62             };
63         } catch (final REException e) {
64             throw new IllegalArgumentException JavaDoc(e.getMessage()) {
65                 public Throwable JavaDoc getCause() {
66                     return e;
67                 }
68             };
69         }
70     }
71 }
72
Popular Tags