]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/xml/NamespaceUtils.java
Improve executions and system calls
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / xml / NamespaceUtils.java
1 package org.argeo.slc.core.execution.xml;
2
3 import org.argeo.slc.SlcException;
4 import org.springframework.beans.factory.config.BeanDefinition;
5 import org.springframework.beans.factory.config.RuntimeBeanReference;
6 import org.springframework.beans.factory.xml.ParserContext;
7 import org.springframework.util.xml.DomUtils;
8 import org.w3c.dom.Element;
9 import org.w3c.dom.Node;
10 import org.w3c.dom.NodeList;
11
12 /**
13 * Utilities to simplify common tasks when interpreting a custom namespace and
14 * converting it into bean definitions.
15 */
16 public class NamespaceUtils {
17 /**
18 * Returns the value defined either: directly by the the 'value' attribute,
19 * as reference by the 'ref' attribute or as a nested bean.
20 */
21 public static Object parseValue(Element element,
22 ParserContext parserContext,
23 BeanDefinition containingBeanDefintion, String valueTagName) {
24 Object value = null;
25 if (element.hasAttribute("value")) {
26 value = element.getAttribute("value");
27 }
28
29 if (element.hasAttribute("ref")) {
30 if (value != null)
31 throw new SlcException("Multiple value definition for "
32 + element);
33 value = new RuntimeBeanReference(element.getAttribute("ref"));
34 }
35
36 Element uniqueSubElem = null;
37 if (valueTagName != null) {
38 Element valueElem = DomUtils.getChildElementByTagName(element,
39 valueTagName);
40 if (valueElem != null) {
41 uniqueSubElem = findUniqueSubElement(valueElem);
42 if (uniqueSubElem == null)
43 throw new SlcException("No subelement found under "
44 + valueElem);
45 }
46 } else {// no intermediary tag
47 uniqueSubElem = findUniqueSubElement(element);
48 }
49
50 if (uniqueSubElem != null) {
51 if (value != null)
52 throw new SlcException("Multiple value definition for "
53 + element);
54 value = parseBeanReference(uniqueSubElem, parserContext,
55 containingBeanDefintion);
56 }
57 return value;
58 }
59
60 public static Element findUniqueSubElement(Element element) {
61 NodeList childNodes = element.getChildNodes();
62
63 Element uniqueSubElem = null;
64 for (int i = 0; i < childNodes.getLength(); i++) {
65 Node node = childNodes.item(i);
66 if (node != null && node instanceof Element) {
67 if (uniqueSubElem == null)
68 uniqueSubElem = (Element) node;
69 else
70 throw new SlcException(
71 "There are more than one sub element under "
72 + element);
73 }
74 }
75 return uniqueSubElem;
76 }
77
78 public static Object parseBeanReference(Element element,
79 ParserContext parserContext, BeanDefinition beanDefinition) {
80 return parserContext.getDelegate().parsePropertySubElement(element,
81 beanDefinition);
82 }
83
84 }