1 % Check Octave / Matlab environment for spreadsheet I/O support.
3 % usage: [ RETVAL ] = chk_spreadsheet_support ( [/PATH/TO/JARS], [,DEBUG_LEVEL] [,PATH_TO_OOO])
5 % CHK_SPREADSHEET_SUPPORT first checks ActiveX (native MS-Excel); then
6 % Java JRE presence, then Java support (builtin/activated (Matlab) or
7 % added tru octave-forge Java package (Octave); then check existing
8 % javaclasspath for Java class libraries (.jar) needed for various
9 % Java-based spreadsheet I/O interfaces.
10 % If desired the relevant classes can be added to the dynamic
11 % javaclasspath. In that case the path name to the directory
12 % containing these classes should be specified as input argument
13 % with -TAKE NOTICE- /forward/ slashes. In these jars reside in
14 % different directories, multiple calls to chk_spreadsheet_support
17 % Input arguments (all are optional, but the order is important):
18 % /PATH/TO/JARS = (string) path (relative or absolute) to a
19 % subdirectory where java class libraries (.jar)
20 % for spreadsheet I/O reside. Can be [] or ''
21 % DEBUG_LEVEL = (integer) between [0 (no output) .. 3 (full output]
22 % PATH_TO_OOO = (string) installation directory of Openffice.org,
23 % usually (but not guaranteed):
24 % - Windows: C:\Program Files\OpenOffice.org
25 % - *nix: /usr/lib/ooo
27 % IMPORTANT: PATH_TO_OOO should be such that both:
28 % 1. PATH_TO_OOO/program/
30 % 2. PATH_TO_OOO/ure/.../ridl.jar
33 % RETVAL = 0 No spreadsheet I/O support found
34 % <> 0 At least one spreadsheet I/O interface found. RETVAL
35 % RETVAL will be set to the sum of values for found interfaces:
36 % ---------- XLS (Excel) interfaces: ----------
37 % 1 = COM (ActiveX / Excel)
38 % 2 = POI (Java / Apache POI)
39 % 4 = POI+OOXML (Java / Apache POI)
40 % 8 = JXL (Java / JExcelAPI)
41 % 16 = OXS (Java / OpenXLS)
42 % --- ODS (OpenOffice.org Calc) interfaces ----
43 % 32 = OTK (Java/ ODF Toolkit)
44 % 64 = JOD (Java / jOpenDocument)
45 % ----------------- XLS & ODS: ----------------
46 % 128 = UNO (Java / UNO bridge - OpenOffice.org)
48 function [ retval ] = chk_spreadsheet_support (path_to_jars, dbug, path_to_ooo)
50 % Copyright (C) 2009,2010,2011 Philip Nienhuis <prnienhuis at users.sf.net>
52 % This program is free software; you can redistribute it and/or modify it under
53 % the terms of the GNU General Public License as published by the Free Software
54 % Foundation; either version 3 of the License, or (at your option) any later
57 % This program is distributed in the hope that it will be useful, but WITHOUT
58 % ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
59 % FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
62 % You should have received a copy of the GNU General Public License along with
63 % this program; if not, see <http://www.gnu.org/licenses/>.
66 % Author: Philip Nienhuis
67 % Created 2010-11-03 for Octave & Matlab
69 % 2010-12-19 Found that dom4j-1.6.1.jar is needed regardless of ML's dom4j
70 % presence in static classpath (ML r2007a)
71 % 2011-01-04 Adapted for general checks, debugging & set up, both Octave & ML
72 % 2011-04-04 Rebuilt into general setup/debug tool for spreadsheet I/O support
73 % and renamed chk_spreadsheet_support()
74 % 2011-05-04 Added in UNO support (OpenOffice.org & clones)
75 % '' Improved finding jar names in javaclasspath
76 % 2011-05-07 Improved help text
77 % 2011-05-15 Better error msg if OOo instal dir isn't found
78 % 2011-05-20 Attempt to cope with case variations in subdir names of OOo install dir (_get_dir_)
79 % 2011-05-27 Fix proper return value (retval); header text improved
80 % 2011-05-29 Made retval value dependent on detected interfaces & adapted help text
81 % 2011-06-06 Fix for javaclasspath format in *nix w. octave-java-1.2.8 pkg
82 % '' Fixed wrong return value update when adding UNO classes
83 % 2011-09-03 Small fix to better detect Basis* subdir when searching unoil.jar
84 % 2011-09-18 Fixed 'Matlab style short circuit' warning in L. 152
85 % 2012-12-24 Amended code stanze to find unoil.jar; now works in LibreOffice 3.5b2 as well
86 % 2012-06-07 Replaced all tabs by double space
89 if (nargin < 3); path_to_ooo= ''; end %if
90 if (nargin < 2); dbug = 0; end %if
91 isOctave = exist ('OCTAVE_VERSION', 'builtin') ~= 0;
92 if (dbug); fprintf ('\n'); end %if
93 % interfaces = {'COM', 'POI', 'POI+OOXML', 'JXL', 'OXS', 'OTK', 'JOD', 'UNO'}; % Order = vital
95 % Check if MS-Excel COM ActiveX server runs
96 if (dbug), fprintf ('Checking Excel/ActiveX/COM... '); end %if
98 app = actxserver ('Excel.application');
99 % If we get here, the call succeeded & COM works.
100 xlsinterfaces.COM = 1;
101 % Close Excel to avoid zombie Excel invocation
104 if (dbug), fprintf ('OK.\n\n'); end %if
108 if (dbug), fprintf ('not working.\n\n'); end %if
112 if (dbug), fprintf ('Checking Java support...\n'); end %if
113 if (dbug > 1), fprintf (' 1. Checking Java JRE presence.... '); end %if
114 % Try if Java is installed at all
117 jtst = (system ('java -version 2> nul'));
119 jtst = (system ('java -version 2> /dev/null'));
122 tst1 = version ('-java');
123 jtst = isempty (strfind (tst1, 'Java'));
126 error ('Apparently no Java JRE installed.');
128 if (dbug > 1), fprintf ('OK, found one.\n'); end %if
130 if (dbug > 1 && isOctave), fprintf (' 2. Checking Octave Java support... '); end %if
132 jcp = javaclasspath ('-all'); % For Octave java pkg > 1.2.7
133 if (isempty (jcp)), jcp = javaclasspath; end %if % For Octave java pkg < 1.2.8
134 % If we get here, at least Java works.
135 if (dbug > 1 && isOctave), fprintf ('Java package seems to work OK.\n'); end %if
136 % Now check for proper version (> 1.6.x.x)
137 jver = char (javaMethod ('getProperty', 'java.lang.System', 'java.version'));
138 cjver = strsplit (jver, '.');
139 if (sscanf (cjver{2}, '%d') < 6)
141 fprintf (' Java version (%s) too old - you need at least Java 6 (v. 1.6.x.x)\n', jver);
143 warning (' At Octave prompt, try "!system ("java -version")"');
145 warning (' At Matlab prompt, try "version -java"');
150 if (dbug > 2), fprintf (' Java (version %s) seems OK.\n', jver); end %if
152 % Under *nix the classpath must first be split up.
153 % Matlab is braindead here. For ML we need a replacement for Octave's builtin strsplit()
154 % This is found on ML Central (BSD license so this is allowed) & adapted for input arg order
155 if (isunix && ~iscell (jcp)); jcp = strsplit (char (jcp), ':'); end %if
157 % Check JVM virtual memory settings
158 jrt = javaMethod ('getRuntime', 'java.lang.Runtime');
159 jmem = jrt.maxMemory ();
160 if (isOctave), jmem = jmem.doubleValue(); end %if
161 jmem = int16 (jmem/1024/1024);
162 fprintf (' Maximum JVM memory: %5d MiB; ', jmem);
164 fprintf ('should better be at least 400 MB!\n');
165 fprintf (' Hint: adapt setting -Xmx in file "java.opts" (supposed to be here:)\n');
167 fprintf (' %s\n', [matlabroot filesep 'share' filesep 'octave' filesep 'packages' filesep 'java-<version>' filesep 'java.opts']);
169 fprintf (' $matlabroot/bin/<arch>]\n');
172 fprintf ('sufficient.\n');
175 if (dbug), fprintf ('Java support OK\n'); end %if
177 error ('No Java support found: %s.', lasterr);
180 if (dbug), fprintf ('\nChecking javaclasspath for .jar class libraries needed for spreadsheet I/O...:\n'); end %if
182 % Try Java & Apache POI. First Check basic .xls (BIFF8) support
183 if (dbug > 1), fprintf ('\nBasic POI (.xls) <poi-3> <poi-ooxml>:\n'); end %if
184 jpchk1 = 0; entries1 = {'poi-3', 'poi-ooxml-3'}; missing1 = zeros (1, numel (entries1));
185 % Only under *nix we might use brute force: e.g., strfind (javaclasspath, classname)
186 % as javaclasspath is one long string. Under Windows however classpath is a cell array
187 % so we need the following more subtle, platform-independent approach:
188 for jj=1:length (entries1)
190 for ii=1:length (jcp)
191 jcplst = strsplit (jcp{ii}, filesep);
192 jcpentry = jcplst {end};
193 if (~isempty (strfind (lower (jcpentry), lower (entries1{jj}))))
194 jpchk1 = jpchk1 + 1; found = 1;
195 if (dbug > 2), fprintf (' - %s OK\n', jcp{ii}); end %if
199 if (dbug > 2), fprintf (' %s....jar missing\n', entries1{jj}); end %if
203 if (jpchk1 >= numel (entries1)), retval = retval + 2; end %if
205 if (jpchk1 >= numel (entries1))
206 fprintf (' => Apache (POI) OK\n');
208 fprintf (' => Not all classes (.jar) required for POI in classpath\n');
211 % Next, check OOXML support
212 if (dbug > 1), fprintf ('\nPOI OOXML (.xlsx) <xbean> <poi-ooxml-schemas> <dom4j>:\n'); end %if
213 jpchk2 = 0; entries2 = {'xbean', 'poi-ooxml-schemas', 'dom4j-1.6.1'};
214 missing2 = zeros (1, numel (entries2));
215 for jj=1:length (entries2)
217 for ii=1:length (jcp)
218 jcplst = strsplit (jcp{ii}, filesep);
219 jcpentry = jcplst {end};
220 if (~isempty (strfind (lower (jcpentry), lower (entries2{jj}))))
221 jpchk2 = jpchk2 + 1; found = 1;
222 if (dbug > 2), fprintf (' - %s OK\n', jcp{ii}); end %if
226 if (dbug > 2), fprintf (' %s....jar missing\n', entries2{jj}); end %if
230 % Only update retval if all classes for basic POI have been found in javaclasspath
231 if (jpchk1 >= numel (entries1) && jpchk2 >= numel (entries2)), retval = retval + 4; end %if
233 if (jpchk2 >= numel (entries2))
234 fprintf (' => POI OOXML OK\n');
236 fprintf (' => Some classes for POI OOXML support missing\n');
240 % Try Java & JExcelAPI
241 if (dbug > 1), fprintf ('\nJExcelAPI (.xls (incl. BIFF5 read)) <jxl>:\n'); end %if
242 jpchk = 0; entries3 = {'jxl'}; missing3 = zeros (1, numel (entries3));
243 for jj=1:length (entries3)
245 for ii=1:length (jcp)
246 jcplst = strsplit (jcp{ii}, filesep);
247 jcpentry = jcplst {end};
248 if (~isempty (strfind (lower (jcpentry), lower (entries3{jj}))))
249 jpchk = jpchk + 1; found = 1;
250 if (dbug > 2), fprintf (' - %s OK\n', jcp{ii}); end %if
254 if (dbug > 2), fprintf (' %s....jar missing\n', entries3{jj}); end %if
258 if (jpchk >= numel (entries3)), retval = retval + 8; end %if
260 if (jpchk >= numel (entries3))
261 fprintf (' => Java/JExcelAPI (JXL) OK.\n');
263 fprintf (' => Not all classes (.jar) required for JXL in classpath\n');
268 if (dbug > 1), fprintf ('\nOpenXLS (.xls (BIFF8)) <OpenXLS>:\n'); end %if
269 jpchk = 0; entries4 = {'OpenXLS'}; missing4 = zeros (1, numel (entries4));
270 for jj=1:length (entries4)
272 for ii=1:length (jcp)
273 jcplst = strsplit (jcp{ii}, filesep);
274 jcpentry = jcplst {end};
275 if (~isempty (strfind (lower (jcpentry), lower (entries4{jj}))))
276 jpchk = jpchk + 1; found = 1;
277 if (dbug > 2), fprintf (' - %s OK\n', jcp{ii}); end %if
281 if (dbug > 2), fprintf (' %s....jar missing\n', entries4{jj}); end %if
285 if (jpchk >= numel (entries4)), retval = retval + 16; end %if
287 if (jpchk >= numel (entries4))
288 fprintf (' => Java/OpenXLS (OXS) OK.\n');
290 fprintf (' => Not all classes (.jar) required for OXS in classpath\n');
294 % Try Java & ODF toolkit
295 if (dbug > 1), fprintf ('\nODF Toolkit (.ods) <odfdom> <xercesImpl>:\n'); end %if
296 jpchk = 0; entries5 = {'odfdom', 'xercesImpl'}; missing5 = zeros (1, numel (entries5));
297 for jj=1:length (entries5)
299 for ii=1:length (jcp)
300 jcplst = strsplit (jcp{ii}, filesep);
301 jcpentry = jcplst {end};
302 if (~isempty (strfind ( lower (jcpentry), lower (entries5{jj}))))
303 jpchk = jpchk + 1; found = 1;
304 if (dbug > 2), fprintf (' - %s OK\n', jcp{ii}); end %if
308 if (dbug > 2), fprintf (' %s....jar missing\n', entries5{jj}); end %if
312 if (jpchk >= numel (entries5)) % Apparently all requested classes present.
313 % Only now we can check for proper odfdom version (only 0.7.5 & 0.8.6 work OK).
314 % The odfdom team deemed it necessary to change the version call so we need this:
318 odfvsn = javaMethod ('getOdfdomVersion', 'org.odftoolkit.odfdom.JarManifest');
321 odfvsn = javaMethod ('getApplicationVersion', 'org.odftoolkit.odfdom.Version');
323 if ~(strcmp (odfvsn, '0.7.5') || strcmp (odfvsn, '0.8.6') || strcmp (odfvsn, '0.8.7'))
324 warning (' *** odfdom version (%s) is not supported - use v. 0.8.6 or 0.8.7.\n', odfvsn);
326 if (dbug > 1), fprintf (' => ODFtoolkit (OTK) OK.\n'); end %if
327 retval = retval + 32;
330 fprintf (' => Not all required classes (.jar) in classpath for OTK\n');
333 % Try Java & jOpenDocument
334 if (dbug > 1), fprintf ('\njOpenDocument (.ods + experimental .sxc readonly) <jOpendocument>:\n'); end %if
335 jpchk = 0; entries6 = {'jOpenDocument'}; missing6 = zeros (1, numel (entries6));
336 for jj=1:length (entries6)
338 for ii=1:length (jcp)
339 jcplst = strsplit (jcp{ii}, filesep);
340 jcpentry = jcplst {end};
341 if (~isempty (strfind (lower (jcpentry), lower (entries6{jj}))))
342 jpchk = jpchk + 1; found = 1;
343 if (dbug > 2), fprintf (' - %s OK\n', jcp{ii}); end %if
347 if (dbug > 2), fprintf (' %s....jar missing\n', entries6{jj}); end %if
351 if (jpchk >= numel (entries6)), retval = retval + 64; end %if
353 if (jpchk >= numel(entries6))
354 fprintf (' => jOpenDocument (JOD) OK.\n');
356 fprintf (' => Not all required classes (.jar) in classpath for JOD\n');
361 if (dbug > 1), fprintf ('\nUNO/Java (.ods, .xls, .xlsx, .sxc) <OpenOffice.org>:\n'); end %if
362 % entries0(1) = not a jar but a directory (<000_install_dir/program/>)
363 jpchk = 0; entries0 = {'program', 'unoil', 'jurt', 'juh', 'unoloader', 'ridl'};
364 missing0 = zeros (1, numel (entries0));
365 for jj=1:numel (entries0)
368 jcplst = strsplit (jcp{ii}, filesep);
369 jcpentry = jcplst {end};
370 if (~isempty (strfind (lower (jcpentry), lower (entries0{jj}))))
371 jpchk = jpchk + 1; found = 1;
372 if (dbug > 2), fprintf (' - %s OK\n', jcp{ii}); end %if
379 fprintf (' %s.... (directory) not found\n', entries0{jj});
381 fprintf (' %s....jar missing\n', entries0{jj});
387 if (jpchk >= numel (entries0)), retval = retval + 128; end %if
389 if (jpchk >= numel (entries0))
390 fprintf (' => UNO (OOo) OK\n');
392 fprintf (' => One or more UNO classes (.jar) missing in javaclasspath\n');
396 % If requested, try to add UNO stuff to javaclasspath
397 ujars_complete = isempty (find (missing0, 1));
399 if (~ujars_complete && nargin > 0 && ~isempty (path_to_ooo))
400 if (dbug), fprintf ('\nTrying to add missing UNO java class libs to javaclasspath...\n'); end %if
401 if (~ischar (path_to_jars)), error ('Path expected for arg # 1'); end %if
402 % Add missing jars to javaclasspath. First combine all entries
403 targt = sum (missing0);
405 % Add program dir (= where soffice or soffice.exe or ooffice resides)
406 programdir = [path_to_ooo filesep entries0{1}];
407 if (exist (programdir, 'dir'))
408 if (dbug > 2), fprintf (' Found %s, adding it to javaclasspath ... ', programdir); end %if
410 javaaddpath (programdir);
412 if (dbug > 2), fprintf ('OK\n'); end %if
414 if (dbug > 2), fprintf ('FAILED\n'); end %if
417 if (dbug > 2), error ('Suggested OpenOffice.org install directory: %s not found!\n', path_to_ooo); end %if
420 % Rest of missing entries. Find where URE is located. Watch out because case of ./ure is unknown
421 uredir = get_dir_ (path_to_ooo, 'ure');
422 if (isempty (uredir)), return; end %if
423 % Now search for UNO jars
424 for ii=2:length (entries0)
427 % Special case as unoil.jar usually resides in ./Basis<something>/program/classes
428 % Find out the exact name of Basis.....
429 basisdirlst = dir ([path_to_ooo filesep '?asis' '*']);
431 if (numel (basisdirlst) > 0)
432 while (jj <= size (basisdirlst, 1) && jj > 0)
433 basisdir = basisdirlst(jj).name;
434 if (basisdirlst(jj).isdir)
435 basisdir = basisdirlst(jj).name;
441 basisdir = [path_to_ooo filesep basisdir ];
443 basisdir = path_to_ooo;
445 basisdirentries = {'program', 'classes'};
446 tmp = basisdir; jj=1;
447 while (~isempty (tmp) && jj <= numel (basisdirentries))
448 tmp = get_dir_ (tmp, basisdirentries{jj});
452 file = dir ([ unojarpath filesep entries0{2} '*' ]);
454 % Rest of jars in ./ure/share/java or ./ure/java
455 unojardir = get_dir_ (uredir, 'share');
456 if (isempty (unojardir))
461 unojarpath = get_dir_ (tmp, 'java');
462 file = dir ([unojarpath filesep entries0{ii} '*']);
464 % Path found, now try to add jar
466 if (dbug > 2), fprintf (' ? %s<...>.jar ?\n', entries0{ii}); end %if
468 if (dbug > 2), fprintf (' Found %s, adding it to javaclasspath ... ', file.name); end %if
470 javaaddpath ([unojarpath filesep file.name]);
472 if (dbug > 2), fprintf ('OK\n'); end %if
474 if (dbug > 2), fprintf ('FAILED\n'); end %if
479 if (~targt); retval = retval + 128; end %if
482 fprintf ('Some UNO class libs still lacking...\n\n');
484 fprintf ('UNO interface supported now.\n\n');
489 % ----------Rest of Java interfaces----------------------------------
491 missing = [missing1 missing2 missing3 missing4 missing5 missing6];
492 jars_complete = isempty (find (missing, 1));
495 fprintf ('All interfaces already fully supported.\n\n');
497 fprintf ('Some class libs lacking yet...\n\n');
501 if (~jars_complete && nargin > 0 && ~isempty (path_to_jars))
502 % Add missing jars to javaclasspath. Assume they're all in the same place
503 if (dbug), fprintf ('Trying to add missing java class libs to javaclasspath...\n'); end %if
504 if (~ischar (path_to_jars)), error ('Path expected for arg # 1'); end %if
505 % First combine all entries
506 targt = sum (missing);
507 % Search tru list of missing entries
508 for ii=1:6 % Adapt in case of future new interfaces
509 tmpe = eval ([ 'entries' char(ii + '0') ]);
510 tmpm = eval ([ 'missing' char(ii + '0') ]);
512 for jj=1:numel (tmpe)
514 file = dir ([path_to_jars filesep tmpe{jj} '*']);
516 if (dbug > 2), fprintf (' ? %s<...>.jar ?\n', tmpe{jj}); end %if
518 if (dbug > 2), fprintf (' Found %s, adding it to javaclasspath ... ', file(1).name); end %if
520 javaaddpath ([path_to_jars filesep file(1).name]);
523 if (dbug > 2), fprintf ('OK\n'); end %if
525 if (dbug > 2), fprintf ('FAILED\n'); end %if
531 retval = retval + 2^ii;
537 fprintf ('Some other class libs still lacking...\n\n');
539 fprintf ('All interfaces fully supported.now.\n\n');
547 function [ ret_dir ] = get_dir_ (base_dir, req_dir)
549 % Construct path to subdirectory req_dir in a subdir tree, aimed
550 % at taking care of proper case (esp. for *nix) of existing subdir
551 % in the result. Case of input var req_dir is ignored on purpose.
554 % Get list of directory entries
555 ret_dir_list = dir (base_dir);
556 % Find matching entries
557 idx = find (strcmpi ({ret_dir_list.name}, req_dir));
558 % On *nix, several files and subdirs in one dir may have the same name as long as case differs
561 while (~ret_dir_list(idx(ii)).isdir)
563 if (ii > numel (idx)); return; end %if
565 % If we get here, a dir with proper name has been found. Construct path
566 ret_dir = [ base_dir filesep ret_dir_list(idx(ii)).name ];