1/*2 * $Id: CompatibilityDigesterDefinitionsReader.java 947472 2010-05-23 19:32:49Z apetrelli $3 *4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122package org.apache.tiles.compat.definition.digester;
2324import org.apache.commons.digester.Digester;
25import org.apache.commons.digester.Rule;
26import org.apache.tiles.Attribute;
27import org.apache.tiles.beans.SimpleMenuItem;
28import org.apache.tiles.definition.digester.DigesterDefinitionsReader;
29import org.xml.sax.Attributes;
3031/**32 * Digester reader that can read Tiles 1.1, 1.2, 1.3, 1.4 and 2.0 files.33 *34 * @version $Rev: 947472 $ $Date: 2010-05-24 05:32:49 +1000 (Mon, 24 May 2010) $35 * @since 2.1.036 */37publicclassCompatibilityDigesterDefinitionsReaderextends38DigesterDefinitionsReader {
3940/**41 * Intercepts a <item> tag.42 */43privatestaticfinal String ADD_WILDCARD = "*/item";
4445/**46 * Intercepts a <bean> tag.47 */48privatestaticfinal String BEAN_TAG = "*/bean";
4950/**51 * The set of public identifiers, and corresponding resource names for the52 * versions of the configuration file DTDs we know about. There <strong>MUST</strong>53 * be an even number of Strings in this list!54 *55 * @since 2.1.056 */57protected String[] registrations;
5859/** {@inheritDoc} */60 @Override
61protectedvoid initSyntax(Digester digester) {
62super.initSyntax(digester);
63 initDigesterForComponentsDefinitionsSyntax(digester);
64 initDigesterForInstancesSyntax(digester);
65 initDigesterForTilesDefinitionsSyntax(digester);
66 initDigesterForBeans(digester);
67 }
6869/**70 * Init digester for components syntax. This is an old set of rules, left71 * for backward compatibility.72 *73 * @param digester Digester instance to use.74 */75privatevoid initDigesterForComponentsDefinitionsSyntax(Digester digester) {
76// Common constants77 String definitionTag = "component-definitions/definition";
7879 String putTag = definitionTag + "/put";
8081 String listTag = definitionTag + "/putList";
8283 String addListElementTag = listTag + "/add";
8485// syntax rules86 digester.addObjectCreate(definitionTag, DEFINITION_HANDLER_CLASS);
87 digester.addRule(definitionTag, newFillDefinitionRule());
88 digester.addSetNext(definitionTag, "addDefinition",
89 DEFINITION_HANDLER_CLASS);
90// put / putAttribute rules91 digester.addObjectCreate(putTag, PUT_ATTRIBUTE_HANDLER_CLASS);
92 digester.addRule(putTag, newFillAttributeRule());
93 digester.addRule(putTag, newPutAttributeRule());
94// list rules95 digester.addObjectCreate(listTag, LIST_HANDLER_CLASS);
96 digester.addSetProperties(listTag);
97 digester.addRule(listTag, newPutAttributeRule());
98// list elements rules99// We use Attribute class to avoid rewriting a new class.100// Name part can't be used in listElement attribute.101 digester.addObjectCreate(addListElementTag, PUT_ATTRIBUTE_HANDLER_CLASS);
102 digester.addRule(addListElementTag, newFillAttributeRule());
103 digester.addSetNext(addListElementTag, "add",
104 PUT_ATTRIBUTE_HANDLER_CLASS);
105 }
106107/**108 * Init digester for Tiles syntax. Same as components, but with first109 * element = tiles-definitions110 *111 * @param digester Digester instance to use.112 */113privatevoid initDigesterForTilesDefinitionsSyntax(Digester digester) {
114// Common constants115 String definitionTag = "tiles-definitions/definition";
116117 String putTag = definitionTag + "/put";
118119// String LIST_TAG = DEFINITION_TAG + "/putList";120// List tag value121 String listTag = "putList";
122 String definitionListTag = definitionTag + "/" + listTag;
123// Tag value for adding an element in a list124 String addListElementTag = "*/" + listTag + "/add";
125126// put / putAttribute rules127// Rules for a same pattern are called in order, but rule.end() are128// called129// in reverse order.130// SetNext and CallMethod use rule.end() method. So, placing SetNext in131// first position ensure it will be called last (sic).132 digester.addObjectCreate(putTag, PUT_ATTRIBUTE_HANDLER_CLASS);
133 digester.addRule(putTag, newFillAttributeRule());
134 digester.addRule(putTag, newPutAttributeRule());
135// Definition level list rules136// This is rules for lists nested in a definition137 digester.addObjectCreate(definitionListTag, LIST_HANDLER_CLASS);
138 digester.addSetProperties(definitionListTag);
139 digester.addRule(definitionListTag, newPutAttributeRule());
140// list elements rules141// We use Attribute class to avoid rewriting a new class.142// Name part can't be used in listElement attribute.143 digester.addObjectCreate(addListElementTag, PUT_ATTRIBUTE_HANDLER_CLASS);
144 digester.addRule(addListElementTag, newFillAttributeRule());
145 digester.addSetNext(addListElementTag, "add",
146 PUT_ATTRIBUTE_HANDLER_CLASS);
147148// nested list elements rules149// Create a list handler, and add it to parent list150 String nestedList = "*/" + listTag + "/" + listTag;
151 digester.addObjectCreate(nestedList, LIST_HANDLER_CLASS);
152 digester.addSetProperties(nestedList);
153 digester.addSetNext(nestedList, "add", PUT_ATTRIBUTE_HANDLER_CLASS);
154 }
155156/**157 * Init digester in order to parse instances definition file syntax.158 * Instances is an old name for "definition". This method is left for159 * backwards compatibility.160 *161 * @param digester Digester instance to use.162 */163privatevoid initDigesterForInstancesSyntax(Digester digester) {
164// Build a digester to process our configuration resource165 String instanceTag = "component-instances/instance";
166167 String putTag = instanceTag + "/put";
168 String putAttributeTag = instanceTag + "/putAttribute";
169170 String listTag = instanceTag + "/putList";
171172 String addListElementTag = listTag + "/add";
173174// component instance rules175 digester.addObjectCreate(instanceTag, DEFINITION_HANDLER_CLASS);
176 digester.addRule(instanceTag, newFillDefinitionRule());
177 digester
178 .addSetNext(instanceTag, "addDefinition", DEFINITION_HANDLER_CLASS);
179// put / putAttribute rules180 digester.addObjectCreate(putAttributeTag, PUT_ATTRIBUTE_HANDLER_CLASS);
181 digester.addRule(putTag, newFillAttributeRule());
182 digester.addRule(putTag, newPutAttributeRule());
183// put / putAttribute rules184 digester.addObjectCreate(putTag, PUT_ATTRIBUTE_HANDLER_CLASS);
185 digester.addSetProperties(putTag);
186 digester.addRule(putTag, newPutAttributeRule());
187// list rules188 digester.addObjectCreate(listTag, PUT_ATTRIBUTE_HANDLER_CLASS);
189 digester.addSetProperties(listTag);
190 digester.addRule(listTag, newPutAttributeRule());
191// list elements rules192// We use Attribute class to avoid rewriting a new class.193// Name part can't be used in listElement attribute.194 digester.addObjectCreate(addListElementTag, PUT_ATTRIBUTE_HANDLER_CLASS);
195 digester.addRule(addListElementTag, newFillAttributeRule());
196 digester.addSetNext(addListElementTag, "add", PUT_ATTRIBUTE_HANDLER_CLASS);
197 }
198199/**200 * Init digester for Tiles syntax with first element = tiles-definitions.201 *202 * @param digester Digester instance to use.203 */204privatevoid initDigesterForBeans(Digester digester) {
205206// item elements rules207// We use Attribute class to avoid rewriting a new class.208// Name part can't be used in listElement attribute.209//String ADD_WILDCARD = LIST_TAG + "/addItem";210// non String ADD_WILDCARD = LIST_TAG + "/addx*";211 String menuItemDefaultClass = SimpleMenuItem.class.getName();
212 digester.addObjectCreate(ADD_WILDCARD, menuItemDefaultClass, "classtype");
213 digester.addSetProperties(ADD_WILDCARD);
214 digester.addRule(ADD_WILDCARD, newSetValueToAttributeRule());
215 digester.addSetNext(ADD_WILDCARD, "add", PUT_ATTRIBUTE_HANDLER_CLASS);
216217// bean elements rules218 String beanDefaultClass = SimpleMenuItem.class.getName();
219 digester.addObjectCreate(BEAN_TAG, beanDefaultClass, "classtype");
220 digester.addSetProperties(BEAN_TAG);
221 digester.addRule(BEAN_TAG, newSetValueToAttributeRule());
222 digester.addSetNext(BEAN_TAG, "add", PUT_ATTRIBUTE_HANDLER_CLASS);
223224// Set properties to surrounding element225 digester.addSetProperty(BEAN_TAG + "/set-property", "property", "value");
226 }
227228/** {@inheritDoc} */229 @Override
230protected String[] getRegistrations() {
231if (registrations == null) {
232 registrations = new String[] {
233"-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN",
234"/org/apache/tiles/resources/tiles-config_3_0.dtd",
235"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN",
236"/org/apache/tiles/compat/resources/tiles-config_2_0.dtd",
237"-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN",
238"/org/apache/tiles/compat/resources/tiles-config_2_1.dtd",
239"-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN",
240"/org/apache/tiles/compat/resources/tiles-config_1_1.dtd",
241"-//Apache Software Foundation//DTD Tiles Configuration 1.3//EN",
242"/org/apache/tiles/compat/resources/tiles-config_1_3.dtd",
243"-//Apache Software Foundation//DTD Tiles Configuration 1.4//EN",
244"/org/apache/tiles/compat/resources/tiles-config_1_4.dtd"};
245 }
246return registrations;
247 }
248249/**250 * Digester rule to manage assignment of an object as an attribute value.251 *252 * @since 3.0.0253 */254publicstaticclassSetValueToAttributeRuleextends Rule {
255256/** {@inheritDoc} */257 @Override
258publicvoid begin(String namespace, String name, Attributes attributes) {
259 Object obj = digester.pop();
260Attribute attribute = newAttribute(obj);
261 digester.push(attribute);
262 }
263 }
264 }