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