xref: /AOO42X/main/qadevOOo/runner/util/SysUtils.java (revision 1a9ecf28287d28b6b3ee0448b1cc3a0b3483019e)
1ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5ef39d40dSAndrew Rist  * distributed with this work for additional information
6ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18ef39d40dSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20ef39d40dSAndrew Rist  *************************************************************/
21ef39d40dSAndrew Rist 
22cdf0e10cSrcweir package util;
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import java.io.File;
25cdf0e10cSrcweir import java.io.FileFilter;
26cdf0e10cSrcweir import java.util.ArrayList;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir import com.sun.star.frame.XDesktop;
29cdf0e10cSrcweir import com.sun.star.frame.XFrame;
30cdf0e10cSrcweir import com.sun.star.lang.XComponent;
31cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
32cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
33cdf0e10cSrcweir import com.sun.star.datatransfer.clipboard.*;
34cdf0e10cSrcweir import com.sun.star.datatransfer.*;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir public class SysUtils {
37cdf0e10cSrcweir 
getJavaPath()38cdf0e10cSrcweir     public static String getJavaPath() {
39cdf0e10cSrcweir         String cp = (String) System.getProperty("java.class.path");
40cdf0e10cSrcweir         String jh = (String) System.getProperty("java.home");
41cdf0e10cSrcweir         String fs = (String) System.getProperty("file.separator");
42cdf0e10cSrcweir         jh = jh + fs + "bin" + fs;
43cdf0e10cSrcweir         jh = jh + "java -classpath "+cp;
44cdf0e10cSrcweir         return jh;
45cdf0e10cSrcweir     }
46cdf0e10cSrcweir 
47cdf0e10cSrcweir static ArrayList files = new ArrayList();
48cdf0e10cSrcweir 
traverse( String afileDirectory )49cdf0e10cSrcweir public static Object[] traverse( String afileDirectory ) {
50cdf0e10cSrcweir 
51cdf0e10cSrcweir     File fileDirectory = new File(afileDirectory);
52cdf0e10cSrcweir     // Testing, if the file is a directory, and if so, it throws an exception
53cdf0e10cSrcweir     if ( !fileDirectory.isDirectory() ) {
54cdf0e10cSrcweir       throw new IllegalArgumentException(
55cdf0e10cSrcweir       "not a directory: " + fileDirectory.getName()
56cdf0e10cSrcweir       );
57cdf0e10cSrcweir     }
58cdf0e10cSrcweir 
59cdf0e10cSrcweir     // Getting all files and directories in the current directory
60cdf0e10cSrcweir     File[] entries = fileDirectory.listFiles(
61cdf0e10cSrcweir     new FileFilter() {
62cdf0e10cSrcweir       public boolean accept( File pathname ) {
63cdf0e10cSrcweir         return true;
64cdf0e10cSrcweir       }
65cdf0e10cSrcweir     }
66cdf0e10cSrcweir     );
67cdf0e10cSrcweir 
68cdf0e10cSrcweir     // Iterating for each file and directory
69cdf0e10cSrcweir     for ( int i = 0; i < entries.length; ++i ) {
70cdf0e10cSrcweir       // Testing, if the entry in the list is a directory
71cdf0e10cSrcweir       if ( entries[ i ].isDirectory() ) {
72cdf0e10cSrcweir         // Recursive call for the new directory
73cdf0e10cSrcweir         traverse( entries[ i ].getAbsolutePath() );
74cdf0e10cSrcweir       } else {
75cdf0e10cSrcweir         // adding file to List
76cdf0e10cSrcweir         try {
77*1a9ecf28SJohn Bampton           // Composing the URL by replacing all backslashes
78cdf0e10cSrcweir           String stringUrl = "file:///"
79cdf0e10cSrcweir           + entries[ i ].getAbsolutePath().replace( '\\', '/' );
80cdf0e10cSrcweir           files.add(stringUrl);
81cdf0e10cSrcweir         }
82cdf0e10cSrcweir         catch( Exception exception ) {
83cdf0e10cSrcweir           exception.printStackTrace();
84cdf0e10cSrcweir         }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir         }
87cdf0e10cSrcweir     }
88cdf0e10cSrcweir     return files.toArray();
89cdf0e10cSrcweir }
90cdf0e10cSrcweir 
getActiveComponent(XMultiServiceFactory msf)91cdf0e10cSrcweir   public static XComponent getActiveComponent(XMultiServiceFactory msf) {
92cdf0e10cSrcweir     XComponent ac = null;
93cdf0e10cSrcweir     try {
94cdf0e10cSrcweir         Object desk = msf.createInstance("com.sun.star.frame.Desktop");
95cdf0e10cSrcweir         XDesktop xDesk = (XDesktop) UnoRuntime.queryInterface(XDesktop.class,desk);
96cdf0e10cSrcweir         ac = xDesk.getCurrentComponent();
97cdf0e10cSrcweir     } catch (com.sun.star.uno.Exception e) {
98cdf0e10cSrcweir         System.out.println("Couldn't get active Component");
99cdf0e10cSrcweir     }
100cdf0e10cSrcweir     return ac;
101cdf0e10cSrcweir   }
102cdf0e10cSrcweir 
getActiveFrame(XMultiServiceFactory msf)103cdf0e10cSrcweir   public static XFrame getActiveFrame(XMultiServiceFactory msf) {
104cdf0e10cSrcweir     try {
105cdf0e10cSrcweir         Object desk = msf.createInstance("com.sun.star.frame.Desktop");
106cdf0e10cSrcweir         XDesktop xDesk = (XDesktop) UnoRuntime.queryInterface(XDesktop.class,desk);
107cdf0e10cSrcweir         return xDesk.getCurrentFrame();
108cdf0e10cSrcweir     } catch (com.sun.star.uno.Exception e) {
109cdf0e10cSrcweir         System.out.println("Couldn't get active Component");
110cdf0e10cSrcweir     }
111cdf0e10cSrcweir 
112cdf0e10cSrcweir     return null;
113cdf0e10cSrcweir   }
114cdf0e10cSrcweir 
115cdf0e10cSrcweir   /**
116a1236c7fSmseidel    * Tries to obtain text data from clipboard if such one exists.
117cdf0e10cSrcweir    * The method iterates through all 'text/plain' supported data
118cdf0e10cSrcweir    * flavors and returns the first non-null String value.
119cdf0e10cSrcweir    *
120cdf0e10cSrcweir    * @param msf MultiserviceFactory
121cdf0e10cSrcweir    * @return First found string clipboard contents or null if no
122cdf0e10cSrcweir    *    text contents were found.
123cdf0e10cSrcweir    * @throws com.sun.star.uno.Exception if system clipboard is not accessible.
124cdf0e10cSrcweir    */
getSysClipboardText(XMultiServiceFactory msf)125cdf0e10cSrcweir   public static String getSysClipboardText(XMultiServiceFactory msf)
126cdf0e10cSrcweir         throws com.sun.star.uno.Exception {
127cdf0e10cSrcweir 
128cdf0e10cSrcweir     XClipboard xCB = (XClipboard) UnoRuntime.queryInterface
129cdf0e10cSrcweir         (XClipboard.class, msf.createInstance
130cdf0e10cSrcweir         ("com.sun.star.datatransfer.clipboard.SystemClipboard"));
131cdf0e10cSrcweir 
132cdf0e10cSrcweir     XTransferable xTrans = xCB.getContents();
133cdf0e10cSrcweir 
134cdf0e10cSrcweir     DataFlavor[] dfs = xTrans.getTransferDataFlavors();
135cdf0e10cSrcweir 
136cdf0e10cSrcweir     for (int i = 0; i < dfs.length; i++) {
137cdf0e10cSrcweir         if (dfs[i].MimeType.startsWith("text/plain")) {
138cdf0e10cSrcweir             Object data = xTrans.getTransferData(dfs[i]);
139cdf0e10cSrcweir             if (data != null && data instanceof String) {
140cdf0e10cSrcweir                 return (String) data;
141cdf0e10cSrcweir             }
142cdf0e10cSrcweir         }
143cdf0e10cSrcweir     }
144cdf0e10cSrcweir 
145cdf0e10cSrcweir     return null;
146cdf0e10cSrcweir   }
147cdf0e10cSrcweir }
148a1236c7fSmseidel 
149a1236c7fSmseidel /* vim: set noet sw=4 ts=4: */
150