001: /**
002: * $Id: ABSearchTerm.java,v 1.6 2003/03/28 19:32:18 dpolla Exp $
003: * Copyright 2002 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and iPlanet
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.addressbook;
014:
015: import java.util.Vector;
016: import java.util.Properties;
017: import java.util.Enumeration;
018:
019: /**
020: * Address Book Filter Search Term represents a search term like "ln=doe".
021: * The search terms can be nested in a tree structure and combined with
022: * an 'and' or an 'or' e.g. "ln=doe & (fn=john | fn=joe)".
023: *
024: */
025: public abstract class ABSearchTerm {
026:
027: /**
028: * Values for operators
029: */
030: public static final int NO_OP = -1;
031:
032: public static final int NOT = 0;
033: public static final int OR = 1;
034: public static final int AND = 2;
035:
036: /**
037: * Name of the attribute to search on.
038: */
039: protected String name = null;
040:
041: /**
042: * Value of the attribute to search.
043: */
044: protected String value = null;
045:
046: /**
047: * Default search is carried out as an exact search. Can be set to
048: * 'contains' search like '*str*' in ldap by setting exact to false.
049: */
050: protected boolean exact = true;
051:
052: /**
053: * Boolean indicating the relationship between name and value
054: */
055: protected boolean not = false;
056:
057: /**
058: * ABSearchTerm array which stores the recursive ABSearchTerms.
059: */
060: protected ABSearchTerm[] terms = null;
061:
062: /**
063: * Operator binding the recursive searchTerms in this instance of ABSearchTerm.
064: */
065: protected int op = NO_OP;
066:
067: /**
068: * Initializes the search term object. This constructor indicates this search term
069: * is an end node and does not contain recursive search terms.
070: *
071: * @param name Name of the attribute to search on.
072: * @param value Value of the attribute to search.
073: * @param exact Boolean indicating whether search is 'exact'(true) or
074: * 'contains'(false).
075: */
076: public ABSearchTerm(String name, String value, boolean exact) {
077: this .name = name;
078: this .value = value;
079: this .exact = exact;
080: this .terms = null;
081: };
082:
083: /**
084: * Initializes the search term object. This constructor is used for the unary
085: * operator NOT on the ABSearchTerm. Usage of this constructor means that ABSearchTerm
086: * is recursive and is not the end node, and it will contain terms, op and not name,
087: * value.
088: *
089: * @param The recursive ABSearchTerm.
090: * @param Operator binding the recursive searchTerms. The value can only be NOT.
091: */
092: public ABSearchTerm(ABSearchTerm term, int op)
093: throws ABStoreException {
094: if (op != NOT) {
095: String msg = "ABSearchTerm: Invalid operator - " + op
096: + ". Operator can only be NOT";
097: throw new ABStoreException(msg);
098: }
099: ABSearchTerm[] terms = new ABSearchTerm[1];
100: terms[0] = term;
101: this .terms = terms;
102: this .op = op;
103: };
104:
105: /**
106: * Initializes the search term object. This constructor is used for the binary
107: * operators like AND and OR on the ABSearchTerms array. Usage of this constructor
108: * means that ABSearchTerm is recursive and is not the end node, and it will
109: * contain terms, op and not name, value.
110: *
111: * @param The recursive ABSearchTerm array.
112: * @param Operator binding the recursive searchTerms. The values can be AND or OR.
113: */
114: public ABSearchTerm(ABSearchTerm[] terms, int op)
115: throws ABStoreException {
116: if ((op != AND) && (op != OR)) {
117: String msg = "ABSearchTerm: Invalid operator - " + op
118: + ". Operator can only be AND/OR";
119: throw new ABStoreException(msg);
120: }
121: if ((terms.length < 2)) {
122: String msg = "ABSearchTerm: Number of search Terms in array to be AND/ORed wrong - "
123: + terms.length;
124: throw new ABStoreException(msg);
125: }
126: this .terms = terms;
127: this .op = op;
128: };
129:
130: /**
131: * Get the name of the attribute to search on.
132: *
133: * @return name Name of the attribute to search on.
134: */
135: public String getName() {
136: return name;
137: };
138:
139: /**
140: * Get the value of the attribute to search.
141: *
142: * @return value Value of the attribute to search.
143: */
144: public String getValue() {
145: return value;
146: };
147:
148: /**
149: * Get the ABSearchTerm array which stores the recursive ABSearchTerms.
150: * Present only in case the ABSearchTerm is not an end node.
151: *
152: * @return terms
153: */
154: public ABSearchTerm[] getTerms() {
155: return terms;
156: };
157:
158: /**
159: * Get the value of the operator binding the recursive searchTerms
160: * Present only in case the ABSearchTerm is not an end node.
161: *
162: * @return op - int
163: */
164: public int getOp() {
165: return op;
166: };
167:
168: /**
169: * Return the value of the exact boolean indicating whether
170: * the search is exact or approximate.
171: *
172: * @return exact Boolean indicating whether search is exact
173: * or contains.
174: */
175: public boolean isExact() {
176: return exact;
177: };
178:
179: /**
180: * Set the name of the attribute to search on.
181: *
182: * @param name Name of the attribute to search on.
183: */
184: public void setName(String name) {
185: this .name = name;
186: };
187:
188: /**
189: * Set the value of the attribute to search.
190: *
191: * @param value Value of the attribute to search.
192: */
193: public void setValue(String value) {
194: this .value = value;
195: };
196:
197: /**
198: * Set the value of the exact boolean indicating whether
199: * the search is exact or approximate.
200: *
201: * @param exact Boolean indicating whether search is exact
202: * or contains.
203: */
204: public void setExact(boolean exact) {
205: this .exact = exact;
206: };
207:
208: /**
209: * Set the value of the not boolean indicating the relationship between name and value
210: *
211: * @param not Boolean indicating the relationship between name and value
212: */
213: public void setNot(boolean not) {
214: this .not = not;
215: };
216:
217: /**
218: * Set the value of the operator binding the recursive searchTerms
219: * Present only in case the ABSearchTerm is not an end node.
220: *
221: * @param op - int
222: */
223: public void setOp(int op) {
224: this .op = op;
225: };
226:
227: /**
228: * Abstract method. The adapters compute the value of the search term following
229: * the recursive search terms to the end node.
230: *
231: * @return search filter object to base the search on.
232: */
233: public abstract Object compute() throws ABStoreException;
234:
235: }
|