1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.argeo.slc.core.execution.xml;
17
18 import org.argeo.slc.SlcException;
19 import org.springframework.beans.factory.config.BeanDefinition;
20 import org.springframework.beans.factory.config.RuntimeBeanReference;
21 import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate;
22 import org.springframework.beans.factory.xml.ParserContext;
23 import org.springframework.util.xml.DomUtils;
24 import org.w3c.dom.Element;
25 import org.w3c.dom.Node;
26 import org.w3c.dom.NodeList;
27
28
29
30
31
32 public class NamespaceUtils {
33
34
35
36
37
38
39 public static Object parseValue(Element element,
40 ParserContext parserContext,
41 BeanDefinition containingBeanDefintion, String valueTagName) {
42 Object value = null;
43 if (element.hasAttribute("value")) {
44 value = element.getAttribute("value");
45 }
46
47 if (element.hasAttribute("ref")) {
48 if (value != null)
49 throw new SlcException("Multiple value definition for "
50 + element);
51 value = new RuntimeBeanReference(element.getAttribute("ref"));
52 }
53
54 Element uniqueSubElem = null;
55 if (valueTagName != null) {
56 Element valueElem = DomUtils.getChildElementByTagName(element,
57 valueTagName);
58 if (valueElem != null) {
59 uniqueSubElem = findUniqueSubElement(valueElem);
60 if (uniqueSubElem == null)
61 throw new SlcException("No subelement found under "
62 + valueElem);
63 }
64 } else {
65 uniqueSubElem = findUniqueSubElement(element);
66 }
67
68 if (uniqueSubElem != null) {
69 if (value != null)
70 throw new SlcException("Multiple value definition for "
71 + element);
72 value = parseBeanOrReference(uniqueSubElem, parserContext,
73 containingBeanDefintion);
74 }
75 return value;
76 }
77
78 public static Element findUniqueSubElement(Element element) {
79 NodeList childNodes = element.getChildNodes();
80
81 Element uniqueSubElem = null;
82 for (int i = 0; i < childNodes.getLength(); i++) {
83 Node node = childNodes.item(i);
84 if (node != null && node instanceof Element) {
85 if (uniqueSubElem == null)
86 uniqueSubElem = (Element) node;
87 else
88 throw new SlcException(
89 "There are more than one sub element under "
90 + element);
91 }
92 }
93 return uniqueSubElem;
94 }
95
96 public static Object parseBeanOrReference(Element element,
97 ParserContext parserContext, BeanDefinition beanDefinition) {
98
99
100
101 BeanDefinitionParserDelegate deleg = parserContext.getDelegate();
102
103
104
105 return deleg.parsePropertySubElement(element, beanDefinition);
106 }
107 }