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 graphical;
29 
30 import java.io.File;
31 import helper.OSHelper;
32 
33 public class BuildID
34 {
35     private static String getOfficePath(String _sApp)
36     {
37         String sOfficePath = "";
38         // TODO: StringHelper.removeQuote?
39         if (_sApp.startsWith("\""))
40         {
41             int nIdx = _sApp.indexOf("\"", 1);
42             if (nIdx != -1)
43             {
44                 // leave double qoute out.
45                 sOfficePath = _sApp.substring(1, nIdx);
46             }
47         }
48         else
49         {
50             // check if _sApp ends with the office executable, if not
51             if (! (_sApp.endsWith("soffice.exe") || _sApp.endsWith("soffice")))
52             {
53                 // check if a space exist, so we get all until space
54                 int nIdx = _sApp.indexOf(" ", 1);
55                 if (nIdx == -1)
56                 {
57                     sOfficePath = _sApp;
58                 }
59                 else
60                 {
61                     sOfficePath = _sApp.substring(0, nIdx);
62                 }
63             }
64             else
65             {
66                 sOfficePath = _sApp;
67             }
68         }
69         // GlobalLogWriter.get().println("Office path: " + sOfficePath);
70         return sOfficePath;
71     }
72 
73     public static String getBuildID(String _sApp)
74         {
75             final String sOfficePath = getOfficePath(_sApp);
76             final String sBuildID = getBuildID(sOfficePath, "buildid");
77             return sBuildID;
78     }
79 
80     private static String getBuildID(String _sOfficePath, String _sIniSection)
81         {
82             File aSOfficeFile = new File(_sOfficePath);
83             String sBuildID = "";
84             if (aSOfficeFile.exists())
85             {
86                 String sOfficePath = FileHelper.getPath(_sOfficePath);
87                 // ok. System.out.println("directory: " + sOfficePath);
88                 sBuildID = getBuildIDFromBootstrap(sOfficePath, _sIniSection);
89                 if (sBuildID.length() == 0)
90                 {
91                     sBuildID = getBuildIDFromVersion(sOfficePath, _sIniSection);
92                 }
93             }
94             else
95             {
96                 GlobalLogWriter.println("soffice executable not found.");
97             }
98 
99 //            int dummy = 0;
100             return sBuildID;
101         }
102 
103     private static String getBuildIDFromBootstrap(String _sOfficePath, String _sIniSection)
104         {
105             String sBuildID = "";
106             String sOfficePath;
107             if (OSHelper.isWindows())
108             {
109                 sOfficePath = FileHelper.appendPath(_sOfficePath, "bootstrap.ini");
110             }
111             else
112             {
113                 sOfficePath = FileHelper.appendPath(_sOfficePath, "bootstraprc");
114             }
115             IniFile aIniFile = new IniFile(sOfficePath);
116             if (aIniFile.is())
117             {
118                 sBuildID = aIniFile.getValue("Bootstrap", /*"buildid"*/ _sIniSection);
119             }
120             else
121             {
122                 GlobalLogWriter.println("Property Build, can't open file '" + sOfficePath + "', please check.");
123             }
124             return sBuildID;
125         }
126 
127     private static String getBuildIDFromVersion(String _sOfficePath, String _sIniSection)
128         {
129             // String fs = System.getProperty("file.separator");
130             String sBuildID = "";
131             String sOfficePath;
132             if (OSHelper.isWindows())
133             {
134                 sOfficePath = FileHelper.appendPath(_sOfficePath, "version.ini");
135             }
136             else
137             {
138                 sOfficePath = FileHelper.appendPath(_sOfficePath, "versionrc");
139             }
140             IniFile aIniFile = new IniFile(sOfficePath);
141             if (aIniFile.is())
142             {
143                 sBuildID = aIniFile.getValue("Version", /*"buildid"*/ _sIniSection);
144             }
145             else
146             {
147                 GlobalLogWriter.println("Property Build, can't open file '" + sOfficePath + "', please check.");
148             }
149             return sBuildID;
150         }
151 
152 //    public static void main(String[] args)
153 //        {
154 //            String sApp;
155 //            sApp = "/opt/staroffice8_m116/program/soffice -headless -accept=socket,host=localhost,port=8100;urp;";
156 //            String sBuildID;
157 //            sBuildID = getBuildID(sApp);
158 //            System.out.println("BuildID is: " + sBuildID);
159 //
160 //            Date aDate = new Date();
161 //            long nStart = aDate.getTime();
162 //            System.out.println("Time:" + nStart);
163 //            // LLA: Just some more tests for getBuildID
164 //            // sApp = "/opt/staroffice8_net/program/soffice";
165 //            // sBuildID = getBuildID(sApp);
166 //            // System.out.println("BuildID is: " + sBuildID);
167 //            //
168 //            // sApp = "\"/opt/staroffice8_net/program/soffice\" test blah";
169 //            // sBuildID = getBuildID(sApp);
170 //            //
171 //            // System.out.println("BuildID is: " + sBuildID);
172 //            System.exit(1);
173 //        }
174 
175             public static String getMaster(String _sOfficePath)
176             {
177                 final String sOfficePath = getOfficePath(_sOfficePath);
178                 final String sMaster = getBuildID(sOfficePath, "ProductSource");
179                 return sMaster;
180             }
181 
182             public static String getMinor(String _sOfficePath)
183             {
184                 final String sOfficePath = getOfficePath(_sOfficePath);
185                 final String sMinor = "m" + getBuildID(sOfficePath, "ProductMinor");
186                 return sMinor;
187             }
188 
189             public static String getCWSName(String _sOfficePath)
190             {
191                 final String sOfficePath = getOfficePath(_sOfficePath);
192                 final String sBuildID = getBuildID(sOfficePath, "buildid");
193                 String sCWSName = "MWS";
194                 int nIdx = sBuildID.indexOf("[CWS:");
195                 if (nIdx > 0)
196                 {
197                     int nIdx2 = sBuildID.indexOf("]", nIdx);
198                     sCWSName = sBuildID.substring(nIdx + 5, nIdx2);
199                 }
200                 return sCWSName;
201             }
202 
203 //    public static void main(String[] args)
204 //        {
205 //            String sApp;
206 //            sApp = "D:/staroffice9_m63/Sun/StarOffice 9/program/soffice.exe";
207 //            String sBuildID;
208 //            sBuildID = getBuildID(sApp);
209 //            System.out.println("BuildID is: " + sBuildID);
210 //
211 //            String sMaster;
212 //            sMaster = getMaster(sApp);
213 //            System.out.println("Master is: " + sMaster);
214 //
215 //            String sMinor;
216 //            sMinor = getMinor(sApp);
217 //            System.out.println("Minor is: " + sMinor);
218 //
219 //            String sCWSName;
220 //            sCWSName = getCWSName(sApp);
221 //            System.out.println("CWSName is: " + sCWSName);
222 //
223 //            System.exit(1);
224 //        }
225 
226 }
227