01: /*
02: * $Id: AttributeMapDefinitionParser.java 10494 2008-01-23 21:09:56Z acooke $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
10: package org.mule.config.spring.parsers.collection;
11:
12: import org.mule.config.spring.parsers.generic.ChildDefinitionParser;
13: import org.mule.config.spring.util.SpringXMLUtils;
14:
15: import org.springframework.beans.factory.config.MapFactoryBean;
16: import org.springframework.beans.factory.config.RuntimeBeanReference;
17: import org.springframework.beans.factory.support.BeanDefinitionBuilder;
18: import org.springframework.beans.factory.support.ManagedMap;
19: import org.springframework.beans.factory.xml.ParserContext;
20: import org.w3c.dom.Attr;
21: import org.w3c.dom.Element;
22: import org.w3c.dom.NamedNodeMap;
23:
24: /**
25: * Creates a single, stand-alone map object and processes all attributes to this map
26: */
27: public class AttributeMapDefinitionParser extends ChildDefinitionParser {
28: public AttributeMapDefinitionParser(String setter) {
29: super (setter, ManagedMap.class);
30: }
31:
32: protected Class getBeanClass(Element element) {
33: return MapFactoryBean.class;
34: }
35:
36: protected void parseChild(Element element,
37: ParserContext parserContext, BeanDefinitionBuilder builder) {
38: ManagedMap values = new ManagedMap();
39: NamedNodeMap attributes = element.getAttributes();
40: for (int x = 0; x < attributes.getLength(); x++) {
41: Attr attribute = (Attr) attributes.item(x);
42: String oldName = SpringXMLUtils.attributeName(attribute);
43: //TODO How can I use bestGuessName
44: String name = beanPropertyConfiguration
45: .translateName(oldName);
46: Object value = beanPropertyConfiguration.translateValue(
47: oldName, attribute.getNodeValue());
48: if (beanPropertyConfiguration.isReference(oldName)) {
49: values.put(name, new RuntimeBeanReference(attribute
50: .getNodeValue()));
51: } else {
52: values.put(name, value);
53: }
54: }
55: builder.addPropertyValue("sourceMap", values);
56: builder.addPropertyValue("targetMapClass", super
57: .getBeanClass(element));
58: postProcess(parserContext, getBeanAssembler(element, builder),
59: element);
60: }
61:
62: }
|