Skip to content

Commit ba200ea

Browse files
committed
I added an extra check on the user's chosen extension in the objectsToExcel method. Now the CSVReader is closed in the csvToExcel method
1 parent d624ced commit ba200ea

File tree

6 files changed

+89
-26
lines changed

6 files changed

+89
-26
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.mbenincasa</groupId>
88
<artifactId>java-excel-utils</artifactId>
9-
<version>0.1.0</version>
9+
<version>0.1.1</version>
1010
<packaging>jar</packaging>
1111
<name>Java library with tools for Excel files</name>
1212
<description>Java library that collects tools and methods to speed up development with Excel sheets</description>

src/main/java/enums/Extension.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,12 @@ public static Extension getExcelExtension(String ext) throws ExtensionNotValidEx
5454
}
5555
return extensionOptional.get();
5656
}
57+
58+
/**
59+
* @return {@code true} if it has type EXCEL
60+
* @since 0.1.1
61+
*/
62+
public Boolean isExcelExtension() {
63+
return this.getType().equalsIgnoreCase("EXCEL");
64+
}
5765
}

src/main/java/tools/implementations/ExcelConverterImpl.java

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ public class ExcelConverterImpl implements ExcelConverter {
3939
* @throws IllegalAccessException {@inheritDoc}
4040
* @throws IOException {@inheritDoc}
4141
* @throws FileAlreadyExistsException {@inheritDoc}
42+
* @throws ExtensionNotValidException {@inheritDoc}
4243
*/
4344
@Override
44-
public File objectsToExcel(List<?> objects, Class<?> clazz) throws IllegalAccessException, IOException, FileAlreadyExistsException {
45+
public File objectsToExcel(List<?> objects, Class<?> clazz) throws IllegalAccessException, IOException, FileAlreadyExistsException, ExtensionNotValidException {
4546
return objectsToExcel(objects, clazz, System.getProperty("java.io.tmpdir"), clazz.getSimpleName(), Extension.XLSX, true);
4647
}
4748

@@ -55,9 +56,10 @@ public File objectsToExcel(List<?> objects, Class<?> clazz) throws IllegalAccess
5556
* @throws IllegalAccessException {@inheritDoc}
5657
* @throws IOException {@inheritDoc}
5758
* @throws FileAlreadyExistsException {@inheritDoc}
59+
* @throws ExtensionNotValidException {@inheritDoc}
5860
*/
5961
@Override
60-
public File objectsToExcel(List<?> objects, Class<?> clazz, String filename) throws IllegalAccessException, IOException, FileAlreadyExistsException {
62+
public File objectsToExcel(List<?> objects, Class<?> clazz, String filename) throws IllegalAccessException, IOException, FileAlreadyExistsException, ExtensionNotValidException {
6163
return objectsToExcel(objects, clazz, System.getProperty("java.io.tmpdir"), filename, Extension.XLSX, true);
6264
}
6365

@@ -72,9 +74,10 @@ public File objectsToExcel(List<?> objects, Class<?> clazz, String filename) thr
7274
* @throws IllegalAccessException {@inheritDoc}
7375
* @throws IOException {@inheritDoc}
7476
* @throws FileAlreadyExistsException {@inheritDoc}
77+
* @throws ExtensionNotValidException {@inheritDoc}
7578
*/
7679
@Override
77-
public File objectsToExcel(List<?> objects, Class<?> clazz, String path, String filename) throws IllegalAccessException, IOException, FileAlreadyExistsException {
80+
public File objectsToExcel(List<?> objects, Class<?> clazz, String path, String filename) throws IllegalAccessException, IOException, FileAlreadyExistsException, ExtensionNotValidException {
7881
return objectsToExcel(objects, clazz, path, filename, Extension.XLSX, true);
7982
}
8083

@@ -90,9 +93,10 @@ public File objectsToExcel(List<?> objects, Class<?> clazz, String path, String
9093
* @throws IllegalAccessException {@inheritDoc}
9194
* @throws IOException {@inheritDoc}
9295
* @throws FileAlreadyExistsException {@inheritDoc}
96+
* @throws ExtensionNotValidException {@inheritDoc}
9397
*/
9498
@Override
95-
public File objectsToExcel(List<?> objects, Class<?> clazz, String path, String filename, Boolean writeHeader) throws IllegalAccessException, IOException, FileAlreadyExistsException {
99+
public File objectsToExcel(List<?> objects, Class<?> clazz, String path, String filename, Boolean writeHeader) throws IllegalAccessException, IOException, FileAlreadyExistsException, ExtensionNotValidException {
96100
return objectsToExcel(objects, clazz, path, filename, Extension.XLSX, writeHeader);
97101
}
98102

@@ -106,9 +110,10 @@ public File objectsToExcel(List<?> objects, Class<?> clazz, String path, String
106110
* @throws IllegalAccessException {@inheritDoc}
107111
* @throws IOException {@inheritDoc}
108112
* @throws FileAlreadyExistsException {@inheritDoc}
113+
* @throws ExtensionNotValidException {@inheritDoc}
109114
*/
110115
@Override
111-
public File objectsToExcel(List<?> objects, Class<?> clazz, Boolean writeHeader) throws IllegalAccessException, IOException, FileAlreadyExistsException {
116+
public File objectsToExcel(List<?> objects, Class<?> clazz, Boolean writeHeader) throws IllegalAccessException, IOException, FileAlreadyExistsException, ExtensionNotValidException {
112117
return objectsToExcel(objects, clazz, System.getProperty("java.io.tmpdir"), clazz.getSimpleName(), Extension.XLSX, writeHeader);
113118
}
114119

@@ -123,9 +128,10 @@ public File objectsToExcel(List<?> objects, Class<?> clazz, Boolean writeHeader)
123128
* @throws IllegalAccessException {@inheritDoc}
124129
* @throws IOException {@inheritDoc}
125130
* @throws FileAlreadyExistsException {@inheritDoc}
131+
* @throws ExtensionNotValidException {@inheritDoc}
126132
*/
127133
@Override
128-
public File objectsToExcel(List<?> objects, Class<?> clazz, String filename, Boolean writeHeader) throws IllegalAccessException, IOException, FileAlreadyExistsException {
134+
public File objectsToExcel(List<?> objects, Class<?> clazz, String filename, Boolean writeHeader) throws IllegalAccessException, IOException, FileAlreadyExistsException, ExtensionNotValidException {
129135
return objectsToExcel(objects, clazz, System.getProperty("java.io.tmpdir"), filename, Extension.XLSX, writeHeader);
130136
}
131137

@@ -141,9 +147,10 @@ public File objectsToExcel(List<?> objects, Class<?> clazz, String filename, Boo
141147
* @throws IllegalAccessException {@inheritDoc}
142148
* @throws IOException {@inheritDoc}
143149
* @throws FileAlreadyExistsException {@inheritDoc}
150+
* @throws ExtensionNotValidException {@inheritDoc}
144151
*/
145152
@Override
146-
public File objectsToExcel(List<?> objects, Class<?> clazz, String path, String filename, Extension extension) throws IllegalAccessException, IOException, FileAlreadyExistsException {
153+
public File objectsToExcel(List<?> objects, Class<?> clazz, String path, String filename, Extension extension) throws IllegalAccessException, IOException, FileAlreadyExistsException, ExtensionNotValidException {
147154
return objectsToExcel(objects, clazz, path, filename, extension, true);
148155
}
149156

@@ -157,9 +164,10 @@ public File objectsToExcel(List<?> objects, Class<?> clazz, String path, String
157164
* @throws IllegalAccessException {@inheritDoc}
158165
* @throws IOException {@inheritDoc}
159166
* @throws FileAlreadyExistsException {@inheritDoc}
167+
* @throws ExtensionNotValidException {@inheritDoc}
160168
*/
161169
@Override
162-
public File objectsToExcel(List<?> objects, Class<?> clazz, Extension extension) throws IllegalAccessException, IOException, FileAlreadyExistsException {
170+
public File objectsToExcel(List<?> objects, Class<?> clazz, Extension extension) throws IllegalAccessException, IOException, FileAlreadyExistsException, ExtensionNotValidException {
163171
return objectsToExcel(objects, clazz, System.getProperty("java.io.tmpdir"), clazz.getSimpleName(), extension, true);
164172
}
165173

@@ -174,9 +182,10 @@ public File objectsToExcel(List<?> objects, Class<?> clazz, Extension extension)
174182
* @throws IllegalAccessException {@inheritDoc}
175183
* @throws IOException {@inheritDoc}
176184
* @throws FileAlreadyExistsException {@inheritDoc}
185+
* @throws ExtensionNotValidException {@inheritDoc}
177186
*/
178187
@Override
179-
public File objectsToExcel(List<?> objects, Class<?> clazz, Extension extension, Boolean writeHeader) throws IllegalAccessException, IOException, FileAlreadyExistsException {
188+
public File objectsToExcel(List<?> objects, Class<?> clazz, Extension extension, Boolean writeHeader) throws IllegalAccessException, IOException, FileAlreadyExistsException, ExtensionNotValidException {
180189
return objectsToExcel(objects, clazz, System.getProperty("java.io.tmpdir"), clazz.getSimpleName(), extension, writeHeader);
181190
}
182191

@@ -191,9 +200,10 @@ public File objectsToExcel(List<?> objects, Class<?> clazz, Extension extension,
191200
* @throws IllegalAccessException {@inheritDoc}
192201
* @throws IOException {@inheritDoc}
193202
* @throws FileAlreadyExistsException {@inheritDoc}
203+
* @throws ExtensionNotValidException {@inheritDoc}
194204
*/
195205
@Override
196-
public File objectsToExcel(List<?> objects, Class<?> clazz, String filename, Extension extension) throws IllegalAccessException, IOException, FileAlreadyExistsException {
206+
public File objectsToExcel(List<?> objects, Class<?> clazz, String filename, Extension extension) throws IllegalAccessException, IOException, FileAlreadyExistsException, ExtensionNotValidException {
197207
return objectsToExcel(objects, clazz, System.getProperty("java.io.tmpdir"), filename, extension, true);
198208
}
199209

@@ -209,9 +219,10 @@ public File objectsToExcel(List<?> objects, Class<?> clazz, String filename, Ext
209219
* @throws IllegalAccessException {@inheritDoc}
210220
* @throws IOException {@inheritDoc}
211221
* @throws FileAlreadyExistsException {@inheritDoc}
222+
* @throws ExtensionNotValidException {@inheritDoc}
212223
*/
213224
@Override
214-
public File objectsToExcel(List<?> objects, Class<?> clazz, String filename, Extension extension, Boolean writeHeader) throws IllegalAccessException, IOException, FileAlreadyExistsException {
225+
public File objectsToExcel(List<?> objects, Class<?> clazz, String filename, Extension extension, Boolean writeHeader) throws IllegalAccessException, IOException, FileAlreadyExistsException, ExtensionNotValidException {
215226
return objectsToExcel(objects, clazz, System.getProperty("java.io.tmpdir"), filename, extension, writeHeader);
216227
}
217228

@@ -227,9 +238,14 @@ public File objectsToExcel(List<?> objects, Class<?> clazz, String filename, Ext
227238
* @throws IllegalAccessException {@inheritDoc}
228239
* @throws IOException {@inheritDoc}
229240
* @throws FileAlreadyExistsException {@inheritDoc}
241+
* @throws ExtensionNotValidException {@inheritDoc}
230242
*/
231243
@Override
232-
public File objectsToExcel(List<?> objects, Class<?> clazz, String path, String filename, Extension extension, Boolean writeHeader) throws IllegalAccessException, IOException, FileAlreadyExistsException {
244+
public File objectsToExcel(List<?> objects, Class<?> clazz, String path, String filename, Extension extension, Boolean writeHeader) throws IllegalAccessException, IOException, FileAlreadyExistsException, ExtensionNotValidException {
245+
/* Check extension*/
246+
if(!extension.isExcelExtension())
247+
throw new ExtensionNotValidException("Select an extension for an Excel file");
248+
233249
/* Open file */
234250
String pathname = this.getPathname(path, filename, extension);
235251
File file = new File(pathname);
@@ -552,7 +568,7 @@ public File csvToExcel(File fileInput, String path, String filename, Extension e
552568
workbook.write(fileOutputStream);
553569

554570
/* Close file */
555-
excelWorkbookUtils.close(workbook, fileOutputStream);
571+
excelWorkbookUtils.close(workbook, fileOutputStream, csvReader);
556572

557573
return outputFile;
558574
}

src/main/java/tools/implementations/ExcelWorkbookUtilsImpl.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package tools.implementations;
22

3+
import com.opencsv.CSVReader;
34
import com.opencsv.CSVWriter;
45
import enums.Extension;
56
import exceptions.ExtensionNotValidException;
@@ -171,4 +172,19 @@ public void close(Workbook workbook, FileInputStream fileInputStream, CSVWriter
171172
fileInputStream.close();
172173
writer.close();
173174
}
175+
176+
/**
177+
* {@inheritDoc}
178+
* @param workbook {@inheritDoc}
179+
* @param fileOutputStream {@inheritDoc}
180+
* @param reader {@inheritDoc}
181+
* @throws IOException {@inheritDoc}
182+
* @since 0.1.1
183+
*/
184+
@Override
185+
public void close(Workbook workbook, FileOutputStream fileOutputStream, CSVReader reader) throws IOException {
186+
workbook.close();
187+
fileOutputStream.close();
188+
reader.close();
189+
}
174190
}

0 commit comments

Comments
 (0)