All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.redis.riot.file.xml.XmlItemReader Maven / Gradle / Ivy

There is a newer version: 4.1.9
Show newest version
package com.redis.riot.file.xml;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemStreamReader;
import org.springframework.batch.item.file.ResourceAwareItemReaderItemStream;
import org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader;
import org.springframework.core.io.Resource;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

/**
 * {@link ItemStreamReader} implementation that reads XML objects from a {@link Resource} having the following format:
 * 
 * 
 * {@code
 * 
 *    ...
 *    ...
 * 
 * }
 * 
* * The implementation is not thread-safe. * * @param the type of XML objects to read * * @author Julien Ruaux */ public class XmlItemReader extends AbstractItemCountingItemStreamItemReader implements ResourceAwareItemReaderItemStream { private final Logger log = LoggerFactory.getLogger(XmlItemReader.class); private Resource resource; private XmlObjectReader xmlObjectReader; private boolean strict = true; /** * Create a new {@link XmlItemReader} instance. * * @param resource the input XML resource * @param xmlObjectReader the XML object reader to use */ public XmlItemReader(Resource resource, XmlObjectReader xmlObjectReader) { Assert.notNull(resource, "The resource must not be null."); Assert.notNull(xmlObjectReader, "The XML object reader must not be null."); this.resource = resource; this.xmlObjectReader = xmlObjectReader; } /** * Set the {@link XmlObjectReader} to use to read and map XML elements to domain objects. * * @param xmlObjectReader the XML object reader to use */ public void setXmlObjectReader(XmlObjectReader xmlObjectReader) { this.xmlObjectReader = xmlObjectReader; } /** * In strict mode the reader will throw an exception on {@link #open(org.springframework.batch.item.ExecutionContext)} if * the input resource does not exist. * * @param strict true by default */ public void setStrict(boolean strict) { this.strict = strict; } @Override public void setResource(Resource resource) { this.resource = resource; } @Nullable @Override protected T doRead() throws Exception { return xmlObjectReader.read(); } @Override protected void doOpen() throws Exception { if (!this.resource.exists()) { if (this.strict) { throw new IllegalStateException("Input resource must exist (reader is in 'strict' mode)"); } log.warn("Input resource does not exist: {}", resource.getDescription()); return; } if (!this.resource.isReadable()) { if (this.strict) { throw new IllegalStateException("Input resource must be readable (reader is in 'strict' mode)"); } log.warn("Input resource is not readable: {}", resource.getDescription()); return; } this.xmlObjectReader.open(this.resource); } @Override protected void doClose() throws Exception { this.xmlObjectReader.close(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy