xref: /AOO41X/main/scripting/java/org/openoffice/idesupport/SVersionRCFile.java (revision cd519653a6b6a9e2ff38774da567b1ae7cbeddbb)
1*cd519653SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*cd519653SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*cd519653SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*cd519653SAndrew Rist  * distributed with this work for additional information
6*cd519653SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*cd519653SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*cd519653SAndrew Rist  * "License"); you may not use this file except in compliance
9*cd519653SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*cd519653SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*cd519653SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*cd519653SAndrew Rist  * software distributed under the License is distributed on an
15*cd519653SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*cd519653SAndrew Rist  * KIND, either express or implied.  See the License for the
17*cd519653SAndrew Rist  * specific language governing permissions and limitations
18*cd519653SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*cd519653SAndrew Rist  *************************************************************/
21*cd519653SAndrew Rist 
22*cd519653SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package org.openoffice.idesupport;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.io.File;
27cdf0e10cSrcweir import java.io.BufferedReader;
28cdf0e10cSrcweir import java.io.FileReader;
29cdf0e10cSrcweir import java.io.IOException;
30cdf0e10cSrcweir import java.io.FileNotFoundException;
31cdf0e10cSrcweir import java.util.Vector;
32cdf0e10cSrcweir import java.util.HashMap;
33cdf0e10cSrcweir import java.util.Enumeration;
34cdf0e10cSrcweir import java.util.StringTokenizer;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir public class SVersionRCFile {
37cdf0e10cSrcweir 
38cdf0e10cSrcweir     public static final String DEFAULT_NAME =
39cdf0e10cSrcweir         System.getProperty("os.name").startsWith("Windows") == true ?
40cdf0e10cSrcweir             System.getProperty("user.home") + File.separator +
41cdf0e10cSrcweir             "Application Data" + File.separator + "sversion.ini" :
42cdf0e10cSrcweir             System.getProperty("user.home") + File.separator +
43cdf0e10cSrcweir             ".sversionrc";
44cdf0e10cSrcweir 
45cdf0e10cSrcweir     public static final String FILE_URL_PREFIX =
46cdf0e10cSrcweir         System.getProperty("os.name").startsWith("Windows") == true ?
47cdf0e10cSrcweir             "file:///" : "file://";
48cdf0e10cSrcweir 
49cdf0e10cSrcweir     public static final String PKGCHK =
50cdf0e10cSrcweir         System.getProperty("os.name").startsWith("Windows") == true ?
51cdf0e10cSrcweir             "pkgchk.exe" : "pkgchk";
52cdf0e10cSrcweir 
53cdf0e10cSrcweir     private static final String VERSIONS_LINE = "[Versions]";
54cdf0e10cSrcweir 
55cdf0e10cSrcweir     private static final String UNOILJAR =
56cdf0e10cSrcweir         "skip_registration" + File.separator + "unoil.jar";
57cdf0e10cSrcweir 
58cdf0e10cSrcweir     private static final String UNOPACKAGEDIR =
59cdf0e10cSrcweir         File.separator + "user" + File.separator + "uno_packages" +
60cdf0e10cSrcweir         File.separator + "cache" + File.separator + "uno_packages";
61cdf0e10cSrcweir 
62cdf0e10cSrcweir     /* Make sure this is in LowerCase !!!!! */
63cdf0e10cSrcweir     private static final String SCRIPTF = "scriptf";
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     private static final HashMap files = new HashMap(3);
66cdf0e10cSrcweir 
67cdf0e10cSrcweir     private File sversionrc = null;
68cdf0e10cSrcweir     private OfficeInstallation defaultversion = null;
69cdf0e10cSrcweir     private Vector versions = null;
70cdf0e10cSrcweir     private long lastModified = 0;
71cdf0e10cSrcweir 
SVersionRCFile()72cdf0e10cSrcweir     public SVersionRCFile() {
73cdf0e10cSrcweir         this(DEFAULT_NAME);
74cdf0e10cSrcweir     }
75cdf0e10cSrcweir 
SVersionRCFile(String name)76cdf0e10cSrcweir     public SVersionRCFile(String name) {
77cdf0e10cSrcweir         sversionrc = new File(name);
78cdf0e10cSrcweir         versions = new Vector(5);
79cdf0e10cSrcweir     }
80cdf0e10cSrcweir 
createInstance()81cdf0e10cSrcweir     public static SVersionRCFile createInstance() {
82cdf0e10cSrcweir         return(createInstance(DEFAULT_NAME));
83cdf0e10cSrcweir     }
84cdf0e10cSrcweir 
createInstance(String name)85cdf0e10cSrcweir     public static SVersionRCFile createInstance(String name) {
86cdf0e10cSrcweir         SVersionRCFile result = null;
87cdf0e10cSrcweir 
88cdf0e10cSrcweir         synchronized(SVersionRCFile.class) {
89cdf0e10cSrcweir             result = (SVersionRCFile)files.get(name);
90cdf0e10cSrcweir 
91cdf0e10cSrcweir             if (result == null) {
92cdf0e10cSrcweir                 result = new SVersionRCFile(name);
93cdf0e10cSrcweir                 files.put(name, result);
94cdf0e10cSrcweir             }
95cdf0e10cSrcweir         }
96cdf0e10cSrcweir         return result;
97cdf0e10cSrcweir     }
98cdf0e10cSrcweir 
getDefaultVersion()99cdf0e10cSrcweir     public OfficeInstallation getDefaultVersion() throws IOException {
100cdf0e10cSrcweir         if (defaultversion == null) {
101cdf0e10cSrcweir             getVersions();
102cdf0e10cSrcweir         }
103cdf0e10cSrcweir 
104cdf0e10cSrcweir         return defaultversion;
105cdf0e10cSrcweir     }
106cdf0e10cSrcweir 
getVersions()107cdf0e10cSrcweir     public Enumeration getVersions() throws IOException {
108cdf0e10cSrcweir 
109cdf0e10cSrcweir         long l = sversionrc.lastModified();
110cdf0e10cSrcweir 
111cdf0e10cSrcweir         if (l > lastModified) {
112cdf0e10cSrcweir             BufferedReader br = null;
113cdf0e10cSrcweir 
114cdf0e10cSrcweir             try {
115cdf0e10cSrcweir                 br = new BufferedReader(new FileReader(sversionrc));
116cdf0e10cSrcweir                 load(br);
117cdf0e10cSrcweir                 lastModified = l;
118cdf0e10cSrcweir             }
119cdf0e10cSrcweir             catch (FileNotFoundException fnfe) {
120cdf0e10cSrcweir                 throw new IOException(fnfe.getMessage());
121cdf0e10cSrcweir             }
122cdf0e10cSrcweir             finally {
123cdf0e10cSrcweir                 if (br != null)
124cdf0e10cSrcweir                     br.close();
125cdf0e10cSrcweir             }
126cdf0e10cSrcweir         }
127cdf0e10cSrcweir         return versions.elements();
128cdf0e10cSrcweir     }
129cdf0e10cSrcweir 
load(BufferedReader br)130cdf0e10cSrcweir     private void load(BufferedReader br) throws IOException {
131cdf0e10cSrcweir         String s;
132cdf0e10cSrcweir 
133cdf0e10cSrcweir         while ((s = br.readLine()) != null &&
134cdf0e10cSrcweir               (s.equals(VERSIONS_LINE)) != true);
135cdf0e10cSrcweir 
136cdf0e10cSrcweir         while ((s = br.readLine()) != null &&
137cdf0e10cSrcweir               (s.equals("")) != true) {
138cdf0e10cSrcweir             StringTokenizer tokens = new StringTokenizer(s, "=");
139cdf0e10cSrcweir             int count = tokens.countTokens();
140cdf0e10cSrcweir 
141cdf0e10cSrcweir             if (count != 2)
142cdf0e10cSrcweir                 continue;
143cdf0e10cSrcweir 
144cdf0e10cSrcweir             String name = tokens.nextToken();
145cdf0e10cSrcweir             String path = tokens.nextToken();
146cdf0e10cSrcweir             OfficeInstallation oi = new OfficeInstallation(name, path);
147cdf0e10cSrcweir             if (oi.supportsFramework()) {
148cdf0e10cSrcweir                 versions.add(oi);
149cdf0e10cSrcweir                 defaultversion = oi;
150cdf0e10cSrcweir             }
151cdf0e10cSrcweir         }
152cdf0e10cSrcweir     }
153cdf0e10cSrcweir 
toFileURL(String path)154cdf0e10cSrcweir     public static String toFileURL(String path) {
155cdf0e10cSrcweir         File f = new File(path);
156cdf0e10cSrcweir 
157cdf0e10cSrcweir         if (!f.exists())
158cdf0e10cSrcweir             return null;
159cdf0e10cSrcweir 
160cdf0e10cSrcweir         try {
161cdf0e10cSrcweir             path = f.getCanonicalPath();
162cdf0e10cSrcweir         }
163cdf0e10cSrcweir         catch (IOException ioe) {
164cdf0e10cSrcweir             return null;
165cdf0e10cSrcweir         }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir         if (System.getProperty("os.name").startsWith("Windows"))
168cdf0e10cSrcweir             path = path.replace(File.separatorChar, '/');
169cdf0e10cSrcweir 
170cdf0e10cSrcweir         StringBuffer buf = new StringBuffer(FILE_URL_PREFIX);
171cdf0e10cSrcweir         buf.append(path);
172cdf0e10cSrcweir 
173cdf0e10cSrcweir         if (f.isDirectory())
174cdf0e10cSrcweir             buf.append("/");
175cdf0e10cSrcweir 
176cdf0e10cSrcweir         return buf.toString();
177cdf0e10cSrcweir     }
178cdf0e10cSrcweir 
getPathForUnoil(String officeInstall)179cdf0e10cSrcweir     public static String getPathForUnoil(String officeInstall)
180cdf0e10cSrcweir     {
181cdf0e10cSrcweir         File unopkgdir = new File(officeInstall, UNOPACKAGEDIR);
182cdf0e10cSrcweir         if(!unopkgdir.exists())
183cdf0e10cSrcweir         {
184cdf0e10cSrcweir             return null;
185cdf0e10cSrcweir         }
186cdf0e10cSrcweir         File scriptf = null;
187cdf0e10cSrcweir         String[] listunopkg = unopkgdir.list();
188cdf0e10cSrcweir         int size = listunopkg.length;
189cdf0e10cSrcweir         for(int i=0; i<size; i++)
190cdf0e10cSrcweir         {
191cdf0e10cSrcweir             if (listunopkg[i].toLowerCase().indexOf(SCRIPTF)>-1)
192cdf0e10cSrcweir             {
193cdf0e10cSrcweir                 scriptf = new File(unopkgdir, listunopkg[i]);
194cdf0e10cSrcweir             }
195cdf0e10cSrcweir         }
196cdf0e10cSrcweir         if(scriptf != null)
197cdf0e10cSrcweir         {
198cdf0e10cSrcweir             File unoil = new File(scriptf, UNOILJAR);
199cdf0e10cSrcweir             if(unoil.exists())
200cdf0e10cSrcweir             {
201cdf0e10cSrcweir                 String path = unoil.getParent();
202cdf0e10cSrcweir                 path = path.substring(path.indexOf(UNOPACKAGEDIR));
203cdf0e10cSrcweir                 return officeInstall + path;
204cdf0e10cSrcweir             }
205cdf0e10cSrcweir         }
206cdf0e10cSrcweir         return null;
207cdf0e10cSrcweir     }
208cdf0e10cSrcweir 
main(String[] args)209cdf0e10cSrcweir     public static void main(String[] args) {
210cdf0e10cSrcweir         SVersionRCFile ov;
211cdf0e10cSrcweir 
212cdf0e10cSrcweir         if (args.length == 0)
213cdf0e10cSrcweir             ov = new SVersionRCFile();
214cdf0e10cSrcweir         else
215cdf0e10cSrcweir             ov = new SVersionRCFile(args[0]);
216cdf0e10cSrcweir 
217cdf0e10cSrcweir         Enumeration enumer;
218cdf0e10cSrcweir 
219cdf0e10cSrcweir         try {
220cdf0e10cSrcweir             enumer = ov.getVersions();
221cdf0e10cSrcweir         }
222cdf0e10cSrcweir         catch (IOException ioe) {
223cdf0e10cSrcweir             System.err.println("Error getting versions: " + ioe.getMessage());
224cdf0e10cSrcweir             return;
225cdf0e10cSrcweir         }
226cdf0e10cSrcweir 
227cdf0e10cSrcweir         while (enumer.hasMoreElements()) {
228cdf0e10cSrcweir             OfficeInstallation oi = (OfficeInstallation)enumer.nextElement();
229cdf0e10cSrcweir             System.out.println("Name: " + oi.getName() + ", Path: " + oi.getPath() +
230cdf0e10cSrcweir                 ", URL: " + oi.getURL());
231cdf0e10cSrcweir         }
232cdf0e10cSrcweir     }
233cdf0e10cSrcweir }
234