Skip to content

Commit 1b08eb1

Browse files
author
Eric Fields
committed
POI path related refactoring
1 parent eaa2c0a commit 1b08eb1

4 files changed

Lines changed: 52 additions & 23 deletions

File tree

Ftest2xls.m

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
% format_output - A boolean specifying whether to apply formatting to the
1515
% spreadsheet output. {default: true}
1616
%
17-
%VERSION DATE: 19 November 2017
17+
%VERSION DATE: 20 November 2017
1818
%AUTHOR: Eric Fields
1919
%
2020
%NOTE: This function is provided "as is" and any express or implied warranties
@@ -61,9 +61,8 @@ function Ftest2xls(GND, test_id, output_fname, format_output)
6161
'This can delete global variables and create other problems.\n', ...
6262
'You can avoid this by running add_poi_path.']));
6363
end
64-
for i = 1:length(missing_poi_files)
65-
javaaddpath(missing_poi_files{i});
66-
end
64+
%Add each .jar file to the dynamic path
65+
add_poi_path('-dynamic');
6766
end
6867
writexls = @xlwrite;
6968
end

add_poi_path.m

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
%EXAMPLE USAGE
44
% >> add_poi_path
55
%
6-
%VERSION DATE: 19 November 2017
6+
%INPUT
7+
% segment - if '-static', add POI library .jar files to the static path; if
8+
% '-dynamic', add to the dynamic path.
9+
%
10+
%VERSION DATE: 20 November 2017
711
%AUTHOR: Eric Fields
812
%
913
%NOTE: This function is provided "as is" and any express or implied warranties
@@ -13,14 +17,23 @@
1317
%All rights reserved.
1418
%This code is free and open source software made available under the 3-clause BSD license.
1519

16-
function add_poi_path()
20+
function add_poi_path(segment)
21+
22+
%Default to updating static path
23+
if ~nargin
24+
segment = '-static';
25+
end
1726

1827
%Get full path for POI .jar files not on static Java class path
1928
[~, missing_poi_files] = get_poi_paths();
29+
30+
%Nothing to do if all files are on static path
31+
if isempty(missing_poi_files)
32+
return;
33+
end
34+
35+
if strcmpi(segment, '-static')
2036

21-
%Append POI locations that are not already on the static Java class
22-
%path
23-
if ~isempty(missing_poi_files)
2437
%Full path for javaclasspath file
2538
static_file = [prefdir filesep 'javaclasspath.txt'];
2639
%Append missing POI files to javaclasspath.txt file
@@ -29,6 +42,21 @@ function add_poi_path()
2942
fprintf(f_out, '%s\n', missing_poi_files{i});
3043
end
3144
fclose(f_out);
45+
46+
%POI files will not actually be added to the Java class path until
47+
%MATLAB is restarted
48+
msgbox('Close and restart MATLAB to finish adding the Apache POI library to the Java class path.')
49+
50+
elseif strcmpi(segment, '-dynamic')
51+
52+
%Add any file no on the static or dynamic path to the dynamic path
53+
for i = 1:length(missing_poi_files)
54+
jar_file = missing_poi_files{i};
55+
if ~ismember(jar_file, javaclasspath('-dynamic'))
56+
javaaddpath(jar_file);
57+
end
58+
end
59+
3260
end
3361

3462
end

get_poi_paths.m

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
% missing_poi_files - Full paths for .jar files in the Apache POI library
1010
% that are not on the static Java class path
1111
%
12-
%VERSION DATE: 19 November 2017
12+
%VERSION DATE: 20 November 2017
1313
%AUTHOR: Eric Fields
1414
%
1515
%NOTE: This function is provided "as is" and any express or implied warranties
@@ -20,14 +20,17 @@
2020
%This code is free and open source software made available under the 3-clause BSD license.
2121

2222
function [poi_files, missing_poi_files] = get_poi_paths()
23-
24-
poi_files = {fullfile(fileparts(which('get_poi_paths')), 'poi_library/poi-3.8-20120326.jar')
25-
fullfile(fileparts(which('get_poi_paths')), 'poi_library/poi-ooxml-3.8-20120326.jar')
26-
fullfile(fileparts(which('get_poi_paths')), 'poi_library/poi-ooxml-schemas-3.8-20120326.jar')
27-
fullfile(fileparts(which('get_poi_paths')), 'poi_library/xmlbeans-2.3.0.jar')
28-
fullfile(fileparts(which('get_poi_paths')), 'poi_library/dom4j-1.6.1.jar')
29-
fullfile(fileparts(which('get_poi_paths')), 'poi_library/stax-api-1.0.1.jar')};
30-
31-
missing_poi_files = poi_files(~ismember(poi_files, javaclasspath('-static')));
23+
24+
%Find directory with POI library
25+
poi_dir = fullfile(fileparts(which('get_poi_paths')), 'poi_library');
26+
27+
%Find all .jar files within directory
28+
poi_files = dir(poi_dir);
29+
poi_files = {poi_files.name};
30+
poi_files = poi_files(cell2mat(cellfun(@(x) ~isempty(strfind(x, '.jar')), poi_files, 'UniformOutput', false))); %#ok<STREMP>
31+
poi_files = cellfun(@(x) [poi_dir filesep x], poi_files, 'UniformOutput', false);
32+
33+
%Find which .jar files (if any) are not on the static Java class path
34+
missing_poi_files = poi_files(~ismember(poi_files, javaclasspath('-static')));
3235

3336
end

ttest2xls.m

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
% format_output - A boolean specifying whether to apply formatting to the
1515
% spreadsheet output. {default: true}
1616
%
17-
%VERSION DATE: 19 November 2017
17+
%VERSION DATE: 20 November 2017
1818
%AUTHOR: Eric Fields
1919
%
2020
%NOTE: This function is provided "as is" and any express or implied warranties
@@ -60,9 +60,8 @@ function ttest2xls(GND, test_id, output_fname, format_output)
6060
'This can delete global variables and create other problems.\n', ...
6161
'You can avoid this by running add_poi_path.']));
6262
end
63-
for i = 1:length(missing_poi_files)
64-
javaaddpath(missing_poi_files{i});
65-
end
63+
%Add each .jar file to the dynamic path
64+
add_poi_path('-dynamic');
6665
end
6766
writexls = @xlwrite;
6867
end

0 commit comments

Comments
 (0)