View Javadoc

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;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.jsieve.exception.SieveException;
24  import org.apache.jsieve.mail.MailAdapter;
25  
26  /**
27   * <p>
28   * A parsed representation of the RFC3028 BNF...
29   * </p>
30   * 
31   * <code>command = identifier arguments ( ";" / block )</code>
32   */
33  public class Command implements Executable {
34  
35      /** The name of this Command */
36      private String fieldName;
37  
38      /** The Arguments for this Command */
39      private Arguments fieldArguments;
40  
41      /** The Block for this Command */
42      private Block fieldBlock;
43  
44      /**
45       * Script coordinate for this command. Commands are executed after the
46       * document has been parse. So this must be recorded on construction and
47       * stored for later use.
48       */
49      private ScriptCoordinate coordinate;
50  
51      /**
52       * Constructor for Test.
53       */
54      private Command() {
55          super();
56      }
57  
58      /**
59       * Constructor for Command.
60       * 
61       * @param name
62       * @param arguments
63       * @param block
64       */
65      public Command(String name, Arguments arguments, Block block,
66              ScriptCoordinate coordinate) {
67          this();
68          this.coordinate = coordinate;
69          setName(name);
70          setArguments(arguments);
71          setBlock(block);
72      }
73  
74      /**
75       * Returns the name.
76       * 
77       * @return String
78       */
79      public String getName() {
80          return fieldName;
81      }
82  
83      /**
84       * Sets the name.
85       * 
86       * @param name
87       *            The name to set
88       */
89      protected void setName(String name) {
90          fieldName = name;
91      }
92  
93      /**
94       * @see java.lang.Object#toString()
95       */
96      public String toString() {
97          return "Command name: "
98                  + getName()
99                  + " "
100                 + ((getArguments() == null) ? "null" : getArguments()
101                         .toString()) + " Block: "
102                 + ((getBlock() == null) ? "null" : getBlock().toString());
103     }
104 
105     /**
106      * Returns the arguments.
107      * 
108      * @return Arguments
109      */
110     public Arguments getArguments() {
111         return fieldArguments;
112     }
113 
114     /**
115      * Returns the block.
116      * 
117      * @return Block
118      */
119     public Block getBlock() {
120         return fieldBlock;
121     }
122 
123     /**
124      * Sets the arguments.
125      * 
126      * @param arguments
127      *            The arguments to set
128      */
129     protected void setArguments(Arguments arguments) {
130         fieldArguments = arguments;
131     }
132 
133     /**
134      * Sets the block.
135      * 
136      * @param block
137      *            The block to set
138      */
139     protected void setBlock(Block block) {
140         fieldBlock = block;
141     }
142 
143     /**
144      * @see org.apache.jsieve.Executable#execute(MailAdapter, SieveContext)
145      */
146     public Object execute(MailAdapter mail, SieveContext context)
147             throws SieveException {
148         Log log = context.getLog();
149         if (log.isDebugEnabled()) {
150             log.debug(toString());
151             coordinate.debugDiagnostics(log);
152         }
153         // commands are executed after the parsing phase
154         // recursively from the top level block
155         // so need to use the coordinate recorded from the parse
156         context.setCoordinate(coordinate);
157         final ExecutableCommand executable = context.getCommandManager().getCommand(getName());
158         final Object result = executable.execute(mail, getArguments(),
159                 getBlock(), context);
160         return result;
161     }
162 }