fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / scripting / workben / installer / InstUtil.java
blob26ec2027b4d648d3d9a9741f96c382670a71c232
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package installer;
21 import java.net.URLDecoder;
22 import java.io.*;
23 import java.util.*;
24 import java.net.*;
26 public class InstUtil {
28 public static File buildSversionLocation() throws IOException {
29 File theFile = null;
30 StringBuffer str = new StringBuffer();
31 str.append(System.getProperty("user.home"));
32 str.append(File.separator);
33 StringBuffer thePath = new StringBuffer(str.toString());
35 String os = System.getProperty("os.name");
37 if (os.indexOf("Windows") != -1) {
38 boolean bSVersionInHomeDir = new File(thePath.toString() + "sversion.ini").exists();
40 if (!bSVersionInHomeDir) {
41 thePath.append("Application Data");
42 thePath.append(File.separator);
44 theFile = findVersionFile(new File(thePath.toString()));
45 } else if (os.indexOf("SunOS") != -1) {
46 thePath.append(".sversionrc");
47 theFile = new File(thePath.toString());
48 } else if (os.indexOf("Linux") != -1) {
49 thePath.append(".sversionrc");
50 theFile = new File(thePath.toString());
53 if (theFile == null)
55 throw new IOException("Could not locate the OpenOffice settings file.\nAre you sure StarOffice is installed on your system?");
57 if (!theFile.exists())
59 throw new IOException("Could not locate the OpenOffice settings file.\nAre you sure StarOffice is installed on your system?");
61 return theFile;
66 public static boolean hasNetbeansInstallation() {
67 boolean result = false;
68 try
70 result = checkForSupportedVersion( getNetbeansLocation(), versions );
72 if (result == false)
73 System.out.println("No supported version of NetBeans found.");
75 catch ( IOException ioe )
77 System.err.println("Exception caught trying to determine netbeans installation: " + ioe );
78 ioe.printStackTrace();
79 result = false;
81 return result;
84 private static boolean checkForSupportedVersion( Properties installs, String[] supportedVersions )
86 if ( installs != null )
88 for ( int index = 0; index < supportedVersions.length; index++ )
90 String key = supportedVersions[ index ];
91 String path = null;
92 if ( ( path = installs.getProperty(key) ) != null )
94 // at least one supported version for netbeans present, so return;
95 return true;
100 return false;
104 public static boolean hasJeditInstallation() {
105 boolean result = false;
108 result = checkForSupportedVersion( getJeditLocation(), versions );
109 if ( !result )
111 System.out.println("No supported version for JEdit found.");
114 catch ( IOException ioe )
116 System.err.println("Exception caught trying to determine jedit installation: " + ioe );
117 ioe.printStackTrace();
118 result = false;
120 return result;
125 public static Properties getNetbeansLocation() throws IOException {
126 File theFile = null;
127 Properties results = new Properties();
129 StringBuffer str = new StringBuffer();
130 str.append(System.getProperty("user.home"));
131 str.append(File.separator);
132 StringBuffer thePath = new StringBuffer(str.toString());
134 String os = System.getProperty("os.name");
136 if (os.indexOf("Windows") != -1) {
137 //theFile = findVersionFile(new File(str.toString()));
138 thePath.append(".netbeans");
139 //theFile = new File(thePath.toString());
140 } else if (os.indexOf("SunOS") != -1) {
141 thePath.append(".netbeans");
142 //theFile = new File(thePath.toString());
143 } else if (os.indexOf("Linux") != -1) {
144 thePath.append(".netbeans");
145 //theFile = new File(thePath.toString());
148 if ( thePath.toString().indexOf( ".netbeans" ) == -1 )
149 return null;
150 else if ( new File( thePath.append( File.separator+"3.4"+File.separator ).toString() ).isDirectory() ) {
152 System.out.println( "Found NetBeans 3.4 user directory: " + thePath );
153 File netbeansLogFile = new File( thePath.toString() + File.separator + "system" + File.separator + "ide.log" );
154 if( netbeansLogFile.exists() ) {
155 String installPath = getNetbeansInstallation( netbeansLogFile );
156 File f = new File(installPath);
157 results.put("NetBeans 3.4", f.getPath()+File.separator);
158 System.out.println( "NetBeans Installation directory: " + f.getPath());
160 else {
161 System.out.println( "No NetBeans log file found" );
162 return null;
165 else
167 System.out.println( "No NetBeans user directory found" );
168 return null;
172 return results;
177 public static Properties getJeditLocation() throws IOException {
179 /*if( !hasJeditInstallation() ) {
180 System.out.println( "No Jedit found (line195 InstUtil");
181 return null;
184 File theFile = null;
185 Properties results = new Properties();
187 StringBuffer str = new StringBuffer();
188 str.append(System.getProperty("user.home"));
189 str.append(File.separator);
190 StringBuffer thePath = new StringBuffer(str.toString());
192 String os = System.getProperty("os.name");
193 thePath.append(".jedit");
194 //System.out.println( ".jedit path " + thePath );
196 File jeditLogFile = new File( thePath.toString() + File.separator + "activity.log" );
197 if( jeditLogFile.exists() ) {
198 String[] jeditDetails = getJeditInstallation( jeditLogFile );
199 System.out.println( "getJeditLocation ) " + jeditDetails[0] );
200 File f = new File(jeditDetails[0]);
201 results.put("jEdit "+jeditDetails[1], jeditDetails[0]);
202 System.out.println( "jeditDetails[0] is " + jeditDetails[0]);
204 else {
205 System.out.println( "Prompt user for Jedit installation path" );
209 return results;
216 private static String getNetbeansInstallation( File logFile ) {
217 String installPath = "";
218 try {
219 BufferedReader reader = new BufferedReader(new FileReader(logFile));
221 for (String s = reader.readLine(); s != null; s = reader.readLine()) {
222 s.trim();
223 if( s.indexOf( "IDE Install" ) != -1 ) {
224 int pathStart = s.indexOf( "=" ) + 2;
225 //System.out.println( "pathStart " + pathStart );
226 installPath = s.substring( pathStart, s.length() );
227 //System.out.println( "installPath 1" + installPath );
228 int pathEnd = installPath.indexOf( ";");
229 //System.out.println( "pathEnd " + pathEnd );
230 installPath = installPath.substring( 0, pathEnd ) +File.separator;
231 //System.out.println( "pathStart " + pathStart );
232 //int pathEnd = s.indexOf( ";");
233 //System.out.println( "pathEnd " + pathEnd );
234 //System.out.println( "s is " + s + " and " + s.length() + " long" );
235 //installPath = s.substring( pathStart, pathEnd - 1 );
236 installPath.trim();
237 break;
240 reader.close();
242 catch( IOException ioe ) {
243 System.out.println( "Error reading Netbeans location information" );
245 //catch( FileNotFoundException fnfe ) {
246 //System.out.println( "NetBeans ide.log FileNotFoundException" );
249 return installPath;
253 private static String[] getJeditInstallation( File logFile ) {
254 String[] jeditDetails = new String[2];
255 try {
256 BufferedReader reader = new BufferedReader(new FileReader(logFile));
257 String installPath = "";
258 String version = "";
260 for (String s = reader.readLine(); s != null; s = reader.readLine()) {
261 s.trim();
262 if( s.indexOf( "jEdit home directory is" ) != -1 ) {
263 int pathStart = new String( "[message] jEdit: jEdit home directory is " ).length();
264 //System.out.println( "pathStart " + pathStart );
265 installPath = s.substring( pathStart, s.length() ) +File.separator;
266 System.out.println( "installPath 1" + installPath );
267 //int pathEnd = installPath.indexOf( ";");
268 //System.out.println( "pathEnd " + pathEnd );
269 //installPath = installPath.substring( 0, pathEnd ) +File.separator;
270 //System.out.println( "pathStart " + pathStart );
271 //int pathEnd = s.indexOf( ";");
272 //System.out.println( "pathEnd " + pathEnd );
273 //System.out.println( "s is " + s + " and " + s.length() + " long" );
274 //installPath = s.substring( pathStart, pathEnd - 1 );
275 installPath.trim();
276 //System.out.println( "installPath 2 " + installPath );
277 //break;
278 jeditDetails[0] = installPath;
280 if( s.indexOf( "jEdit: jEdit version" ) != -1 ) {
281 int versionStart = s.indexOf( "version" ) + 8;
282 System.out.println( "versionStart is: " + versionStart );
283 version = s.substring( versionStart, s.length() );
284 version.trim();
285 System.out.println( "jEdit version is: " + version );
286 jeditDetails[1] = version;
289 reader.close();
291 catch( IOException ioe ) {
292 System.out.println( "Error reading Jedit location information" );
294 //catch( FileNotFoundException fnfe ) {
295 //System.out.println( "Jedit activity.log FileNotFoundException" );
297 return jeditDetails;
302 public static File findVersionFile(File start)
304 File versionFile = null;
306 File files[] = start.listFiles(new VersionFilter());
307 if (files.length == 0)
309 File dirs[] = start.listFiles(new DirFilter());
310 for (int i=0; i< dirs.length; i++)
312 versionFile = findVersionFile(dirs[i]);
313 if (versionFile != null)
315 break;
319 else
321 versionFile = files[0];
324 return versionFile;
327 public static boolean verifySversionExists(File sversionFile) {
328 if (!sversionFile.exists())
329 return false;
330 return true;
333 public static Properties getOfficeVersions(File sversionFile) throws IOException {
334 BufferedReader reader = new BufferedReader(new FileReader(sversionFile));
335 String sectionName = null;
336 Properties results = new Properties();
338 for (String s = reader.readLine(); s != null; s = reader.readLine()) {
339 s.trim();
340 //System.out.println(s);
341 if (s.length() == 0)
342 continue;
343 if (s.charAt(0) == '[') {
344 sectionName = s.substring(1, s.length() - 1);
345 //System.out.println(sectionName);
346 continue;
348 if ((sectionName != null) && sectionName.equalsIgnoreCase("Versions")) {
349 int equals = s.indexOf( "=" );
350 String officeName = s.substring(0, equals );
352 String instPath = s.substring(equals + 8, s.length());
353 String [] parts = new String[2];
354 parts[0] = officeName;
355 parts[1] = instPath + File.separator;
356 //System.out.println( "InstUtil officeName " + officeName );
357 //System.out.println( "InstUtil instPath " + instPath );
359 //String [] parts = s.split("=");
360 if (parts.length == 2) {
361 //ver.version = parts[0].trim();
362 //File f = new File(parts[1].trim());
363 //results.put(parts[0].trim(), f.getPath());
364 try {
365 URL url = new URL("file://" + parts[1].trim());
366 String opSys =System.getProperty("os.name");
367 if (opSys.indexOf("Windows")!=-1){
368 String windowsPath = URLDecoder.decode( url.getPath() );
369 boolean firstSlash = true;
370 while( windowsPath.indexOf("/") != -1 ) {
371 int forwardSlashPos = windowsPath.indexOf("/");
372 String firstPart = windowsPath.substring( 0, forwardSlashPos );
373 String lastPart = windowsPath.substring( forwardSlashPos + 1, windowsPath.length() );
374 if( firstSlash ) {
375 windowsPath = lastPart;
376 firstSlash = false;
378 else {
379 windowsPath = firstPart + "\\" + lastPart;
382 int lastSlash = windowsPath.lastIndexOf("\\");
383 windowsPath = windowsPath.substring( 0, lastSlash );
384 results.put( parts[0].trim(), windowsPath );
386 else {
387 //System.err.println( " InstUtil URLDecoder " + URLDecoder.decode(url.getPath()) );
388 results.put(parts[0].trim(), URLDecoder.decode(url.getPath()));
390 //File f = new File(url);
392 //.sversion: OpenOffice.org 643=file:///scriptdev/neil/ScriptFrameOpenoffice1.0.1
393 // parts = Installation name. f.getPath = Installation path
394 //results.put(parts[0].trim(), f.getPath());
396 //results.put(parts[0].trim(), URLDecoder.decode(url.getPath()));
397 //results.put( parts[0].trim(), windowsPath );
400 catch (MalformedURLException eSyntax) {
401 //throw new IOException("Error while reading version information");
402 results.put(parts[0].trim(), parts[1].trim());
403 //System.out.println(parts[0].trim() + " : " + parts[1].trim());
404 System.err.println("GotHereException");
407 else {
408 System.out.println("not splitting on equals");
412 reader.close();
413 return results;
416 public static String getJavaVersion() {
417 return System.getProperty("java.version");
420 public static boolean isCorrectJavaVersion() {
421 if (System.getProperty("java.version").startsWith("1.4"))
422 return true;
423 return false;
426 public static void main(String args[]) {
427 InstUtil inst = new InstUtil();
428 File f = null;
431 f = InstUtil.buildSversionLocation();
433 catch (IOException e)
435 e.printStackTrace();
436 System.out.println(e.getMessage());
438 if (!InstUtil.verifySversionExists(f)) {
439 System.err.println("Problem with sversion.ini");
441 try {
442 Properties vers = InstUtil.getOfficeVersions(f);
443 } catch (IOException e) {
444 e.printStackTrace();
445 System.err.println(e);
447 System.out.println(InstUtil.getJavaVersion());
448 if (!InstUtil.isCorrectJavaVersion()) {
449 System.err.println("Not correct Java Version");
453 public static final String [] versions = {"NetBeans 3.4", "jEdit 4.0.3", "jEdit 4.1pre5" };
454 private static File tmpDir = null;
459 class DirFilter implements java.io.FileFilter
461 public boolean accept(File aFile)
463 return aFile.isDirectory();
466 class VersionFilter implements java.io.FileFilter
468 public boolean accept(File aFile)
470 if (aFile.getName().compareToIgnoreCase("sversion.ini") == 0)
472 return true;
475 return false;