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