Skip to content

Commit 3af1b6c

Browse files
URLs to xlsx files as well as local file paths are supported now +
bugfixes
1 parent d304500 commit 3af1b6c

File tree

4 files changed

+77
-8
lines changed

4 files changed

+77
-8
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ repositories {
1515

1616
dependencies {
1717
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.3'
18+
testImplementation 'org.junit.platform:junit-platform-launcher:1.9.3'
1819
}
1920

2021
sourceSets {

src/com/inet/excel/ExcelDriver.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515
*/
1616
package com.inet.excel;
1717

18+
import java.io.File;
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.net.URL;
1822
import java.nio.file.Files;
1923
import java.nio.file.Path;
2024
import java.nio.file.Paths;
25+
import java.nio.file.StandardCopyOption;
2126
import java.sql.Connection;
2227
import java.sql.Driver;
2328
import java.sql.DriverPropertyInfo;
@@ -82,6 +87,33 @@ public Connection connect( String url, Properties info ) throws SQLException {
8287
throw new SQLException( "Excel file is not specified" );
8388
}
8489

90+
String lowerCasedFilePath = filePath.toLowerCase();
91+
String fileProtocol = "file:";
92+
if( lowerCasedFilePath.startsWith( fileProtocol ) ) {
93+
try {
94+
URL fileURL = new URL( filePath );
95+
String authority = fileURL.getAuthority();
96+
if( authority == null || authority.isEmpty() ) {
97+
filePath = new File( fileURL.toURI() ).toString();
98+
} else {
99+
filePath = fileURL.getPath();
100+
if( filePath.startsWith( "/" ) ) {
101+
filePath = filePath.substring( 1 );
102+
}
103+
}
104+
} catch( Exception e ) {
105+
filePath = filePath.substring( fileProtocol.length() );
106+
}
107+
} else if( lowerCasedFilePath.indexOf( ':' ) > 1 ) {
108+
try (InputStream in = new URL( filePath ).openStream()) {
109+
Path tempFile = Files.createTempFile( null, null ).toAbsolutePath();
110+
Files.copy( in, tempFile, StandardCopyOption.REPLACE_EXISTING );
111+
filePath = tempFile.toString();
112+
} catch( IOException e ) {
113+
throw new SQLException( "An error occurred while accessing the file", e );
114+
}
115+
}
116+
85117
Path file = Paths.get( filePath ).toAbsolutePath();
86118
if( !Files.isRegularFile( file ) ) {
87119
throw new SQLException( "Specified Excel file does not exist" );

src/com/inet/excel/ExcelResultSet.java

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,55 +145,83 @@ public String getString( int columnIndex ) throws SQLException {
145145
*/
146146
@Override
147147
public boolean getBoolean( int columnIndex ) throws SQLException {
148-
return getValue( columnIndex );
148+
Boolean value = getValue( columnIndex );
149+
if( value == null ) {
150+
return false;
151+
}
152+
return value.booleanValue();
149153
}
150154

151155
/**
152156
* {@inheritDoc}
153157
*/
154158
@Override
155159
public byte getByte( int columnIndex ) throws SQLException {
156-
return getValue( columnIndex );
160+
Number value = getValue( columnIndex );
161+
if( value == null ) {
162+
return 0;
163+
}
164+
return value.byteValue();
157165
}
158166

159167
/**
160168
* {@inheritDoc}
161169
*/
162170
@Override
163171
public short getShort( int columnIndex ) throws SQLException {
164-
return getValue( columnIndex );
172+
Number value = getValue( columnIndex );
173+
if( value == null ) {
174+
return 0;
175+
}
176+
return value.shortValue();
165177
}
166178

167179
/**
168180
* {@inheritDoc}
169181
*/
170182
@Override
171183
public int getInt( int columnIndex ) throws SQLException {
172-
return getValue( columnIndex );
184+
Number value = getValue( columnIndex );
185+
if( value == null ) {
186+
return 0;
187+
}
188+
return value.intValue();
173189
}
174190

175191
/**
176192
* {@inheritDoc}
177193
*/
178194
@Override
179195
public long getLong( int columnIndex ) throws SQLException {
180-
return getValue( columnIndex );
196+
Number value = getValue( columnIndex );
197+
if( value == null ) {
198+
return 0;
199+
}
200+
return value.longValue();
181201
}
182202

183203
/**
184204
* {@inheritDoc}
185205
*/
186206
@Override
187207
public float getFloat( int columnIndex ) throws SQLException {
188-
return getValue( columnIndex );
208+
Number value = getValue( columnIndex );
209+
if( value == null ) {
210+
return 0;
211+
}
212+
return value.floatValue();
189213
}
190214

191215
/**
192216
* {@inheritDoc}
193217
*/
194218
@Override
195219
public double getDouble( int columnIndex ) throws SQLException {
196-
return getValue( columnIndex );
220+
Number value = getValue( columnIndex );
221+
if( value == null ) {
222+
return 0;
223+
}
224+
return value.doubleValue();
197225
}
198226

199227
/**

test/com/inet/excel/ExcelDriverTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,15 @@ public void connect_throws_exception_if_path_to_excel_file_does_not_represent_ex
7777
public void connect_returns_connection_to_specified_existing_file() throws IOException, SQLException {
7878
Path file = Files.createTempFile( "ExcelDriverTest_" + UUID.randomUUID(), ".xlsx" );
7979
assertTrue( Files.isRegularFile( file ) ); // precondition check
80-
String urlWithPathToExistingFile = ExcelDriver.URL_PREFIX + file.toAbsolutePath().toString();
80+
String fileAbsolutePath = file.toAbsolutePath().toString();
81+
82+
String urlWithPathToExistingFile = ExcelDriver.URL_PREFIX + fileAbsolutePath;
83+
assertNotNull( newDriver().connect( urlWithPathToExistingFile, new Properties() ) );
84+
85+
urlWithPathToExistingFile = ExcelDriver.URL_PREFIX + "file:///" + fileAbsolutePath;
86+
assertNotNull( newDriver().connect( urlWithPathToExistingFile, new Properties() ) );
87+
88+
urlWithPathToExistingFile = ExcelDriver.URL_PREFIX + "file://localhost/" + fileAbsolutePath;
8189
assertNotNull( newDriver().connect( urlWithPathToExistingFile, new Properties() ) );
8290
}
8391

0 commit comments

Comments
 (0)