001/*
002 * Copyright 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 */
016
017package org.springframework.batch.item.xml.stax;
018
019import java.util.LinkedList;
020import java.util.List;
021
022import javax.xml.namespace.QName;
023import javax.xml.stream.XMLEventWriter;
024import javax.xml.stream.XMLStreamException;
025import javax.xml.stream.events.XMLEvent;
026
027/**
028 * Delegating XMLEventWriter, which collects the QNames of elements that were opened but not closed.
029 *
030 * @author Jimmy Praet
031 * @since 3.0
032 */
033public class UnclosedElementCollectingEventWriter extends AbstractEventWriterWrapper {
034
035        private LinkedList<QName> unclosedElements = new LinkedList<QName>();
036
037        public UnclosedElementCollectingEventWriter(XMLEventWriter wrappedEventWriter) {
038                super(wrappedEventWriter);
039        }
040
041        /* (non-Javadoc)
042         * @see org.springframework.batch.item.xml.stax.AbstractEventWriterWrapper#add(javax.xml.stream.events.XMLEvent)
043         */
044        @Override
045        public void add(XMLEvent event) throws XMLStreamException {
046                if (event.isStartElement()) {
047                        unclosedElements.addLast(event.asStartElement().getName());
048                } else if (event.isEndElement()) {
049                        unclosedElements.removeLast();
050                }
051                super.add(event);
052        }
053
054        public List<QName> getUnclosedElements() {
055                return unclosedElements;
056        }
057}