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 <mvc:tiles-configurer> MVC namespace element and register
033 * a corresponding TilesConfigurer bean.
034
035 * @author Rossen Stoyanchev
036 * @author Juergen Hoeller
037 * @since 4.1
038 */
039public class TilesConfigurerBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
040
041        public static final String BEAN_NAME = "mvcTilesConfigurer";
042
043
044        @Override
045        protected String getBeanClassName(Element element) {
046                return "org.springframework.web.servlet.view.tiles3.TilesConfigurer";
047        }
048
049        @Override
050        protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) {
051                return BEAN_NAME;
052        }
053
054        @Override
055        protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
056                List<Element> childElements = DomUtils.getChildElementsByTagName(element, "definitions");
057                if (!childElements.isEmpty()) {
058                        List<String> locations = new ArrayList<String>(childElements.size());
059                        for (Element childElement : childElements) {
060                                locations.add(childElement.getAttribute("location"));
061                        }
062                        builder.addPropertyValue("definitions", StringUtils.toStringArray(locations));
063                }
064                if (element.hasAttribute("check-refresh")) {
065                        builder.addPropertyValue("checkRefresh", element.getAttribute("check-refresh"));
066                }
067                if (element.hasAttribute("validate-definitions")) {
068                        builder.addPropertyValue("validateDefinitions", element.getAttribute("validate-definitions"));
069                }
070                if (element.hasAttribute("definitions-factory")) {
071                        builder.addPropertyValue("definitionsFactoryClass", element.getAttribute("definitions-factory"));
072                }
073                if (element.hasAttribute("preparer-factory")) {
074                        builder.addPropertyValue("preparerFactoryClass", element.getAttribute("preparer-factory"));
075                }
076        }
077
078}