001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.web.servlet.view.tiles;
018:
019: import org.apache.struts.tiles.DefinitionsFactory;
020: import org.apache.struts.tiles.DefinitionsFactoryConfig;
021: import org.apache.struts.tiles.DefinitionsFactoryException;
022: import org.apache.struts.tiles.TilesUtil;
023: import org.apache.struts.tiles.xmlDefinition.I18nFactorySet;
024:
025: import org.springframework.beans.factory.InitializingBean;
026: import org.springframework.util.StringUtils;
027: import org.springframework.web.context.support.WebApplicationObjectSupport;
028:
029: /**
030: * Helper class to configure Tiles for the Spring Framework. See
031: * <a href="http://jakarta.apache.org/struts">http://jakarta.apache.org/struts</a>
032: * for more information about Tiles, which basically is a templating mechanism
033: * for JSP-based web applications.
034: *
035: * <p>The TilesConfigurer simply configures a Tiles DefinitionsFactory using a
036: * set of files containing definitions, to be accessed by TilesView instances.
037: * TilesViews can be managed by any ViewResolver.
038: *
039: * <p>A typical TilesConfigurer bean definition looks as follows:
040: *
041: * <pre>
042: * <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles.TilesConfigurer">
043: * <property name="definitions">
044: * <list>
045: * <value>/WEB-INF/defs/general.xml</value>
046: * <value>/WEB-INF/defs/widgets.xml</value>
047: * <value>/WEB-INF/defs/administrator.xml</value>
048: * <value>/WEB-INF/defs/customer.xml</value>
049: * <value>/WEB-INF/defs/templates.xml</value>
050: * </list>
051: * </property>
052: * </bean></pre>
053: *
054: * The values in the list are the actual files containing the definitions.
055: *
056: * @author Alef Arendsen
057: * @author Juergen Hoeller
058: * @see TilesView
059: * @see org.springframework.web.servlet.ViewResolver
060: */
061: public class TilesConfigurer extends WebApplicationObjectSupport
062: implements InitializingBean {
063:
064: /** factory class for Tiles */
065: private Class factoryClass = I18nFactorySet.class;
066:
067: /** validate the Tiles definitions? */
068: private boolean validateDefinitions = true;
069:
070: /** definition URLs mapped to descriptions */
071: private String[] definitions;
072:
073: /**
074: * Set the factory class for Tiles. Default is I18nFactorySet.
075: * @see org.apache.struts.tiles.xmlDefinition.I18nFactorySet
076: */
077: public void setFactoryClass(Class factoryClass) {
078: this .factoryClass = factoryClass;
079: }
080:
081: /**
082: * Set whether to validate the Tiles definitions. Default is "true".
083: */
084: public void setValidateDefinitions(boolean validateDefinitions) {
085: this .validateDefinitions = validateDefinitions;
086: }
087:
088: /**
089: * Set the Tiles definitions, i.e. the list of files containing the definitions.
090: */
091: public void setDefinitions(String[] definitions) {
092: this .definitions = definitions;
093: }
094:
095: /**
096: * Initialize the Tiles definition factory.
097: * Delegates to createDefinitionsFactory for the actual creation.
098: * @throws DefinitionsFactoryException if an error occurs
099: * @see #createDefinitionsFactory
100: */
101: public void afterPropertiesSet() throws DefinitionsFactoryException {
102: logger.debug("TilesConfigurer: initializion started");
103:
104: // initialize the configuration for the definitions factory
105: DefinitionsFactoryConfig factoryConfig = new DefinitionsFactoryConfig();
106: factoryConfig.setFactoryClassname(this .factoryClass.getName());
107: factoryConfig.setParserValidate(this .validateDefinitions);
108:
109: if (this .definitions != null) {
110: String defs = StringUtils
111: .arrayToCommaDelimitedString(this .definitions);
112: if (logger.isInfoEnabled()) {
113: logger.info("TilesConfigurer: adding definitions ["
114: + defs + "]");
115: }
116: factoryConfig.setDefinitionConfigFiles(defs);
117: }
118:
119: // initialize the definitions factory
120: createDefinitionsFactory(factoryConfig);
121:
122: logger.debug("TilesConfigurer: initialization completed");
123: }
124:
125: /**
126: * Create the Tiles DefinitionsFactory and expose it to the ServletContext.
127: * @param factoryConfig the configuration for the DefinitionsFactory
128: * @return the DefinitionsFactory
129: * @throws DefinitionsFactoryException if an error occurs
130: */
131: protected DefinitionsFactory createDefinitionsFactory(
132: DefinitionsFactoryConfig factoryConfig)
133: throws DefinitionsFactoryException {
134:
135: return TilesUtil.createDefinitionsFactory(getServletContext(),
136: factoryConfig);
137: }
138:
139: }
|