xref: /AOO41X/main/scripting/java/org/openoffice/idesupport/SVersionRCFile.java (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package org.openoffice.idesupport;
29 
30 import java.io.File;
31 import java.io.BufferedReader;
32 import java.io.FileReader;
33 import java.io.IOException;
34 import java.io.FileNotFoundException;
35 import java.util.Vector;
36 import java.util.HashMap;
37 import java.util.Enumeration;
38 import java.util.StringTokenizer;
39 
40 public class SVersionRCFile {
41 
42     public static final String DEFAULT_NAME =
43         System.getProperty("os.name").startsWith("Windows") == true ?
44             System.getProperty("user.home") + File.separator +
45             "Application Data" + File.separator + "sversion.ini" :
46             System.getProperty("user.home") + File.separator +
47             ".sversionrc";
48 
49     public static final String FILE_URL_PREFIX =
50         System.getProperty("os.name").startsWith("Windows") == true ?
51             "file:///" : "file://";
52 
53     public static final String PKGCHK =
54         System.getProperty("os.name").startsWith("Windows") == true ?
55             "pkgchk.exe" : "pkgchk";
56 
57     private static final String VERSIONS_LINE = "[Versions]";
58 
59     private static final String UNOILJAR =
60         "skip_registration" + File.separator + "unoil.jar";
61 
62     private static final String UNOPACKAGEDIR =
63         File.separator + "user" + File.separator + "uno_packages" +
64         File.separator + "cache" + File.separator + "uno_packages";
65 
66     /* Make sure this is in LowerCase !!!!! */
67     private static final String SCRIPTF = "scriptf";
68 
69     private static final HashMap files = new HashMap(3);
70 
71     private File sversionrc = null;
72     private OfficeInstallation defaultversion = null;
73     private Vector versions = null;
74     private long lastModified = 0;
75 
76     public SVersionRCFile() {
77         this(DEFAULT_NAME);
78     }
79 
80     public SVersionRCFile(String name) {
81         sversionrc = new File(name);
82         versions = new Vector(5);
83     }
84 
85     public static SVersionRCFile createInstance() {
86         return(createInstance(DEFAULT_NAME));
87     }
88 
89     public static SVersionRCFile createInstance(String name) {
90         SVersionRCFile result = null;
91 
92         synchronized(SVersionRCFile.class) {
93             result = (SVersionRCFile)files.get(name);
94 
95             if (result == null) {
96                 result = new SVersionRCFile(name);
97                 files.put(name, result);
98             }
99         }
100         return result;
101     }
102 
103     public OfficeInstallation getDefaultVersion() throws IOException {
104         if (defaultversion == null) {
105             getVersions();
106         }
107 
108         return defaultversion;
109     }
110 
111     public Enumeration getVersions() throws IOException {
112 
113         long l = sversionrc.lastModified();
114 
115         if (l > lastModified) {
116             BufferedReader br = null;
117 
118             try {
119                 br = new BufferedReader(new FileReader(sversionrc));
120                 load(br);
121                 lastModified = l;
122             }
123             catch (FileNotFoundException fnfe) {
124                 throw new IOException(fnfe.getMessage());
125             }
126             finally {
127                 if (br != null)
128                     br.close();
129             }
130         }
131         return versions.elements();
132     }
133 
134     private void load(BufferedReader br) throws IOException {
135         String s;
136 
137         while ((s = br.readLine()) != null &&
138               (s.equals(VERSIONS_LINE)) != true);
139 
140         while ((s = br.readLine()) != null &&
141               (s.equals("")) != true) {
142             StringTokenizer tokens = new StringTokenizer(s, "=");
143             int count = tokens.countTokens();
144 
145             if (count != 2)
146                 continue;
147 
148             String name = tokens.nextToken();
149             String path = tokens.nextToken();
150             OfficeInstallation oi = new OfficeInstallation(name, path);
151             if (oi.supportsFramework()) {
152                 versions.add(oi);
153                 defaultversion = oi;
154             }
155         }
156     }
157 
158     public static String toFileURL(String path) {
159         File f = new File(path);
160 
161         if (!f.exists())
162             return null;
163 
164         try {
165             path = f.getCanonicalPath();
166         }
167         catch (IOException ioe) {
168             return null;
169         }
170 
171         if (System.getProperty("os.name").startsWith("Windows"))
172             path = path.replace(File.separatorChar, '/');
173 
174         StringBuffer buf = new StringBuffer(FILE_URL_PREFIX);
175         buf.append(path);
176 
177         if (f.isDirectory())
178             buf.append("/");
179 
180         return buf.toString();
181     }
182 
183     public static String getPathForUnoil(String officeInstall)
184     {
185         File unopkgdir = new File(officeInstall, UNOPACKAGEDIR);
186         if(!unopkgdir.exists())
187         {
188             return null;
189         }
190         File scriptf = null;
191         String[] listunopkg = unopkgdir.list();
192         int size = listunopkg.length;
193         for(int i=0; i<size; i++)
194         {
195             if (listunopkg[i].toLowerCase().indexOf(SCRIPTF)>-1)
196             {
197                 scriptf = new File(unopkgdir, listunopkg[i]);
198             }
199         }
200         if(scriptf != null)
201         {
202             File unoil = new File(scriptf, UNOILJAR);
203             if(unoil.exists())
204             {
205                 String path = unoil.getParent();
206                 path = path.substring(path.indexOf(UNOPACKAGEDIR));
207                 return officeInstall + path;
208             }
209         }
210         return null;
211     }
212 
213     public static void main(String[] args) {
214         SVersionRCFile ov;
215 
216         if (args.length == 0)
217             ov = new SVersionRCFile();
218         else
219             ov = new SVersionRCFile(args[0]);
220 
221         Enumeration enumer;
222 
223         try {
224             enumer = ov.getVersions();
225         }
226         catch (IOException ioe) {
227             System.err.println("Error getting versions: " + ioe.getMessage());
228             return;
229         }
230 
231         while (enumer.hasMoreElements()) {
232             OfficeInstallation oi = (OfficeInstallation)enumer.nextElement();
233             System.out.println("Name: " + oi.getName() + ", Path: " + oi.getPath() +
234                 ", URL: " + oi.getURL());
235         }
236     }
237 }
238