1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.jsieve.util;
20
21 import java.io.IOException;
22
23
24
25
26
27
28
29
30
31
32 public class SieveToXml {
33
34 public static final String DEFAULT_NAME_ATTRIBUTE = "name";
35
36 public static final String DEFAULT_NAME_ACTION_COMMAND = "action";
37
38 public static final String DEFAULT_NAME_CONTROL_COMMAND = "control";
39
40 public static final String DEFAULT_NAME_TEST = "test";
41
42 public static final String DEFAULT_NAME_LIST = "list";
43
44 public static final String DEFAULT_NAME_NUM = "num";
45
46 public static final String DEFAULT_NAME_TAG = "tag";
47
48 public static final String DEFAULT_NAME_STRING = "str";
49
50 public static final String DEFAULT_PREFIX = "sieve";
51
52 public static final String DEFAULT_NAMESPACE = "urn:ietf:params:xml:ns:sieve";
53
54
55 public static final String[] CONTROL_COMMANDS = {"If", "Require", "Stop"};
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 public interface Out {
80
81
82
83
84
85 public void openElement(CharSequence localName, CharSequence uri, CharSequence prefix) throws IOException;
86
87
88
89
90
91
92 public void attribute(CharSequence localName, CharSequence uri, CharSequence prefix, CharSequence value) throws IOException;
93
94
95
96
97
98
99
100
101
102 public void content(CharSequence text) throws IOException;
103
104
105
106
107
108 public void closeElement() throws IOException;
109 }
110
111
112
113
114 public interface NameMapper {
115
116
117
118
119
120
121 public String toElementName(String name);
122
123 }
124
125
126
127
128
129
130
131 public static final NameMapper uniformMapper(final String elementLocalName) {
132 return new NameMapper() {
133 public String toElementName(String name) {
134 return elementLocalName;
135 }
136 };
137 }
138
139
140
141
142
143
144
145
146
147 public static final NameMapper sieveInXmlMapper() {
148 return new NameMapper() {
149
150 public String toElementName(String name) {
151 boolean isControlCommand = false;
152 for (int i=0;i< CONTROL_COMMANDS.length;i++) {
153 if (CONTROL_COMMANDS[i].equalsIgnoreCase(name)) {
154 isControlCommand = true;
155 break;
156 }
157 }
158 final String result;
159 if (isControlCommand) {
160 result = DEFAULT_NAME_CONTROL_COMMAND;
161 } else {
162 result = DEFAULT_NAME_ACTION_COMMAND;
163 }
164 return result;
165 }
166 };
167 }
168
169 private String namespaceUri = DEFAULT_NAMESPACE;
170 private String namespacePrefix = DEFAULT_PREFIX;
171 private String stringElementName = DEFAULT_NAME_STRING;
172 private String tagElementName = DEFAULT_NAME_TAG;
173 private String numberElementName = DEFAULT_NAME_NUM;
174 private String listElementName = DEFAULT_NAME_LIST;
175
176 private String nameAttributeName = DEFAULT_NAME_ATTRIBUTE;
177 private NameMapper commandNameMapper = sieveInXmlMapper();
178 private NameMapper testNameMapper = uniformMapper(DEFAULT_NAME_TEST);
179
180
181
182
183
184 public NameMapper getCommandNameMapper() {
185 return commandNameMapper;
186 }
187
188
189
190
191
192 public void setCommandNameMapper(NameMapper commandNameMapper) {
193 this.commandNameMapper = commandNameMapper;
194 }
195
196
197
198
199
200 public String getListElementName() {
201 return listElementName;
202 }
203
204
205
206
207
208 public void setListElementName(String listElementName) {
209 this.listElementName = listElementName;
210 }
211
212
213
214
215
216 public String getNameAttributeName() {
217 return nameAttributeName;
218 }
219
220
221
222
223
224
225 public void setNameAttributeName(String nameAttributeName) {
226 this.nameAttributeName = nameAttributeName;
227 }
228
229
230
231
232
233 public String getNamespacePrefix() {
234 return namespacePrefix;
235 }
236
237
238
239
240
241 public void setNamespacePrefix(String namespacePrefix) {
242 this.namespacePrefix = namespacePrefix;
243 }
244
245
246
247
248
249 public String getNamespaceUri() {
250 return namespaceUri;
251 }
252
253
254
255
256
257 public void setNamespaceUri(String namespaceUri) {
258 this.namespaceUri = namespaceUri;
259 }
260
261
262
263
264
265 public String getNumberElementName() {
266 return numberElementName;
267 }
268
269
270
271
272
273 public void setNumberElementName(String numberElementName) {
274 this.numberElementName = numberElementName;
275 }
276
277
278
279
280
281 public String getStringElementName() {
282 return stringElementName;
283 }
284
285
286
287
288
289 public void setStringElementName(String stringElementName) {
290 this.stringElementName = stringElementName;
291 }
292
293
294
295
296
297 public String getTagElementName() {
298 return tagElementName;
299 }
300
301
302
303
304
305 public void setTagElementName(String tagElementName) {
306 this.tagElementName = tagElementName;
307 }
308
309
310
311
312
313 public NameMapper getTestNameMapper() {
314 return testNameMapper;
315 }
316
317
318
319
320
321 public void setTestNameMapper(NameMapper testNameMapper) {
322 this.testNameMapper = testNameMapper;
323 }
324
325
326
327
328
329
330 public SieveHandler build(final Out out) {
331 final Worker worker = new Worker(nameAttributeName, namespaceUri, namespacePrefix, stringElementName,
332 tagElementName, numberElementName, listElementName, commandNameMapper, testNameMapper, out);
333 return worker;
334 }
335
336
337
338
339
340 private static final class Worker extends SieveHandler.Base {
341
342
343 private final String nameAttributeName;
344
345 private final String namespaceUri;
346 private final String namespacePrefix;
347 private final String stringElementName;
348 private final String tagElementName;
349 private final String numberElementName;
350 private final String listElementName;
351
352 private final NameMapper commandNameMapper;
353 private final NameMapper testNameMapper;
354
355 private final Out out;
356
357 public Worker(final String nameAttributeName, final String namespaceUri, final String namespacePrefix,
358 final String stringElementName, final String tagElementName, final String numberElementName,
359 final String listElementName, final NameMapper commandNameMapper, final NameMapper testNameMapper,
360 final Out out) {
361 super();
362 this.nameAttributeName = nameAttributeName;
363 this.namespaceUri = namespaceUri;
364 this.namespacePrefix = namespacePrefix;
365 this.stringElementName = stringElementName;
366 this.tagElementName = tagElementName;
367 this.numberElementName = numberElementName;
368 this.listElementName = listElementName;
369 this.commandNameMapper = commandNameMapper;
370 this.testNameMapper = testNameMapper;
371 this.out = out;
372 }
373
374
375 public SieveHandler endCommand(String commandName) throws HaltTraversalException {
376 return closeElement();
377 }
378
379 private SieveHandler closeElement() throws HaltTraversalException {
380 try {
381 out.closeElement();
382 return this;
383 } catch (IOException e) {
384 throw new HaltTraversalException(e);
385 }
386 }
387
388
389 public SieveHandler startCommand(String commandName) throws HaltTraversalException {
390 try {
391 out.openElement(commandNameMapper.toElementName(commandName), namespaceUri, namespacePrefix);
392 nameAttribute(commandName);
393 return this;
394 } catch (IOException e) {
395 throw new HaltTraversalException(e);
396 }
397 }
398
399
400 public SieveHandler listMember(String string) throws HaltTraversalException {
401 try {
402 out.openElement(stringElementName, namespaceUri, namespacePrefix);
403 out.content(string);
404 out.closeElement();
405 return this;
406 } catch (IOException e) {
407 throw new HaltTraversalException(e);
408 }
409 }
410
411
412 public SieveHandler argument(int number) throws HaltTraversalException {
413 try {
414 out.openElement(numberElementName, namespaceUri, namespacePrefix);
415 out.content(Integer.toString(number));
416 out.closeElement();
417 return this;
418 } catch (IOException e) {
419 throw new HaltTraversalException(e);
420 }
421 }
422
423
424 public SieveHandler argument(String tag) throws HaltTraversalException {
425 try {
426 out.openElement(tagElementName, namespaceUri, namespacePrefix);
427 out.content(tag);
428 out.closeElement();
429 return this;
430 } catch (IOException e) {
431 throw new HaltTraversalException(e);
432 }
433 }
434
435
436 public SieveHandler endTest(String testName) throws HaltTraversalException {
437 return closeElement();
438 }
439
440
441 public SieveHandler startTest(String testName) throws HaltTraversalException {
442 try {
443 out.openElement(testNameMapper.toElementName(testName), namespaceUri, namespacePrefix);
444 nameAttribute(testName);
445 return this;
446 } catch (IOException e) {
447 throw new HaltTraversalException(e);
448 }
449 }
450
451
452 public SieveHandler endTestList() throws HaltTraversalException {
453 return closeElement();
454 }
455
456
457 public SieveHandler startTestList() throws HaltTraversalException {
458 try {
459 out.openElement(listElementName, namespaceUri, namespacePrefix);
460 return this;
461 } catch (IOException e) {
462 throw new HaltTraversalException(e);
463 }
464 }
465
466 private void nameAttribute(String name) throws IOException {
467 if (nameAttributeName != null) {
468 out.attribute(nameAttributeName, namespaceUri, namespacePrefix, name);
469 }
470 }
471 }
472 }