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:freemarker-configurer&gt;</code> MVC namespace element and
033 * register {@code FreeMarkerConfigurer} bean.
034 *
035 * @author Rossen Stoyanchev
036 * @since 4.1
037 */
038public class FreeMarkerConfigurerBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
039
040        /**
041         * The bean name used for the {@code FreeMarkerConfigurer}.
042         */
043        public static final String BEAN_NAME = "mvcFreeMarkerConfigurer";
044
045
046        @Override
047        protected String getBeanClassName(Element element) {
048                return "org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer";
049        }
050
051        @Override
052        protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) {
053                return BEAN_NAME;
054        }
055
056        @Override
057        protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
058                List<Element> childElements = DomUtils.getChildElementsByTagName(element, "template-loader-path");
059                if (!childElements.isEmpty()) {
060                        List<String> locations = new ArrayList<>(childElements.size());
061                        for (Element childElement : childElements) {
062                                locations.add(childElement.getAttribute("location"));
063                        }
064                        if (locations.isEmpty()) {
065                                locations.add("/WEB-INF/");
066                        }
067                        builder.addPropertyValue("templateLoaderPaths", StringUtils.toStringArray(locations));
068                }
069        }
070
071}