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.ListIterator;
23
24 import org.apache.jsieve.Arguments;
25 import org.apache.jsieve.NumberArgument;
26 import org.apache.jsieve.SieveContext;
27 import org.apache.jsieve.TagArgument;
28 import org.apache.jsieve.exception.SieveException;
29 import org.apache.jsieve.exception.SyntaxException;
30 import org.apache.jsieve.mail.MailAdapter;
31 import org.apache.jsieve.mail.SieveMailException;
32
33
34
35
36 public class Size extends AbstractTest {
37
38
39
40
41 public Size() {
42 super();
43 }
44
45
46
47
48
49
50
51
52
53
54
55 protected boolean executeBasic(MailAdapter mail, Arguments arguments,
56 SieveContext context) throws SyntaxException, SieveMailException {
57 String comparator = null;
58 Integer size = null;
59 ListIterator argumentsIter = arguments.getArgumentList().listIterator();
60
61
62 if (argumentsIter.hasNext()) {
63 Object argument = argumentsIter.next();
64 if (argument instanceof TagArgument) {
65 String tag = ((TagArgument) argument).getTag();
66 if (tag.equals(":under") || tag.equals(":over"))
67 comparator = tag;
68 else
69 throw context.getCoordinate().syntaxException(
70 "Found unexpected TagArgument: \"" + tag + "\"");
71 }
72 }
73 if (null == comparator)
74 throw context.getCoordinate().syntaxException("Expecting a Tag");
75
76
77 if (argumentsIter.hasNext()) {
78 Object argument = argumentsIter.next();
79 if (argument instanceof NumberArgument)
80 size = ((NumberArgument) argument).getInteger();
81 }
82 if (null == size)
83 throw context.getCoordinate().syntaxException("Expecting a Number");
84
85
86 if (argumentsIter.hasNext())
87 throw context.getCoordinate().syntaxException(
88 "Found unexpected argument(s)");
89
90 return test(mail, comparator, size.intValue());
91 }
92
93
94
95
96
97
98
99
100
101
102 protected boolean test(MailAdapter mail, String comparator, int size)
103 throws SieveMailException {
104 boolean isTestPassed = false;
105 if (comparator.equals(":over"))
106 isTestPassed = testOver(mail, size);
107 else if (comparator.equals(":under"))
108 isTestPassed = testUnder(mail, size);
109 return isTestPassed;
110 }
111
112
113
114
115
116
117
118
119
120 protected boolean testUnder(MailAdapter mail, int size)
121 throws SieveMailException {
122 return mail.getSize() < size;
123 }
124
125
126
127
128
129
130
131
132
133 protected boolean testOver(MailAdapter mail, int size)
134 throws SieveMailException {
135 return mail.getSize() > size;
136 }
137
138
139
140
141
142 protected void validateArguments(Arguments arguments, SieveContext context)
143 throws SieveException {
144
145 }
146
147 }