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