]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/xml/SpecBeanDefinitionParser.java
ExcecutionScopeDecorator: change default to proxy interfaces
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / xml / SpecBeanDefinitionParser.java
1 package org.argeo.slc.core.execution.xml;
2
3 import java.util.List;
4
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7 import org.argeo.slc.core.execution.DefaultExecutionSpec;
8 import org.argeo.slc.core.execution.PrimitiveSpecAttribute;
9 import org.argeo.slc.core.execution.RefSpecAttribute;
10 import org.argeo.slc.core.execution.RefValueChoice;
11 import org.springframework.beans.factory.config.BeanDefinition;
12 import org.springframework.beans.factory.support.BeanDefinitionBuilder;
13 import org.springframework.beans.factory.support.ManagedList;
14 import org.springframework.beans.factory.support.ManagedMap;
15 import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
16 import org.springframework.beans.factory.xml.ParserContext;
17 import org.springframework.util.StringUtils;
18 import org.springframework.util.xml.DomUtils;
19 import org.w3c.dom.Element;
20
21 public class SpecBeanDefinitionParser extends
22 AbstractSingleBeanDefinitionParser {
23 private Log log = LogFactory.getLog(SpecBeanDefinitionParser.class);
24
25 @SuppressWarnings("unchecked")
26 @Override
27 protected void doParse(Element element, ParserContext parserContext,
28 BeanDefinitionBuilder builder) {
29 builder.getBeanDefinition().setDescription(DomUtils.getChildElementValueByTagName(element,
30 "description"));
31
32 ManagedMap attributes = new ManagedMap();
33
34 // Primitives
35 for (Element child : (List<Element>) DomUtils
36 .getChildElementsByTagName(element, "primitive")) {
37 BeanDefinitionBuilder childBuilder = BeanDefinitionBuilder
38 .genericBeanDefinition(PrimitiveSpecAttribute.class);
39 addCommonProperties(child, parserContext, childBuilder);
40
41 String type = child.getAttribute("type");
42 if (StringUtils.hasText(type))
43 childBuilder.addPropertyValue("type", type);
44
45 putInAttributes(attributes, child,
46 childBuilder.getBeanDefinition(), "primitive");
47 }
48
49 // Refs
50 for (Element refAttrElem : (List<Element>) DomUtils
51 .getChildElementsByTagName(element, "ref")) {
52 BeanDefinitionBuilder refAttrBuilder = BeanDefinitionBuilder
53 .genericBeanDefinition(RefSpecAttribute.class);
54 addCommonProperties(refAttrElem, parserContext, refAttrBuilder);
55
56 String targetClassName = refAttrElem.getAttribute("targetClass");
57 if (StringUtils.hasText(targetClassName))
58 refAttrBuilder.addPropertyValue("targetClass", targetClassName);
59
60 // Choices
61 Element choicesElem = DomUtils.getChildElementByTagName(
62 refAttrElem, "choices");
63 if (choicesElem != null) {
64 List<Element> choices = DomUtils.getChildElementsByTagName(
65 choicesElem, "choice");
66 ManagedList choiceBeans = new ManagedList(choices.size());
67 for (Element choiceElem : choices) {
68 BeanDefinitionBuilder choiceBuilder = BeanDefinitionBuilder
69 .genericBeanDefinition(RefValueChoice.class);
70 choiceBuilder.addPropertyValue("name", choiceElem
71 .getAttribute("name"));
72 String desc = choiceElem.getAttribute("description");
73 if (StringUtils.hasText(desc))
74 choiceBuilder.addPropertyValue("description", desc);
75
76 choiceBeans.add(choiceBuilder.getBeanDefinition());
77 }
78 refAttrBuilder.addPropertyValue("choices", choiceBeans);
79 }
80
81 putInAttributes(attributes, refAttrElem, refAttrBuilder
82 .getBeanDefinition(), "ref");
83 }
84
85 builder.addPropertyValue("attributes", attributes);
86 }
87
88 protected void addCommonProperties(Element element,
89 ParserContext parserContext, BeanDefinitionBuilder specAttr) {
90 addBooleanProperty("isParameter", specAttr, element);
91 addBooleanProperty("isFrozen", specAttr, element);
92 addBooleanProperty("isHidden", specAttr, element);
93
94 Object value = NamespaceUtils.parseValue(element, parserContext,
95 specAttr.getBeanDefinition(), "value");
96 if (value != null)
97 specAttr.addPropertyValue("value", value);
98
99 }
100
101 @SuppressWarnings("unchecked")
102 protected void putInAttributes(ManagedMap attributes, Element child,
103 BeanDefinition beanDefinition, String nature) {
104 String name = child.getAttribute("name");
105 attributes.put(name, beanDefinition);
106 if (log.isTraceEnabled())
107 log.debug("Added " + nature + " attribute " + name);
108
109 }
110
111 private void addBooleanProperty(String name,
112 BeanDefinitionBuilder specAttr, Element element) {
113 String bool = element.getAttribute(name);
114 if (StringUtils.hasText(bool))
115 specAttr.addPropertyValue(name, Boolean.parseBoolean(bool));
116
117 }
118
119 @Override
120 protected Class<DefaultExecutionSpec> getBeanClass(Element element) {
121 return DefaultExecutionSpec.class;
122 }
123
124 protected boolean shouldGenerateIdAsFallback() {
125 return false;
126 }
127
128 }