1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 package installer; 23 24 import java.net.URLDecoder; 25 import java.io.*; 26 import java.util.*; 27 import java.util.zip.*; 28 import java.awt.*; 29 import java.awt.event.*; 30 import javax.swing.*; 31 import java.net.*; 32 33 public class InstUtil { 34 buildSversionLocation()35 public static File buildSversionLocation() throws IOException { 36 File theFile = null; 37 StringBuilder str = new StringBuilder(); 38 str.append(System.getProperty("user.home")); 39 str.append(File.separator); 40 StringBuilder thePath = new StringBuilder(str.toString()); 41 42 String os = System.getProperty("os.name"); 43 44 if (os.indexOf("Windows") != -1) { 45 boolean bSVersionInHomeDir = new File(thePath.toString() + "sversion.ini").exists(); 46 47 if (!bSVersionInHomeDir) { 48 thePath.append("Application Data"); 49 thePath.append(File.separator); 50 } 51 theFile = findVersionFile(new File(thePath.toString())); 52 } else if (os.indexOf("SunOS") != -1) { 53 thePath.append(".sversionrc"); 54 theFile = new File(thePath.toString()); 55 } else if (os.indexOf("Linux") != -1) { 56 thePath.append(".sversionrc"); 57 theFile = new File(thePath.toString()); 58 } 59 60 if (theFile == null) 61 { 62 throw new IOException("Could not locate the OpenOffice settings file.\nAre you sure StarOffice is installed on your system?"); 63 } 64 if (!theFile.exists()) 65 { 66 throw new IOException("Could not locate the OpenOffice settings file.\nAre you sure StarOffice is installed on your system?"); 67 } 68 return theFile; 69 } 70 71 72 hasNetbeansInstallation()73 public static boolean hasNetbeansInstallation() { 74 boolean result = false; 75 try 76 { 77 result = checkForSupportedVersion( getNetbeansLocation(), versions ); 78 79 if (result == false) 80 System.out.println("No supported version of NetBeans found."); 81 } 82 catch ( IOException ioe ) 83 { 84 System.err.println("Exception caught trying to determine netbeans installation: " + ioe ); 85 ioe.printStackTrace(); 86 result = false; 87 } 88 return result; 89 } 90 checkForSupportedVersion( Properties installs, String[] supportedVersions )91 private static boolean checkForSupportedVersion( Properties installs, String[] supportedVersions ) 92 { 93 if ( installs != null ) 94 { 95 for ( int index = 0; index < supportedVersions.length; index++ ) 96 { 97 String key = supportedVersions[ index ]; 98 String path = null; 99 if ( ( path = installs.getProperty(key) ) != null ) 100 { 101 // at least one supported version for netbeans present, so return; 102 return true; 103 } 104 105 } 106 } 107 return false; 108 } 109 110 hasJeditInstallation()111 public static boolean hasJeditInstallation() { 112 boolean result = false; 113 try 114 { 115 result = checkForSupportedVersion( getJeditLocation(), versions ); 116 if ( !result ) 117 { 118 System.out.println("No supported version for JEdit found."); 119 } 120 } 121 catch ( IOException ioe ) 122 { 123 System.err.println("Exception caught trying to determine jedit installation: " + ioe ); 124 ioe.printStackTrace(); 125 result = false; 126 } 127 return result; 128 } 129 130 131 getNetbeansLocation()132 public static Properties getNetbeansLocation() throws IOException { 133 File theFile = null; 134 Properties results = new Properties(); 135 136 StringBuilder str = new StringBuilder(); 137 str.append(System.getProperty("user.home")); 138 str.append(File.separator); 139 StringBuilder thePath = new StringBuilder(str.toString()); 140 141 String os = System.getProperty("os.name"); 142 143 if (os.indexOf("Windows") != -1) { 144 //theFile = findVersionFile(new File(str.toString())); 145 thePath.append(".netbeans"); 146 //theFile = new File(thePath.toString()); 147 } else if (os.indexOf("SunOS") != -1) { 148 thePath.append(".netbeans"); 149 //theFile = new File(thePath.toString()); 150 } else if (os.indexOf("Linux") != -1) { 151 thePath.append(".netbeans"); 152 //theFile = new File(thePath.toString()); 153 } 154 155 if ( thePath.toString().indexOf( ".netbeans" ) == -1 ) 156 return null; 157 else if ( new File( thePath.append( File.separator+"3.4"+File.separator ).toString() ).isDirectory() ) { 158 159 System.out.println( "Found NetBeans 3.4 user directory: " + thePath ); 160 File netbeansLogFile = new File( thePath.toString() + File.separator + "system" + File.separator + "ide.log" ); 161 if( netbeansLogFile.exists() ) { 162 String installPath = getNetbeansInstallation( netbeansLogFile ); 163 File f = new File(installPath); 164 results.put("NetBeans 3.4", f.getPath()+File.separator); 165 System.out.println( "NetBeans Installation directory: " + f.getPath()); 166 } 167 else { 168 System.out.println( "No NetBeans log file found" ); 169 return null; 170 } 171 } 172 else 173 { 174 System.out.println( "No NetBeans user directory found" ); 175 return null; 176 } 177 178 179 return results; 180 } 181 182 183 getJeditLocation()184 public static Properties getJeditLocation() throws IOException { 185 186 /*if( !hasJeditInstallation() ) { 187 System.out.println( "No Jedit found (line195 InstUtil"); 188 return null; 189 }*/ 190 191 File theFile = null; 192 Properties results = new Properties(); 193 194 StringBuilder str = new StringBuilder(); 195 str.append(System.getProperty("user.home")); 196 str.append(File.separator); 197 StringBuilder thePath = new StringBuilder(str.toString()); 198 199 String os = System.getProperty("os.name"); 200 thePath.append(".jedit"); 201 //System.out.println( ".jedit path " + thePath ); 202 203 File jeditLogFile = new File( thePath.toString() + File.separator + "activity.log" ); 204 if( jeditLogFile.exists() ) { 205 String[] jeditDetails = getJeditInstallation( jeditLogFile ); 206 System.out.println( "getJeditLocation ) " + jeditDetails[0] ); 207 File f = new File(jeditDetails[0]); 208 results.put("jEdit "+jeditDetails[1], jeditDetails[0]); 209 System.out.println( "jeditDetails[0] is " + jeditDetails[0]); 210 } 211 else { 212 System.out.println( "Prompt user for Jedit installation path" ); 213 } 214 215 216 return results; 217 } 218 219 220 221 222 getNetbeansInstallation( File logFile )223 private static String getNetbeansInstallation( File logFile ) { 224 String installPath = ""; 225 try { 226 BufferedReader reader = new BufferedReader(new FileReader(logFile)); 227 228 for (String s = reader.readLine(); s != null; s = reader.readLine()) { 229 s.trim(); 230 if( s.indexOf( "IDE Install" ) != -1 ) { 231 int pathStart = s.indexOf( "=" ) + 2; 232 //System.out.println( "pathStart " + pathStart ); 233 installPath = s.substring( pathStart, s.length() ); 234 //System.out.println( "installPath 1" + installPath ); 235 int pathEnd = installPath.indexOf( ";"); 236 //System.out.println( "pathEnd " + pathEnd ); 237 installPath = installPath.substring( 0, pathEnd ) +File.separator; 238 //System.out.println( "pathStart " + pathStart ); 239 //int pathEnd = s.indexOf( ";"); 240 //System.out.println( "pathEnd " + pathEnd ); 241 //System.out.println( "s is " + s + " and " + s.length() + " long" ); 242 //installPath = s.substring( pathStart, pathEnd - 1 ); 243 installPath.trim(); 244 break; 245 } 246 } 247 } 248 catch( IOException ioe ) { 249 System.out.println( "Error reading Netbeans location information" ); 250 } 251 //catch( FileNotFoundException fnfe ) { 252 //System.out.println( "NetBeans ide.log FileNotFoundException" ); 253 //} 254 255 return installPath; 256 } 257 258 getJeditInstallation( File logFile )259 private static String[] getJeditInstallation( File logFile ) { 260 String[] jeditDetails = new String[2]; 261 try { 262 BufferedReader reader = new BufferedReader(new FileReader(logFile)); 263 String installPath = ""; 264 String version = ""; 265 266 for (String s = reader.readLine(); s != null; s = reader.readLine()) { 267 s.trim(); 268 if( s.indexOf( "jEdit home directory is" ) != -1 ) { 269 int pathStart = new String( "[message] jEdit: jEdit home directory is " ).length(); 270 //System.out.println( "pathStart " + pathStart ); 271 installPath = s.substring( pathStart, s.length() ) +File.separator; 272 System.out.println( "installPath 1" + installPath ); 273 //int pathEnd = installPath.indexOf( ";"); 274 //System.out.println( "pathEnd " + pathEnd ); 275 //installPath = installPath.substring( 0, pathEnd ) +File.separator; 276 //System.out.println( "pathStart " + pathStart ); 277 //int pathEnd = s.indexOf( ";"); 278 //System.out.println( "pathEnd " + pathEnd ); 279 //System.out.println( "s is " + s + " and " + s.length() + " long" ); 280 //installPath = s.substring( pathStart, pathEnd - 1 ); 281 installPath.trim(); 282 //System.out.println( "installPath 2 " + installPath ); 283 //break; 284 jeditDetails[0] = installPath; 285 } 286 if( s.indexOf( "jEdit: jEdit version" ) != -1 ) { 287 int versionStart = s.indexOf( "version" ) + 8; 288 System.out.println( "versionStart is: " + versionStart ); 289 version = s.substring( versionStart, s.length() ); 290 version.trim(); 291 System.out.println( "jEdit version is: " + version ); 292 jeditDetails[1] = version; 293 } 294 } 295 } 296 catch( IOException ioe ) { 297 System.out.println( "Error reading Jedit location information" ); 298 } 299 //catch( FileNotFoundException fnfe ) { 300 //System.out.println( "Jedit activity.log FileNotFoundException" ); 301 //} 302 303 return jeditDetails; 304 } 305 306 307 findVersionFile(File start)308 public static File findVersionFile(File start) 309 { 310 File versionFile = null; 311 312 File files[] = start.listFiles(new VersionFilter()); 313 if (files.length == 0) 314 { 315 File dirs[] = start.listFiles(new DirFilter()); 316 for (int i=0; i< dirs.length; i++) 317 { 318 versionFile = findVersionFile(dirs[i]); 319 if (versionFile != null) 320 { 321 break; 322 } 323 } 324 } 325 else 326 { 327 versionFile = files[0]; 328 } 329 330 return versionFile; 331 } 332 verifySversionExists(File sversionFile)333 public static boolean verifySversionExists(File sversionFile) { 334 if (!sversionFile.exists()) 335 return false; 336 return true; 337 } 338 getOfficeVersions(File sversionFile)339 public static Properties getOfficeVersions(File sversionFile) throws IOException { 340 BufferedReader reader = new BufferedReader(new FileReader(sversionFile)); 341 String sectionName = null; 342 Properties results = new Properties(); 343 344 for (String s = reader.readLine(); s != null; s = reader.readLine()) { 345 s.trim(); 346 //System.out.println(s); 347 if (s.length() == 0) 348 continue; 349 if (s.charAt(0) == '[') { 350 sectionName = s.substring(1, s.length() - 1); 351 //System.out.println(sectionName); 352 continue; 353 } 354 if ((sectionName != null) && sectionName.equalsIgnoreCase("Versions")) { 355 int equals = s.indexOf( "=" ); 356 String officeName = s.substring(0, equals ); 357 358 String instPath = s.substring(equals + 8, s.length()); 359 String [] parts = new String[2]; 360 parts[0] = officeName; 361 parts[1] = instPath + File.separator; 362 //System.out.println( "InstUtil officeName " + officeName ); 363 //System.out.println( "InstUtil instPath " + instPath ); 364 365 //String [] parts = s.split("="); 366 if (parts.length == 2) { 367 //ver.version = parts[0].trim(); 368 //File f = new File(parts[1].trim()); 369 //results.put(parts[0].trim(), f.getPath()); 370 try { 371 URL url = new URL("file://" + parts[1].trim()); 372 String opSys =System.getProperty("os.name"); 373 if (opSys.indexOf("Windows")!=-1){ 374 String windowsPath = URLDecoder.decode( url.getPath() ); 375 boolean firstSlash = true; 376 while( windowsPath.indexOf("/") != -1 ) { 377 int forwardSlashPos = windowsPath.indexOf("/"); 378 String firstPart = windowsPath.substring( 0, forwardSlashPos ); 379 String lastPart = windowsPath.substring( forwardSlashPos + 1, windowsPath.length() ); 380 if( firstSlash ) { 381 windowsPath = lastPart; 382 firstSlash = false; 383 } 384 else { 385 windowsPath = firstPart + "\\" + lastPart; 386 } 387 } 388 int lastSlash = windowsPath.lastIndexOf("\\"); 389 windowsPath = windowsPath.substring( 0, lastSlash ); 390 results.put( parts[0].trim(), windowsPath ); 391 } 392 else { 393 //System.err.println( " InstUtil URLDecoder " + URLDecoder.decode(url.getPath()) ); 394 results.put(parts[0].trim(), URLDecoder.decode(url.getPath())); 395 } 396 //File f = new File(url); 397 398 //.sversion: OpenOffice.org 643=file:///scriptdev/neil/ScriptFrameOpenoffice1.0.1 399 // parts = Installation name. f.getPath = Installation path 400 //results.put(parts[0].trim(), f.getPath()); 401 402 //results.put(parts[0].trim(), URLDecoder.decode(url.getPath())); 403 //results.put( parts[0].trim(), windowsPath ); 404 405 } 406 catch (MalformedURLException eSyntax) { 407 //throw new IOException("Error while reading version information"); 408 results.put(parts[0].trim(), parts[1].trim()); 409 //System.out.println(parts[0].trim() + " : " + parts[1].trim()); 410 System.err.println("GotHereException"); 411 } 412 } 413 else { 414 System.out.println("not splitting on equals"); 415 } 416 } 417 } 418 419 return results; 420 } 421 getJavaVersion()422 public static String getJavaVersion() { 423 return System.getProperty("java.version"); 424 } 425 isCorrectJavaVersion()426 public static boolean isCorrectJavaVersion() { 427 if (System.getProperty("java.version").startsWith("1.4")) 428 return true; 429 return false; 430 } 431 main(String args[])432 public static void main(String args[]) { 433 InstUtil inst = new InstUtil(); 434 File f = null; 435 try 436 { 437 f = inst.buildSversionLocation(); 438 } 439 catch (IOException e) 440 { 441 e.printStackTrace(); 442 System.out.println(e.getMessage()); 443 } 444 if (!inst.verifySversionExists(f)) { 445 System.err.println("Problem with sversion.ini"); 446 } 447 try { 448 Properties vers = inst.getOfficeVersions(f); 449 } catch (IOException e) { 450 e.printStackTrace(); 451 System.err.println(e); 452 } 453 System.out.println(inst.getJavaVersion()); 454 if (!inst.isCorrectJavaVersion()) { 455 System.err.println("Not correct Java Version"); 456 } 457 } 458 459 public static final String [] versions = {"NetBeans 3.4", "jEdit 4.0.3", "jEdit 4.1pre5" }; 460 private static File tmpDir = null; 461 } 462 463 464 465 class DirFilter implements java.io.FileFilter 466 { accept(File aFile)467 public boolean accept(File aFile) 468 { 469 return aFile.isDirectory(); 470 } 471 } 472 class VersionFilter implements java.io.FileFilter 473 { accept(File aFile)474 public boolean accept(File aFile) 475 { 476 if (aFile.getName().compareToIgnoreCase("sversion.ini") == 0) 477 { 478 return true; 479 } 480 481 return false; 482 } 483 } 484