2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.core
.execution
.xml
;
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
;
30 * Utilities to simplify common tasks when interpreting a custom namespace and
31 * converting it into bean definitions.
33 public class NamespaceUtils
{
34 // private final static Log log = LogFactory.getLog(NamespaceUtils.class);
37 * Returns the value defined either: directly by the the 'value' attribute,
38 * as reference by the 'ref' attribute or as a nested bean.
40 public static Object
parseValue(Element element
,
41 ParserContext parserContext
,
42 BeanDefinition containingBeanDefintion
, String valueTagName
) {
44 if (element
.hasAttribute("value")) {
45 value
= element
.getAttribute("value");
48 if (element
.hasAttribute("ref")) {
50 throw new SlcException("Multiple value definition for "
52 value
= new RuntimeBeanReference(element
.getAttribute("ref"));
55 Element uniqueSubElem
= null;
56 if (valueTagName
!= null) {
57 Element valueElem
= DomUtils
.getChildElementByTagName(element
,
59 if (valueElem
!= null) {
60 uniqueSubElem
= findUniqueSubElement(valueElem
);
61 if (uniqueSubElem
== null)
62 throw new SlcException("No subelement found under "
65 } else {// no intermediary tag
66 uniqueSubElem
= findUniqueSubElement(element
);
69 if (uniqueSubElem
!= null) {
71 throw new SlcException("Multiple value definition for "
73 value
= parseBeanOrReference(uniqueSubElem
, parserContext
,
74 containingBeanDefintion
);
79 public static Element
findUniqueSubElement(Element element
) {
80 NodeList childNodes
= element
.getChildNodes();
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
;
89 throw new SlcException(
90 "There are more than one sub element under "
97 public static Object
parseBeanOrReference(Element element
,
98 ParserContext parserContext
, BeanDefinition beanDefinition
) {
99 // return parserContext.getDelegate().parsePropertySubElement(element,
102 BeanDefinitionParserDelegate deleg
= parserContext
.getDelegate();
103 // if ("bean".equals(element.getNodeName()))
104 // return deleg.parseBeanDefinitionElement(element, beanDefinition);
106 return deleg
.parsePropertySubElement(element
, beanDefinition
);