1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.jsieve.commands;
21
22 import static org.apache.jsieve.Constants.COMPARATOR_PREFIX;
23 import static org.apache.jsieve.Constants.COMPARATOR_PREFIX_LENGTH;
24
25 import java.util.List;
26
27 import org.apache.jsieve.Argument;
28 import org.apache.jsieve.Arguments;
29 import org.apache.jsieve.Block;
30 import org.apache.jsieve.SieveContext;
31 import org.apache.jsieve.StringListArgument;
32 import org.apache.jsieve.exception.FeatureException;
33 import org.apache.jsieve.exception.LookupException;
34 import org.apache.jsieve.exception.SieveException;
35 import org.apache.jsieve.mail.MailAdapter;
36
37
38
39
40
41 public class Require extends AbstractPrologCommand {
42
43
44
45
46 public Require() {
47 super();
48 }
49
50
51
52
53
54
55
56
57
58
59
60
61 protected Object executeBasic(MailAdapter mail, Arguments arguments,
62 Block block, SieveContext context) throws SieveException {
63 final List<String> stringArgumentList = ((StringListArgument) arguments
64 .getArgumentList().get(0)).getList();
65 for (String stringArgument: stringArgumentList) {
66 validateFeature(stringArgument, mail, context);
67 }
68 return null;
69 }
70
71
72
73
74
75
76
77
78
79
80 protected void validateFeature(String name, MailAdapter mail,
81 SieveContext context) throws FeatureException {
82 if (name.startsWith(COMPARATOR_PREFIX)) {
83 final String comparatorName = name.substring(COMPARATOR_PREFIX_LENGTH);
84 if (!context.getComparatorManager().isSupported(comparatorName)) {
85 throw new FeatureException("Comparator \"" + comparatorName
86 + "\" is not supported.");
87 }
88 } else {
89
90 try {
91 validateCommand(name, context);
92 return;
93 } catch (LookupException e) {
94
95 }
96
97
98 try {
99 validateTest(name, context);
100 } catch (LookupException e) {
101 throw new FeatureException("Feature \"" + name
102 + "\" is not supported.");
103 }
104 }
105 }
106
107
108
109
110
111
112
113 protected void validateCommand(String name, SieveContext context)
114 throws LookupException {
115 context.getCommandManager().getCommand(name);
116 }
117
118
119
120
121
122
123
124
125 protected void validateTest(String name, SieveContext context)
126 throws LookupException {
127 context.getTestManager().getTest(name);
128 }
129
130
131
132
133
134 protected void validateArguments(Arguments arguments, SieveContext context)
135 throws SieveException {
136 List<Argument> args = arguments.getArgumentList();
137 if (args.size() != 1)
138 throw context.getCoordinate().syntaxException(
139 new StringBuilder("Exactly 1 argument permitted. Found ").append((int)args.size()));
140
141 Argument argument = args.get(0);
142 if (!(argument instanceof StringListArgument))
143 throw context.getCoordinate().syntaxException(
144 "Expecting a string-list");
145 }
146
147 }