moved parseAttribute method to parser support class

pull/894/head
Rui Gu 8 years ago
parent c693816a87
commit e63fe6fd6c

@ -15,7 +15,6 @@
*/ */
package org.redisson.spring.support; package org.redisson.spring.support;
import java.net.URI;
import java.util.List; import java.util.List;
import org.redisson.Redisson; import org.redisson.Redisson;
@ -124,7 +123,7 @@ public final class RedissonDefinitionParser
String id = parserContext.getReaderContext().generateBeanName(bd); String id = parserContext.getReaderContext().generateBeanName(bd);
helper.registerBeanDefinition(builder, id, helper.registerBeanDefinition(builder, id,
helper.parseAliase(element), parserContext); helper.parseAliase(element), parserContext);
parseAttributes(element, parserContext, builder); helper.parseAttributes(element, parserContext, builder);
redissonDef.addDependsOn(id); redissonDef.addDependsOn(id);
parseChildElements(element, id, null, redissonDef, parserContext); parseChildElements(element, id, null, redissonDef, parserContext);
parserContext.getDelegate().parseQualifierElements(element, bd); parserContext.getDelegate().parseQualifierElements(element, bd);
@ -140,45 +139,6 @@ public final class RedissonDefinitionParser
redissonDef.addDependsOn(id); redissonDef.addDependsOn(id);
} }
private void parseAttributes(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
NamedNodeMap attributes = element.getAttributes();
for (int x = 0; x < attributes.getLength(); x++) {
Attr attribute = (Attr) attributes.item(x);
if (helper.isEligibleAttribute(attribute)) {
String propertyName
= attribute.getLocalName().endsWith(REF_SUFFIX)
? attribute.getLocalName()
.substring(0, attribute.getLocalName()
.length() - REF_SUFFIX.length())
: attribute.getLocalName();
propertyName = Conventions
.attributeNameToPropertyName(propertyName);
Assert.state(StringUtils.hasText(propertyName),
"Illegal property name returned from"
+ " 'extractPropertyName(String)': cannot be"
+ " null or empty.");
if (attribute.getLocalName().endsWith(REF_SUFFIX)) {
builder.addPropertyReference(propertyName,
attribute.getValue());
} else {
Object value = attribute.getValue();
String localName = helper.getName(element);
if ("masterAddress".equals(propertyName)
&& ConfigType.masterSlaveServers.name()
.equals(localName)) {
try {
value = URI.create((String) value);
} catch (Exception e) {
//value may be a placeholder
value = "redis://" + value;
}
}
builder.addPropertyValue(propertyName, value);
}
}
}
}
@Override @Override
public BeanDefinition parse(Element element, ParserContext parserContext) { public BeanDefinition parse(Element element, ParserContext parserContext) {
//Sort out the Config Class //Sort out the Config Class
@ -186,7 +146,7 @@ public final class RedissonDefinitionParser
= helper.createBeanDefinitionBuilder(element, parserContext, = helper.createBeanDefinitionBuilder(element, parserContext,
Config.class); Config.class);
String configId = helper.getId(null, configBuilder, parserContext); String configId = helper.getId(null, configBuilder, parserContext);
parseAttributes(element, parserContext, configBuilder); helper.parseAttributes(element, parserContext, configBuilder);
helper.registerBeanDefinition(configBuilder, configId, helper.registerBeanDefinition(configBuilder, configId,
null, parserContext); null, parserContext);
@ -200,7 +160,7 @@ public final class RedissonDefinitionParser
parserContext.getDelegate().parseQualifierElements(element, parserContext.getDelegate().parseQualifierElements(element,
builder.getRawBeanDefinition()); builder.getRawBeanDefinition());
String id = helper.getId(element, builder, parserContext); String id = helper.getId(element, builder, parserContext);
parseAttributes(element, parserContext, configBuilder); helper.parseAttributes(element, parserContext, configBuilder);
//Sort out all the nested elements //Sort out all the nested elements
parseChildElements(element, configId, id, builder, parserContext); parseChildElements(element, configId, id, builder, parserContext);

@ -25,9 +25,11 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.xml.ParserContext; import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.core.Conventions; import org.springframework.core.Conventions;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.w3c.dom.Attr; import org.w3c.dom.Attr;
import org.w3c.dom.Element; import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node; import org.w3c.dom.Node;
/** /**
@ -39,6 +41,7 @@ public class RedissonNamespaceParserSupport {
public final static String REDISSON_NAMESPACE public final static String REDISSON_NAMESPACE
= "http://redisson.org/schema/redisson"; = "http://redisson.org/schema/redisson";
static final String REF_SUFFIX = "-ref";
static final String API_CLASS_PATH_PREFIX = "org.redisson.api.R"; static final String API_CLASS_PATH_PREFIX = "org.redisson.api.R";
static final String IMPL_CLASS_PATH_PREFIX = "org.redisson.Redisson"; static final String IMPL_CLASS_PATH_PREFIX = "org.redisson.Redisson";
@ -93,6 +96,33 @@ public class RedissonNamespaceParserSupport {
return aliases; return aliases;
} }
public void parseAttributes(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
NamedNodeMap attributes = element.getAttributes();
for (int x = 0; x < attributes.getLength(); x++) {
Attr attribute = (Attr) attributes.item(x);
if (isEligibleAttribute(attribute)) {
String propertyName
= attribute.getLocalName().endsWith(REF_SUFFIX)
? attribute.getLocalName()
.substring(0, attribute.getLocalName()
.length() - REF_SUFFIX.length())
: attribute.getLocalName();
propertyName = Conventions
.attributeNameToPropertyName(propertyName);
Assert.state(StringUtils.hasText(propertyName),
"Illegal property name returned from"
+ " 'extractPropertyName(String)': cannot be"
+ " null or empty.");
if (attribute.getLocalName().endsWith(REF_SUFFIX)) {
builder.addPropertyReference(propertyName,
attribute.getValue());
} else {
builder.addPropertyValue(propertyName, attribute.getValue());
}
}
}
}
public BeanDefinitionBuilder createBeanDefinitionBuilder(Element element, ParserContext parserContext, Class<?> cls) { public BeanDefinitionBuilder createBeanDefinitionBuilder(Element element, ParserContext parserContext, Class<?> cls) {
BeanDefinitionBuilder builder BeanDefinitionBuilder builder
= BeanDefinitionBuilder.genericBeanDefinition(); = BeanDefinitionBuilder.genericBeanDefinition();

Loading…
Cancel
Save