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