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 ************************************************************************/
30 // __________ Imports __________
33 import java
.net
.MalformedURLException
;
36 import com
.sun
.star
.util
.XURLTransformer
;
40 import java
.util
.Vector
;
41 import java
.util
.Enumeration
;
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 // ____________________
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.
61 * represent the file in system notation
64 * a file url which represent the given system path
66 public static String
getFileURLFromSystemPath( File aSystemPath
)
68 String sFileURL
= null;
71 sFileURL
= aSystemPath
.toURI().toURL().toString();
73 catch( MalformedURLException exWrong
)
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
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();
94 // ____________________
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 ...
103 * represent the file in system notation
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:///".
120 * represent the file in system notation
123 * define the base path of the aSystemPath value,
124 * which must be replaced with the value of "sServerPath".
127 * Will be used to replace sBasePath.
130 * System Path = "d:\test\file.txt"
131 * Base Path = "d:\test"
132 * Server Path = "http://alaska:8000"
133 * => "http://alaska:8000/file.txt"
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
);
145 if (sBaseURL
.lastIndexOf('/')==(sBaseURL
.length()-1))
146 sBaseURL
= sBaseURL
.substring(0,sBaseURL
.length()-1);
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);
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 ...
168 * represent the file in system notation
171 * define the base path of the aSystemPath value,
172 * which must be replaced with the value of "sServerPath".
175 * Will be used to replace sBasePath.
178 * System Path = "d:\test\file.txt"
179 * Base Path = "d:\test"
180 * Server Path = "http://alaska:8000"
181 * => "http://alaska:8000/file.txt"
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.
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(""))
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
;
226 xParser
.parseStrict(aParseURL
);
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.
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.
249 * the start directory, which should include all test files
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())
262 if (! aRoot
.isDirectory())
265 File
[] lAllFiles
= aRoot
.listFiles();
266 if (lAllFiles
== 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
]);
276 if (lAllFiles
[i
].isDirectory())
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
;