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