2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.argeo
.slc
.core
.execution
.xml
;
19 import java
.util
.List
;
21 import org
.apache
.commons
.logging
.Log
;
22 import org
.apache
.commons
.logging
.LogFactory
;
23 import org
.argeo
.slc
.core
.execution
.DefaultExecutionSpec
;
24 import org
.argeo
.slc
.core
.execution
.PrimitiveSpecAttribute
;
25 import org
.argeo
.slc
.core
.execution
.RefSpecAttribute
;
26 import org
.argeo
.slc
.core
.execution
.RefValueChoice
;
27 import org
.springframework
.beans
.factory
.config
.BeanDefinition
;
28 import org
.springframework
.beans
.factory
.support
.BeanDefinitionBuilder
;
29 import org
.springframework
.beans
.factory
.support
.ManagedList
;
30 import org
.springframework
.beans
.factory
.support
.ManagedMap
;
31 import org
.springframework
.beans
.factory
.xml
.AbstractSingleBeanDefinitionParser
;
32 import org
.springframework
.beans
.factory
.xml
.ParserContext
;
33 import org
.springframework
.util
.StringUtils
;
34 import org
.springframework
.util
.xml
.DomUtils
;
35 import org
.w3c
.dom
.Element
;
37 public class SpecBeanDefinitionParser
extends
38 AbstractSingleBeanDefinitionParser
{
39 private Log log
= LogFactory
.getLog(SpecBeanDefinitionParser
.class);
41 @SuppressWarnings("unchecked")
43 protected void doParse(Element element
, ParserContext parserContext
,
44 BeanDefinitionBuilder builder
) {
45 builder
.getBeanDefinition().setDescription(DomUtils
.getChildElementValueByTagName(element
,
48 ManagedMap attributes
= new ManagedMap();
51 for (Element child
: (List
<Element
>) DomUtils
52 .getChildElementsByTagName(element
, "primitive")) {
53 BeanDefinitionBuilder childBuilder
= BeanDefinitionBuilder
54 .genericBeanDefinition(PrimitiveSpecAttribute
.class);
55 addCommonProperties(child
, parserContext
, childBuilder
);
57 String type
= child
.getAttribute("type");
58 if (StringUtils
.hasText(type
))
59 childBuilder
.addPropertyValue("type", type
);
61 putInAttributes(attributes
, child
,
62 childBuilder
.getBeanDefinition(), "primitive");
66 for (Element refAttrElem
: (List
<Element
>) DomUtils
67 .getChildElementsByTagName(element
, "ref")) {
68 BeanDefinitionBuilder refAttrBuilder
= BeanDefinitionBuilder
69 .genericBeanDefinition(RefSpecAttribute
.class);
70 addCommonProperties(refAttrElem
, parserContext
, refAttrBuilder
);
72 String targetClassName
= refAttrElem
.getAttribute("targetClass");
73 if (StringUtils
.hasText(targetClassName
))
74 refAttrBuilder
.addPropertyValue("targetClass", targetClassName
);
77 Element choicesElem
= DomUtils
.getChildElementByTagName(
78 refAttrElem
, "choices");
79 if (choicesElem
!= null) {
80 List
<Element
> choices
= DomUtils
.getChildElementsByTagName(
81 choicesElem
, "choice");
82 ManagedList choiceBeans
= new ManagedList(choices
.size());
83 for (Element choiceElem
: choices
) {
84 BeanDefinitionBuilder choiceBuilder
= BeanDefinitionBuilder
85 .genericBeanDefinition(RefValueChoice
.class);
86 choiceBuilder
.addPropertyValue("name", choiceElem
87 .getAttribute("name"));
88 String desc
= choiceElem
.getAttribute("description");
89 if (StringUtils
.hasText(desc
))
90 choiceBuilder
.addPropertyValue("description", desc
);
92 choiceBeans
.add(choiceBuilder
.getBeanDefinition());
94 refAttrBuilder
.addPropertyValue("choices", choiceBeans
);
97 putInAttributes(attributes
, refAttrElem
, refAttrBuilder
98 .getBeanDefinition(), "ref");
101 builder
.addPropertyValue("attributes", attributes
);
104 protected void addCommonProperties(Element element
,
105 ParserContext parserContext
, BeanDefinitionBuilder specAttr
) {
106 addBooleanProperty("isParameter", specAttr
, element
);
107 addBooleanProperty("isFrozen", specAttr
, element
);
108 addBooleanProperty("isHidden", specAttr
, element
);
110 Object value
= NamespaceUtils
.parseValue(element
, parserContext
,
111 specAttr
.getBeanDefinition(), "value");
113 specAttr
.addPropertyValue("value", value
);
117 @SuppressWarnings("unchecked")
118 protected void putInAttributes(ManagedMap attributes
, Element child
,
119 BeanDefinition beanDefinition
, String nature
) {
120 String name
= child
.getAttribute("name");
121 attributes
.put(name
, beanDefinition
);
122 if (log
.isTraceEnabled())
123 log
.debug("Added " + nature
+ " attribute " + name
);
127 private void addBooleanProperty(String name
,
128 BeanDefinitionBuilder specAttr
, Element element
) {
129 String bool
= element
.getAttribute(name
);
130 if (StringUtils
.hasText(bool
))
131 specAttr
.addPropertyValue(name
, Boolean
.parseBoolean(bool
));
136 protected Class
<DefaultExecutionSpec
> getBeanClass(Element element
) {
137 return DefaultExecutionSpec
.class;
140 protected boolean shouldGenerateIdAsFallback() {