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