1 16 17 package org.springframework.scripting.config; 18 19 import java.util.List ; 20 21 import org.w3c.dom.Element ; 22 23 import org.springframework.beans.factory.config.ConstructorArgumentValues; 24 import org.springframework.beans.factory.config.RuntimeBeanReference; 25 import org.springframework.beans.factory.support.AbstractBeanDefinition; 26 import org.springframework.beans.factory.support.BeanDefinitionRegistry; 27 import org.springframework.beans.factory.support.RootBeanDefinition; 28 import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser; 29 import org.springframework.beans.factory.xml.ParserContext; 30 import org.springframework.beans.factory.xml.XmlReaderContext; 31 import org.springframework.scripting.support.ScriptFactoryPostProcessor; 32 import org.springframework.util.StringUtils; 33 import org.springframework.util.xml.DomUtils; 34 35 53 class ScriptBeanDefinitionParser extends AbstractBeanDefinitionParser { 54 55 59 private static final String SCRIPT_FACTORY_POST_PROCESSOR_BEAN_NAME = ".scriptFactoryPostProcessor"; 60 61 private static final String SCRIPT_SOURCE_ATTRIBUTE = "script-source"; 62 63 private static final String INLINE_SCRIPT_ELEMENT = "inline-script"; 64 65 private static final String SCOPE_ATTRIBUTE = "scope"; 66 67 private static final String SCRIPT_INTERFACES_ATTRIBUTE = "script-interfaces"; 68 69 private static final String REFRESH_CHECK_DELAY_ATTRIBUTE = "refresh-check-delay"; 70 71 private static final String CUSTOMIZER_REF_ATTRIBUTE = "customizer-ref"; 72 73 74 78 private final String scriptFactoryClassName; 79 80 81 86 public ScriptBeanDefinitionParser(String scriptFactoryClassName) { 87 this.scriptFactoryClassName = scriptFactoryClassName; 88 } 89 90 91 95 protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) { 96 String value = resolveScriptSource(element, parserContext.getReaderContext()); 98 if (value == null) { 99 return null; 100 } 101 102 registerScriptFactoryPostProcessorIfNecessary(parserContext.getRegistry()); 104 105 RootBeanDefinition beanDefinition = new RootBeanDefinition(); 107 beanDefinition.setBeanClassName(this.scriptFactoryClassName); 108 beanDefinition.setSource(parserContext.extractSource(element)); 109 110 String scope = element.getAttribute(SCOPE_ATTRIBUTE); 112 if (StringUtils.hasLength(scope)) { 113 beanDefinition.setScope(scope); 114 } 115 116 String refreshCheckDelay = element.getAttribute(REFRESH_CHECK_DELAY_ATTRIBUTE); 118 if (StringUtils.hasText(refreshCheckDelay)) { 119 beanDefinition.setAttribute( 120 ScriptFactoryPostProcessor.REFRESH_CHECK_DELAY_ATTRIBUTE, new Long (refreshCheckDelay)); 121 } 122 123 ConstructorArgumentValues cav = beanDefinition.getConstructorArgumentValues(); 125 int constructorArgNum = 0; 126 cav.addIndexedArgumentValue(constructorArgNum++, value); 127 if (element.hasAttribute(SCRIPT_INTERFACES_ATTRIBUTE)) { 128 cav.addIndexedArgumentValue(constructorArgNum++, element.getAttribute(SCRIPT_INTERFACES_ATTRIBUTE)); 129 } 130 131 if (element.hasAttribute(CUSTOMIZER_REF_ATTRIBUTE)) { 133 String customizerBeanName = element.getAttribute(CUSTOMIZER_REF_ATTRIBUTE); 134 cav.addIndexedArgumentValue(constructorArgNum++, new RuntimeBeanReference(customizerBeanName)); 135 } 136 137 parserContext.getDelegate().parsePropertyElements(element, beanDefinition); 139 140 return beanDefinition; 141 } 142 143 148 private String resolveScriptSource(Element element, XmlReaderContext readerContext) { 149 boolean hasScriptSource = element.hasAttribute(SCRIPT_SOURCE_ATTRIBUTE); 150 List elements = DomUtils.getChildElementsByTagName(element, INLINE_SCRIPT_ELEMENT); 151 if (hasScriptSource && !elements.isEmpty()) { 152 readerContext.error("Only one of 'script-source' and 'inline-script' should be specified.", element); 153 return null; 154 } 155 else if (hasScriptSource) { 156 return element.getAttribute(SCRIPT_SOURCE_ATTRIBUTE); 157 } 158 else if (!elements.isEmpty()) { 159 Element inlineElement = (Element ) elements.get(0); 160 return "inline:" + DomUtils.getTextValue(inlineElement); 161 } 162 else { 163 readerContext.error("Must specify either 'script-source' or 'inline-script'.", element); 164 return null; 165 } 166 } 167 168 173 private static void registerScriptFactoryPostProcessorIfNecessary(BeanDefinitionRegistry registry) { 174 if (!registry.containsBeanDefinition(SCRIPT_FACTORY_POST_PROCESSOR_BEAN_NAME)) { 175 RootBeanDefinition beanDefinition = new RootBeanDefinition(ScriptFactoryPostProcessor.class); 176 registry.registerBeanDefinition(SCRIPT_FACTORY_POST_PROCESSOR_BEAN_NAME, beanDefinition); 177 } 178 } 179 180 } 181 | Popular Tags |