Source code: org/pqt/autorib/instr/InstrForNames.java
1 //AutoRIB
2 // Copyright © 1998 - 2002, P W Quint
3 //
4 // Contact: autorib00@aol.com
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public
8 // License as published by the Free Software Foundation; either
9 // version 2 of the License, or (at your option) any later version.
10 //
11 // This library 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 GNU
14 // General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20 package org.pqt.autorib.instr;
21 import java.io.*;
22 import java.util.*;
23
24 import org.pqt.autorib.tokenizer.*;
25 import org.pqt.autorib.globals.*;
26 import org.pqt.autorib.util.*;
27 import org.pqt.autorib.rib.*;
28
29 /** A class for handling a name based filter of the form
30 *ForNames name name [name name] where names can contain
31 *wild cards * and ?. The filter is passed if the name matches
32 *on of the names given, and does not match one of the names
33 *given in square brackets.*/
34 public class InstrForNames extends InstrForRequest {
35 Vector names = new Vector(5,5);
36 Vector excludeNames = new Vector(5, 5);
37
38 public InstrForNames()
39 { }
40
41 public InstrForNames(InstrWReader in) throws IOException, FormatException {
42 read(in);
43 }
44
45 public void readParams(InstrWReader in) throws IOException,
46 FormatException {
47 int t;
48 Object o;
49 while (true) {
50 t = in.tokenizer.getToken();
51 if (t == Token.STRING)
52 names.addElement(in.tokenizer.token.stringVal);
53 else if (t == Token.ARRAY) {
54 Iterator i = in.tokenizer.token.arrayVal.iterator();
55 while (i.hasNext()) {
56 o = i.next();
57 if (o instanceof String)
58 excludeNames.add(o);
59 else throw new
60 FormatException("String expected", in.tokenizer);
61 }
62 }
63 else if (t == Token.OPENBRACE)
64 break;
65 else
66 throw new FormatException("{ expected",in.tokenizer);
67 }
68 }
69
70 public boolean filter(RIBObjectGroup object) {
71 Iterator i = excludeNames.iterator();
72 boolean fail = false;
73 //first check the excluded names
74 while (i.hasNext() && !fail)
75 fail = WildString.wildCompare((String)i.next(), object.attributes.name);
76 if (fail) return false;
77 i = names.iterator();
78 boolean pass = false;
79 while ((i.hasNext()) && (!pass))
80 pass = WildString.wildCompare((String)i.next(), object.attributes.name);
81 return pass;
82 }
83
84 public void writeParams(Writer out) throws IOException {
85 Enumeration i = names.elements();
86 while (i.hasMoreElements())
87 out.write('"' + (String)i.nextElement() + "\" ");
88 }
89
90
91 }//class
92
93