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