]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/org/argeo/slc/core/execution/xml/NamespaceUtils.java
ccf94f131cd0842d1ab68c0cf0da4b6814e7ddcb
[gpl/argeo-slc.git] / org.argeo.slc.core / src / org / argeo / slc / core / execution / xml / NamespaceUtils.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
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 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 * Utilities to simplify common tasks when interpreting a custom namespace and
30 * converting it into bean definitions.
31 */
32 public class NamespaceUtils {
33 // private final static Log log = LogFactory.getLog(NamespaceUtils.class);
34
35 /**
36 * Returns the value defined either: directly by the the 'value' attribute,
37 * as reference by the 'ref' attribute or as a nested bean.
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 {// no intermediary tag
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 // return parserContext.getDelegate().parsePropertySubElement(element,
99 // beanDefinition);
100
101 BeanDefinitionParserDelegate deleg = parserContext.getDelegate();
102 // if ("bean".equals(element.getNodeName()))
103 // return deleg.parseBeanDefinitionElement(element, beanDefinition);
104 // else
105 return deleg.parsePropertySubElement(element, beanDefinition);
106 }
107 }