01: /*
02: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
03: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
04: */
05:
06: package com.sun.portal.search.robot;
07:
08: public class StringFilter {
09: int op;
10: public String filter;
11: boolean doCase = false;
12: public String directive;
13: public int method;
14:
15: public StringFilter(String dir, int method, String text) {
16: directive = dir;
17: this .method = method;
18: filter = text;
19:
20: }
21:
22: /* public StringFilter(int method, String text) {
23: op = method;
24: filter = text;
25: }
26: */
27: String escapeString(String s) {
28: char[] line = s.toCharArray();
29: StringBuffer outStr = new StringBuffer();
30:
31: for (int i = 0; i < line.length; i++) {
32: if (line[i] == '\\' || line[i] == '"') {
33: outStr.append('\\');
34: outStr.append(line[i]);
35: } else {
36: outStr.append(line[i]);
37: }
38: }
39: return outStr.toString();
40: }
41:
42: public String toXMLString() {
43: return "<Filter src=\"" + directive + "\" " + method + "=\""
44: + filter + "\"/>\n";
45: }
46:
47: public String toConfigString() {
48: return "Filter src=" + directive + " "
49: + FilterRule.getMethodText(method) + "=\""
50: + escapeString(filter) + "\"\n";
51: }
52:
53: boolean do_prefix(String s) {
54: if (doCase)
55: return s.startsWith(filter);
56: else
57: return s.toLowerCase().startsWith(filter.toLowerCase());
58: }
59:
60: boolean do_suffix(String s) {
61: if (doCase)
62: return s.endsWith(filter);
63: else
64: return s.toLowerCase().endsWith(filter.toLowerCase());
65: }
66:
67: boolean do_exact(String s) {
68: if (doCase)
69: return s.equals(filter);
70: else
71: return s.equalsIgnoreCase(filter);
72: }
73:
74: boolean do_substr(String s) {
75: if (doCase)
76: return (s.indexOf(filter) > 0);
77: else
78: return (s.toLowerCase().indexOf(filter.toLowerCase()) > 0);
79: }
80:
81: public boolean isMatch(String s) {
82: switch (op) {
83: case 0:
84: return do_exact(s);
85: case 1:
86: return do_prefix(s);
87: case 2:
88: return do_suffix(s);
89: case 3:
90: return do_substr(s);
91: //case 4: return do_regex(s);
92: }
93: return false;
94: }
95:
96: }
|