1 package org.apache.velocity.tools.config;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
29
30
31
32
33
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
68
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
84 if (value != null && !value.equals(toolbox.getScope()))
85 {
86
87 FactoryConfiguration factory = (FactoryConfiguration)digester.peek(2);
88 factory.addToolbox(toolbox);
89
90
91 digester.pop();
92 digester.pop();
93
94
95 ToolboxConfiguration newbox = new ToolboxConfiguration();
96 newbox.setScope(value);
97 digester.push(newbox);
98
99
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 }