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 25 /** 26 * Stop command for directive Control objects. In an ideal JDK, 27 * this would be able to extend a RuntimeThrowable class, but we 28 * don't have that. So to avoid the interface changes needed by 29 * extending Throwable and the potential errant catches were we 30 * to extend RuntimeException, we'll have to extend Error, 31 * despite the fact that this is never an error. 32 * 33 * @author Nathan Bubna 34 * @version $Id$ 35 */ 36 public class StopCommand extends Error 37 { 38 private static final long serialVersionUID = 2577683435802825964L; 39 private Object stopMe; 40 private boolean nearest = false; 41 StopCommand()42 public StopCommand() 43 { 44 this.nearest = true; 45 } 46 StopCommand(String message)47 public StopCommand(String message) 48 { 49 super(message); 50 } 51 StopCommand(Object stopMe)52 public StopCommand(Object stopMe) 53 { 54 this.stopMe = stopMe; 55 } 56 57 @Override getMessage()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 isFor(Object that)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 } 88 } 89 } 90