001/*
002 * Copyright 2013-2014 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.List;
019
020import org.springframework.batch.core.step.item.ForceRollbackForWriteSkipException;
021import org.springframework.beans.factory.config.TypedStringValue;
022import org.springframework.beans.factory.support.ManagedMap;
023import org.springframework.beans.factory.xml.ParserContext;
024import org.springframework.util.xml.DomUtils;
025import org.w3c.dom.Element;
026
027public class ExceptionElementParser {
028
029        public ManagedMap<TypedStringValue, Boolean> parse(Element element, ParserContext parserContext, String exceptionListName) {
030                List<Element> children = DomUtils.getChildElementsByTagName(element, exceptionListName);
031                if (children.size() == 1) {
032                        ManagedMap<TypedStringValue, Boolean> map = new ManagedMap<TypedStringValue, Boolean>();
033                        Element exceptionClassesElement = children.get(0);
034                        addExceptionClasses("include", true, exceptionClassesElement, map, parserContext);
035                        addExceptionClasses("exclude", false, exceptionClassesElement, map, parserContext);
036                        map.put(new TypedStringValue(ForceRollbackForWriteSkipException.class.getName(), Class.class), true);
037                        return map;
038                }
039                else if (children.size() > 1) {
040                        parserContext.getReaderContext().error(
041                                        "The <" + exceptionListName + "/> element may not appear more than once in a single <"
042                                                        + element.getNodeName() + "/>.", element);
043                }
044                return null;
045        }
046
047        private void addExceptionClasses(String elementName, boolean include, Element exceptionClassesElement,
048                        ManagedMap<TypedStringValue, Boolean> map, ParserContext parserContext) {
049                for (Element child : DomUtils.getChildElementsByTagName(exceptionClassesElement, elementName)) {
050                        String className = child.getAttribute("class");
051                        map.put(new TypedStringValue(className, Class.class), include);
052                }
053        }
054}