]> 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
Improve executions and system calls
[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 import org.w3c.dom.NodeList;
21
22 public class SpecBeanDefinitionParser extends
23 AbstractSingleBeanDefinitionParser {
24 private Log log = LogFactory.getLog(SpecBeanDefinitionParser.class);
25
26 @SuppressWarnings("unchecked")
27 @Override
28 protected void doParse(Element element, ParserContext parserContext,
29 BeanDefinitionBuilder builder) {
30 ManagedMap attributes = new ManagedMap();
31
32 // Primitives
33 for (Element child : (List<Element>) DomUtils
34 .getChildElementsByTagName(element, "primitive")) {
35 BeanDefinitionBuilder childBuilder = BeanDefinitionBuilder
36 .genericBeanDefinition(PrimitiveSpecAttribute.class);
37 addCommonProperties(child, parserContext, childBuilder);
38
39 String type = child.getAttribute("type");
40 if (StringUtils.hasText(type))
41 childBuilder.addPropertyValue("type", type);
42
43 putInAttributes(attributes, child,
44 childBuilder.getBeanDefinition(), "primitive");
45 }
46
47 // Refs
48 for (Element child : (List<Element>) DomUtils
49 .getChildElementsByTagName(element, "ref")) {
50 BeanDefinitionBuilder childBuilder = BeanDefinitionBuilder
51 .genericBeanDefinition(RefSpecAttribute.class);
52 addCommonProperties(child, parserContext, childBuilder);
53
54 String targetClassName = child.getAttribute("targetClass");
55 if (StringUtils.hasText(targetClassName))
56 childBuilder.addPropertyValue("targetClass", targetClassName);
57
58 // Choices
59 NodeList choicesNd = child.getElementsByTagName("choices");
60 if (choicesNd.getLength() > 0) {
61 Element choicesElem = (Element) choicesNd.item(0);
62 List choices = DomUtils.getChildElementsByTagName(choicesElem,
63 "choice");
64 ManagedList choiceBeans = new ManagedList(choices.size());
65 for (Element choiceElem : (List<Element>) choices) {
66 BeanDefinitionBuilder choiceBuilder = BeanDefinitionBuilder
67 .genericBeanDefinition(RefValueChoice.class);
68 choiceBuilder.addPropertyValue("name", choiceElem
69 .getAttribute("name"));
70 String desc = choiceElem.getAttribute("description");
71 if (StringUtils.hasText(desc))
72 choiceBuilder.addPropertyValue("description", desc);
73
74 choiceBeans.add(choiceBuilder.getBeanDefinition());
75 }
76
77 }
78
79 putInAttributes(attributes, child,
80 childBuilder.getBeanDefinition(), "ref");
81 }
82
83 builder.addPropertyValue("attributes", attributes);
84 }
85
86 protected void addCommonProperties(Element element,
87 ParserContext parserContext, BeanDefinitionBuilder specAttr) {
88 addBooleanProperty("isParameter", specAttr, element);
89 addBooleanProperty("isFrozen", specAttr, element);
90 addBooleanProperty("isHidden", specAttr, element);
91
92 Object value = NamespaceUtils.parseValue(element, parserContext,
93 specAttr.getBeanDefinition(), "value");
94 if (value != null)
95 specAttr.addPropertyValue("value", value);
96
97 }
98
99 @SuppressWarnings("unchecked")
100 protected void putInAttributes(ManagedMap attributes, Element child,
101 BeanDefinition beanDefinition, String nature) {
102 String name = child.getAttribute("name");
103 attributes.put(name, beanDefinition);
104 if (log.isTraceEnabled())
105 log.debug("Added " + nature + " attribute " + name);
106
107 }
108
109 private void addBooleanProperty(String name,
110 BeanDefinitionBuilder specAttr, Element element) {
111 String bool = element.getAttribute(name);
112 if (StringUtils.hasText(bool))
113 specAttr.addPropertyValue(name, Boolean.parseBoolean(bool));
114
115 }
116
117 @Override
118 protected Class<DefaultExecutionSpec> getBeanClass(Element element) {
119 return DefaultExecutionSpec.class;
120 }
121
122 protected boolean shouldGenerateIdAsFallback() {
123 return false;
124 }
125
126 }