| 1 | /* |
| 2 | * Copyright 2006-2008 the original author or authors. |
| 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.springframework.batch.core.configuration.xml; |
| 17 | |
| 18 | import java.util.Collection; |
| 19 | |
| 20 | import org.springframework.beans.MutablePropertyValues; |
| 21 | import org.springframework.beans.factory.config.BeanDefinition; |
| 22 | import org.springframework.beans.factory.support.AbstractBeanDefinition; |
| 23 | import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
| 24 | import org.springframework.beans.factory.support.GenericBeanDefinition; |
| 25 | import org.springframework.beans.factory.xml.ParserContext; |
| 26 | import org.w3c.dom.Element; |
| 27 | |
| 28 | /** |
| 29 | * Internal parser for the <flow/> elements inside a job.. |
| 30 | * |
| 31 | * @see JobParser |
| 32 | * |
| 33 | * @author Dave Syer |
| 34 | * |
| 35 | */ |
| 36 | public class FlowElementParser { |
| 37 | |
| 38 | private static final String ID_ATTR = "id"; |
| 39 | |
| 40 | private static final String REF_ATTR = "parent"; |
| 41 | |
| 42 | /** |
| 43 | * Parse the flow and turn it into a list of transitions. |
| 44 | * |
| 45 | * @param element the <flow/gt; element to parse |
| 46 | * @param parserContext the parser context for the bean factory |
| 47 | * @return a collection of bean definitions for |
| 48 | * {@link org.springframework.batch.core.job.flow.support.StateTransition} |
| 49 | * instances objects |
| 50 | */ |
| 51 | public Collection<BeanDefinition> parse(Element element, ParserContext parserContext) { |
| 52 | |
| 53 | String refAttribute = element.getAttribute(REF_ATTR); |
| 54 | String idAttribute = element.getAttribute(ID_ATTR); |
| 55 | |
| 56 | BeanDefinitionBuilder stateBuilder = BeanDefinitionBuilder |
| 57 | .genericBeanDefinition("org.springframework.batch.core.job.flow.support.state.FlowState"); |
| 58 | |
| 59 | AbstractBeanDefinition flowDefinition = new GenericBeanDefinition(); |
| 60 | flowDefinition.setParentName(refAttribute); |
| 61 | MutablePropertyValues propertyValues = flowDefinition.getPropertyValues(); |
| 62 | propertyValues.addPropertyValue("name", idAttribute); |
| 63 | stateBuilder.addConstructorArgValue(flowDefinition); |
| 64 | stateBuilder.addConstructorArgValue(idAttribute); |
| 65 | return InlineFlowParser.getNextElements(parserContext, stateBuilder.getBeanDefinition(), element); |
| 66 | |
| 67 | } |
| 68 | } |