KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * com.mckoi.database.regexbridge.JavaRegex 06 Mar 2002
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.Table;
28 import com.mckoi.database.TObject;
29 import com.mckoi.util.IntegerVector;
30 import java.util.regex.*;
31
32 /**
33  * A bridge to the internal Java regular expression library that was introduced
34  * in Java 1.4. This bridge will only work if the regular expression API
35  * is available in the class library. It is not available in 1.3 and 1.2.
36  *
37  * @author Tobias Downer
38  */

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

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

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