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