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