KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > tools > view > servlet > ServletToolboxRuleSet


1 /*
2  * Copyright 2003 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.velocity.tools.view.servlet;
18
19 import org.apache.commons.digester.Digester;
20 import org.apache.commons.digester.Rule;
21 import org.apache.velocity.tools.view.ToolboxRuleSet;
22 import org.apache.velocity.tools.view.servlet.ServletToolboxManager;
23 import org.apache.velocity.tools.view.servlet.ServletToolInfo;
24
25 /**
26  * <p>The set of Digester rules required to parse a toolbox
27  * configuration file (<code>toolbox.xml</code>) for the
28  * ServletToolboxManager class.</p>
29  *
30  * @since VelocityTools 1.1
31  * @author <a HREF="mailto:nathan@esha.com">Nathan Bubna</a>
32  * @version $Id: ServletToolboxRuleSet.java,v 1.4 2004/02/18 20:07:02 nbubna Exp $
33  */

34 public class ServletToolboxRuleSet extends ToolboxRuleSet
35 {
36
37     /**
38      * Overrides {@link ToolboxRuleSet} to add create-session rule.
39      *
40      * <p>These rules assume that an instance of
41      * <code>org.apache.velocity.tools.view.ServletToolboxManager</code> is
42      * pushed onto the evaluation stack before parsing begins.</p>
43      *
44      * @param digester Digester instance to which the new Rule instances
45      * should be added.
46      */

47     public void addRuleInstances(Digester digester)
48     {
49         digester.addRule("toolbox/create-session", new CreateSessionRule());
50         digester.addRule("toolbox/xhtml", new XhtmlRule());
51         super.addRuleInstances(digester);
52     }
53
54
55     /**
56      * Overrides {@link ToolboxRuleSet} to add rule for scope element.
57      */

58     protected void addToolRules(Digester digester)
59     {
60         super.addToolRules(digester);
61         digester.addBeanPropertySetter("toolbox/tool/scope", "scope");
62     }
63
64
65     /**
66      * Overrides {@link ToolboxRuleSet} to use ServletToolInfo class.
67      */

68     protected Class JavaDoc getToolInfoClass()
69     {
70         return ServletToolInfo.class;
71     }
72
73
74     /****************************** Custom Rules *****************************/
75
76     /**
77      * Abstract rule for configuring boolean options on the parent
78      * object/element of the matching element.
79      */

80     protected abstract class BooleanConfigRule extends Rule
81     {
82         public void body(String JavaDoc ns, String JavaDoc name, String JavaDoc text) throws Exception JavaDoc
83         {
84             Object JavaDoc parent = digester.peek();
85             if ("yes".equalsIgnoreCase(text))
86             {
87                 setBoolean(parent, Boolean.TRUE);
88             }
89             else
90             {
91                 setBoolean(parent, Boolean.valueOf(text));
92             }
93         }
94
95         /**
96          * Takes the parent object and boolean value in order to
97          * call the appropriate method on the parent for the
98          * implementing rule.
99          *
100          * @param parent the parent object/element in the digester's stack
101          * @param value the boolean value contained in the current element
102          */

103         public abstract void setBoolean(Object JavaDoc parent, Boolean JavaDoc value)
104             throws Exception JavaDoc;
105     }
106
107
108     /**
109      * Rule that sets <code>setCreateSession()</code> for the top object
110      * on the stack, which must be a
111      * <code>org.apache.velocity.tools.ServletToolboxManager</code>.
112      */

113     protected final class CreateSessionRule extends BooleanConfigRule
114     {
115         public void setBoolean(Object JavaDoc obj, Boolean JavaDoc b) throws Exception JavaDoc
116         {
117             ((ServletToolboxManager)obj).setCreateSession(b.booleanValue());
118         }
119     }
120
121
122     /**
123      * Rule that sets <code>setCreateSession()</code> for the top object
124      * on the stack, which must be a
125      * <code>org.apache.velocity.tools.ServletToolboxManager</code>.
126      */

127     protected final class XhtmlRule extends BooleanConfigRule
128     {
129         public void setBoolean(Object JavaDoc obj, Boolean JavaDoc b) throws Exception JavaDoc
130         {
131             ((ServletToolboxManager)obj).setXhtml(b);
132         }
133     }
134
135 }
136
Popular Tags