View Javadoc

1   package org.apache.velocity.tools.config;
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.commons.digester.RuleSetBase;
25  import org.xml.sax.Attributes;
26  
27  /**
28   * <p>The set of Digester rules required to parse a old toolbox
29   * configuration file (<code>toolbox.xml</code>).</p>
30   *
31   * @author Nathan Bubna
32   * @deprecated This is provided merely for 1.x compatibility.
33   * @version $Id: OldXmlFactoryConfigurationRuleSet.java 511959 2007-02-26 19:24:39Z nbubna $
34   */
35  @Deprecated
36  public class OldXmlFactoryConfigurationRuleSet extends RuleSetBase
37  {
38      public void addRuleInstances(Digester digester)
39      {
40          digester.addRule("toolbox/create-session", new CreateSessionRule());
41          digester.addRule("toolbox/xhtml", new XhtmlRule());
42  
43          digester.addObjectCreate("toolbox", ToolboxConfiguration.class);
44          digester.addRule("toolbox", new DeprecationRule());
45          digester.addSetNext("toolbox", "addToolbox");
46  
47          digester.addObjectCreate("toolbox/tool", ToolConfiguration.class);
48          digester.addBeanPropertySetter("toolbox/tool/key", "key");
49          digester.addBeanPropertySetter("toolbox/tool/class", "classname");
50          digester.addBeanPropertySetter("toolbox/tool/request-path", "restrictTo");
51          digester.addRule("toolbox/tool/scope", new ScopeRule());
52          digester.addRule("toolbox/tool/parameter", new ParameterRule());
53          digester.addSetNext("toolbox/tool", "addTool");
54  
55          digester.addObjectCreate("toolbox/data", Data.class);
56          digester.addSetProperties("toolbox/data");
57          digester.addBeanPropertySetter("toolbox/data/key", "key");
58          digester.addBeanPropertySetter("toolbox/data/value", "value");
59          digester.addRule("toolbox/data", new SetNextDataRule());
60      }
61  
62      protected static class DeprecationRule extends Rule
63      {
64          public void begin(String ns, String ln, Attributes attributes)
65              throws Exception
66          {
67              // add a property to the FactoryConfiguration that
68              // will trigger a deprecation warning in the logs
69              FactoryConfiguration factory =
70                  (FactoryConfiguration)digester.getRoot();
71              factory.setProperty("deprecationSupportMode", true);
72          }
73      }
74  
75  
76      protected static class ScopeRule extends Rule
77      {
78          public void body(String namespace, String element, String value)
79              throws Exception
80          {
81              ToolConfiguration tool = (ToolConfiguration)digester.peek(0);
82              ToolboxConfiguration toolbox = (ToolboxConfiguration)digester.peek(1);
83              // if the scope is different than that of the current toolbox
84              if (value != null && !value.equals(toolbox.getScope()))
85              {
86                  // add the old ToolboxConfiguration to the FactoryConfiguration
87                  FactoryConfiguration factory = (FactoryConfiguration)digester.peek(2);
88                  factory.addToolbox(toolbox);
89  
90                  // pop off the old toolbox and the tool
91                  digester.pop();
92                  digester.pop();
93  
94                  // and push a new toolbox on the stack with the new scope
95                  ToolboxConfiguration newbox = new ToolboxConfiguration();
96                  newbox.setScope(value);
97                  digester.push(newbox);
98  
99                  // push the tool back on the stack
100                 digester.push(tool);
101             }
102         }
103     }
104 
105     protected static class ParameterRule extends Rule
106     {
107         public void begin(String ns, String ln, Attributes attributes)
108             throws Exception
109         {
110             ToolConfiguration config = (ToolConfiguration)digester.peek();
111             String name = attributes.getValue("name");
112             String value = attributes.getValue("value");
113             config.setProperty(name, value);
114         }
115     }
116 
117     protected static class SetNextDataRule extends Rule
118     {
119         public void end() throws Exception
120         {
121             Data data = (Data)digester.peek(0);
122             FactoryConfiguration factory = (FactoryConfiguration)digester.getRoot();
123             factory.addData(data);
124         }
125     }
126 
127     protected static abstract class BooleanConfigRule extends Rule
128     {
129         public void body(String ns, String name, String text) throws Exception
130         {
131             FactoryConfiguration factory =
132                 (FactoryConfiguration)digester.getRoot();
133             if ("yes".equalsIgnoreCase(text))
134             {
135                 setBoolean(factory, Boolean.TRUE);
136             }
137             else
138             {
139                 setBoolean(factory, Boolean.valueOf(text));
140             }
141         }
142 
143         public abstract void setBoolean(FactoryConfiguration parent, Boolean value);
144     }
145 
146     protected static class CreateSessionRule extends BooleanConfigRule
147     {
148         public void setBoolean(FactoryConfiguration factory, Boolean b)
149         {
150             factory.setProperty("createSession", b);
151         }
152     }
153 
154     protected static class XhtmlRule extends BooleanConfigRule
155     {
156         public void setBoolean(FactoryConfiguration factory, Boolean b)
157         {
158             factory.setProperty("XHTML", b);
159         }
160     }
161 
162 }