1 package org
.argeo
.slc
.core
.execution
.xml
;
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
;
21 public class SpecBeanDefinitionParser
extends
22 AbstractSingleBeanDefinitionParser
{
23 private Log log
= LogFactory
.getLog(SpecBeanDefinitionParser
.class);
25 @SuppressWarnings("unchecked")
27 protected void doParse(Element element
, ParserContext parserContext
,
28 BeanDefinitionBuilder builder
) {
29 builder
.getBeanDefinition().setDescription(DomUtils
.getChildElementValueByTagName(element
,
32 ManagedMap attributes
= new ManagedMap();
35 for (Element child
: (List
<Element
>) DomUtils
36 .getChildElementsByTagName(element
, "primitive")) {
37 BeanDefinitionBuilder childBuilder
= BeanDefinitionBuilder
38 .genericBeanDefinition(PrimitiveSpecAttribute
.class);
39 addCommonProperties(child
, parserContext
, childBuilder
);
41 String type
= child
.getAttribute("type");
42 if (StringUtils
.hasText(type
))
43 childBuilder
.addPropertyValue("type", type
);
45 putInAttributes(attributes
, child
,
46 childBuilder
.getBeanDefinition(), "primitive");
50 for (Element refAttrElem
: (List
<Element
>) DomUtils
51 .getChildElementsByTagName(element
, "ref")) {
52 BeanDefinitionBuilder refAttrBuilder
= BeanDefinitionBuilder
53 .genericBeanDefinition(RefSpecAttribute
.class);
54 addCommonProperties(refAttrElem
, parserContext
, refAttrBuilder
);
56 String targetClassName
= refAttrElem
.getAttribute("targetClass");
57 if (StringUtils
.hasText(targetClassName
))
58 refAttrBuilder
.addPropertyValue("targetClass", targetClassName
);
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
);
76 choiceBeans
.add(choiceBuilder
.getBeanDefinition());
78 refAttrBuilder
.addPropertyValue("choices", choiceBeans
);
81 putInAttributes(attributes
, refAttrElem
, refAttrBuilder
82 .getBeanDefinition(), "ref");
85 builder
.addPropertyValue("attributes", attributes
);
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
);
94 Object value
= NamespaceUtils
.parseValue(element
, parserContext
,
95 specAttr
.getBeanDefinition(), "value");
97 specAttr
.addPropertyValue("value", value
);
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
);
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
));
120 protected Class
<DefaultExecutionSpec
> getBeanClass(Element element
) {
121 return DefaultExecutionSpec
.class;
124 protected boolean shouldGenerateIdAsFallback() {