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 .
21 // __________ Imports __________
25 import java
.net
.MalformedURLException
;
26 import java
.util
.ArrayList
;
27 import java
.util
.Iterator
;
29 import com
.sun
.star
.util
.XURLTransformer
;
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 // ____________________
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.
49 * represent the file in system notation
52 * a file url which represent the given system path
54 public static String
getFileURLFromSystemPath( File aSystemPath
)
56 String sFileURL
= null;
59 sFileURL
= aSystemPath
.toURI().toURL().toString();
61 catch( MalformedURLException exWrong
)
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
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();
82 // ____________________
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 ...
91 * represent the file in system notation
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:///".
108 * represent the file in system notation
111 * define the base path of the aSystemPath value,
112 * which must be replaced with the value of "sServerPath".
115 * Will be used to replace sBasePath.
118 * System Path = "d:\test\file.txt"
119 * Base Path = "d:\test"
120 * Server Path = "http://alaska:8000"
121 * => "http://alaska:8000/file.txt"
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
);
133 if (sBaseURL
.lastIndexOf('/')==(sBaseURL
.length()-1))
134 sBaseURL
= sBaseURL
.substring(0,sBaseURL
.length()-1);
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);
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 ...
156 * represent the file in system notation
159 * define the base path of the aSystemPath value,
160 * which must be replaced with the value of "sServerPath".
163 * Will be used to replace sBasePath.
166 * System Path = "d:\test\file.txt"
167 * Base Path = "d:\test"
168 * Server Path = "http://alaska:8000"
169 * => "http://alaska:8000/file.txt"
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.
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(""))
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
;
214 xParser
.parseStrict(aParseURL
);
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.
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.
237 * the start directory, which should include all test files
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())
250 if (! aRoot
.isDirectory())
253 File
[] lAllFiles
= aRoot
.listFiles();
254 if (lAllFiles
== 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
]);
264 if (lAllFiles
[i
].isDirectory())
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
;