View Javadoc

1   package org.apache.velocity.runtime.directive;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import org.apache.velocity.Template;
23  import org.apache.velocity.runtime.RuntimeInstance;
24  import org.apache.velocity.runtime.directive.Evaluate;
25  
26  /**
27   * Stop command for directive Control objects.  In an ideal JDK,
28   * this would be able to extend a RuntimeThrowable class, but we
29   * don't have that. So to avoid the interface changes needed by
30   * extending Throwable and the potential errant catches were we
31   * to extend RuntimeException, we'll have to extend Error,
32   * despite the fact that this is never an error.
33   *
34   * @author Nathan Bubna
35   * @version $Id$
36   */
37  public class StopCommand extends Error
38  {
39      private static final long serialVersionUID = 2577683435802825964L;
40      private Object stopMe;
41      private boolean nearest = false;
42  
43      public StopCommand()
44      {
45          this.nearest = true;
46      }
47  
48      public StopCommand(String message)
49      {
50          super(message);
51      }
52  
53      public StopCommand(Object stopMe)
54      {
55          this.stopMe = stopMe;
56      }
57  
58      public String getMessage()
59      {
60          if (stopMe != null)
61          {
62              // only create a useful message if requested (which is unlikely)
63              return "StopCommand: "+stopMe;
64          }
65          else
66          {
67              return "StopCommand: "+super.getMessage();
68          }
69      }
70  
71      public boolean isFor(Object that)
72      {
73          if (nearest) // if we're stopping at the first chance
74          {
75              // save that for message
76              stopMe = that;
77              return true;
78          }
79          else if (stopMe != null) // if we have a specified stopping point
80          {
81              return (that == stopMe);
82          }
83          else // only stop for the top :)
84          {
85              return (that instanceof Template ||
86                      that instanceof RuntimeInstance ||
87                      that instanceof Evaluate);
88          }
89      }
90  }