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