]> 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 system calls
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / xml / NamespaceUtils.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
17 package org.argeo.slc.core.execution.xml;
18
19 import org.argeo.slc.SlcException;
20 import org.springframework.beans.factory.config.BeanDefinition;
21 import org.springframework.beans.factory.config.RuntimeBeanReference;
22 import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate;
23 import org.springframework.beans.factory.xml.ParserContext;
24 import org.springframework.util.xml.DomUtils;
25 import org.w3c.dom.Element;
26 import org.w3c.dom.Node;
27 import org.w3c.dom.NodeList;
28
29 /**
30 * Utilities to simplify common tasks when interpreting a custom namespace and
31 * converting it into bean definitions.
32 */
33 public class NamespaceUtils {
34 // private final static Log log = LogFactory.getLog(NamespaceUtils.class);
35
36 /**
37 * Returns the value defined either: directly by the the 'value' attribute,
38 * as reference by the 'ref' attribute or as a nested bean.
39 */
40 public static Object parseValue(Element element,
41 ParserContext parserContext,
42 BeanDefinition containingBeanDefintion, String valueTagName) {
43 Object value = null;
44 if (element.hasAttribute("value")) {
45 value = element.getAttribute("value");
46 }
47
48 if (element.hasAttribute("ref")) {
49 if (value != null)
50 throw new SlcException("Multiple value definition for "
51 + element);
52 value = new RuntimeBeanReference(element.getAttribute("ref"));
53 }
54
55 Element uniqueSubElem = null;
56 if (valueTagName != null) {
57 Element valueElem = DomUtils.getChildElementByTagName(element,
58 valueTagName);
59 if (valueElem != null) {
60 uniqueSubElem = findUniqueSubElement(valueElem);
61 if (uniqueSubElem == null)
62 throw new SlcException("No subelement found under "
63 + valueElem);
64 }
65 } else {// no intermediary tag
66 uniqueSubElem = findUniqueSubElement(element);
67 }
68
69 if (uniqueSubElem != null) {
70 if (value != null)
71 throw new SlcException("Multiple value definition for "
72 + element);
73 value = parseBeanOrReference(uniqueSubElem, parserContext,
74 containingBeanDefintion);
75 }
76 return value;
77 }
78
79 public static Element findUniqueSubElement(Element element) {
80 NodeList childNodes = element.getChildNodes();
81
82 Element uniqueSubElem = null;
83 for (int i = 0; i < childNodes.getLength(); i++) {
84 Node node = childNodes.item(i);
85 if (node != null && node instanceof Element) {
86 if (uniqueSubElem == null)
87 uniqueSubElem = (Element) node;
88 else
89 throw new SlcException(
90 "There are more than one sub element under "
91 + element);
92 }
93 }
94 return uniqueSubElem;
95 }
96
97 public static Object parseBeanOrReference(Element element,
98 ParserContext parserContext, BeanDefinition beanDefinition) {
99 // return parserContext.getDelegate().parsePropertySubElement(element,
100 // beanDefinition);
101
102 BeanDefinitionParserDelegate deleg = parserContext.getDelegate();
103 // if ("bean".equals(element.getNodeName()))
104 // return deleg.parseBeanDefinitionElement(element, beanDefinition);
105 // else
106 return deleg.parsePropertySubElement(element, beanDefinition);
107 }
108 }