committed by
Mahmoud Ben Hassine
parent
a88b074c8d
commit
2952e9a039
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2006-2019 the original author or authors.
|
||||
* Copyright 2006-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,6 +18,7 @@ package org.springframework.batch.item.xml;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
@@ -62,6 +63,8 @@ ResourceAwareItemReaderItemStream<T>, InitializingBean {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(StaxEventItemReader.class);
|
||||
|
||||
public static final String DEFAULT_ENCODING = Charset.defaultCharset().name();
|
||||
|
||||
private FragmentEventReader fragmentReader;
|
||||
|
||||
private XMLEventReader eventReader;
|
||||
@@ -80,6 +83,8 @@ ResourceAwareItemReaderItemStream<T>, InitializingBean {
|
||||
|
||||
private XMLInputFactory xmlInputFactory = StaxUtils.createXmlInputFactory();
|
||||
|
||||
private String encoding = DEFAULT_ENCODING;
|
||||
|
||||
public StaxEventItemReader() {
|
||||
setName(ClassUtils.getShortName(StaxEventItemReader.class));
|
||||
}
|
||||
@@ -131,6 +136,16 @@ ResourceAwareItemReaderItemStream<T>, InitializingBean {
|
||||
this.xmlInputFactory = xmlInputFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set encoding to be used for the input file. Defaults to {@link #DEFAULT_ENCODING}.
|
||||
*
|
||||
* @param encoding the encoding to be used
|
||||
*/
|
||||
public void setEncoding(String encoding) {
|
||||
Assert.notNull(encoding, "The encoding must not be null");
|
||||
this.encoding = encoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that all required dependencies for the ItemReader to run are provided after all properties have been set.
|
||||
*
|
||||
@@ -221,7 +236,7 @@ ResourceAwareItemReaderItemStream<T>, InitializingBean {
|
||||
}
|
||||
|
||||
inputStream = resource.getInputStream();
|
||||
eventReader = xmlInputFactory.createXMLEventReader(inputStream);
|
||||
eventReader = xmlInputFactory.createXMLEventReader(inputStream, this.encoding);
|
||||
fragmentReader = new DefaultFragmentEventReader(eventReader);
|
||||
noInput = false;
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2017-2019 the original author or authors.
|
||||
* Copyright 2017-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -33,6 +33,7 @@ import org.springframework.util.StringUtils;
|
||||
*
|
||||
* @author Michael Minella
|
||||
* @author Glenn Renfro
|
||||
* @author Mahmoud Ben Hassine
|
||||
* @since 4.0
|
||||
*/
|
||||
public class StaxEventItemReaderBuilder<T> {
|
||||
@@ -55,6 +56,8 @@ public class StaxEventItemReaderBuilder<T> {
|
||||
|
||||
private XMLInputFactory xmlInputFactory = StaxUtils.createXmlInputFactory();
|
||||
|
||||
private String encoding = StaxEventItemReader.DEFAULT_ENCODING;
|
||||
|
||||
/**
|
||||
* Configure if the state of the {@link org.springframework.batch.item.ItemStreamSupport}
|
||||
* should be persisted within the {@link org.springframework.batch.item.ExecutionContext}
|
||||
@@ -193,6 +196,19 @@ public class StaxEventItemReaderBuilder<T> {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encoding for the input file. Defaults to {@link StaxEventItemReader#DEFAULT_ENCODING}.
|
||||
*
|
||||
* @param encoding String encoding algorithm
|
||||
* @return the current instance of the builder
|
||||
* @see StaxEventItemReader#setEncoding(String)
|
||||
*/
|
||||
public StaxEventItemReaderBuilder<T> encoding(String encoding) {
|
||||
this.encoding = encoding;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the configuration and builds a new {@link StaxEventItemReader}
|
||||
*
|
||||
@@ -222,6 +238,7 @@ public class StaxEventItemReaderBuilder<T> {
|
||||
reader.setCurrentItemCount(this.currentItemCount);
|
||||
reader.setMaxItemCount(this.maxItemCount);
|
||||
reader.setXmlInputFactory(this.xmlInputFactory);
|
||||
reader.setEncoding(this.encoding);
|
||||
|
||||
return reader;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user