20package com.sun.star.lib.loader;
22import java.io.BufferedReader;
24import java.io.FileInputStream;
25import java.io.InputStream;
26import java.io.InputStreamReader;
27import java.io.IOException;
28import java.io.UnsupportedEncodingException;
29import java.net.MalformedURLException;
31import java.util.StringTokenizer;
32import java.util.ArrayList;
61final class InstallationFinder {
63 private static final String SYSPROP_NAME =
64 "com.sun.star.lib.loader.unopath";
65 private static final String ENVVAR_NAME =
"UNO_PATH";
66 private static final String SOFFICE =
"libreoffice";
68 private InstallationFinder() {}
83 path = getPathFromProperty( SYSPROP_NAME );
89 path = getPathFromEnvVar( ENVVAR_NAME );
96 osname =
System.getProperty(
"os.name" );
97 }
catch ( SecurityException e ) {
102 if ( osname ==
null ) {
106 if ( osname.startsWith(
"Windows" ) ) {
109 path = getPathFromWindowsRegistry();
114 path = getPathFromPathEnvVar();
115 if ( path ==
null ) {
118 path = getPathFromWhich();
119 if ( path ==
null ) {
123 path = getPathFromSVersionFile();
142 private static String getPathFromProperty(
String prop ) {
147 path =
System.getProperty( prop );
148 }
catch ( SecurityException e ) {
171 path =
System.getenv( var );
172 }
catch ( SecurityException e ) {
174 }
catch ( java.lang.Error
err ) {
190 private static String getPathFromWindowsRegistry() {
192 final String SUBKEYNAME =
"\\Software\\LibreOffice\\UNO\\InstallPath";
198 WinRegKey key =
new WinRegKey(
"HKEY_CURRENT_USER" + SUBKEYNAME );
199 path = key.getStringValue();
200 }
catch ( WinRegKeyException e ) {
203 WinRegKey key =
new WinRegKey(
"HKEY_LOCAL_MACHINE" +
205 path = key.getStringValue();
206 }
catch ( WinRegKeyException we ) {
207 System.err.println(
"com.sun.star.lib.loader." +
208 "InstallationFinder::getPathFromWindowsRegistry: " +
209 "reading key from Windows Registry failed: " + we );
229 private static String getPathFromPathEnvVar() {
231 final String PATH_ENVVAR_NAME =
"PATH";
237 str =
System.getenv( PATH_ENVVAR_NAME );
238 }
catch ( SecurityException e ) {
241 }
catch ( java.lang.Error
err ) {
248 StringTokenizer
tokens =
new StringTokenizer(
249 str,
File.pathSeparator );
250 while (
tokens.hasMoreTokens() ) {
253 if ( file.exists() ) {
256 path = file.getCanonicalFile().getParent();
259 }
catch ( IOException e ) {
262 System.err.println(
"com.sun.star.lib.loader." +
263 "InstallationFinder::getPathFromEnvVar: " +
267 }
catch ( SecurityException e ) {
288 private static String getPathFromWhich() {
290 final String WHICH =
"which";
299 proc = rt.exec( cmdArray );
300 }
catch ( SecurityException e ) {
302 }
catch ( IOException e ) {
304 System.err.println(
"com.sun.star.lib.loader." +
305 "InstallationFinder::getPathFromWhich: " +
306 "which command failed: " + e );
311 StreamGobbler gobbler =
new StreamGobbler( proc.getErrorStream() );
316 BufferedReader br =
new BufferedReader(
317 new InputStreamReader( proc.getInputStream(),
"UTF-8" ) );
320 while ( (
line = br.readLine() ) !=
null ) {
321 if ( path ==
null ) {
326 for (
int i = 0;
i <=
index;
i++ ) {
329 if ( file.exists() ) {
331 path = file.getCanonicalFile().getParent();
335 }
catch ( SecurityException e ) {
342 }
catch ( IOException e ) {
344 System.err.println(
"com.sun.star.lib.loader." +
345 "InstallationFinder::getPathFromWhich: " +
346 "reading which command output failed: " + e );
351 }
catch ( IOException e ) {
355 }
catch ( UnsupportedEncodingException e ) {
357 System.err.println(
"com.sun.star.lib.loader." +
358 "InstallationFinder::getPathFromWhich: " +
359 "encoding failed: " + e );
366 }
catch ( InterruptedException e ) {
371 Thread.currentThread().interrupt();
388 private static String getPathFromSVersionFile() {
390 final String SVERSION =
".sversionrc";
391 final String VERSIONS =
"[Versions]";
397 System.getProperty(
"user.home" ) ,SVERSION );
398 if ( fSVersion.exists() ) {
399 ArrayList<String> lines =
new ArrayList<String>();
400 BufferedReader br =
null;
402 br =
new BufferedReader(
new InputStreamReader(
403 new FileInputStream( fSVersion ),
"UTF-8" ) );
405 while ( (
line = br.readLine() ) !=
null &&
406 !
line.equals( VERSIONS ) ) {
409 while ( (
line = br.readLine() ) !=
null &&
410 line.length() != 0 ) {
411 if ( !
line.startsWith(
";" ) )
414 }
catch ( IOException e ) {
417 System.err.println(
"com.sun.star.lib.loader." +
418 "InstallationFinder::getPathFromSVersionFile: " +
419 "reading .sversionrc file failed: " + e );
424 }
catch ( IOException e ) {
429 for (
int i = lines.size() - 1;
i >= 0;
i-- ) {
430 StringTokenizer
tokens =
new StringTokenizer(
431 lines.get(
i ),
"=" );
432 if (
tokens.countTokens() != 2 )
436 path = getCanonicalPathFromFileURL( url );
441 }
catch ( SecurityException e ) {
459 private static String getCanonicalPathFromFileURL(
String oooUrl ) {
462 if (oooUrl.length() <
prefix.length()
463 || !oooUrl.substring(0,
prefix.length()).equalsIgnoreCase(
468 StringBuffer buf =
new StringBuffer(
prefix);
469 int n = oooUrl.indexOf(
'/',
prefix.length());
474 if (host.length() != 0 && !host.equalsIgnoreCase(
"localhost")) {
478 if (
n == oooUrl.length()) {
482 while (
n < oooUrl.length()) {
485 int n2 = oooUrl.indexOf(
'/',
n);
487 n2 = oooUrl.length();
490 char c = oooUrl.charAt(
n);
493 byte[]
bytes =
new byte[(
n2 -
n) / 3];
495 while (oooUrl.length() -
n > 2
496 && oooUrl.charAt(
n) ==
'%')
498 int d1 =
Character.digit(oooUrl.charAt(
n + 1), 16);
499 int d2 =
Character.digit(oooUrl.charAt(
n + 2), 16);
500 if (d1 < 0 || d2 < 0) {
503 int d = 16 * d1 + d2;
513 }
catch (UnsupportedEncodingException e) {
532 url =
new URL(buf.toString());
533 }
catch (MalformedURLException e) {
536 String path = url.getFile();
542 File file =
new File( path, SOFFICE );
544 if ( file.isAbsolute() && file.exists() ) {
547 ret = file.getCanonicalFile().getParent();
548 }
catch ( IOException e ) {
552 }
catch ( SecurityException e ) {
565 InputStream m_istream;
574 BufferedReader br =
new BufferedReader(
575 new InputStreamReader( m_istream,
"UTF-8" ) );
577 while ( br.readLine() !=
null ) {
581 }
catch (UnsupportedEncodingException e) {
583 }
catch ( IOException e ) {
This class is used for emptying any stream which is passed into it in a separate thread.
std::vector< sal_uInt8 > bytes
static char * getPath(void)