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 23 24 package org.openoffice.idesupport; 25 26 import java.io.File; 27 import java.io.BufferedReader; 28 import java.io.FileReader; 29 import java.io.IOException; 30 import java.io.FileNotFoundException; 31 import java.util.Vector; 32 import java.util.HashMap; 33 import java.util.Enumeration; 34 import java.util.StringTokenizer; 35 36 public class SVersionRCFile { 37 38 public static final String DEFAULT_NAME = 39 System.getProperty("os.name").startsWith("Windows") == true ? 40 System.getProperty("user.home") + File.separator + 41 "Application Data" + File.separator + "sversion.ini" : 42 System.getProperty("user.home") + File.separator + 43 ".sversionrc"; 44 45 public static final String FILE_URL_PREFIX = 46 System.getProperty("os.name").startsWith("Windows") == true ? 47 "file:///" : "file://"; 48 49 public static final String PKGCHK = 50 System.getProperty("os.name").startsWith("Windows") == true ? 51 "pkgchk.exe" : "pkgchk"; 52 53 private static final String VERSIONS_LINE = "[Versions]"; 54 55 private static final String UNOILJAR = 56 "skip_registration" + File.separator + "unoil.jar"; 57 58 private static final String UNOPACKAGEDIR = 59 File.separator + "user" + File.separator + "uno_packages" + 60 File.separator + "cache" + File.separator + "uno_packages"; 61 62 /* Make sure this is in LowerCase !!!!! */ 63 private static final String SCRIPTF = "scriptf"; 64 65 private static final HashMap files = new HashMap(3); 66 67 private File sversionrc = null; 68 private OfficeInstallation defaultversion = null; 69 private Vector versions = null; 70 private long lastModified = 0; 71 SVersionRCFile()72 public SVersionRCFile() { 73 this(DEFAULT_NAME); 74 } 75 SVersionRCFile(String name)76 public SVersionRCFile(String name) { 77 sversionrc = new File(name); 78 versions = new Vector(5); 79 } 80 createInstance()81 public static SVersionRCFile createInstance() { 82 return(createInstance(DEFAULT_NAME)); 83 } 84 createInstance(String name)85 public static SVersionRCFile createInstance(String name) { 86 SVersionRCFile result = null; 87 88 synchronized(SVersionRCFile.class) { 89 result = (SVersionRCFile)files.get(name); 90 91 if (result == null) { 92 result = new SVersionRCFile(name); 93 files.put(name, result); 94 } 95 } 96 return result; 97 } 98 getDefaultVersion()99 public OfficeInstallation getDefaultVersion() throws IOException { 100 if (defaultversion == null) { 101 getVersions(); 102 } 103 104 return defaultversion; 105 } 106 getVersions()107 public Enumeration getVersions() throws IOException { 108 109 long l = sversionrc.lastModified(); 110 111 if (l > lastModified) { 112 BufferedReader br = null; 113 114 try { 115 br = new BufferedReader(new FileReader(sversionrc)); 116 load(br); 117 lastModified = l; 118 } 119 catch (FileNotFoundException fnfe) { 120 throw new IOException(fnfe.getMessage()); 121 } 122 finally { 123 if (br != null) 124 br.close(); 125 } 126 } 127 return versions.elements(); 128 } 129 load(BufferedReader br)130 private void load(BufferedReader br) throws IOException { 131 String s; 132 133 while ((s = br.readLine()) != null && 134 (s.equals(VERSIONS_LINE)) != true); 135 136 while ((s = br.readLine()) != null && 137 (s.equals("")) != true) { 138 StringTokenizer tokens = new StringTokenizer(s, "="); 139 int count = tokens.countTokens(); 140 141 if (count != 2) 142 continue; 143 144 String name = tokens.nextToken(); 145 String path = tokens.nextToken(); 146 OfficeInstallation oi = new OfficeInstallation(name, path); 147 if (oi.supportsFramework()) { 148 versions.add(oi); 149 defaultversion = oi; 150 } 151 } 152 } 153 toFileURL(String path)154 public static String toFileURL(String path) { 155 File f = new File(path); 156 157 if (!f.exists()) 158 return null; 159 160 try { 161 path = f.getCanonicalPath(); 162 } 163 catch (IOException ioe) { 164 return null; 165 } 166 167 if (System.getProperty("os.name").startsWith("Windows")) 168 path = path.replace(File.separatorChar, '/'); 169 170 StringBuffer buf = new StringBuffer(FILE_URL_PREFIX); 171 buf.append(path); 172 173 if (f.isDirectory()) 174 buf.append("/"); 175 176 return buf.toString(); 177 } 178 getPathForUnoil(String officeInstall)179 public static String getPathForUnoil(String officeInstall) 180 { 181 File unopkgdir = new File(officeInstall, UNOPACKAGEDIR); 182 if(!unopkgdir.exists()) 183 { 184 return null; 185 } 186 File scriptf = null; 187 String[] listunopkg = unopkgdir.list(); 188 int size = listunopkg.length; 189 for(int i=0; i<size; i++) 190 { 191 if (listunopkg[i].toLowerCase().indexOf(SCRIPTF)>-1) 192 { 193 scriptf = new File(unopkgdir, listunopkg[i]); 194 } 195 } 196 if(scriptf != null) 197 { 198 File unoil = new File(scriptf, UNOILJAR); 199 if(unoil.exists()) 200 { 201 String path = unoil.getParent(); 202 path = path.substring(path.indexOf(UNOPACKAGEDIR)); 203 return officeInstall + path; 204 } 205 } 206 return null; 207 } 208 main(String[] args)209 public static void main(String[] args) { 210 SVersionRCFile ov; 211 212 if (args.length == 0) 213 ov = new SVersionRCFile(); 214 else 215 ov = new SVersionRCFile(args[0]); 216 217 Enumeration enumer; 218 219 try { 220 enumer = ov.getVersions(); 221 } 222 catch (IOException ioe) { 223 System.err.println("Error getting versions: " + ioe.getMessage()); 224 return; 225 } 226 227 while (enumer.hasMoreElements()) { 228 OfficeInstallation oi = (OfficeInstallation)enumer.nextElement(); 229 System.out.println("Name: " + oi.getName() + ", Path: " + oi.getPath() + 230 ", URL: " + oi.getURL()); 231 } 232 } 233 } 234