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.nio.charset.Charset; 020import java.util.ArrayList; 021import java.util.List; 022 023import org.w3c.dom.Element; 024 025import org.springframework.beans.factory.support.AbstractBeanDefinition; 026import org.springframework.beans.factory.support.BeanDefinitionBuilder; 027import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser; 028import org.springframework.beans.factory.xml.ParserContext; 029import org.springframework.util.StringUtils; 030import org.springframework.util.xml.DomUtils; 031 032/** 033 * Parse the <mvc:script-template-configurer> MVC namespace element and register a 034 * {@code ScriptTemplateConfigurer} bean. 035 * 036 * @author Sebastien Deleuze 037 * @since 4.2 038 */ 039public class ScriptTemplateConfigurerBeanDefinitionParser extends AbstractSimpleBeanDefinitionParser { 040 041 public static final String BEAN_NAME = "mvcScriptTemplateConfigurer"; 042 043 044 @Override 045 protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) { 046 return BEAN_NAME; 047 } 048 049 @Override 050 protected String getBeanClassName(Element element) { 051 return "org.springframework.web.servlet.view.script.ScriptTemplateConfigurer"; 052 } 053 054 @Override 055 protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { 056 List<Element> childElements = DomUtils.getChildElementsByTagName(element, "script"); 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("scripts", StringUtils.toStringArray(locations)); 063 } 064 builder.addPropertyValue("engineName", element.getAttribute("engine-name")); 065 if (element.hasAttribute("render-object")) { 066 builder.addPropertyValue("renderObject", element.getAttribute("render-object")); 067 } 068 if (element.hasAttribute("render-function")) { 069 builder.addPropertyValue("renderFunction", element.getAttribute("render-function")); 070 } 071 if (element.hasAttribute("content-type")) { 072 builder.addPropertyValue("contentType", element.getAttribute("content-type")); 073 } 074 if (element.hasAttribute("charset")) { 075 builder.addPropertyValue("charset", Charset.forName(element.getAttribute("charset"))); 076 } 077 if (element.hasAttribute("resource-loader-path")) { 078 builder.addPropertyValue("resourceLoaderPath", element.getAttribute("resource-loader-path")); 079 } 080 if (element.hasAttribute("shared-engine")) { 081 builder.addPropertyValue("sharedEngine", element.getAttribute("shared-engine")); 082 } 083 } 084 085 @Override 086 protected boolean isEligibleAttribute(String name) { 087 return (name.equals("engine-name") || name.equals("scripts") || name.equals("render-object") || 088 name.equals("render-function") || name.equals("content-type") || 089 name.equals("charset") || name.equals("resource-loader-path")); 090 } 091 092}