1 package org.apache.velocity.tools.view.servlet; 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.commons.digester.Digester; 23 import org.apache.commons.digester.Rule; 24 import org.apache.velocity.tools.view.ToolboxRuleSet; 25 26 /** 27 * <p>The set of Digester rules required to parse a toolbox 28 * configuration file (<code>toolbox.xml</code>) for the 29 * ServletToolboxManager class.</p> 30 * 31 * @since VelocityTools 1.1 32 * @author Nathan Bubna 33 * @version $Id: ServletToolboxRuleSet.java 534682 2007-05-03 01:50:54Z nbubna $ 34 * @deprecated Use {@link org.apache.velocity.tools.config.XmlFactoryConfigurationRuleSet}. 35 */ 36 @Deprecated 37 public class ServletToolboxRuleSet extends ToolboxRuleSet 38 { 39 40 /** 41 * Overrides {@link ToolboxRuleSet} to add create-session rule. 42 * 43 * <p>These rules assume that an instance of 44 * <code>org.apache.velocity.tools.view.ServletToolboxManager</code> is 45 * pushed onto the evaluation stack before parsing begins.</p> 46 * 47 * @param digester Digester instance to which the new Rule instances 48 * should be added. 49 */ 50 public void addRuleInstances(Digester digester) 51 { 52 digester.addRule("toolbox/create-session", new CreateSessionRule()); 53 digester.addRule("toolbox/xhtml", new XhtmlRule()); 54 super.addRuleInstances(digester); 55 } 56 57 58 /** 59 * Overrides {@link ToolboxRuleSet} to add rule for scope element. 60 */ 61 protected void addToolRules(Digester digester) 62 { 63 super.addToolRules(digester); 64 digester.addBeanPropertySetter("toolbox/tool/scope", "scope"); 65 digester.addBeanPropertySetter("toolbox/tool/request-path", "requestPath"); 66 } 67 68 69 /** 70 * Overrides {@link ToolboxRuleSet} to use ServletToolInfo class. 71 */ 72 protected Class getToolInfoClass() 73 { 74 return ServletToolInfo.class; 75 } 76 77 78 /****************************** Custom Rules *****************************/ 79 80 /** 81 * Abstract rule for configuring boolean options on the parent 82 * object/element of the matching element. 83 */ 84 protected abstract class BooleanConfigRule extends Rule 85 { 86 public void body(String ns, String name, String text) throws Exception 87 { 88 Object parent = digester.peek(); 89 if ("yes".equalsIgnoreCase(text)) 90 { 91 setBoolean(parent, Boolean.TRUE); 92 } 93 else 94 { 95 setBoolean(parent, Boolean.valueOf(text)); 96 } 97 } 98 99 /** 100 * Takes the parent object and boolean value in order to 101 * call the appropriate method on the parent for the 102 * implementing rule. 103 * 104 * @param parent the parent object/element in the digester's stack 105 * @param value the boolean value contained in the current element 106 */ 107 public abstract void setBoolean(Object parent, Boolean value) 108 throws Exception; 109 } 110 111 112 /** 113 * Rule that sets <code>setCreateSession()</code> for the top object 114 * on the stack, which must be a 115 * <code>org.apache.velocity.tools.ServletToolboxManager</code>. 116 */ 117 protected final class CreateSessionRule extends BooleanConfigRule 118 { 119 public void setBoolean(Object obj, Boolean b) throws Exception 120 { 121 ((ServletToolboxManager)obj).setCreateSession(b.booleanValue()); 122 } 123 } 124 125 126 /** 127 * Rule that sets <code>setXhtml()</code> for the top object 128 * on the stack, which must be a 129 * <code>org.apache.velocity.tools.ServletToolboxManager</code>. 130 */ 131 protected final class XhtmlRule extends BooleanConfigRule 132 { 133 public void setBoolean(Object obj, Boolean b) throws Exception 134 { 135 ((ServletToolboxManager)obj).setXhtml(b); 136 } 137 } 138 139 }