2 * Copyright (C) 2007-2012 Argeo GmbH
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.
16 package org
.argeo
.slc
.core
.execution
.xml
;
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
;
29 * Utilities to simplify common tasks when interpreting a custom namespace and
30 * converting it into bean definitions.
32 public class NamespaceUtils
{
33 // private final static Log log = LogFactory.getLog(NamespaceUtils.class);
36 * Returns the value defined either: directly by the the 'value' attribute,
37 * as reference by the 'ref' attribute or as a nested bean.
39 public static Object
parseValue(Element element
,
40 ParserContext parserContext
,
41 BeanDefinition containingBeanDefintion
, String valueTagName
) {
43 if (element
.hasAttribute("value")) {
44 value
= element
.getAttribute("value");
47 if (element
.hasAttribute("ref")) {
49 throw new SlcException("Multiple value definition for "
51 value
= new RuntimeBeanReference(element
.getAttribute("ref"));
54 Element uniqueSubElem
= null;
55 if (valueTagName
!= null) {
56 Element valueElem
= DomUtils
.getChildElementByTagName(element
,
58 if (valueElem
!= null) {
59 uniqueSubElem
= findUniqueSubElement(valueElem
);
60 if (uniqueSubElem
== null)
61 throw new SlcException("No subelement found under "
64 } else {// no intermediary tag
65 uniqueSubElem
= findUniqueSubElement(element
);
68 if (uniqueSubElem
!= null) {
70 throw new SlcException("Multiple value definition for "
72 value
= parseBeanOrReference(uniqueSubElem
, parserContext
,
73 containingBeanDefintion
);
78 public static Element
findUniqueSubElement(Element element
) {
79 NodeList childNodes
= element
.getChildNodes();
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
;
88 throw new SlcException(
89 "There are more than one sub element under "
96 public static Object
parseBeanOrReference(Element element
,
97 ParserContext parserContext
, BeanDefinition beanDefinition
) {
98 // return parserContext.getDelegate().parsePropertySubElement(element,
101 BeanDefinitionParserDelegate deleg
= parserContext
.getDelegate();
102 // if ("bean".equals(element.getNodeName()))
103 // return deleg.parseBeanDefinitionElement(element, beanDefinition);
105 return deleg
.parsePropertySubElement(element
, beanDefinition
);