bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / runner / helper / URLHelper.java
blob568178e4bf9bdbdd98ed8531e9d160c009d794df
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package helper;
21 // __________ Imports __________
23 // exceptions
24 import java.io.File;
25 import java.net.MalformedURLException;
26 import java.util.ArrayList;
27 import java.util.Iterator;
29 import com.sun.star.util.XURLTransformer;
32 /**
33 * It collects some static helper functons to handle URLs.
34 * Sometimes it's neccessary to convert URL from/to system paths.
35 * Or from string to strutural notations (e.g. com.sun.star.util.URL).
36 * And sometimes java had another notation then the office it has.
39 public class URLHelper
41 // ____________________
43 /**
44 * Because the office need URLs for loading/saving documents
45 * we must convert used system paths.
46 * And java use another notation for file URLs ... correct it.
48 * @param aSystemPath
49 * represent the file in system notation
51 * @return [String]
52 * a file url which represent the given system path
54 public static String getFileURLFromSystemPath( File aSystemPath )
56 String sFileURL = null;
57 try
59 sFileURL = aSystemPath.toURI().toURL().toString();
61 catch( MalformedURLException exWrong )
63 sFileURL = null;
66 // problem of java: file URL's are coded with 1 slash instead of 2 or 3 ones!
67 // => correct this problem first, otherwise office can't use these URL's
68 if(
69 (sFileURL != null ) &&
70 (sFileURL.startsWith("file:/") == true ) &&
71 (sFileURL.startsWith("file://") == false)
74 StringBuffer sWorkBuffer = new StringBuffer(sFileURL);
75 sWorkBuffer.insert(6,"//");
76 sFileURL = sWorkBuffer.toString();
79 return sFileURL;
82 // ____________________
84 /**
85 * The same as getFileURLFromSystemPath() before but uses string parameter instead
86 * of a File type. It exist to supress converting of neccessary parameters in the
87 * outside code. But of course getFileURLFromSystemPath(File) will be a little bit faster
88 * then this method ...
90 * @param sSystemPath
91 * represent the file in system notation
93 * @return [String]
94 * a file url which represent the given system path
96 public static String getFileURLFromSystemPath( String sSystemPath )
98 return getFileURLFromSystemPath(new File(sSystemPath));
101 // ____________________
104 * Does the same as getFileURLFromSystemPath() before ... but uses
105 * the given protocol string (e.g."http://") instead of "file:///".
107 * @param aSystemPath
108 * represent the file in system notation
110 * @param aBasePath
111 * define the base path of the aSystemPath value,
112 * which must be replaced with the value of "sServerPath".
114 * @param sServerURL
115 * Will be used to replace sBasePath.
117 * @example
118 * System Path = "d:\test\file.txt"
119 * Base Path = "d:\test"
120 * Server Path = "http://alaska:8000"
121 * => "http://alaska:8000/file.txt"
123 * @return [String]
124 * an url which represent the given system path
125 * and uses the given protocol
127 public static String getURLWithProtocolFromSystemPath( File aSystemPath, File aBasePath, String sServerURL )
129 String sFileURL = URLHelper.getFileURLFromSystemPath(aSystemPath);
130 String sBaseURL = URLHelper.getFileURLFromSystemPath(aBasePath );
132 // cut last '/'!
133 if (sBaseURL.lastIndexOf('/')==(sBaseURL.length()-1))
134 sBaseURL = sBaseURL.substring(0,sBaseURL.length()-1);
136 // cut last '/'!
137 if (sServerURL.lastIndexOf('/')==(sServerURL.length()-1))
138 sServerURL = sServerURL.substring(0,sServerURL.length()-1);
140 int index = sFileURL.indexOf(sBaseURL);
141 String sURL = sFileURL.substring(0,index) + sServerURL +
142 sFileURL.substring(index+sBaseURL.length());
143 //String sURL = sFileURL.replaceFirst(sBaseURL,sServerURL);
144 return sURL;
147 // ____________________
150 * The same as getURLWithProtocolFromSystemPath() before but uses string parameter instead
151 * of a File types. It exist to supress converting of neccessary parameters in the
152 * outside code. But of course getURLWithProtocolFromSystemPath(File,File,String) will be
153 * a little bit faster then this method ...
155 * @param sSystemPath
156 * represent the file in system notation
158 * @param sBasePath
159 * define the base path of the aSystemPath value,
160 * which must be replaced with the value of "sServerPath".
162 * @param sServerPath
163 * Will be used to replace sBasePath.
165 * @example
166 * System Path = "d:\test\file.txt"
167 * Base Path = "d:\test"
168 * Server Path = "http://alaska:8000"
169 * => "http://alaska:8000/file.txt"
171 * @return [String]
172 * an url which represent the given system path
173 * and uses the given protocol
175 public static String getURLWithProtocolFromSystemPath( String sSystemPath, String sBasePath, String sServerPath )
177 return getURLWithProtocolFromSystemPath(new File(sSystemPath), new File(sBasePath), sServerPath);
180 // ____________________
183 * This convert an URL (formated as a string) to a struct com.sun.star.util.URL.
184 * It use a special service to do that: the URLTransformer.
185 * Because some API calls need it and it's not allowed to set "Complete"
186 * part of the util struct only. The URL must be parsed.
188 * @param sURL
189 * URL for parsing in string notation
191 * @return [com.sun.star.util.URL]
192 * URL in UNO struct notation
194 public static com.sun.star.util.URL parseURL(XURLTransformer xParser, String sURL)
196 com.sun.star.util.URL aURL = null;
198 if (sURL==null || sURL.equals(""))
199 return null;
203 // Create special service for parsing of given URL.
204 /* com.sun.star.util.XURLTransformer xParser = (com.sun.star.util.XURLTransformer)OfficeConnect.createRemoteInstance(
205 com.sun.star.util.XURLTransformer.class,
206 "com.sun.star.util.URLTransformer");
208 // Because it's an in/out parameter we must use an array of URL objects.
209 com.sun.star.util.URL[] aParseURL = new com.sun.star.util.URL[1];
210 aParseURL[0] = new com.sun.star.util.URL();
211 aParseURL[0].Complete = sURL;
213 // Parse the URL
214 xParser.parseStrict(aParseURL);
216 aURL = aParseURL[0];
218 catch(com.sun.star.uno.RuntimeException exRuntime)
220 // Any UNO method of this scope can throw this exception.
221 // Reset the return value only.
222 aURL = null;
225 return aURL;
228 //_________________________________
230 * Return a name list of all available files of a directory.
231 * We filter pure sub directories names. All other files
232 * are returned as full qualified URL strings. So they can be
233 * used for further purposes. One parameter define the start directory,
234 * another one describe the url protocol, which the return URL names should have.
236 * @param sStartDir
237 * the start directory, which should include all test files
239 * @return [Vector]
240 * a filtered list of java File objects of all available files of the start dir
241 * and all accessible sub directories.
243 public static ArrayList<File> getSystemFilesFromDir(String sStartDir)
245 File aRoot = new File(sStartDir);
247 if (! aRoot.exists())
248 return null;
250 if (! aRoot.isDirectory())
251 return null;
253 File[] lAllFiles = aRoot.listFiles();
254 if (lAllFiles == null )
255 return null;
257 ArrayList<File> lFilteredFiles = new ArrayList<File>(lAllFiles.length);
259 for (int i=0; i<lAllFiles.length; ++i)
261 if (lAllFiles[i].isFile())
262 lFilteredFiles.add(lAllFiles[i]);
263 else
264 if (lAllFiles[i].isDirectory())
266 // recursion!
267 ArrayList<File> lSubFiles = URLHelper.getSystemFilesFromDir(lAllFiles[i].getPath());
268 if (lSubFiles != null)
270 Iterator<File> aSnapshot = lSubFiles.iterator();
271 while (aSnapshot.hasNext())
272 lFilteredFiles.add(aSnapshot.next());
277 return lFilteredFiles;