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.io.IOException;
020import java.io.Writer;
021import java.util.LinkedList;
022import java.util.List;
023
024import javax.xml.namespace.QName;
025import javax.xml.stream.XMLEventWriter;
026import javax.xml.stream.XMLStreamException;
027import javax.xml.stream.events.XMLEvent;
028
029import org.springframework.dao.DataAccessResourceFailureException;
030import org.springframework.util.StringUtils;
031
032/**
033 * Delegating XMLEventWriter, which writes EndElement events that match a given collection of QNames directly
034 * to the underlying java.io.Writer instead of to the delegate XMLEventWriter.
035 *
036 * @author Jimmy Praet
037 * @since 3.0
038 */
039public class UnopenedElementClosingEventWriter extends AbstractEventWriterWrapper {
040
041        private LinkedList<QName> unopenedElements;
042
043        private Writer ioWriter;
044
045        public UnopenedElementClosingEventWriter(XMLEventWriter wrappedEventWriter, Writer ioWriter, List<QName> unopenedElements) {
046                super(wrappedEventWriter);
047                this.unopenedElements = new LinkedList<QName>(unopenedElements);
048                this.ioWriter = ioWriter;
049        }
050
051        /* (non-Javadoc)
052         * @see org.springframework.batch.item.xml.stax.AbstractEventWriterWrapper#add(javax.xml.stream.events.XMLEvent)
053         */
054        @Override
055        public void add(XMLEvent event) throws XMLStreamException {
056                if (isUnopenedElementCloseEvent(event)) {
057                        QName element = unopenedElements.removeLast();
058                        String nsPrefix = !StringUtils.hasText(element.getPrefix()) ? "" : element.getPrefix() + ":";
059                        try {
060                                super.flush();
061                                ioWriter.write("</" + nsPrefix + element.getLocalPart() + ">");
062                                ioWriter.flush();
063                        }
064                        catch (IOException ioe) {
065                                throw new DataAccessResourceFailureException("Unable to close tag: " + element, ioe);
066                        }
067                } else {
068                        super.add(event);
069                }
070        }
071
072        private boolean isUnopenedElementCloseEvent(XMLEvent event) {
073                if (unopenedElements.isEmpty()) {
074                        return false;
075                } else if (!event.isEndElement()) {
076                        return false;
077                } else {
078                        return unopenedElements.getLast().equals(event.asEndElement().getName());
079                }
080        }
081
082}