KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > tiles > TilesConfigurer


1 /*
2  * Copyright 2002-2006 the original author or authors.
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.springframework.web.servlet.view.tiles;
18
19 import org.apache.struts.tiles.DefinitionsFactory;
20 import org.apache.struts.tiles.DefinitionsFactoryConfig;
21 import org.apache.struts.tiles.DefinitionsFactoryException;
22 import org.apache.struts.tiles.TilesUtil;
23 import org.apache.struts.tiles.xmlDefinition.I18nFactorySet;
24
25 import org.springframework.beans.factory.InitializingBean;
26 import org.springframework.util.StringUtils;
27 import org.springframework.web.context.support.WebApplicationObjectSupport;
28
29 /**
30  * Helper class to configure Tiles for the Spring Framework. See
31  * <a HREF="http://jakarta.apache.org/struts">http://jakarta.apache.org/struts</a>
32  * for more information about Tiles, which basically is a templating mechanism
33  * for JSP-based web applications.
34  *
35  * <p>The TilesConfigurer simply configures a Tiles DefinitionsFactory using a
36  * set of files containing definitions, to be accessed by TilesView instances.
37  * TilesViews can be managed by any ViewResolver.
38  *
39  * <p>A typical TilesConfigurer bean definition looks as follows:
40  *
41  * <pre>
42  * &lt;bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles.TilesConfigurer">
43  * &lt;property name="definitions">
44  * &lt;list>
45  * &lt;value>/WEB-INF/defs/general.xml&lt;/value>
46  * &lt;value>/WEB-INF/defs/widgets.xml&lt;/value>
47  * &lt;value>/WEB-INF/defs/administrator.xml&lt;/value>
48  * &lt;value>/WEB-INF/defs/customer.xml&lt;/value>
49  * &lt;value>/WEB-INF/defs/templates.xml&lt;/value>
50  * &lt;/list>
51  * &lt;/property>
52  * &lt;/bean></pre>
53  *
54  * The values in the list are the actual files containing the definitions.
55  *
56  * @author Alef Arendsen
57  * @author Juergen Hoeller
58  * @see TilesView
59  * @see org.springframework.web.servlet.ViewResolver
60  */

61 public class TilesConfigurer extends WebApplicationObjectSupport implements InitializingBean {
62
63     /** factory class for Tiles */
64     private Class JavaDoc factoryClass = I18nFactorySet.class;
65
66     /** validate the Tiles definitions? */
67     private boolean validateDefinitions = true;
68
69     /** definition URLs mapped to descriptions */
70     private String JavaDoc[] definitions;
71
72
73     /**
74      * Set the factory class for Tiles. Default is I18nFactorySet.
75      * @see org.apache.struts.tiles.xmlDefinition.I18nFactorySet
76      */

77     public void setFactoryClass(Class JavaDoc factoryClass) {
78         this.factoryClass = factoryClass;
79     }
80
81     /**
82      * Set whether to validate the Tiles definitions. Default is "true".
83      */

84     public void setValidateDefinitions(boolean validateDefinitions) {
85         this.validateDefinitions = validateDefinitions;
86     }
87
88     /**
89      * Set the Tiles definitions, i.e. the list of files containing the definitions.
90      */

91     public void setDefinitions(String JavaDoc[] definitions) {
92         this.definitions = definitions;
93     }
94
95
96     /**
97      * Initialize the Tiles definition factory.
98      * Delegates to createDefinitionsFactory for the actual creation.
99      * @throws DefinitionsFactoryException if an error occurs
100      * @see #createDefinitionsFactory
101      */

102     public void afterPropertiesSet() throws DefinitionsFactoryException {
103         logger.debug("TilesConfigurer: initializion started");
104
105         // initialize the configuration for the definitions factory
106
DefinitionsFactoryConfig factoryConfig = new DefinitionsFactoryConfig();
107         factoryConfig.setFactoryClassname(this.factoryClass.getName());
108         factoryConfig.setParserValidate(this.validateDefinitions);
109
110         if (this.definitions != null) {
111             String JavaDoc defs = StringUtils.arrayToCommaDelimitedString(this.definitions);
112             if (logger.isInfoEnabled()) {
113                 logger.info("TilesConfigurer: adding definitions [" + defs + "]");
114             }
115             factoryConfig.setDefinitionConfigFiles(defs);
116         }
117
118         // initialize the definitions factory
119
createDefinitionsFactory(factoryConfig);
120
121         logger.debug("TilesConfigurer: initialization completed");
122     }
123
124     /**
125      * Create the Tiles DefinitionsFactory and expose it to the ServletContext.
126      * @param factoryConfig the configuration for the DefinitionsFactory
127      * @return the DefinitionsFactory
128      * @throws DefinitionsFactoryException if an error occurs
129      */

130     protected DefinitionsFactory createDefinitionsFactory(DefinitionsFactoryConfig factoryConfig)
131             throws DefinitionsFactoryException {
132
133         return TilesUtil.createDefinitionsFactory(getServletContext(), factoryConfig);
134     }
135
136 }
137
Popular Tags