xref: /AOO41X/main/qadevOOo/runner/util/SysUtils.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 
24cdf0e10cSrcweir package util;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.io.File;
27cdf0e10cSrcweir import java.io.FileFilter;
28cdf0e10cSrcweir import java.util.ArrayList;
29cdf0e10cSrcweir 
30cdf0e10cSrcweir import com.sun.star.frame.XDesktop;
31cdf0e10cSrcweir import com.sun.star.frame.XFrame;
32cdf0e10cSrcweir import com.sun.star.lang.XComponent;
33cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
34cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
35cdf0e10cSrcweir import com.sun.star.datatransfer.clipboard.*;
36cdf0e10cSrcweir import com.sun.star.datatransfer.*;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir public class SysUtils {
39cdf0e10cSrcweir 
getJavaPath()40cdf0e10cSrcweir     public static String getJavaPath() {
41cdf0e10cSrcweir         String cp = (String) System.getProperty("java.class.path");
42cdf0e10cSrcweir         String jh = (String) System.getProperty("java.home");
43cdf0e10cSrcweir         String fs = (String) System.getProperty("file.separator");
44cdf0e10cSrcweir         jh = jh + fs + "bin" + fs;
45cdf0e10cSrcweir         jh = jh + "java -classpath "+cp;
46cdf0e10cSrcweir         return jh;
47cdf0e10cSrcweir     }
48cdf0e10cSrcweir 
49cdf0e10cSrcweir   static ArrayList files = new ArrayList();
50cdf0e10cSrcweir 
traverse( String afileDirectory )51cdf0e10cSrcweir   public static Object[] traverse( String afileDirectory ) {
52cdf0e10cSrcweir 
53cdf0e10cSrcweir     File fileDirectory = new File(afileDirectory);
54cdf0e10cSrcweir     // Testing, if the file is a directory, and if so, it throws an exception
55cdf0e10cSrcweir     if ( !fileDirectory.isDirectory() ) {
56cdf0e10cSrcweir       throw new IllegalArgumentException(
57cdf0e10cSrcweir       "not a directory: " + fileDirectory.getName()
58cdf0e10cSrcweir       );
59cdf0e10cSrcweir     }
60cdf0e10cSrcweir 
61cdf0e10cSrcweir     // Getting all files and directories in the current directory
62cdf0e10cSrcweir     File[] entries = fileDirectory.listFiles(
63cdf0e10cSrcweir     new FileFilter() {
64cdf0e10cSrcweir       public boolean accept( File pathname ) {
65cdf0e10cSrcweir         return true;
66cdf0e10cSrcweir       }
67cdf0e10cSrcweir     }
68cdf0e10cSrcweir     );
69cdf0e10cSrcweir 
70cdf0e10cSrcweir     // Iterating for each file and directory
71cdf0e10cSrcweir     for ( int i = 0; i < entries.length; ++i ) {
72cdf0e10cSrcweir       // Testing, if the entry in the list is a directory
73cdf0e10cSrcweir       if ( entries[ i ].isDirectory() ) {
74cdf0e10cSrcweir         // Recursive call for the new directory
75cdf0e10cSrcweir         traverse( entries[ i ].getAbsolutePath() );
76cdf0e10cSrcweir       } else {
77cdf0e10cSrcweir         // adding file to List
78cdf0e10cSrcweir         try {
79cdf0e10cSrcweir           // Composing the URL by replacing all backslashs
80cdf0e10cSrcweir           String stringUrl = "file:///"
81cdf0e10cSrcweir           + entries[ i ].getAbsolutePath().replace( '\\', '/' );
82cdf0e10cSrcweir           files.add(stringUrl);
83cdf0e10cSrcweir         }
84cdf0e10cSrcweir         catch( Exception exception ) {
85cdf0e10cSrcweir           exception.printStackTrace();
86cdf0e10cSrcweir         }
87cdf0e10cSrcweir 
88cdf0e10cSrcweir       }
89cdf0e10cSrcweir     }
90cdf0e10cSrcweir     return files.toArray();
91cdf0e10cSrcweir   }
92cdf0e10cSrcweir 
getActiveComponent(XMultiServiceFactory msf)93cdf0e10cSrcweir   public static XComponent getActiveComponent(XMultiServiceFactory msf) {
94cdf0e10cSrcweir     XComponent ac = null;
95cdf0e10cSrcweir     try {
96cdf0e10cSrcweir         Object desk = msf.createInstance("com.sun.star.frame.Desktop");
97cdf0e10cSrcweir         XDesktop xDesk = (XDesktop) UnoRuntime.queryInterface(XDesktop.class,desk);
98cdf0e10cSrcweir         ac = xDesk.getCurrentComponent();
99cdf0e10cSrcweir     } catch (com.sun.star.uno.Exception e) {
100cdf0e10cSrcweir         System.out.println("Couldn't get active Component");
101cdf0e10cSrcweir     }
102cdf0e10cSrcweir     return ac;
103cdf0e10cSrcweir   }
104cdf0e10cSrcweir 
getActiveFrame(XMultiServiceFactory msf)105cdf0e10cSrcweir   public static XFrame getActiveFrame(XMultiServiceFactory msf) {
106cdf0e10cSrcweir     try {
107cdf0e10cSrcweir         Object desk = msf.createInstance("com.sun.star.frame.Desktop");
108cdf0e10cSrcweir         XDesktop xDesk = (XDesktop) UnoRuntime.queryInterface(XDesktop.class,desk);
109cdf0e10cSrcweir         return xDesk.getCurrentFrame();
110cdf0e10cSrcweir     } catch (com.sun.star.uno.Exception e) {
111cdf0e10cSrcweir         System.out.println("Couldn't get active Component");
112cdf0e10cSrcweir     }
113cdf0e10cSrcweir 
114cdf0e10cSrcweir     return null;
115cdf0e10cSrcweir   }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir   /**
118cdf0e10cSrcweir    * Tries to obtain text data from cliboard if such one exists.
119cdf0e10cSrcweir    * The method iterates through all 'text/plain' supported data
120cdf0e10cSrcweir    * flavors and returns the first non-null String value.
121cdf0e10cSrcweir    *
122cdf0e10cSrcweir    * @param msf MultiserviceFactory
123cdf0e10cSrcweir    * @return First found string clipboard contents or null if no
124cdf0e10cSrcweir    *    text contents were found.
125cdf0e10cSrcweir    * @throws com.sun.star.uno.Exception if system clipboard is not accessible.
126cdf0e10cSrcweir    */
getSysClipboardText(XMultiServiceFactory msf)127cdf0e10cSrcweir   public static String getSysClipboardText(XMultiServiceFactory msf)
128cdf0e10cSrcweir         throws com.sun.star.uno.Exception {
129cdf0e10cSrcweir 
130cdf0e10cSrcweir     XClipboard xCB = (XClipboard) UnoRuntime.queryInterface
131cdf0e10cSrcweir         (XClipboard.class, msf.createInstance
132cdf0e10cSrcweir         ("com.sun.star.datatransfer.clipboard.SystemClipboard"));
133cdf0e10cSrcweir 
134cdf0e10cSrcweir     XTransferable xTrans = xCB.getContents();
135cdf0e10cSrcweir 
136cdf0e10cSrcweir     DataFlavor[] dfs = xTrans.getTransferDataFlavors();
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     for (int i = 0; i < dfs.length; i++) {
139cdf0e10cSrcweir         if (dfs[i].MimeType.startsWith("text/plain")) {
140cdf0e10cSrcweir             Object data = xTrans.getTransferData(dfs[i]);
141cdf0e10cSrcweir             if (data != null && data instanceof String) {
142cdf0e10cSrcweir                 return (String) data;
143cdf0e10cSrcweir             }
144cdf0e10cSrcweir         }
145cdf0e10cSrcweir     }
146cdf0e10cSrcweir 
147cdf0e10cSrcweir     return null;
148cdf0e10cSrcweir   }
149cdf0e10cSrcweir }
150