88 * @return A Keywords object for the specified type and text.
89 * @throws Keywords.Fault if there are errors in the arguments.
90 */
91 public static Keywords create(String type, String text) throws Fault {
92 return create(type, text, null);
93 }
94
95 /**
96 * Create a keywords object.
97 * @param type one of ALL_OF, ANY_OF, or EXPR
98 * @param text if the type is one of "all of" or "any of", text should
99 * be a white-space separated list of keywords; if type is "expr",
100 * text should be a boolean valued expression formed from
101 * keywords, '&' (and), '|' (or), '!' (not) and '(' ')' (parentheses).
102 * @param validKeywords a set of valid keywords for this test suite,
103 * or null.
104 * If not null, all the keywords in <i>text</i> must be in this set.
105 * @return A Keywords object for the specified type and text.
106 * @throws Keywords.Fault if there are errors in the arguments.
107 */
108 public static Keywords create(String type, String text, Set validKeywords) throws Fault {
109 Set lowerCaseValidKeywords = toLowerCase(validKeywords);
110 if (text == null) {
111 text = "";
112 }
113
114 Keywords result = null;
115 if (type == null || type.equals("ignore")) {
116 return null;
117 } else if (type.equals(ALL_OF)) {
118 result = new AllKeywords(StringArray.split(text), lowerCaseValidKeywords);
119 result.setSummary(result.toString());
120 return result;
121 }
122 else if (type.equals(ANY_OF)) {
123 result = new AnyKeywords(StringArray.split(text), lowerCaseValidKeywords);
124 result.setSummary(result.toString());
125 return result;
126 }
127 else if (type.equals(EXPR)) {
128 ExprParser p = new ExprParser(text, lowerCaseValidKeywords);
129 result = p.parse();
174 * @param allowNumericKeywords Value to be set.
175 */
176 public static void setAllowNumericKeywords(boolean allowNumericKeywords) {
177 ExprParser.allowNumericKeywords = allowNumericKeywords;
178 }
179
180 /**
181 * Check if this keywords object accepts, or matches, the specified
182 * set of words. If the keywords type is "any of" or "all of",
183 * the set must have any or of all of the words specified
184 * in the keywords object; if the keywords type is "expr", the
185 * given expression must evaluate to true, when the words in the
186 * expression are true if they are present in the given set of words.
187 *
188 * @param s A set of words to compare against the keywords object.
189 * @return true if the the specified set of words are compatible
190 * with this keywords object.
191 */
192 public abstract boolean accepts(Set<String> s);
193
194 private static Set toLowerCase(Set words) {
195 if (words == null)
196 return null;
197
198 boolean allLowerCase = true;
199 for (Iterator iter = words.iterator(); iter.hasNext() && allLowerCase; ) {
200 String word = (String) (iter.next());
201 allLowerCase &= word.equals(word.toLowerCase());
202 }
203
204 if (allLowerCase)
205 return words;
206
207 Set<String> s = new HashSet<>();
208 for (Iterator iter = words.iterator(); iter.hasNext(); ) {
209 String word = (String) (iter.next());
210 s.add(word.toLowerCase());
211 }
212
213 return s;
214 }
215
216 private static boolean isLowerCase(String s) {
217 for (int i = 0; i < s.length(); i++) {
218 if (Character.isUpperCase(s.charAt(i)))
219 return false;
220 }
221 return true;
222 }
223
224 static I18NResourceBundle i18n = I18NResourceBundle.getBundleForClass(Keywords.class);
225
226 }
227
228 //------------------------------------------------------------------------------
229
230
231 //------------------------------------------------------------------------------
232
233 abstract class SetKeywords extends Keywords {
234 Set<String> keys;
235 String allKwds = ""; // string to be used by toString()
236
237 SetKeywords(String[] kwds, Set validKeywords) throws Keywords.Fault {
238 if (kwds.length == 0) {
239 throw new Keywords.Fault(i18n, "kw.noKeywords");
240 }
241
242 keys = new HashSet<String>();
243 for (int i = 0; i < kwds.length; i++) {
244 String kwd = kwds[i].toLowerCase();
245 if (validKeywords != null && !validKeywords.contains(kwd)) {
246 throw new Keywords.Fault(i18n, "kw.invalidKeyword", kwds[i]);
247 }
248 keys.add(kwd);
249 allKwds += kwd + " ";
250 }
251 if (allKwds.length() > 0) {
252 // remove last " "
253 allKwds = allKwds.substring(0, allKwds.length() - 1);
254 }
255 }
256
257 @Override
261 }
262 if (getClass() != obj.getClass()) {
263 return false;
264 }
265 final SetKeywords other = (SetKeywords) obj;
266 if (this.keys != other.keys && (this.keys == null || !this.keys.equals(other.keys))) {
267 return false;
268 }
269 return true;
270 }
271
272 @Override
273 public int hashCode() {
274 int hash = 3;
275 hash = 61 * hash + (this.keys != null ? this.keys.hashCode() : 0);
276 return hash;
277 }
278
279 }
280 class AllKeywords extends SetKeywords {
281 AllKeywords(String[] keys, Set validKeywords) throws Keywords.Fault {
282 super(keys, validKeywords);
283 }
284
285
286 /**
287 * Returns true, iff all keywords are in the set.
288 * @param s
289 * @return
290 */
291 @Override
292 public boolean accepts(Set<String> s) {
293 return s.containsAll(keys);
294 }
295
296 @Override
297 public String toString() {
298 return "all of (" + allKwds + ")";
299 }
300 }
301
302
303 class AnyKeywords extends SetKeywords {
304 AnyKeywords(String[] keys, Set validKeywords) throws Keywords.Fault {
305 super(keys, validKeywords);
306 }
307
308 /**
309 * @param s - the set
310 * @return false, if none of the keywords is in the set
311 */
312 @Override
313 public boolean accepts(Set<String> s) {
314 for (String kwd :keys) {
315 if (s.contains(kwd)) {
316 return true;
317 }
318 }
319 return false;
320 }
321
322 @Override
323 public String toString() {
324 return "any of (" + allKwds + ")";
325 }
326 }
327
328 //------------------------------------------------------------------------------
329
330 class ExprParser {
331 ExprParser(String text, Set validKeywords) {
332 this.text = text;
333 this.validKeywords = validKeywords;
334 nextToken();
335 }
336
337 ExprKeywords parse() throws Keywords.Fault {
338 if (text == null || text.trim().length() == 0)
339 throw new Keywords.Fault(i18n, "kw.noExpr");
340
341 ExprKeywords e = parseExpr();
342 expect(END);
343 return e;
344 }
345
346 ExprKeywords parseExpr() throws Keywords.Fault {
347 for (ExprKeywords e = parseTerm() ; e != null ; e = e.order()) {
348 switch (token) {
349 case AND:
350 nextToken();
351 e = new AndExprKeywords(e, parseTerm());
420 && Character.isUnicodeIdentifierPart(text.charAt(index))) {
421 char ch = text.charAt(index++);
422 if (!Character.isIdentifierIgnorable(ch))
423 idValue += Character.toLowerCase(ch);
424 }
425 token = ID;
426 return;
427 }
428 else {
429 token = ERROR;
430 return;
431 }
432 }
433 }
434 token = END;
435 }
436
437 protected static boolean allowNumericKeywords =
438 Boolean.getBoolean("javatest.allowNumericKeywords");
439 private String text;
440 private Set validKeywords;
441 private int index;
442 private int token;
443 private String idValue;
444 private static final int
445 ID = 0, AND = 1, OR = 2, NOT = 3, LPAREN = 4, RPAREN = 5, END = 6, ERROR = 7;
446
447 private static I18NResourceBundle i18n = Keywords.i18n;
448 }
449
450 //------------------------------------------------------------------------------
451
452 abstract class ExprKeywords extends Keywords {
453
454 abstract int precedence();
455
456 ExprKeywords order() {
457 return this;
458 }
459 }
460
|
88 * @return A Keywords object for the specified type and text.
89 * @throws Keywords.Fault if there are errors in the arguments.
90 */
91 public static Keywords create(String type, String text) throws Fault {
92 return create(type, text, null);
93 }
94
95 /**
96 * Create a keywords object.
97 * @param type one of ALL_OF, ANY_OF, or EXPR
98 * @param text if the type is one of "all of" or "any of", text should
99 * be a white-space separated list of keywords; if type is "expr",
100 * text should be a boolean valued expression formed from
101 * keywords, '&' (and), '|' (or), '!' (not) and '(' ')' (parentheses).
102 * @param validKeywords a set of valid keywords for this test suite,
103 * or null.
104 * If not null, all the keywords in <i>text</i> must be in this set.
105 * @return A Keywords object for the specified type and text.
106 * @throws Keywords.Fault if there are errors in the arguments.
107 */
108 public static Keywords create(String type, String text, Set<String> validKeywords) throws Fault {
109 Set<String> lowerCaseValidKeywords = toLowerCase(validKeywords);
110 if (text == null) {
111 text = "";
112 }
113
114 Keywords result = null;
115 if (type == null || type.equals("ignore")) {
116 return null;
117 } else if (type.equals(ALL_OF)) {
118 result = new AllKeywords(StringArray.split(text), lowerCaseValidKeywords);
119 result.setSummary(result.toString());
120 return result;
121 }
122 else if (type.equals(ANY_OF)) {
123 result = new AnyKeywords(StringArray.split(text), lowerCaseValidKeywords);
124 result.setSummary(result.toString());
125 return result;
126 }
127 else if (type.equals(EXPR)) {
128 ExprParser p = new ExprParser(text, lowerCaseValidKeywords);
129 result = p.parse();
174 * @param allowNumericKeywords Value to be set.
175 */
176 public static void setAllowNumericKeywords(boolean allowNumericKeywords) {
177 ExprParser.allowNumericKeywords = allowNumericKeywords;
178 }
179
180 /**
181 * Check if this keywords object accepts, or matches, the specified
182 * set of words. If the keywords type is "any of" or "all of",
183 * the set must have any or of all of the words specified
184 * in the keywords object; if the keywords type is "expr", the
185 * given expression must evaluate to true, when the words in the
186 * expression are true if they are present in the given set of words.
187 *
188 * @param s A set of words to compare against the keywords object.
189 * @return true if the the specified set of words are compatible
190 * with this keywords object.
191 */
192 public abstract boolean accepts(Set<String> s);
193
194 private static Set<String> toLowerCase(Set<String> words) {
195 if (words == null)
196 return null;
197
198 boolean allLowerCase = true;
199 for (Iterator<String> iter = words.iterator(); iter.hasNext() && allLowerCase; ) {
200 String word = iter.next();
201 allLowerCase &= word.equals(word.toLowerCase());
202 }
203
204 if (allLowerCase)
205 return words;
206
207 Set<String> s = new HashSet<>();
208 for (Iterator<String> iter = words.iterator(); iter.hasNext(); ) {
209 String word = iter.next();
210 s.add(word.toLowerCase());
211 }
212
213 return s;
214 }
215
216 private static boolean isLowerCase(String s) {
217 for (int i = 0; i < s.length(); i++) {
218 if (Character.isUpperCase(s.charAt(i)))
219 return false;
220 }
221 return true;
222 }
223
224 static I18NResourceBundle i18n = I18NResourceBundle.getBundleForClass(Keywords.class);
225
226 }
227
228 //------------------------------------------------------------------------------
229
230
231 //------------------------------------------------------------------------------
232
233 abstract class SetKeywords extends Keywords {
234 Set<String> keys;
235 String allKwds = ""; // string to be used by toString()
236
237 SetKeywords(String[] kwds, Set<String> validKeywords) throws Keywords.Fault {
238 if (kwds.length == 0) {
239 throw new Keywords.Fault(i18n, "kw.noKeywords");
240 }
241
242 keys = new HashSet<String>();
243 for (int i = 0; i < kwds.length; i++) {
244 String kwd = kwds[i].toLowerCase();
245 if (validKeywords != null && !validKeywords.contains(kwd)) {
246 throw new Keywords.Fault(i18n, "kw.invalidKeyword", kwds[i]);
247 }
248 keys.add(kwd);
249 allKwds += kwd + " ";
250 }
251 if (allKwds.length() > 0) {
252 // remove last " "
253 allKwds = allKwds.substring(0, allKwds.length() - 1);
254 }
255 }
256
257 @Override
261 }
262 if (getClass() != obj.getClass()) {
263 return false;
264 }
265 final SetKeywords other = (SetKeywords) obj;
266 if (this.keys != other.keys && (this.keys == null || !this.keys.equals(other.keys))) {
267 return false;
268 }
269 return true;
270 }
271
272 @Override
273 public int hashCode() {
274 int hash = 3;
275 hash = 61 * hash + (this.keys != null ? this.keys.hashCode() : 0);
276 return hash;
277 }
278
279 }
280 class AllKeywords extends SetKeywords {
281 AllKeywords(String[] keys, Set<String> validKeywords) throws Keywords.Fault {
282 super(keys, validKeywords);
283 }
284
285
286 /**
287 * Returns true, iff all keywords are in the set.
288 * @param s
289 * @return
290 */
291 @Override
292 public boolean accepts(Set<String> s) {
293 return s.containsAll(keys);
294 }
295
296 @Override
297 public String toString() {
298 return "all of (" + allKwds + ")";
299 }
300 }
301
302
303 class AnyKeywords extends SetKeywords {
304 AnyKeywords(String[] keys, Set<String> validKeywords) throws Keywords.Fault {
305 super(keys, validKeywords);
306 }
307
308 /**
309 * @param s - the set
310 * @return false, if none of the keywords is in the set
311 */
312 @Override
313 public boolean accepts(Set<String> s) {
314 for (String kwd :keys) {
315 if (s.contains(kwd)) {
316 return true;
317 }
318 }
319 return false;
320 }
321
322 @Override
323 public String toString() {
324 return "any of (" + allKwds + ")";
325 }
326 }
327
328 //------------------------------------------------------------------------------
329
330 class ExprParser {
331 ExprParser(String text, Set<String> validKeywords) {
332 this.text = text;
333 this.validKeywords = validKeywords;
334 nextToken();
335 }
336
337 ExprKeywords parse() throws Keywords.Fault {
338 if (text == null || text.trim().length() == 0)
339 throw new Keywords.Fault(i18n, "kw.noExpr");
340
341 ExprKeywords e = parseExpr();
342 expect(END);
343 return e;
344 }
345
346 ExprKeywords parseExpr() throws Keywords.Fault {
347 for (ExprKeywords e = parseTerm() ; e != null ; e = e.order()) {
348 switch (token) {
349 case AND:
350 nextToken();
351 e = new AndExprKeywords(e, parseTerm());
420 && Character.isUnicodeIdentifierPart(text.charAt(index))) {
421 char ch = text.charAt(index++);
422 if (!Character.isIdentifierIgnorable(ch))
423 idValue += Character.toLowerCase(ch);
424 }
425 token = ID;
426 return;
427 }
428 else {
429 token = ERROR;
430 return;
431 }
432 }
433 }
434 token = END;
435 }
436
437 protected static boolean allowNumericKeywords =
438 Boolean.getBoolean("javatest.allowNumericKeywords");
439 private String text;
440 private Set<String> validKeywords;
441 private int index;
442 private int token;
443 private String idValue;
444 private static final int
445 ID = 0, AND = 1, OR = 2, NOT = 3, LPAREN = 4, RPAREN = 5, END = 6, ERROR = 7;
446
447 private static I18NResourceBundle i18n = Keywords.i18n;
448 }
449
450 //------------------------------------------------------------------------------
451
452 abstract class ExprKeywords extends Keywords {
453
454 abstract int precedence();
455
456 ExprKeywords order() {
457 return this;
458 }
459 }
460
|