1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20 package org.apache.jsieve.commands;
21
22 import org.apache.jsieve.Block;
23 import org.apache.jsieve.ConditionManager;
24 import org.apache.jsieve.SieveContext;
25 import org.apache.jsieve.exception.SieveException;
26 import org.apache.jsieve.mail.MailAdapter;
27
28 /**
29 * Abstract class AbstractConditionalCommand defines a framework of common
30 * behavior for conditional Commands (if, elsif, else). Conditional commands use
31 * a ConditionManager to relate and validate Commands within their Blocks.
32 */
33 public abstract class AbstractConditionalCommand extends AbstractControlCommand {
34
35 /**
36 * Constructor for AbstractConditionalCommand.
37 */
38 public AbstractConditionalCommand() {
39 super();
40 }
41
42 /**
43 * Method execute executes a Block within the context of a new
44 * ConditionManager.
45 *
46 * @param mail not null
47 * @param block not null
48 * @param context not null
49 * @return Object
50 * @throws SieveException
51 */
52 protected Object execute(MailAdapter mail, Block block, SieveContext context)
53 throws SieveException {
54 // Switch to a new ConditionManager
55 ConditionManager oldManager = context.getConditionManager();
56 context.setConditionManager(new ConditionManager());
57
58 try {
59 // Execute the Block
60 Object result = block.execute(mail, context);
61 return result;
62 } finally {
63 // Always restore the old ConditionManager
64 context.setConditionManager(oldManager);
65 }
66 }
67
68 /**
69 * @see org.apache.jsieve.commands.AbstractCommand#validateBlock(Block,
70 * SieveContext)
71 */
72 protected void validateBlock(Block block, SieveContext context)
73 throws SieveException {
74 if (null == block)
75 throw context.getCoordinate().syntaxException("Expecting a Block.");
76 }
77
78 }