KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > regexbridge > GNURegex


1 /**
2  * com.mckoi.database.regexbridge.GNURegex 14 Oct 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
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  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database.regexbridge;
26
27 import com.mckoi.database.*;
28 import com.mckoi.util.IntegerVector;
29 import gnu.regexp.*;
30
31 /**
32  * A bridge to the GNU Java regular expression library. This library is
33  * released under the LGPL license which is fully compatible with the GPL
34  * license but may be incompatible with other licenses.
35  *
36  * @author Tobias Downer
37  */

38
39 public class GNURegex implements com.mckoi.database.RegexLibrary {
40
41   public boolean regexMatch(String JavaDoc regular_expression, String JavaDoc expression_ops,
42                             String JavaDoc value) {
43     try {
44       // PENDING: Compile and cache most commonly used regular expressions...
45

46       int c_flags = 0;
47       if (expression_ops != null) {
48         if (expression_ops.indexOf('i') != -1) {
49           c_flags += RE.REG_ICASE;
50         }
51         if (expression_ops.indexOf('s') != -1) {
52           c_flags += RE.REG_DOT_NEWLINE;
53         }
54         if (expression_ops.indexOf('m') != -1) {
55           c_flags += RE.REG_MULTILINE;
56         }
57       }
58
59       RE re = new RE(regular_expression, c_flags);
60       return re.isMatch(value);
61     }
62     catch (REException e) {
63       // Incorrect syntax means we always match to false,
64
return false;
65     }
66   }
67
68   public IntegerVector regexSearch(Table table, int column,
69                            String JavaDoc regular_expression, String JavaDoc expression_ops) {
70     // Get the ordered column,
71
IntegerVector row_list = table.selectAll(column);
72     // The result matched rows,
73
IntegerVector result_list = new IntegerVector();
74
75     // Make into a new list that matches the pattern,
76
RE re;
77     try {
78       // PENDING: Compile and cache most commonly used regular expressions...
79

80       int c_flags = 0;
81       if (expression_ops != null) {
82         if (expression_ops.indexOf('i') != -1) {
83           c_flags += RE.REG_ICASE;
84         }
85         if (expression_ops.indexOf('s') != -1) {
86           c_flags += RE.REG_DOT_NEWLINE;
87         }
88         if (expression_ops.indexOf('m') != -1) {
89           c_flags += RE.REG_MULTILINE;
90         }
91       }
92
93       re = new RE(regular_expression, c_flags);
94     }
95     catch (REException e) {
96       // Incorrect syntax means we always match to an empty list,
97
return result_list;
98     }
99
100     // For each row in the column, test it against the regular expression.
101
int size = row_list.size();
102     for (int i = 0; i < size; ++i) {
103       int row_index = row_list.intAt(i);
104       TObject cell = table.getCellContents(column, row_index);
105       // Only try and match against non-null cells.
106
if (!cell.isNull()) {
107         Object JavaDoc ob = cell.getObject();
108         String JavaDoc str = ob.toString();
109         // If the column matches the regular expression then return it,
110
if (re.isMatch(str)) {
111           result_list.addInt(row_index);
112         }
113       }
114     }
115
116     return result_list;
117   }
118
119 }
120
Popular Tags