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 22ef39d40dSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package helper; 25cdf0e10cSrcweir 26cdf0e10cSrcweir // __________ Imports __________ 27cdf0e10cSrcweir 28cdf0e10cSrcweir // exceptions 29cdf0e10cSrcweir import java.net.MalformedURLException; 30cdf0e10cSrcweir 31cdf0e10cSrcweir // interfaces 32cdf0e10cSrcweir import com.sun.star.util.XURLTransformer; 33cdf0e10cSrcweir 34cdf0e10cSrcweir // others 35cdf0e10cSrcweir import java.io.File; 36cdf0e10cSrcweir import java.util.Vector; 37cdf0e10cSrcweir import java.util.Enumeration; 38cdf0e10cSrcweir 39cdf0e10cSrcweir 40cdf0e10cSrcweir /** 41cdf0e10cSrcweir * It collects some static helper functons to handle URLs. 42cdf0e10cSrcweir * Sometimes it's neccessary to convert URL from/to system pathes. 43cdf0e10cSrcweir * Or from string to strutural notations (e.g. com.sun.star.util.URL). 44cdf0e10cSrcweir * And sometimes java had another notation then the office it has. 45cdf0e10cSrcweir * 46cdf0e10cSrcweir */ 47cdf0e10cSrcweir public class URLHelper 48cdf0e10cSrcweir { 49cdf0e10cSrcweir // ____________________ 50cdf0e10cSrcweir 51cdf0e10cSrcweir /** 52cdf0e10cSrcweir * Because the office need URLs for loading/saving documents 53cdf0e10cSrcweir * we must convert used system pathes. 54cdf0e10cSrcweir * And java use another notation for file URLs ... correct it. 55cdf0e10cSrcweir * 56cdf0e10cSrcweir * @param aSystemPath 57cdf0e10cSrcweir * represent the file in system notation 58cdf0e10cSrcweir * 59cdf0e10cSrcweir * @return [String] 60cdf0e10cSrcweir * a file url which represent the given system path 61cdf0e10cSrcweir */ getFileURLFromSystemPath( File aSystemPath )62cdf0e10cSrcweir public static String getFileURLFromSystemPath( File aSystemPath ) 63cdf0e10cSrcweir { 64cdf0e10cSrcweir String sFileURL = null; 65cdf0e10cSrcweir try 66cdf0e10cSrcweir { 67cdf0e10cSrcweir //sFileURL = aSystemPath.toURI().toURL().toString(); 68cdf0e10cSrcweir sFileURL = aSystemPath.toURL().toString(); 69cdf0e10cSrcweir } 70cdf0e10cSrcweir catch( MalformedURLException exWrong ) 71cdf0e10cSrcweir { 72cdf0e10cSrcweir sFileURL = null; 73cdf0e10cSrcweir } 74cdf0e10cSrcweir 75cdf0e10cSrcweir // problem of java: file URL's are coded with 1 slash instead of 2 or 3 ones! 76cdf0e10cSrcweir // => correct this problem first, otherwise office can't use these URL's 77cdf0e10cSrcweir if( 78cdf0e10cSrcweir (sFileURL != null ) && 79cdf0e10cSrcweir (sFileURL.startsWith("file:/") == true ) && 80cdf0e10cSrcweir (sFileURL.startsWith("file://") == false) 81cdf0e10cSrcweir ) 82cdf0e10cSrcweir { 83cdf0e10cSrcweir StringBuffer sWorkBuffer = new StringBuffer(sFileURL); 84cdf0e10cSrcweir sWorkBuffer.insert(6,"//"); 85cdf0e10cSrcweir sFileURL = sWorkBuffer.toString(); 86cdf0e10cSrcweir } 87cdf0e10cSrcweir 88cdf0e10cSrcweir return sFileURL; 89cdf0e10cSrcweir } 90cdf0e10cSrcweir 91cdf0e10cSrcweir // ____________________ 92cdf0e10cSrcweir 93cdf0e10cSrcweir /** 94cdf0e10cSrcweir * The same as getFileURLFromSystemPath() before but uses string parameter instead 95cdf0e10cSrcweir * of a File type. It exist to supress converting of neccessary parameters in the 96cdf0e10cSrcweir * outside code. But of course getFileURLFromSystemPath(File) will be a little bit faster 97cdf0e10cSrcweir * then this method ... 98cdf0e10cSrcweir * 99cdf0e10cSrcweir * @param sSystemPath 100cdf0e10cSrcweir * represent the file in system notation 101cdf0e10cSrcweir * 102cdf0e10cSrcweir * @return [String] 103cdf0e10cSrcweir * a file url which represent the given system path 104cdf0e10cSrcweir */ getFileURLFromSystemPath( String sSystemPath )105cdf0e10cSrcweir public static String getFileURLFromSystemPath( String sSystemPath ) 106cdf0e10cSrcweir { 107cdf0e10cSrcweir return getFileURLFromSystemPath(new File(sSystemPath)); 108cdf0e10cSrcweir } 109cdf0e10cSrcweir 110cdf0e10cSrcweir // ____________________ 111cdf0e10cSrcweir 112cdf0e10cSrcweir /** 113cdf0e10cSrcweir * Does the same as getFileURLFromSystemPath() before ... but uses 114cdf0e10cSrcweir * the given protocol string (e.g."http://") insted of "file:///". 115cdf0e10cSrcweir * 116cdf0e10cSrcweir * @param aSystemPath 117cdf0e10cSrcweir * represent the file in system notation 118cdf0e10cSrcweir * 119cdf0e10cSrcweir * @param aBasePath 120cdf0e10cSrcweir * define the base path of the aSystemPath value, 121cdf0e10cSrcweir * which must be replaced with the value of "sServerPath". 122cdf0e10cSrcweir * 123cdf0e10cSrcweir * @param sServerURL 124cdf0e10cSrcweir * Will be used to replace sBasePath. 125cdf0e10cSrcweir * 126cdf0e10cSrcweir * @example 127cdf0e10cSrcweir * System Path = "d:\test\file.txt" 128cdf0e10cSrcweir * Base Path = "d:\test" 129cdf0e10cSrcweir * Server Path = "http://alaska:8000" 130cdf0e10cSrcweir * => "http://alaska:8000/file.txt" 131cdf0e10cSrcweir * 132cdf0e10cSrcweir * @return [String] 133cdf0e10cSrcweir * an url which represent the given system path 134cdf0e10cSrcweir * and uses the given protocol 135cdf0e10cSrcweir */ getURLWithProtocolFromSystemPath( File aSystemPath, File aBasePath, String sServerURL )136cdf0e10cSrcweir public static String getURLWithProtocolFromSystemPath( File aSystemPath, File aBasePath, String sServerURL ) 137cdf0e10cSrcweir { 138cdf0e10cSrcweir String sFileURL = URLHelper.getFileURLFromSystemPath(aSystemPath); 139cdf0e10cSrcweir String sBaseURL = URLHelper.getFileURLFromSystemPath(aBasePath ); 140cdf0e10cSrcweir 141cdf0e10cSrcweir // cut last '/'! 142cdf0e10cSrcweir if (sBaseURL.lastIndexOf('/')==(sBaseURL.length()-1)) 143cdf0e10cSrcweir sBaseURL = sBaseURL.substring(0,sBaseURL.length()-1); 144cdf0e10cSrcweir 145cdf0e10cSrcweir // cut last '/'! 146cdf0e10cSrcweir if (sServerURL.lastIndexOf('/')==(sServerURL.length()-1)) 147cdf0e10cSrcweir sServerURL = sServerURL.substring(0,sServerURL.length()-1); 148cdf0e10cSrcweir 149cdf0e10cSrcweir int index = sFileURL.indexOf(sBaseURL); 150cdf0e10cSrcweir String sURL = sFileURL.substring(0,index) + sServerURL + 151cdf0e10cSrcweir sFileURL.substring(index+sBaseURL.length()); 152cdf0e10cSrcweir //String sURL = sFileURL.replaceFirst(sBaseURL,sServerURL); 153cdf0e10cSrcweir return sURL; 154cdf0e10cSrcweir } 155cdf0e10cSrcweir 156cdf0e10cSrcweir // ____________________ 157cdf0e10cSrcweir 158cdf0e10cSrcweir /** 159cdf0e10cSrcweir * The same as getURLWithProtocolFromSystemPath() before but uses string parameter instead 160cdf0e10cSrcweir * of a File types. It exist to supress converting of neccessary parameters in the 161cdf0e10cSrcweir * outside code. But of course getURLWithProtocolFromSystemPath(File,File,String) will be 162cdf0e10cSrcweir * a little bit faster then this method ... 163cdf0e10cSrcweir * 164cdf0e10cSrcweir * @param sSystemPath 165cdf0e10cSrcweir * represent the file in system notation 166cdf0e10cSrcweir * 167cdf0e10cSrcweir * @param sBasePath 168cdf0e10cSrcweir * define the base path of the aSystemPath value, 169cdf0e10cSrcweir * which must be replaced with the value of "sServerPath". 170cdf0e10cSrcweir * 171cdf0e10cSrcweir * @param sServerPath 172cdf0e10cSrcweir * Will be used to replace sBasePath. 173cdf0e10cSrcweir * 174cdf0e10cSrcweir * @example 175cdf0e10cSrcweir * System Path = "d:\test\file.txt" 176cdf0e10cSrcweir * Base Path = "d:\test" 177cdf0e10cSrcweir * Server Path = "http://alaska:8000" 178cdf0e10cSrcweir * => "http://alaska:8000/file.txt" 179cdf0e10cSrcweir * 180cdf0e10cSrcweir * @return [String] 181cdf0e10cSrcweir * an url which represent the given system path 182cdf0e10cSrcweir * and uses the given protocol 183cdf0e10cSrcweir */ getURLWithProtocolFromSystemPath( String sSystemPath, String sBasePath, String sServerPath )184cdf0e10cSrcweir public static String getURLWithProtocolFromSystemPath( String sSystemPath, String sBasePath, String sServerPath ) 185cdf0e10cSrcweir { 186cdf0e10cSrcweir return getURLWithProtocolFromSystemPath(new File(sSystemPath), new File(sBasePath), sServerPath); 187cdf0e10cSrcweir } 188cdf0e10cSrcweir 189cdf0e10cSrcweir // ____________________ 190cdf0e10cSrcweir 191cdf0e10cSrcweir /** 192cdf0e10cSrcweir * This convert an URL (formated as a string) to a struct com.sun.star.util.URL. 193cdf0e10cSrcweir * It use a special service to do that: the URLTransformer. 194cdf0e10cSrcweir * Because some API calls need it and it's not allowed to set "Complete" 195cdf0e10cSrcweir * part of the util struct only. The URL must be parsed. 196cdf0e10cSrcweir * 197cdf0e10cSrcweir * @param sURL 198cdf0e10cSrcweir * URL for parsing in string notation 199cdf0e10cSrcweir * 200cdf0e10cSrcweir * @return [com.sun.star.util.URL] 201cdf0e10cSrcweir * URL in UNO struct notation 202cdf0e10cSrcweir */ parseURL(XURLTransformer xParser, String sURL)203cdf0e10cSrcweir public static com.sun.star.util.URL parseURL(XURLTransformer xParser, String sURL) 204cdf0e10cSrcweir { 205cdf0e10cSrcweir com.sun.star.util.URL aURL = null; 206cdf0e10cSrcweir 207cdf0e10cSrcweir if (sURL==null || sURL.equals("")) 208cdf0e10cSrcweir return null; 209cdf0e10cSrcweir 210cdf0e10cSrcweir try 211cdf0e10cSrcweir { 212cdf0e10cSrcweir // Create special service for parsing of given URL. 213cdf0e10cSrcweir /* com.sun.star.util.XURLTransformer xParser = (com.sun.star.util.XURLTransformer)OfficeConnect.createRemoteInstance( 214cdf0e10cSrcweir com.sun.star.util.XURLTransformer.class, 215cdf0e10cSrcweir "com.sun.star.util.URLTransformer"); 216cdf0e10cSrcweir */ 217cdf0e10cSrcweir // Because it's an in/out parameter we must use an array of URL objects. 218cdf0e10cSrcweir com.sun.star.util.URL[] aParseURL = new com.sun.star.util.URL[1]; 219cdf0e10cSrcweir aParseURL[0] = new com.sun.star.util.URL(); 220cdf0e10cSrcweir aParseURL[0].Complete = sURL; 221cdf0e10cSrcweir 222cdf0e10cSrcweir // Parse the URL 223cdf0e10cSrcweir xParser.parseStrict(aParseURL); 224cdf0e10cSrcweir 225cdf0e10cSrcweir aURL = aParseURL[0]; 226cdf0e10cSrcweir } 227cdf0e10cSrcweir catch(com.sun.star.uno.RuntimeException exRuntime) 228cdf0e10cSrcweir { 229cdf0e10cSrcweir // Any UNO method of this scope can throw this exception. 230cdf0e10cSrcweir // Reset the return value only. 231cdf0e10cSrcweir aURL = null; 232cdf0e10cSrcweir } 233cdf0e10cSrcweir 234cdf0e10cSrcweir return aURL; 235cdf0e10cSrcweir } 236cdf0e10cSrcweir 237cdf0e10cSrcweir //_________________________________ 238cdf0e10cSrcweir /** 239cdf0e10cSrcweir * Return a name list of all available files of a directory. 240cdf0e10cSrcweir * We filter pure sub directories names. All other files 241cdf0e10cSrcweir * are returned as full qualified URL strings. So they can be 242cdf0e10cSrcweir * used for further purposes. One parameter define the start directory, 243cdf0e10cSrcweir * another one describe the url protocol, which the return URL names should have. 244cdf0e10cSrcweir * 245*e6b649b5SPedro Giffuni * @param sStartDir 246cdf0e10cSrcweir * the start directory, which should include all test files 247cdf0e10cSrcweir * 248cdf0e10cSrcweir * @return [Vector] 249cdf0e10cSrcweir * a filtered list of java File objects of all available files of the start dir 250cdf0e10cSrcweir * and all accessable sub directories. 251cdf0e10cSrcweir */ getSystemFilesFromDir(String sStartDir)252cdf0e10cSrcweir public static Vector getSystemFilesFromDir(String sStartDir) 253cdf0e10cSrcweir { 254cdf0e10cSrcweir File aRoot = new File(sStartDir); 255cdf0e10cSrcweir 256cdf0e10cSrcweir if (! aRoot.exists()) 257cdf0e10cSrcweir return null; 258cdf0e10cSrcweir 259cdf0e10cSrcweir if (! aRoot.isDirectory()) 260cdf0e10cSrcweir return null; 261cdf0e10cSrcweir 262cdf0e10cSrcweir File[] lAllFiles = aRoot.listFiles(); 263cdf0e10cSrcweir if (lAllFiles == null ) 264cdf0e10cSrcweir return null; 265cdf0e10cSrcweir 266cdf0e10cSrcweir Vector lFilteredFiles = new Vector(lAllFiles.length); 267cdf0e10cSrcweir 268cdf0e10cSrcweir for (int i=0; i<lAllFiles.length; ++i) 269cdf0e10cSrcweir { 270cdf0e10cSrcweir if (lAllFiles[i].isFile()) 271cdf0e10cSrcweir lFilteredFiles.add(lAllFiles[i]); 272cdf0e10cSrcweir else 273cdf0e10cSrcweir if (lAllFiles[i].isDirectory()) 274cdf0e10cSrcweir { 275cdf0e10cSrcweir // recursion! 276cdf0e10cSrcweir Vector lSubFiles = URLHelper.getSystemFilesFromDir(lAllFiles[i].getPath()); 277cdf0e10cSrcweir if (lSubFiles != null) 278cdf0e10cSrcweir { 279cdf0e10cSrcweir Enumeration aSnapshot = lSubFiles.elements(); 280cdf0e10cSrcweir while (aSnapshot.hasMoreElements()) 281cdf0e10cSrcweir lFilteredFiles.add(aSnapshot.nextElement()); 282cdf0e10cSrcweir } 283cdf0e10cSrcweir } 284cdf0e10cSrcweir } 285cdf0e10cSrcweir 286cdf0e10cSrcweir return lFilteredFiles; 287cdf0e10cSrcweir } 288cdf0e10cSrcweir } 289