001/*
002 * Copyright 2002-2018 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 *      https://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
017package org.springframework.web.servlet.config;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.w3c.dom.Element;
023
024import org.springframework.beans.factory.support.AbstractBeanDefinition;
025import org.springframework.beans.factory.support.BeanDefinitionBuilder;
026import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
027import org.springframework.beans.factory.xml.ParserContext;
028import org.springframework.util.StringUtils;
029import org.springframework.util.xml.DomUtils;
030
031/**
032 * Parse the <code>&lt;mvc:tiles-configurer&gt;</code> MVC namespace element and register
033 * a corresponding {@code TilesConfigurer} bean.
034 *
035 * @author Rossen Stoyanchev
036 * @author Juergen Hoeller
037 * @since 4.1
038 */
039public class TilesConfigurerBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
040
041        /**
042         * The bean name used for the {@code TilesConfigurer}.
043         */
044        public static final String BEAN_NAME = "mvcTilesConfigurer";
045
046
047        @Override
048        protected String getBeanClassName(Element element) {
049                return "org.springframework.web.servlet.view.tiles3.TilesConfigurer";
050        }
051
052        @Override
053        protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) {
054                return BEAN_NAME;
055        }
056
057        @Override
058        protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
059                List<Element> childElements = DomUtils.getChildElementsByTagName(element, "definitions");
060                if (!childElements.isEmpty()) {
061                        List<String> locations = new ArrayList<>(childElements.size());
062                        for (Element childElement : childElements) {
063                                locations.add(childElement.getAttribute("location"));
064                        }
065                        builder.addPropertyValue("definitions", StringUtils.toStringArray(locations));
066                }
067                if (element.hasAttribute("check-refresh")) {
068                        builder.addPropertyValue("checkRefresh", element.getAttribute("check-refresh"));
069                }
070                if (element.hasAttribute("validate-definitions")) {
071                        builder.addPropertyValue("validateDefinitions", element.getAttribute("validate-definitions"));
072                }
073                if (element.hasAttribute("definitions-factory")) {
074                        builder.addPropertyValue("definitionsFactoryClass", element.getAttribute("definitions-factory"));
075                }
076                if (element.hasAttribute("preparer-factory")) {
077                        builder.addPropertyValue("preparerFactoryClass", element.getAttribute("preparer-factory"));
078                }
079        }
080
081}