xref: /AOO42X/main/qadevOOo/runner/util/utils.java (revision b275ba1e209f5c0cd98ea59c7ca7c8c3c906970f)
1ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5ef39d40dSAndrew Rist  * distributed with this work for additional information
6ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18ef39d40dSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20ef39d40dSAndrew Rist  *************************************************************/
21ef39d40dSAndrew Rist 
22cdf0e10cSrcweir package util;
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.frame.XController;
25cdf0e10cSrcweir import com.sun.star.frame.XDispatch;
26cdf0e10cSrcweir import com.sun.star.frame.XDispatchProvider;
27cdf0e10cSrcweir import com.sun.star.frame.XModel;
28cdf0e10cSrcweir import com.sun.star.lang.XComponent;
29cdf0e10cSrcweir import java.lang.System;
30cdf0e10cSrcweir import java.util.StringTokenizer;
31cdf0e10cSrcweir import java.io.*;
32cdf0e10cSrcweir import java.util.ArrayList;
33cdf0e10cSrcweir import java.io.RandomAccessFile;
34cdf0e10cSrcweir import java.net.Socket;
35cdf0e10cSrcweir import java.net.ServerSocket;
36cdf0e10cSrcweir import java.net.URI;
37cdf0e10cSrcweir import java.net.URISyntaxException;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
40cdf0e10cSrcweir import com.sun.star.beans.Property;
41cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
42cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
43cdf0e10cSrcweir import com.sun.star.ucb.InteractiveAugmentedIOException;
44cdf0e10cSrcweir import com.sun.star.ucb.XSimpleFileAccess;
45cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir import com.sun.star.util.URL;
48cdf0e10cSrcweir import com.sun.star.util.XURLTransformer;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
51cdf0e10cSrcweir import com.sun.star.uno.Type;
52cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
53cdf0e10cSrcweir import com.sun.star.util.XMacroExpander;
54cdf0e10cSrcweir import java.text.DecimalFormat;
55cdf0e10cSrcweir import java.util.Calendar;
56cdf0e10cSrcweir 
57cdf0e10cSrcweir import java.util.Collections;
58cdf0e10cSrcweir import java.util.GregorianCalendar;
59cdf0e10cSrcweir 
60cdf0e10cSrcweir public class utils {
61cdf0e10cSrcweir 
62cdf0e10cSrcweir     /**
63cdf0e10cSrcweir      *
64cdf0e10cSrcweir      * This method adds the DOCPTH to a given file
65cdf0e10cSrcweir      *
66cdf0e10cSrcweir      * @param sDocName the file which should be completed to the test doc path
67cdf0e10cSrcweir      * @return $TESTDOCPATH/sDocName
68cdf0e10cSrcweir      */
getFullTestDocName(String sDocName)69cdf0e10cSrcweir     public static String getFullTestDocName(String sDocName) {
70cdf0e10cSrcweir         String docpth = System.getProperty("DOCPTH");
71cdf0e10cSrcweir         if (docpth.endsWith("\\") || docpth.endsWith("/")) {
72cdf0e10cSrcweir             docpth = docpth.substring(0, docpth.length() - 1);
73cdf0e10cSrcweir         }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir         System.out.println("docpth:" + docpth);
76cdf0e10cSrcweir 
77cdf0e10cSrcweir         String pthSep = System.getProperty("file.separator");
78cdf0e10cSrcweir 
79cdf0e10cSrcweir         if (docpth.equals("unknown")) {
80cdf0e10cSrcweir             System.out.println("try to get tDoc from $SRC_ROOT/qadevOOo");
81cdf0e10cSrcweir             String srcRoot = System.getProperty(PropertyName.SRC_ROOT);
82cdf0e10cSrcweir             if (srcRoot != null) {
83cdf0e10cSrcweir                 File srcR = new File(srcRoot);
84cdf0e10cSrcweir                 String[] list = srcR.list(new FilenameFilter() {
85cdf0e10cSrcweir 
86cdf0e10cSrcweir                     public boolean accept(File dir, String name) {
87cdf0e10cSrcweir                         return name.startsWith("qadevOOo");
88cdf0e10cSrcweir                     }
89cdf0e10cSrcweir                 });
90cdf0e10cSrcweir 
91cdf0e10cSrcweir                 if (list[0] != null) {
92cdf0e10cSrcweir                     String tDoc = srcRoot.concat(pthSep).concat(list[0]).concat(pthSep).concat("testdocs");
93cdf0e10cSrcweir 
94cdf0e10cSrcweir                     if (new File(tDoc).exists()) {
95cdf0e10cSrcweir                         docpth = tDoc;
96cdf0e10cSrcweir                     }
97cdf0e10cSrcweir                 }
98cdf0e10cSrcweir             }
99cdf0e10cSrcweir         }
100cdf0e10cSrcweir 
101cdf0e10cSrcweir         if (docpth.startsWith("http:")) {
102cdf0e10cSrcweir             return docpth + "/" + sDocName;
103cdf0e10cSrcweir         }
104cdf0e10cSrcweir         String testdocPth = "";
105cdf0e10cSrcweir 
106cdf0e10cSrcweir         if (docpth.equals("unknown")) {
107cdf0e10cSrcweir             System.out.println("try to get tDoc from OBJDSCS");
108cdf0e10cSrcweir             String objdscPth = System.getProperty("OBJDSCS");
109cdf0e10cSrcweir             if (objdscPth != null) {
110cdf0e10cSrcweir                 int i = objdscPth.indexOf("objdsc");
111cdf0e10cSrcweir                 String arcPth = objdscPth.substring(0, i - 1);
112cdf0e10cSrcweir                 testdocPth = arcPth + pthSep + "doc" + pthSep + "java" +
113cdf0e10cSrcweir                     pthSep + "testdocs" + pthSep + sDocName;
114cdf0e10cSrcweir             }
115cdf0e10cSrcweir         } else {
116cdf0e10cSrcweir             testdocPth = docpth + pthSep + sDocName;
117cdf0e10cSrcweir         }
118cdf0e10cSrcweir         return testdocPth;
119cdf0e10cSrcweir     }
120cdf0e10cSrcweir 
121cdf0e10cSrcweir     /**
122cdf0e10cSrcweir      *
123cdf0e10cSrcweir      * This method adds the DOCPTH to a given file
124cdf0e10cSrcweir      * and changes the format to an file URL
125cdf0e10cSrcweir      *
126cdf0e10cSrcweir      */
getFullTestURL(String sDocName)127cdf0e10cSrcweir     public static String getFullTestURL(String sDocName) {
128cdf0e10cSrcweir         String fullDocPath = getFullTestDocName(sDocName);
129cdf0e10cSrcweir         if (fullDocPath.startsWith("http:")) {
130cdf0e10cSrcweir             return fullDocPath;
131cdf0e10cSrcweir         }
132cdf0e10cSrcweir         if (fullDocPath.startsWith("file:")) {
133cdf0e10cSrcweir             return fullDocPath;
134cdf0e10cSrcweir         }
135cdf0e10cSrcweir         String prefix = null;
136cdf0e10cSrcweir 
137cdf0e10cSrcweir         //  Windows: \\\\margritte\\qaapi\\workspace\\qadev\\testdocs/emptyChart.sds
138cdf0e10cSrcweir         if (fullDocPath.startsWith("\\\\")) {
139cdf0e10cSrcweir             prefix = "file:";
140cdf0e10cSrcweir         }
141cdf0e10cSrcweir 
142cdf0e10cSrcweir         fullDocPath = fullDocPath.replace('\\', '/');
143cdf0e10cSrcweir         if (prefix == null) {
144cdf0e10cSrcweir             if (fullDocPath.startsWith("//")) {
145cdf0e10cSrcweir                 prefix = "file:/";
146cdf0e10cSrcweir             } else if (fullDocPath.startsWith("/")) {
147cdf0e10cSrcweir                 prefix = "file://";
148cdf0e10cSrcweir             } else {
149cdf0e10cSrcweir                 prefix = "file:///";
150cdf0e10cSrcweir             }
151cdf0e10cSrcweir         }
152cdf0e10cSrcweir         if (!fullDocPath.endsWith("/")) {
153cdf0e10cSrcweir             File aFile = new File(fullDocPath);
154cdf0e10cSrcweir             if (aFile.isDirectory()) {
155cdf0e10cSrcweir                 fullDocPath += "/";
156cdf0e10cSrcweir             }
157cdf0e10cSrcweir         }
158cdf0e10cSrcweir         String fulldocURL = prefix + fullDocPath;
159cdf0e10cSrcweir         return fulldocURL;
160cdf0e10cSrcweir     }
161cdf0e10cSrcweir 
162cdf0e10cSrcweir     /**
163cdf0e10cSrcweir      *
164cdf0e10cSrcweir      * This method changes a given URL to a valid file URL
165cdf0e10cSrcweir      *
166cdf0e10cSrcweir      */
getFullURL(String sDocName)167cdf0e10cSrcweir     public static String getFullURL(String sDocName) {
168cdf0e10cSrcweir         String fullDocPath = sDocName;
169cdf0e10cSrcweir         fullDocPath = fullDocPath.replace('\\', '/');
170cdf0e10cSrcweir 
171cdf0e10cSrcweir         if (fullDocPath.startsWith("http:")) {
172cdf0e10cSrcweir             return fullDocPath;
173cdf0e10cSrcweir         }
174cdf0e10cSrcweir         if (fullDocPath.startsWith("ftp:")) {
175cdf0e10cSrcweir             return fullDocPath;
176cdf0e10cSrcweir         }
177cdf0e10cSrcweir         String prefix = "";
178cdf0e10cSrcweir         if (!fullDocPath.startsWith("file:///")) {
179cdf0e10cSrcweir             if (fullDocPath.startsWith("//")) {
180cdf0e10cSrcweir                 prefix = "file:";
181cdf0e10cSrcweir             } else {
182cdf0e10cSrcweir                 if (fullDocPath.startsWith("/")) {
183cdf0e10cSrcweir                     prefix = "file://";
184cdf0e10cSrcweir //                    if (helper.OSHelper.isLinuxIntel())
185cdf0e10cSrcweir //                    {
186cdf0e10cSrcweir //                        prefix = "file:/";
187cdf0e10cSrcweir //                    }
188cdf0e10cSrcweir                 }
189cdf0e10cSrcweir                 else
190cdf0e10cSrcweir                 {
191cdf0e10cSrcweir                     prefix = "file:///";
192cdf0e10cSrcweir                 }
193cdf0e10cSrcweir             }
194cdf0e10cSrcweir         }
195cdf0e10cSrcweir         if (!fullDocPath.endsWith("/")) {
196cdf0e10cSrcweir             File aFile = new File(fullDocPath);
197cdf0e10cSrcweir             if (aFile.isDirectory()) {
198cdf0e10cSrcweir                 fullDocPath += "/";
199cdf0e10cSrcweir             }
200cdf0e10cSrcweir         }
201cdf0e10cSrcweir         String fulldocURL = prefix + fullDocPath;
202cdf0e10cSrcweir 
203cdf0e10cSrcweir         return fulldocURL;
204cdf0e10cSrcweir     }
205cdf0e10cSrcweir 
206cdf0e10cSrcweir     /**
207cdf0e10cSrcweir      *
208cdf0e10cSrcweir      * This method creates folders needed
209cdf0e10cSrcweir      *
210cdf0e10cSrcweir      */
make_Directories(String first, String path)211cdf0e10cSrcweir     public static void make_Directories(String first, String path) {
212cdf0e10cSrcweir         String already_done = null;
213cdf0e10cSrcweir         String fs = System.getProperty("file.separator");
214cdf0e10cSrcweir         StringTokenizer path_tokenizer = new StringTokenizer(path, fs, false);
215cdf0e10cSrcweir         already_done = first;
216cdf0e10cSrcweir         while (path_tokenizer.hasMoreTokens()) {
217cdf0e10cSrcweir             String part = path_tokenizer.nextToken();
218cdf0e10cSrcweir             File new_dir = new File(already_done + File.separatorChar + part);
219cdf0e10cSrcweir             already_done = new_dir.toString();
220cdf0e10cSrcweir             //create the directory
221cdf0e10cSrcweir             new_dir.mkdirs();
222cdf0e10cSrcweir         }
223cdf0e10cSrcweir         return;
224cdf0e10cSrcweir     }
225cdf0e10cSrcweir 
226cdf0e10cSrcweir     /**
227cdf0e10cSrcweir      *
228cdf0e10cSrcweir      * This method get the version for a given TestBase/platform combination
229cdf0e10cSrcweir      *
230cdf0e10cSrcweir      */
getVersion(String aFile, String aPlatform, String aTestbase)231cdf0e10cSrcweir     public static String getVersion(String aFile, String aPlatform, String aTestbase) {
232cdf0e10cSrcweir         if ((aFile == null) || (aPlatform == null) || (aTestbase == null)) {
233cdf0e10cSrcweir             return "/";
234cdf0e10cSrcweir         }
235cdf0e10cSrcweir 
236cdf0e10cSrcweir         File the_file = new File(aFile);
237cdf0e10cSrcweir         try {
238cdf0e10cSrcweir             RandomAccessFile raf = new RandomAccessFile(the_file, "r");
239cdf0e10cSrcweir             String res = "";
240cdf0e10cSrcweir             while (!res.equals("[" + aTestbase.toUpperCase() + "]")) {
241cdf0e10cSrcweir                 res = raf.readLine();
242cdf0e10cSrcweir             }
243cdf0e10cSrcweir             res = "=/";
244cdf0e10cSrcweir             while ((!res.startsWith(aPlatform)) || (res.startsWith("["))) {
245cdf0e10cSrcweir                 res = raf.readLine();
246cdf0e10cSrcweir             }
247cdf0e10cSrcweir             raf.close();
248cdf0e10cSrcweir             if (res.startsWith("[")) {
249cdf0e10cSrcweir                 res = "/";
250cdf0e10cSrcweir             }
251cdf0e10cSrcweir             return res.substring(res.indexOf("=") + 1);
252cdf0e10cSrcweir 
253cdf0e10cSrcweir         } catch (Exception e) {
254cdf0e10cSrcweir             System.out.println("Couldn't find version");
255cdf0e10cSrcweir             return "/";
256cdf0e10cSrcweir         }
257cdf0e10cSrcweir     }
258cdf0e10cSrcweir 
259cdf0e10cSrcweir     /**
260cdf0e10cSrcweir      *
2619bc83aaaSmseidel      * This method gets the user dir of the connected office
262cdf0e10cSrcweir      *
263cdf0e10cSrcweir      */
getOfficeUserPath(XMultiServiceFactory msf)264cdf0e10cSrcweir     public static String getOfficeUserPath(XMultiServiceFactory msf) {
265cdf0e10cSrcweir         String userPath = null;
266cdf0e10cSrcweir 
267bb6af6bcSPedro Giffuni         // get a folder which is located in the user dir
268cdf0e10cSrcweir         try {
269cdf0e10cSrcweir             userPath = (String) getOfficeSettingsValue(msf, "UserConfig");
270cdf0e10cSrcweir         } catch (Exception e) {
271cdf0e10cSrcweir             System.out.println("Couldn't get Office User Path");
272cdf0e10cSrcweir             e.printStackTrace();
273cdf0e10cSrcweir         }
274cdf0e10cSrcweir 
275cdf0e10cSrcweir         // strip the returned folder to the user dir
276cdf0e10cSrcweir         if (userPath.charAt(userPath.length() - 1) == '/') {
277cdf0e10cSrcweir             userPath = userPath.substring(0, userPath.length() - 1);
278cdf0e10cSrcweir         }
279cdf0e10cSrcweir         int index = userPath.lastIndexOf('/');
280cdf0e10cSrcweir         if (index != -1) {
281cdf0e10cSrcweir             userPath = userPath.substring(0, index);
282cdf0e10cSrcweir         }
283cdf0e10cSrcweir 
284cdf0e10cSrcweir         return userPath;
285cdf0e10cSrcweir     }
286cdf0e10cSrcweir 
287cdf0e10cSrcweir     /**
2889bc83aaaSmseidel      * In the office there are some settings available. This function
289cdf0e10cSrcweir      * returns the value of the given setting name. For Example the setting name "Temp"
290cdf0e10cSrcweir      * "Temp" returns the temp folder of the office instance.
291cdf0e10cSrcweir      * @param msf a XMultiServiceFactory
292cdf0e10cSrcweir      * @param setting  the name of the setting the value should be returned.
2939bc83aaaSmseidel      * For example "Temp" returns the temp folder of the current office instance.
294cdf0e10cSrcweir      * @see com.sun.star.util.PathSettings
295cdf0e10cSrcweir      * @return the value as String
296cdf0e10cSrcweir      */
getOfficeSettingsValue(XMultiServiceFactory msf, String setting)297cdf0e10cSrcweir     public static String getOfficeSettingsValue(XMultiServiceFactory msf, String setting) {
298cdf0e10cSrcweir 
299cdf0e10cSrcweir         String settingPath = null;
300cdf0e10cSrcweir         try {
301cdf0e10cSrcweir             Object settings = msf.createInstance("com.sun.star.comp.framework.PathSettings");
302cdf0e10cSrcweir             XPropertySet pthSettings = null;
303cdf0e10cSrcweir             try {
304cdf0e10cSrcweir                 pthSettings = (XPropertySet) AnyConverter.toObject(
305cdf0e10cSrcweir                     new Type(XPropertySet.class), settings);
306cdf0e10cSrcweir             } catch (com.sun.star.lang.IllegalArgumentException iae) {
307cdf0e10cSrcweir                 System.out.println("### couldn't get Office Settings");
308cdf0e10cSrcweir             }
309cdf0e10cSrcweir             settingPath = (String) pthSettings.getPropertyValue(setting);
310cdf0e10cSrcweir 
311cdf0e10cSrcweir         } catch (Exception e) {
312*b275ba1eSJohn Bampton             System.out.println("Couldn't get setting value for " + setting);
313cdf0e10cSrcweir             e.printStackTrace();
314cdf0e10cSrcweir         }
315cdf0e10cSrcweir         return settingPath;
316cdf0e10cSrcweir     }
317cdf0e10cSrcweir 
setOfficeSettingsValue(XMultiServiceFactory msf, String setting, String value)318cdf0e10cSrcweir     public static void setOfficeSettingsValue(XMultiServiceFactory msf, String setting, String value) {
319cdf0e10cSrcweir 
320cdf0e10cSrcweir         String settingPath = null;
321cdf0e10cSrcweir         try {
322cdf0e10cSrcweir             Object settings = msf.createInstance("com.sun.star.comp.framework.PathSettings");
323cdf0e10cSrcweir             XPropertySet pthSettings = null;
324cdf0e10cSrcweir             try {
325cdf0e10cSrcweir                 pthSettings = (XPropertySet) AnyConverter.toObject(
326cdf0e10cSrcweir                     new Type(XPropertySet.class), settings);
327cdf0e10cSrcweir             } catch (com.sun.star.lang.IllegalArgumentException iae) {
328cdf0e10cSrcweir                 System.out.println("### couldn't get Office Settings");
329cdf0e10cSrcweir             }
330cdf0e10cSrcweir             pthSettings.setPropertyValue(setting, value);
331cdf0e10cSrcweir 
332cdf0e10cSrcweir         } catch (Exception e) {
333cdf0e10cSrcweir             System.out.println("Couldn't set '" + setting + "' to value '" + value + "'");
334cdf0e10cSrcweir             e.printStackTrace();
335cdf0e10cSrcweir         }
336cdf0e10cSrcweir     }
337cdf0e10cSrcweir 
338cdf0e10cSrcweir     /**
3399bc83aaaSmseidel      * This method returns the temp directory of the user.
340cdf0e10cSrcweir      * Since Java 1.4 it is not possible to read environment variables. To workaround
341cdf0e10cSrcweir      * this, the Java parameter -D could be used.
342cdf0e10cSrcweir      */
getUsersTempDir()343cdf0e10cSrcweir     public static String getUsersTempDir() {
344cdf0e10cSrcweir         String tempDir = System.getProperty("my.temp");
345cdf0e10cSrcweir         if (tempDir == null) {
346cdf0e10cSrcweir             tempDir = System.getProperty("my.tmp");
347cdf0e10cSrcweir             if (tempDir == null) {
348cdf0e10cSrcweir                 tempDir = System.getProperty("java.io.tmpdir");
349cdf0e10cSrcweir             }
350cdf0e10cSrcweir         }
351cdf0e10cSrcweir         // remove ending file separator
352cdf0e10cSrcweir         if (tempDir.endsWith(System.getProperty("file.separator"))) {
353cdf0e10cSrcweir             tempDir = tempDir.substring(0, tempDir.length() - 1);
354cdf0e10cSrcweir         }
355cdf0e10cSrcweir 
356cdf0e10cSrcweir         return tempDir;
357cdf0e10cSrcweir     }
358cdf0e10cSrcweir 
359cdf0e10cSrcweir     /**
360cdf0e10cSrcweir      *
3619bc83aaaSmseidel      * This method gets the temp dir of the connected office
362cdf0e10cSrcweir      *
363cdf0e10cSrcweir      */
getOfficeTemp(XMultiServiceFactory msf)364cdf0e10cSrcweir     public static String getOfficeTemp(XMultiServiceFactory msf) {
365cdf0e10cSrcweir         String url = getOfficeUserPath(msf) + "/test-temp/";
366cdf0e10cSrcweir         try {
367cdf0e10cSrcweir             new File(new URI(url)).mkdir();
368cdf0e10cSrcweir         } catch (URISyntaxException e) {
369cdf0e10cSrcweir             throw new RuntimeException(e);
370cdf0e10cSrcweir         }
371cdf0e10cSrcweir         return url;
372cdf0e10cSrcweir     }
373cdf0e10cSrcweir 
374cdf0e10cSrcweir     /**
3759bc83aaaSmseidel      * Gets OpenOffice temp directory without 'file:///' prefix.
376bb6af6bcSPedro Giffuni      * For example is useful for Registry URL specifying.
377cdf0e10cSrcweir      * @msf Office factory for accessing its settings.
378cdf0e10cSrcweir      * @return SOffice temporary directory in form for example
379cdf0e10cSrcweir      * 'd:/Office60/user/temp/'.
380cdf0e10cSrcweir      */
getOfficeTempDir(XMultiServiceFactory msf)381cdf0e10cSrcweir     public static String getOfficeTempDir(XMultiServiceFactory msf) {
382cdf0e10cSrcweir 
383cdf0e10cSrcweir         String dir = getOfficeTemp(msf);
384cdf0e10cSrcweir 
385cdf0e10cSrcweir         int idx = dir.indexOf("file:///");
386cdf0e10cSrcweir 
387cdf0e10cSrcweir         if (idx < 0) {
388cdf0e10cSrcweir             return dir;
389cdf0e10cSrcweir         }
390cdf0e10cSrcweir 
391cdf0e10cSrcweir         dir = dir.substring("file:///".length());
392cdf0e10cSrcweir 
393cdf0e10cSrcweir         idx = dir.indexOf(":");
394cdf0e10cSrcweir 
395cdf0e10cSrcweir         // is the last character a '/' or a '\'?
396cdf0e10cSrcweir         boolean lastCharSet = dir.endsWith("/") || dir.endsWith("\\");
397cdf0e10cSrcweir 
398cdf0e10cSrcweir         if (idx < 0) { // linux or solaris
399cdf0e10cSrcweir             dir = "/" + dir;
400cdf0e10cSrcweir             dir += lastCharSet ? "" : "/";
401cdf0e10cSrcweir         } else {  // windows
402cdf0e10cSrcweir             dir += lastCharSet ? "" : "\\";
403cdf0e10cSrcweir         }
404cdf0e10cSrcweir 
405cdf0e10cSrcweir         return dir;
406cdf0e10cSrcweir     }
407cdf0e10cSrcweir 
408cdf0e10cSrcweir     /**
4099bc83aaaSmseidel      * Gets OpenOffice temp directory without 'file:///' prefix.
4109bc83aaaSmseidel      * and System dependent file separator
411cdf0e10cSrcweir      */
getOfficeTempDirSys(XMultiServiceFactory msf)412cdf0e10cSrcweir     public static String getOfficeTempDirSys(XMultiServiceFactory msf) {
413cdf0e10cSrcweir 
414cdf0e10cSrcweir         String dir = getOfficeTemp(msf);
415cdf0e10cSrcweir         String sysDir = "";
416cdf0e10cSrcweir 
417cdf0e10cSrcweir         int idx = dir.indexOf("file://");
418cdf0e10cSrcweir 
419cdf0e10cSrcweir         // remove leading 'file://'
420cdf0e10cSrcweir         if (idx < 0) {
421cdf0e10cSrcweir             sysDir = dir;
422cdf0e10cSrcweir         } else {
423cdf0e10cSrcweir             sysDir = dir.substring("file://".length());
424cdf0e10cSrcweir         }
425cdf0e10cSrcweir 
426cdf0e10cSrcweir         // append '/' if not there (e.g. linux)
427cdf0e10cSrcweir         if (sysDir.charAt(sysDir.length() - 1) != '/') {
428cdf0e10cSrcweir             sysDir += "/";
429cdf0e10cSrcweir         }
430cdf0e10cSrcweir 
431888f552bSmseidel         // remove leading '/' and replace others with '\' on Windows machines
432cdf0e10cSrcweir         if (sysDir.indexOf(":") != -1) {
433cdf0e10cSrcweir             sysDir = sysDir.substring(1);
434cdf0e10cSrcweir             sysDir = sysDir.replace('/', '\\');
435cdf0e10cSrcweir         }
436cdf0e10cSrcweir         return sysDir;
437cdf0e10cSrcweir     }
438cdf0e10cSrcweir 
439cdf0e10cSrcweir     /**
440cdf0e10cSrcweir      * converts a fileURL to a system URL
441e6b649b5SPedro Giffuni      * @param fileURL a file URL
442cdf0e10cSrcweir      * @return a system URL
443cdf0e10cSrcweir      */
getSystemURL(String fileURL)444cdf0e10cSrcweir     public static String getSystemURL(String fileURL) {
445cdf0e10cSrcweir         String sysDir = "";
446cdf0e10cSrcweir 
447cdf0e10cSrcweir         int idx = fileURL.indexOf("file://");
448cdf0e10cSrcweir 
449cdf0e10cSrcweir         // remove leading 'file://'
450cdf0e10cSrcweir         if (idx < 0) {
451cdf0e10cSrcweir             sysDir = fileURL;
452cdf0e10cSrcweir         } else {
453cdf0e10cSrcweir             sysDir = fileURL.substring("file://".length());
454cdf0e10cSrcweir         }
455cdf0e10cSrcweir 
456888f552bSmseidel         // remove leading '/' and replace others with '\' on Windows machines
457cdf0e10cSrcweir         if (sysDir.indexOf(":") != -1) {
458cdf0e10cSrcweir             sysDir = sysDir.substring(1);
459cdf0e10cSrcweir             sysDir = sysDir.replace('/', '\\');
460cdf0e10cSrcweir         }
461cdf0e10cSrcweir         return sysDir;
462cdf0e10cSrcweir     }
463cdf0e10cSrcweir 
464cdf0e10cSrcweir     /**
465bb6af6bcSPedro Giffuni      *  This method check via Office the existence of the given file URL
466cdf0e10cSrcweir      * @param msf the multiservice factory
467bb6af6bcSPedro Giffuni      * @param fileURL the file which existence should be checked
468cdf0e10cSrcweir      * @return true if the file exists, else false
469cdf0e10cSrcweir      */
fileExists(XMultiServiceFactory msf, String fileURL)470cdf0e10cSrcweir     public static boolean fileExists(XMultiServiceFactory msf, String fileURL) {
471cdf0e10cSrcweir         boolean exists = false;
472cdf0e10cSrcweir         try {
473cdf0e10cSrcweir 
474cdf0e10cSrcweir             Object fileacc = msf.createInstance("com.sun.star.comp.ucb.SimpleFileAccess");
475cdf0e10cSrcweir             XSimpleFileAccess simpleAccess = (XSimpleFileAccess) UnoRuntime.queryInterface(XSimpleFileAccess.class,
476cdf0e10cSrcweir                 fileacc);
477cdf0e10cSrcweir             if (simpleAccess.exists(fileURL)) {
478cdf0e10cSrcweir                 exists = true;
479cdf0e10cSrcweir             }
480cdf0e10cSrcweir 
481cdf0e10cSrcweir         } catch (Exception e) {
482cdf0e10cSrcweir             System.out.println("Couldn't access file '" + fileURL + "'");
483cdf0e10cSrcweir             e.printStackTrace();
484cdf0e10cSrcweir             exists = false;
485cdf0e10cSrcweir         }
486cdf0e10cSrcweir         return exists;
487cdf0e10cSrcweir     }
488cdf0e10cSrcweir 
489cdf0e10cSrcweir     /**
490bb6af6bcSPedro Giffuni      * This method deletes via office the given file URL. It checks the existence
4919bc83aaaSmseidel      * of <CODE>fileURL</CODE>. If exists it will be deleted.
492e6b649b5SPedro Giffuni      * @param xMsf the multiservice factory
493cdf0e10cSrcweir      * @param fileURL the file to delete
4949bc83aaaSmseidel      * @return true if the file could be deleted or the file does not exist
495cdf0e10cSrcweir      */
deleteFile(XMultiServiceFactory xMsf, String fileURL)496cdf0e10cSrcweir     public static boolean deleteFile(XMultiServiceFactory xMsf, String fileURL) {
497cdf0e10cSrcweir         boolean delete = true;
498cdf0e10cSrcweir         try {
499cdf0e10cSrcweir 
500cdf0e10cSrcweir             Object fileacc = xMsf.createInstance("com.sun.star.comp.ucb.SimpleFileAccess");
501cdf0e10cSrcweir             XSimpleFileAccess simpleAccess = (XSimpleFileAccess) UnoRuntime.queryInterface(XSimpleFileAccess.class,
502cdf0e10cSrcweir                 fileacc);
503cdf0e10cSrcweir             if (simpleAccess.exists(fileURL)) {
504cdf0e10cSrcweir                 simpleAccess.kill(fileURL);
505cdf0e10cSrcweir             }
506cdf0e10cSrcweir 
507cdf0e10cSrcweir         } catch (Exception e) {
508cdf0e10cSrcweir             System.out.println("Couldn't delete file '" + fileURL + "'");
509cdf0e10cSrcweir             e.printStackTrace();
510cdf0e10cSrcweir             delete = false;
511cdf0e10cSrcweir         }
512cdf0e10cSrcweir         return delete;
513cdf0e10cSrcweir     }
514cdf0e10cSrcweir 
515cdf0e10cSrcweir     /**
516cdf0e10cSrcweir      * This method copies via office a given file to a new one
517e6b649b5SPedro Giffuni      * @param xMsf the multi service factory
518e6b649b5SPedro Giffuni      * @param source the source file
5199bc83aaaSmseidel      * @param destination the destination file
520cdf0e10cSrcweir      * @return true at success
521cdf0e10cSrcweir      */
copyFile(XMultiServiceFactory xMsf, String source, String destinaion)522cdf0e10cSrcweir     public static boolean copyFile(XMultiServiceFactory xMsf, String source, String destinaion) {
523cdf0e10cSrcweir         boolean res = false;
524cdf0e10cSrcweir         try {
525cdf0e10cSrcweir             Object fileacc = xMsf.createInstance("com.sun.star.comp.ucb.SimpleFileAccess");
526cdf0e10cSrcweir             XSimpleFileAccess simpleAccess = (XSimpleFileAccess) UnoRuntime.queryInterface(XSimpleFileAccess.class,
527cdf0e10cSrcweir                 fileacc);
528cdf0e10cSrcweir             if (!simpleAccess.exists(destinaion)) {
529cdf0e10cSrcweir                 simpleAccess.copy(source, destinaion);
530cdf0e10cSrcweir             }
531cdf0e10cSrcweir 
532cdf0e10cSrcweir             res = true;
533cdf0e10cSrcweir         } catch (Exception e) {
534cdf0e10cSrcweir             System.out.println("Couldn't copy file '" + source + "' -> '" + destinaion + "'");
535cdf0e10cSrcweir             e.printStackTrace();
536cdf0e10cSrcweir             res = false;
537cdf0e10cSrcweir         }
538cdf0e10cSrcweir         return res;
539cdf0e10cSrcweir     }
540cdf0e10cSrcweir 
overwriteFile_impl( XMultiServiceFactory xMsf, String oldF, String newF)541cdf0e10cSrcweir     private static void overwriteFile_impl(
542cdf0e10cSrcweir         XMultiServiceFactory xMsf, String oldF, String newF)
543cdf0e10cSrcweir         throws InteractiveAugmentedIOException
544cdf0e10cSrcweir     {
545cdf0e10cSrcweir         try {
546cdf0e10cSrcweir             Object fileacc = xMsf.createInstance("com.sun.star.comp.ucb.SimpleFileAccess");
547cdf0e10cSrcweir 
548cdf0e10cSrcweir             XSimpleFileAccess simpleAccess = (XSimpleFileAccess) UnoRuntime.queryInterface(XSimpleFileAccess.class,
549cdf0e10cSrcweir                 fileacc);
550cdf0e10cSrcweir             if (simpleAccess.exists(newF)) {
551cdf0e10cSrcweir                 simpleAccess.kill(newF);
552cdf0e10cSrcweir             }
553cdf0e10cSrcweir             simpleAccess.copy(oldF, newF);
554cdf0e10cSrcweir         } catch (InteractiveAugmentedIOException e) {
555cdf0e10cSrcweir             throw e;
556cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
557cdf0e10cSrcweir             System.out.println("Couldn't copy " + oldF + " to " + newF + ":");
558cdf0e10cSrcweir             e.printStackTrace();
559cdf0e10cSrcweir             throw new RuntimeException(e);
560cdf0e10cSrcweir         }
561cdf0e10cSrcweir     }
562cdf0e10cSrcweir 
563cdf0e10cSrcweir     /**
564cdf0e10cSrcweir      * Copies file to a new location using OpenOffice.org features. If the target
565cdf0e10cSrcweir      * file already exists, the file is deleted.
566cdf0e10cSrcweir      *
567cdf0e10cSrcweir      * @returns <code>true</code> if the file was successfully copied,
568bb6af6bcSPedro Giffuni      * <code>false</code> if some errors occurred (e.g. file is locked, used
569cdf0e10cSrcweir      * by another process).
570cdf0e10cSrcweir      */
tryOverwriteFile( XMultiServiceFactory xMsf, String oldF, String newF)571cdf0e10cSrcweir     public static boolean tryOverwriteFile(
572cdf0e10cSrcweir         XMultiServiceFactory xMsf, String oldF, String newF)
573cdf0e10cSrcweir     {
574cdf0e10cSrcweir         try {
575cdf0e10cSrcweir             overwriteFile_impl(xMsf, oldF, newF);
576cdf0e10cSrcweir         } catch (InteractiveAugmentedIOException e) {
577cdf0e10cSrcweir             return false;
578cdf0e10cSrcweir         }
579cdf0e10cSrcweir         return true;
580cdf0e10cSrcweir     }
581cdf0e10cSrcweir 
doOverwriteFile( XMultiServiceFactory xMsf, String oldF, String newF)582cdf0e10cSrcweir     public static void doOverwriteFile(
583cdf0e10cSrcweir         XMultiServiceFactory xMsf, String oldF, String newF)
584cdf0e10cSrcweir     {
585cdf0e10cSrcweir         try {
586cdf0e10cSrcweir             overwriteFile_impl(xMsf, oldF, newF);
587cdf0e10cSrcweir         } catch (InteractiveAugmentedIOException e) {
588cdf0e10cSrcweir             throw new RuntimeException(e);
589cdf0e10cSrcweir         }
590cdf0e10cSrcweir     }
591cdf0e10cSrcweir 
hasPropertyByName(XPropertySet props, String aName)592cdf0e10cSrcweir     public static boolean hasPropertyByName(XPropertySet props, String aName) {
593cdf0e10cSrcweir         Property[] list = props.getPropertySetInfo().getProperties();
594cdf0e10cSrcweir         boolean res = false;
595cdf0e10cSrcweir         for (int i = 0; i < list.length; i++) {
596cdf0e10cSrcweir             String the_name = list[i].Name;
597cdf0e10cSrcweir             if (aName.equals(the_name)) {
598cdf0e10cSrcweir                 res = true;
599cdf0e10cSrcweir             }
600cdf0e10cSrcweir         }
601cdf0e10cSrcweir         return res;
602cdf0e10cSrcweir     }
603cdf0e10cSrcweir 
604cdf0e10cSrcweir     /**
605cdf0e10cSrcweir      *
606cdf0e10cSrcweir      * This method returns the implementation name of a given object
607cdf0e10cSrcweir      *
608cdf0e10cSrcweir      */
getImplName(Object aObject)609cdf0e10cSrcweir     public static String getImplName(Object aObject) {
610cdf0e10cSrcweir         String res = "Error getting Implementation name";
611cdf0e10cSrcweir         try {
612cdf0e10cSrcweir             XServiceInfo xSI = (XServiceInfo) UnoRuntime.queryInterface(XServiceInfo.class, aObject);
613cdf0e10cSrcweir             res = xSI.getImplementationName();
614cdf0e10cSrcweir         } catch (Exception e) {
615cdf0e10cSrcweir             res = "Error getting Implementation name ( " + e + " )";
616cdf0e10cSrcweir         }
617cdf0e10cSrcweir 
618cdf0e10cSrcweir         return res;
619cdf0e10cSrcweir     }
620cdf0e10cSrcweir 
621cdf0e10cSrcweir     /**
622cdf0e10cSrcweir      *
623cdf0e10cSrcweir      * This method checks if an Object is void
624cdf0e10cSrcweir      *
625cdf0e10cSrcweir      */
isVoid(Object aObject)626cdf0e10cSrcweir     public static boolean isVoid(Object aObject) {
627cdf0e10cSrcweir         if (aObject instanceof com.sun.star.uno.Any) {
628cdf0e10cSrcweir             com.sun.star.uno.Any oAny = (com.sun.star.uno.Any) aObject;
629cdf0e10cSrcweir             return (oAny.getType().getTypeName().equals("void"));
630cdf0e10cSrcweir         } else {
631cdf0e10cSrcweir             return false;
632cdf0e10cSrcweir         }
633cdf0e10cSrcweir 
634cdf0e10cSrcweir     }
635cdf0e10cSrcweir 
636cdf0e10cSrcweir     /**
637cdf0e10cSrcweir      *
638cdf0e10cSrcweir      * This method replaces a substring with another
639cdf0e10cSrcweir      *
640cdf0e10cSrcweir      */
replacePart(String all, String toReplace, String replacement)641cdf0e10cSrcweir     public static String replacePart(String all, String toReplace, String replacement) {
642cdf0e10cSrcweir         return replaceAll13(all, toReplace, replacement);
643cdf0e10cSrcweir     }
644cdf0e10cSrcweir 
645cdf0e10cSrcweir     /**
646cdf0e10cSrcweir      * Scan localhost for the next free port-number from a starting port
647cdf0e10cSrcweir      * on. If the starting port is smaller than 1024, port number starts with
648cdf0e10cSrcweir      * 10000 as default, because numbers < 1024 are never free on unix machines.
649cdf0e10cSrcweir      * @param startPort The port where scanning starts.
650cdf0e10cSrcweir      * @return The next free port.
651cdf0e10cSrcweir      */
getNextFreePort(int startPort)652cdf0e10cSrcweir     public static int getNextFreePort(int startPort) {
653cdf0e10cSrcweir         if (startPort < 1024) {
654cdf0e10cSrcweir             startPort = 10000;
655cdf0e10cSrcweir         }
656cdf0e10cSrcweir         for (int port = startPort; port < 65536; port++) {
657cdf0e10cSrcweir             System.out.println("Scan port " + port);
658cdf0e10cSrcweir             try {
659cdf0e10cSrcweir                 // first trying to establish a server-socket on localhost
660cdf0e10cSrcweir                 // fails if there is already a server running
661cdf0e10cSrcweir                 ServerSocket sSock = new ServerSocket(port);
662cdf0e10cSrcweir                 sSock.close();
663cdf0e10cSrcweir             } catch (IOException e) {
664cdf0e10cSrcweir                 System.out.println(" -> server: occupied port " + port);
665cdf0e10cSrcweir                 continue;
666cdf0e10cSrcweir             }
667cdf0e10cSrcweir             try {
668cdf0e10cSrcweir                 // now trying to establish a client-socket
669cdf0e10cSrcweir                 // fails if there is no server on any connectable machine
670cdf0e10cSrcweir                 Socket sock = new Socket("localhost", port);
671cdf0e10cSrcweir                 System.out.println(" -> socket: occupied port: " + port);
672cdf0e10cSrcweir             } catch (IOException e) {
673cdf0e10cSrcweir                 System.out.println(" -> free port");
674cdf0e10cSrcweir                 return port;
675cdf0e10cSrcweir             }
676cdf0e10cSrcweir         }
677cdf0e10cSrcweir         return 65535;
678cdf0e10cSrcweir     }
679cdf0e10cSrcweir 
parseURL(XMultiServiceFactory xMSF, String url)680cdf0e10cSrcweir     public static URL parseURL(XMultiServiceFactory xMSF, String url) {
681cdf0e10cSrcweir         URL[] rUrl = new URL[1];
682cdf0e10cSrcweir         rUrl[0] = new URL();
683cdf0e10cSrcweir         rUrl[0].Complete = url;
684cdf0e10cSrcweir 
685cdf0e10cSrcweir         XURLTransformer xTrans = null;
686cdf0e10cSrcweir         try {
687cdf0e10cSrcweir             Object inst = xMSF.createInstance("com.sun.star.util.URLTransformer");
688cdf0e10cSrcweir             xTrans = (XURLTransformer) UnoRuntime.queryInterface(XURLTransformer.class, inst);
689cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
690cdf0e10cSrcweir         }
691cdf0e10cSrcweir 
692cdf0e10cSrcweir         xTrans.parseStrict(rUrl);
693cdf0e10cSrcweir 
694cdf0e10cSrcweir         return rUrl[0];
695cdf0e10cSrcweir     }
696cdf0e10cSrcweir 
getOfficeURL(XMultiServiceFactory msf)697cdf0e10cSrcweir     public static String getOfficeURL(XMultiServiceFactory msf) {
698cdf0e10cSrcweir         try {
699cdf0e10cSrcweir             Object settings = msf.createInstance("com.sun.star.util.PathSettings");
700cdf0e10cSrcweir             XPropertySet settingProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, settings);
701cdf0e10cSrcweir             String path = (String) settingProps.getPropertyValue("Module");
702cdf0e10cSrcweir             return path;
703cdf0e10cSrcweir         } catch (Exception e) {
704cdf0e10cSrcweir             System.out.println("Couldn't get Office Settings ");
705cdf0e10cSrcweir             e.printStackTrace();
706cdf0e10cSrcweir         }
707cdf0e10cSrcweir         return null;
708cdf0e10cSrcweir     }
709cdf0e10cSrcweir 
710cdf0e10cSrcweir     /** returns the path to the office binary folder
711cdf0e10cSrcweir      *
712cdf0e10cSrcweir      * @param msf The XMultiSeriveFactory
7139bc83aaaSmseidel      * @return the path to the office binary or an empty string on any error
714cdf0e10cSrcweir      */
getOfficeBinPath(XMultiServiceFactory msf)715cdf0e10cSrcweir     public static String getOfficeBinPath(XMultiServiceFactory msf) {
716cdf0e10cSrcweir         String sysBinDir = "";
717cdf0e10cSrcweir         try {
718cdf0e10cSrcweir             sysBinDir = utils.getSystemURL(utils.expandMacro(msf, "$SYSBINDIR"));
719cdf0e10cSrcweir         } catch (java.lang.Exception e) {
720cdf0e10cSrcweir         }
721cdf0e10cSrcweir 
722cdf0e10cSrcweir         return sysBinDir;
723cdf0e10cSrcweir     }
724cdf0e10cSrcweir 
725cdf0e10cSrcweir     /**
726cdf0e10cSrcweir      * Get an array of all property names from the property set. With the include
727cdf0e10cSrcweir      * and exclude parameters the properties can be filtered. <br>
728cdf0e10cSrcweir      * Set excludePropertyAttribute = 0 and includePropertyAttribute = 0
729cdf0e10cSrcweir      * to include all and exclude none.
730cdf0e10cSrcweir      * @param props The instance of XPropertySet
731cdf0e10cSrcweir      * @param includePropertyAttribute Properties without these attributes are filtered and will not be returned.
732cdf0e10cSrcweir      * @param excludePropertyAttribute Properties with these attributes are filtered and will not be returned.
733cdf0e10cSrcweir      * @return A String array with all property names.
734cdf0e10cSrcweir      * @see com.sun.star.beans.XPropertySet
735cdf0e10cSrcweir      * @see com.sun.star.beans.Property
736cdf0e10cSrcweir      * @see com.sun.star.beans.PropertyAttribute
737cdf0e10cSrcweir      */
getFilteredPropertyNames(XPropertySet props, short includePropertyAttribute, short excludePropertyAttribute)738cdf0e10cSrcweir     public static String[] getFilteredPropertyNames(XPropertySet props, short includePropertyAttribute,
739cdf0e10cSrcweir         short excludePropertyAttribute) {
740cdf0e10cSrcweir         Property[] the_props = props.getPropertySetInfo().getProperties();
741cdf0e10cSrcweir         ArrayList l = new ArrayList();
742cdf0e10cSrcweir         for (int i = 0; i < the_props.length; i++) {
743cdf0e10cSrcweir             boolean exclude = ((the_props[i].Attributes & excludePropertyAttribute) != 0);
744cdf0e10cSrcweir             boolean include = (includePropertyAttribute == 0) ||
745cdf0e10cSrcweir                 ((the_props[i].Attributes & includePropertyAttribute) != 0);
746cdf0e10cSrcweir             if (include && !exclude) {
747cdf0e10cSrcweir                 l.add(the_props[i].Name);
748cdf0e10cSrcweir             }
749cdf0e10cSrcweir         }
750cdf0e10cSrcweir         Collections.sort(l);
751cdf0e10cSrcweir         String[] names = new String[l.size()];
752cdf0e10cSrcweir         names = (String[]) l.toArray(names);
753cdf0e10cSrcweir         return names;
754cdf0e10cSrcweir     }
755cdf0e10cSrcweir 
756cdf0e10cSrcweir     /** Causes the thread to sleep some time.
757cdf0e10cSrcweir      * It can be used f.e. like:
758cdf0e10cSrcweir      * util.utils.shortWait(tParam.getInt("ShortWait"));
759cdf0e10cSrcweir      */
shortWait(int milliseconds)760cdf0e10cSrcweir     public static void shortWait(int milliseconds) {
761cdf0e10cSrcweir         try {
762cdf0e10cSrcweir             Thread.currentThread().sleep(milliseconds);
763cdf0e10cSrcweir         } catch (InterruptedException e) {
764cdf0e10cSrcweir             System.out.println("While waiting :" + e);
765cdf0e10cSrcweir         }
766cdf0e10cSrcweir     }
767cdf0e10cSrcweir 
768cdf0e10cSrcweir     /**
769cdf0e10cSrcweir      * Validate the AppExecutionCommand. Returned is an error message, starting
770cdf0e10cSrcweir      * with "Error:", or a warning, if the command might work.
771cdf0e10cSrcweir      * @param appExecCommand The application execution command that is checked.
772cdf0e10cSrcweir      * @param os The operating system where the check runs.
773cdf0e10cSrcweir      * @return The error message, or OK, if no error was detected.
774cdf0e10cSrcweir      */
validateAppExecutionCommand(String appExecCommand, String os)775cdf0e10cSrcweir     public static String validateAppExecutionCommand(String appExecCommand, String os) {
776cdf0e10cSrcweir         String errorMessage = "OK";
777cdf0e10cSrcweir         appExecCommand = replaceAll13(appExecCommand, "\"", "");
778cdf0e10cSrcweir         appExecCommand = replaceAll13(appExecCommand, "'", "");
779cdf0e10cSrcweir         StringTokenizer commandTokens = new StringTokenizer(appExecCommand, " \t");
780cdf0e10cSrcweir         String officeExecutable = "";
781cdf0e10cSrcweir         String officeExecCommand = "soffice";
782888f552bSmseidel         // is there a 'soffice' in the command? TODO: eliminate case sensitivity on Windows
783cdf0e10cSrcweir         int index = -1;
784cdf0e10cSrcweir         while (commandTokens.hasMoreTokens() && index == -1) {
785cdf0e10cSrcweir             officeExecutable += commandTokens.nextToken() + " ";
786cdf0e10cSrcweir             index = officeExecutable.indexOf(officeExecCommand);
787cdf0e10cSrcweir         }
788cdf0e10cSrcweir         if (index == -1) {
789cdf0e10cSrcweir             errorMessage = "Error: Your 'AppExecutionCommand' parameter does not " +
790cdf0e10cSrcweir                 "contain '" + officeExecCommand + "'.";
791cdf0e10cSrcweir         } else {
792cdf0e10cSrcweir             // does the directory exist?
793cdf0e10cSrcweir             officeExecutable = officeExecutable.trim();
794cdf0e10cSrcweir             String officePath = officeExecutable.substring(0, index);
795cdf0e10cSrcweir             File f = new File(officePath);
796cdf0e10cSrcweir             if (!f.exists() || !f.isDirectory()) {
797cdf0e10cSrcweir                 errorMessage = "Error: Your 'AppExecutionCommand' parameter does not " +
798cdf0e10cSrcweir                     "point to a valid system directory: " + officePath;
799cdf0e10cSrcweir             } else {
800cdf0e10cSrcweir                 // is it an office installation?
801cdf0e10cSrcweir                 f = new File(officeExecutable);
802888f552bSmseidel                 // one try for Windows platform can't be wrong...
803cdf0e10cSrcweir                 if (!f.exists() || !f.isFile()) {
804cdf0e10cSrcweir                     f = new File(officeExecutable + ".exe");
805cdf0e10cSrcweir                 }
806cdf0e10cSrcweir                 if (!f.exists() || !f.isFile()) {
807cdf0e10cSrcweir                     errorMessage = "Error: Your 'AppExecutionCommand' parameter does not " +
808cdf0e10cSrcweir                         "point to a valid office installation.";
809cdf0e10cSrcweir                 } else {
810cdf0e10cSrcweir                     // do we have the accept parameter?
811cdf0e10cSrcweir                     boolean gotNoAccept = true;
812cdf0e10cSrcweir                     while (commandTokens.hasMoreElements()) {
813cdf0e10cSrcweir                         String officeParam = commandTokens.nextToken();
814cdf0e10cSrcweir                         if (officeParam.indexOf("-accept=") != -1) {
815cdf0e10cSrcweir                             gotNoAccept = false;
816cdf0e10cSrcweir                             errorMessage = validateConnectString(officeParam, true);
817cdf0e10cSrcweir                         }
818cdf0e10cSrcweir                     }
819cdf0e10cSrcweir                     if (gotNoAccept) {
820cdf0e10cSrcweir                         errorMessage = "Error: Your 'AppExecutionCommand' parameter does not " +
821cdf0e10cSrcweir                             "contain a '-accept' parameter for connecting the office.";
822cdf0e10cSrcweir                     }
823cdf0e10cSrcweir                 }
824cdf0e10cSrcweir             }
825cdf0e10cSrcweir         }
826cdf0e10cSrcweir         return errorMessage;
827cdf0e10cSrcweir     }
828cdf0e10cSrcweir 
829cdf0e10cSrcweir     /**
830cdf0e10cSrcweir      * Validate the connection string. Returned is an error message, starting
831cdf0e10cSrcweir      * with "Error:", or a warning, if the command might work.
832cdf0e10cSrcweir      * @param connectString The connection string that is checked.
833cdf0e10cSrcweir      * @param checkAppExecutionCommand If the AppExecutionCommand is checked, the error messages willbe different.
834cdf0e10cSrcweir      * @return The error message, or OK, if no error was detected.
835cdf0e10cSrcweir      */
validateConnectString(String connectString, boolean checkAppExecutionCommand)836cdf0e10cSrcweir     public static String validateConnectString(String connectString, boolean checkAppExecutionCommand) {
837cdf0e10cSrcweir         String acceptPrefix = "";
838cdf0e10cSrcweir         if (checkAppExecutionCommand) {
839cdf0e10cSrcweir             acceptPrefix = "-accept=";
840cdf0e10cSrcweir         }
841cdf0e10cSrcweir 
842cdf0e10cSrcweir         String errorMessage = "OK";
843cdf0e10cSrcweir         // a warning, if an unknown connection method is used
844cdf0e10cSrcweir         if (connectString.indexOf("socket") != -1) {
845cdf0e10cSrcweir             if (connectString.indexOf(acceptPrefix + "socket,host=") == -1 ||
846cdf0e10cSrcweir                 connectString.indexOf("port=") == -1) {
847cdf0e10cSrcweir                 if (checkAppExecutionCommand) {
848cdf0e10cSrcweir                     errorMessage = "Error: The '-accept' parameter contains a syntax error: It should be like: '-accept=socket,host=localhost,port=8100;urp;";
849cdf0e10cSrcweir                 } else {
850cdf0e10cSrcweir                     errorMessage = "Error: The 'ConnectionString' parameter contains a syntax error: It should be like: 'socket,host=localhost,port=8100'";
851cdf0e10cSrcweir                 }
852cdf0e10cSrcweir             }
853cdf0e10cSrcweir         } else if (connectString.indexOf("pipe") != -1) {
854cdf0e10cSrcweir             if (connectString.indexOf(acceptPrefix + "pipe,name=") == -1) {
855cdf0e10cSrcweir                 if (checkAppExecutionCommand) {
856cdf0e10cSrcweir                     errorMessage = "Error: The '-accept' parameter contains a syntax error: It should be like: '-accept=pipe,name=myuniquename;urp;'";
857cdf0e10cSrcweir                 } else {
858cdf0e10cSrcweir                     errorMessage = "Error: The 'ConnectionString' parameter contains a syntax error: It should be like: 'pipe,name=myuniquename'";
859cdf0e10cSrcweir                 }
860cdf0e10cSrcweir             }
861cdf0e10cSrcweir         } else {
862cdf0e10cSrcweir             if (checkAppExecutionCommand) {
863cdf0e10cSrcweir                 errorMessage = "Warning: The '-accept' parameter contains an unknown connection method.";
864cdf0e10cSrcweir             } else {
865cdf0e10cSrcweir                 errorMessage = "Warning: The 'ConnectionString' parameter contains an unknown connection method.";
866cdf0e10cSrcweir             }
867cdf0e10cSrcweir         }
868cdf0e10cSrcweir         return errorMessage;
869cdf0e10cSrcweir     }
870cdf0e10cSrcweir 
871cdf0e10cSrcweir     /**
872cdf0e10cSrcweir      * String.replaceAll() ist available since Java 1.4 but the runner must be buldabale with Java 1.3
873cdf0e10cSrcweir      * @param originalString
874cdf0e10cSrcweir      * @param searchString
875cdf0e10cSrcweir      * @param replaceString
876cdf0e10cSrcweir      * @return modified string
877cdf0e10cSrcweir      */
replaceAll13(String originalString, String searchString, String replaceString)878cdf0e10cSrcweir     public static String replaceAll13(String originalString, String searchString, String replaceString) {
879cdf0e10cSrcweir 
880cdf0e10cSrcweir         StringBuffer changeStringBuffer = new StringBuffer(originalString);
881cdf0e10cSrcweir         int searchLength = searchString.length();
882cdf0e10cSrcweir         int replaceLength = replaceString.length();
883cdf0e10cSrcweir         int index = originalString.indexOf(searchString);
884cdf0e10cSrcweir         while (index != -1) {
885cdf0e10cSrcweir             changeStringBuffer = changeStringBuffer.replace(index, index + searchLength, replaceString);
886cdf0e10cSrcweir             originalString = changeStringBuffer.toString();
887cdf0e10cSrcweir             index = originalString.indexOf(searchString, index + replaceLength);
888cdf0e10cSrcweir         }
889cdf0e10cSrcweir         return originalString;
890cdf0e10cSrcweir     }
891cdf0e10cSrcweir 
892cdf0e10cSrcweir     /**
893cdf0e10cSrcweir      * expand macrofied strings like <CODE>${$ORIGIN/bootstrap.ini:UserInstallation}</CODE> or
894cdf0e10cSrcweir      * <CODE>$_OS</CODE>
895cdf0e10cSrcweir      * @param xMSF the MultiServiceFactory
896cdf0e10cSrcweir      * @param expand the string to expand
897cdf0e10cSrcweir      * @throws java.lang.Exception was thrown on any exception
898cdf0e10cSrcweir      * @return return the expanded string
899e6b649b5SPedro Giffuni      * @see com.sun.star.util.XMacroExpander
900cdf0e10cSrcweir      */
expandMacro(XMultiServiceFactory xMSF, String expand)901cdf0e10cSrcweir     public static String expandMacro(XMultiServiceFactory xMSF, String expand) throws java.lang.Exception {
902cdf0e10cSrcweir         try {
903cdf0e10cSrcweir             XPropertySet xPS = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xMSF);
904cdf0e10cSrcweir             XComponentContext xContext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class,
905cdf0e10cSrcweir                 xPS.getPropertyValue("DefaultContext"));
906cdf0e10cSrcweir             XMacroExpander xME = (XMacroExpander) UnoRuntime.queryInterface(XMacroExpander.class,
907cdf0e10cSrcweir                 xContext.getValueByName("/singletons/com.sun.star.util.theMacroExpander"));
908cdf0e10cSrcweir             return xME.expandMacros(expand);
909cdf0e10cSrcweir         } catch (Exception e) {
910cdf0e10cSrcweir             throw new Exception("could not expand macro: " + e.toString(), e);
911cdf0e10cSrcweir         }
912cdf0e10cSrcweir 
913cdf0e10cSrcweir     }
914cdf0e10cSrcweir 
915cdf0e10cSrcweir     /**
916cdf0e10cSrcweir      * returns the platform of the office.<br>
917cdf0e10cSrcweir      * Since the runner and the office could run on different platform this function delivers the
918cdf0e10cSrcweir      * platform the office is running.
919cdf0e10cSrcweir      * @param xMSF the XMultiServiceFactory
920cdf0e10cSrcweir      * @return unxsols, unxsoli, unxlngi, wntmsci
921cdf0e10cSrcweir      */
getOfficeOS(XMultiServiceFactory xMSF)922cdf0e10cSrcweir     public static String getOfficeOS(XMultiServiceFactory xMSF) {
923cdf0e10cSrcweir         String platform = "unknown";
924cdf0e10cSrcweir 
925cdf0e10cSrcweir         try {
926cdf0e10cSrcweir             String theOS = expandMacro(xMSF, "$_OS");
927cdf0e10cSrcweir 
928cdf0e10cSrcweir             if (theOS.equals("Windows")) {
929cdf0e10cSrcweir                 platform = "wntmsci";
930cdf0e10cSrcweir             } else if (theOS.equals("Linux")) {
931cdf0e10cSrcweir                 platform = "unxlngi";
932cdf0e10cSrcweir             } else {
933cdf0e10cSrcweir                 if (theOS.equals("Solaris")) {
934cdf0e10cSrcweir                     String theArch = expandMacro(xMSF, "$_ARCH");
935cdf0e10cSrcweir                     if (theArch.equals("SPARC")) {
936cdf0e10cSrcweir                         platform = "unxsols";
937cdf0e10cSrcweir                     } else if (theArch.equals("x86")) {
938cdf0e10cSrcweir                         platform = "unxsoli";
939cdf0e10cSrcweir                     }
940cdf0e10cSrcweir                 }
941cdf0e10cSrcweir             }
942cdf0e10cSrcweir         } catch (Exception ex) {
943cdf0e10cSrcweir         }
944cdf0e10cSrcweir         return platform;
945cdf0e10cSrcweir     }
946cdf0e10cSrcweir 
947cdf0e10cSrcweir     /**
948cdf0e10cSrcweir      * dispatches given <CODE>URL</CODE> to the document <CODE>XComponent</CODE>
949cdf0e10cSrcweir      * @param xMSF the <CODE>XMultiServiceFactory</CODE>
950cdf0e10cSrcweir      * @param xDoc the document where to dispatch
951cdf0e10cSrcweir      * @param URL the <CODE>URL</CODE> to dispatch
952cdf0e10cSrcweir      * @throws java.lang.Exception throws <CODE>java.lang.Exception</CODE> on any error
953cdf0e10cSrcweir      */
dispatchURL(XMultiServiceFactory xMSF, XComponent xDoc, String URL)954cdf0e10cSrcweir     public static void dispatchURL(XMultiServiceFactory xMSF, XComponent xDoc, String URL) throws java.lang.Exception {
955cdf0e10cSrcweir         XModel aModel = (XModel) UnoRuntime.queryInterface(XModel.class, xDoc);
956cdf0e10cSrcweir 
957cdf0e10cSrcweir         XController xCont = aModel.getCurrentController();
958cdf0e10cSrcweir 
959cdf0e10cSrcweir         dispatchURL(xMSF, xCont, URL);
960cdf0e10cSrcweir 
961cdf0e10cSrcweir     }
962cdf0e10cSrcweir 
963cdf0e10cSrcweir     /**
964cdf0e10cSrcweir      * dispatches given <CODE>URL</CODE> to the <CODE>XController</CODE>
965cdf0e10cSrcweir      * @param xMSF the <CODE>XMultiServiceFactory</CODE>
966e6b649b5SPedro Giffuni      * @param xCont the <CODE>XController</CODE> to query for a XDispatchProvider
967cdf0e10cSrcweir      * @param URL the <CODE>URL</CODE> to dispatch
968cdf0e10cSrcweir      * @throws java.lang.Exception throws <CODE>java.lang.Exception</CODE> on any error
969cdf0e10cSrcweir      */
dispatchURL(XMultiServiceFactory xMSF, XController xCont, String URL)970cdf0e10cSrcweir     public static void dispatchURL(XMultiServiceFactory xMSF, XController xCont, String URL) throws java.lang.Exception {
971cdf0e10cSrcweir         try {
972cdf0e10cSrcweir 
973cdf0e10cSrcweir             XDispatchProvider xDispProv = (XDispatchProvider) UnoRuntime.queryInterface(XDispatchProvider.class, xCont);
974cdf0e10cSrcweir 
975cdf0e10cSrcweir             XURLTransformer xParser = (com.sun.star.util.XURLTransformer) UnoRuntime.queryInterface(
976cdf0e10cSrcweir                 XURLTransformer.class,
977cdf0e10cSrcweir                 xMSF.createInstance("com.sun.star.util.URLTransformer"));
978cdf0e10cSrcweir 
979cdf0e10cSrcweir             // Because it's an in/out parameter we must use an array of URL objects.
980cdf0e10cSrcweir             URL[] aParseURL = new URL[1];
981cdf0e10cSrcweir             aParseURL[0] = new URL();
982cdf0e10cSrcweir             aParseURL[0].Complete = URL;
983cdf0e10cSrcweir             xParser.parseStrict(aParseURL);
984cdf0e10cSrcweir 
985cdf0e10cSrcweir             URL aURL = aParseURL[0];
986cdf0e10cSrcweir 
987cdf0e10cSrcweir             XDispatch xDispatcher = xDispProv.queryDispatch(aURL, "", 0);
988cdf0e10cSrcweir             xDispatcher.dispatch(aURL, null);
989cdf0e10cSrcweir 
990cdf0e10cSrcweir             utils.shortWait(3000);
991cdf0e10cSrcweir 
992cdf0e10cSrcweir         } catch (Exception e) {
993cdf0e10cSrcweir             throw new Exception("ERROR: could not dispatch URL '" + URL + "': " + e.toString());
994cdf0e10cSrcweir         }
995cdf0e10cSrcweir     }
996cdf0e10cSrcweir 
997cdf0e10cSrcweir     /** returns a String which contains the current date and time<br>
998cdf0e10cSrcweir      *  format: [DD.MM.YYYY - HH:MM:SS::mm]
999cdf0e10cSrcweir      *
1000cdf0e10cSrcweir      ** @return a String which contains the current date and time
1001cdf0e10cSrcweir      */
getDateTime()1002cdf0e10cSrcweir     public static String getDateTime() {
1003cdf0e10cSrcweir 
1004cdf0e10cSrcweir         Calendar cal = new GregorianCalendar();
1005cdf0e10cSrcweir         DecimalFormat dfmt = new DecimalFormat("00");
1006cdf0e10cSrcweir         String dateTime = dfmt.format(cal.get(Calendar.DAY_OF_MONTH)) + "." +
1007cdf0e10cSrcweir             dfmt.format(cal.get(Calendar.MONTH) + 1) + "." +
1008cdf0e10cSrcweir             dfmt.format(cal.get(Calendar.YEAR)) + " - " +
1009cdf0e10cSrcweir             dfmt.format(cal.get(Calendar.HOUR_OF_DAY)) + ":" +
1010cdf0e10cSrcweir             dfmt.format(cal.get(Calendar.MINUTE)) + ":" +
1011cdf0e10cSrcweir             dfmt.format(cal.get(Calendar.SECOND)) + "," +
1012cdf0e10cSrcweir             dfmt.format(cal.get(Calendar.MILLISECOND));
1013cdf0e10cSrcweir         return "[" + dateTime + "]";
1014cdf0e10cSrcweir     }
1015cdf0e10cSrcweir }
1016