001/*
002 * Copyright 2006-2008 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 *      https://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 */
016package org.springframework.batch.core.configuration.xml;
017
018import java.util.Collection;
019
020import org.springframework.beans.factory.config.BeanDefinition;
021import org.springframework.beans.factory.config.RuntimeBeanReference;
022import org.springframework.beans.factory.support.BeanDefinitionBuilder;
023import org.springframework.beans.factory.xml.ParserContext;
024import org.w3c.dom.Element;
025
026/**
027 * Internal parser for the <decision/> elements inside a job. A decision
028 * element references a bean definition for a 
029 * {@link org.springframework.batch.core.job.flow.JobExecutionDecider} 
030 * and goes on to list a set of transitions to other states with <next
031 * on="pattern" to="stepName"/>. Used by the {@link JobParser}.
032 * 
033 * @see JobParser
034 * 
035 * @author Dave Syer
036 * 
037 */
038public class DecisionParser {
039
040        /**
041         * Parse the decision and turn it into a list of transitions.
042         * 
043         * @param element the <decision/gt; element to parse
044         * @param parserContext the parser context for the bean factory
045         * @return a collection of bean definitions for 
046         * {@link org.springframework.batch.core.job.flow.support.StateTransition}
047         * instances objects
048         */
049        public Collection<BeanDefinition> parse(Element element, ParserContext parserContext) {
050
051                String refAttribute = element.getAttribute("decider");
052                String idAttribute = element.getAttribute("id");
053
054                BeanDefinitionBuilder stateBuilder = 
055                        BeanDefinitionBuilder.genericBeanDefinition("org.springframework.batch.core.job.flow.support.state.DecisionState");
056                stateBuilder.addConstructorArgValue(new RuntimeBeanReference(refAttribute));
057                stateBuilder.addConstructorArgValue(idAttribute);
058                return InlineFlowParser.getNextElements(parserContext, stateBuilder.getBeanDefinition(), element);
059
060        }
061}