1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.jsieve.tests;
21
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.ListIterator;
25
26 import org.apache.jsieve.Arguments;
27 import org.apache.jsieve.SieveContext;
28 import org.apache.jsieve.StringListArgument;
29 import org.apache.jsieve.TagArgument;
30 import org.apache.jsieve.comparators.ComparatorNames;
31 import org.apache.jsieve.comparators.MatchTypeTags;
32 import org.apache.jsieve.exception.SieveException;
33 import org.apache.jsieve.exception.SyntaxException;
34 import org.apache.jsieve.mail.MailAdapter;
35 import org.apache.jsieve.mail.SieveMailException;
36
37 public abstract class AbstractCompatatorTest extends AbstractTest implements
38 AddressPartTags, ComparatorTags, MatchTypeTags, ComparatorNames {
39
40 public AbstractCompatatorTest() {
41 super();
42 }
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 protected boolean executeBasic(MailAdapter mail, Arguments arguments,
62 SieveContext context) throws SieveException {
63 String addressPart = null;
64 String comparator = null;
65 String matchType = null;
66 List headerNames = null;
67 List keys = null;
68
69 ListIterator argumentsIter = arguments.getArgumentList().listIterator();
70 boolean stop = false;
71
72
73 while (!stop && argumentsIter.hasNext()) {
74 Object argument = argumentsIter.next();
75 if (argument instanceof TagArgument) {
76 String tag = ((TagArgument) argument).getTag();
77
78
79 if (null == addressPart
80 && (tag.equals(LOCALPART_TAG) || tag.equals(DOMAIN_TAG) || tag
81 .equals(ALL_TAG)))
82 addressPart = tag;
83
84 else if (null == comparator && tag.equals(COMPARATOR_TAG)) {
85
86 if (argumentsIter.hasNext()) {
87 argument = argumentsIter.next();
88 if (argument instanceof StringListArgument) {
89 List stringList = ((StringListArgument) argument)
90 .getList();
91 if (stringList.size() != 1)
92 throw new SyntaxException(
93 "Expecting exactly one String");
94 comparator = (String) stringList.get(0);
95 } else
96 throw new SyntaxException("Expecting a StringList");
97 }
98 }
99
100 else if (null == matchType
101 && (tag.equals(IS_TAG) || tag.equals(CONTAINS_TAG) || tag
102 .equals(MATCHES_TAG)))
103 matchType = tag;
104 else
105 throw context.getCoordinate().syntaxException(
106 "Found unexpected TagArgument");
107 } else {
108
109 argumentsIter.previous();
110 stop = true;
111 }
112 }
113
114
115 if (argumentsIter.hasNext()) {
116 Object argument = argumentsIter.next();
117 if (argument instanceof StringListArgument)
118 headerNames = ((StringListArgument) argument).getList();
119 }
120 if (null == headerNames)
121 throw context.getCoordinate().syntaxException(
122 "Expecting a StringList of header names");
123
124
125 if (argumentsIter.hasNext()) {
126 Object argument = argumentsIter.next();
127 if (argument instanceof StringListArgument)
128 keys = ((StringListArgument) argument).getList();
129 } else if (null == keys)
130 throw context.getCoordinate().syntaxException(
131 "Expecting a StringList of keys");
132
133 if (argumentsIter.hasNext())
134 throw context.getCoordinate().syntaxException(
135 "Found unexpected arguments");
136
137 return match(mail, (addressPart == null ? ALL_TAG : addressPart),
138 (comparator == null ? ASCII_CASEMAP_COMPARATOR : comparator),
139 (matchType == null ? IS_TAG : matchType), headerNames, keys,
140 context);
141 }
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157 protected boolean match(MailAdapter mail, String addressPart,
158 String comparator, String matchType, List headerNames, List keys,
159 SieveContext context) throws SieveException {
160
161 boolean isMatched = false;
162 Iterator headerNamesIter = headerNames.iterator();
163 while (!isMatched && headerNamesIter.hasNext()) {
164 isMatched = match(mail, addressPart, comparator, matchType,
165 (String) headerNamesIter.next(), keys, context);
166 }
167 return isMatched;
168 }
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184 protected boolean match(MailAdapter mail, String addressPart,
185 String comparator, String matchType, String headerName, List keys,
186 SieveContext context) throws SieveException {
187
188 boolean isMatched = false;
189 Iterator keysIter = keys.iterator();
190 while (!isMatched && keysIter.hasNext()) {
191 isMatched = match(mail, addressPart, comparator, matchType,
192 headerName, (String) keysIter.next(), context);
193 }
194 return isMatched;
195 }
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 protected abstract boolean match(MailAdapter mail, String addressPart,
212 String comparator, String matchType, String headerName, String key,
213 SieveContext context) throws SieveException;
214
215
216
217
218
219 protected void validateArguments(Arguments arguments, SieveContext context)
220 throws SieveException {
221 if (arguments.hasTests())
222 throw context.getCoordinate().syntaxException(
223 "Found unexpected tests");
224 }
225
226 }