001: /*
002: * Copyright 2004-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.springframework.webflow.config;
017:
018: import java.util.Iterator;
019: import java.util.List;
020: import java.util.Map;
021:
022: import org.springframework.beans.factory.config.MapFactoryBean;
023: import org.springframework.beans.factory.config.TypedStringValue;
024: import org.springframework.beans.factory.support.BeanDefinitionBuilder;
025: import org.springframework.beans.factory.support.ManagedMap;
026: import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
027: import org.springframework.beans.factory.xml.BeanDefinitionParser;
028: import org.springframework.util.StringUtils;
029: import org.springframework.util.xml.DomUtils;
030: import org.springframework.webflow.engine.support.ApplicationViewSelector;
031: import org.w3c.dom.Element;
032:
033: /**
034: * {@link BeanDefinitionParser} for the <code><execution-attributes></code> tag.
035: *
036: * @author Ben Hale
037: */
038: class ExecutionAttributesBeanDefinitionParser extends
039: AbstractSingleBeanDefinitionParser {
040:
041: // elements and attributes
042:
043: private static final String ATTRIBUTE_ELEMENT = "attribute";
044:
045: private static final String NAME_ATTRIBUTE = "name";
046:
047: private static final String TYPE_ATTRIBUTE = "type";
048:
049: private static final String VALUE_ATTRIBUTE = "value";
050:
051: // properties
052:
053: private static final String SOURCE_MAP_PROPERTY = "sourceMap";
054:
055: protected Class getBeanClass(Element element) {
056: return MapFactoryBean.class;
057: }
058:
059: protected void doParse(Element element,
060: BeanDefinitionBuilder definitionBuilder) {
061: List attributeElements = DomUtils.getChildElementsByTagName(
062: element, ATTRIBUTE_ELEMENT);
063: Map attributeMap = new ManagedMap(attributeElements.size());
064: putAttributes(attributeMap, attributeElements);
065: putSpecialAttributes(attributeMap, element);
066: definitionBuilder.addPropertyValue(SOURCE_MAP_PROPERTY,
067: attributeMap);
068: }
069:
070: /**
071: * Add all attributes defined in given list of attribute elements to given map.
072: */
073: private void putAttributes(Map attributeMap, List attributeElements) {
074: for (Iterator i = attributeElements.iterator(); i.hasNext();) {
075: Element attributeElement = (Element) i.next();
076: String type = attributeElement.getAttribute(TYPE_ATTRIBUTE);
077: Object value;
078: if (StringUtils.hasText(type)) {
079: value = new TypedStringValue(attributeElement
080: .getAttribute(VALUE_ATTRIBUTE), type);
081: } else {
082: value = attributeElement.getAttribute(VALUE_ATTRIBUTE);
083: }
084: attributeMap.put(attributeElement
085: .getAttribute(NAME_ATTRIBUTE), value);
086: }
087: }
088:
089: /**
090: * Add all non-generic (special) attributes defined in given element
091: * to given map.
092: */
093: private void putSpecialAttributes(Map attributeMap, Element element) {
094: putAlwaysRedirectOnPauseAttribute(
095: attributeMap,
096: DomUtils
097: .getChildElementByTagName(
098: element,
099: ApplicationViewSelector.ALWAYS_REDIRECT_ON_PAUSE_ATTRIBUTE));
100: }
101:
102: /**
103: * Parse the "alwaysRedirectOnPause" attribute from given element and
104: * add it to given map.
105: */
106: private void putAlwaysRedirectOnPauseAttribute(Map attributeMap,
107: Element element) {
108: if (element != null) {
109: Boolean value = Boolean.valueOf(element
110: .getAttribute(VALUE_ATTRIBUTE));
111: attributeMap
112: .put(
113: ApplicationViewSelector.ALWAYS_REDIRECT_ON_PAUSE_ATTRIBUTE,
114: value);
115: }
116: }
117: }
|