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
;
31 * It collects some static helper functions to handle URLs.
32 * Sometimes it's necessary to convert URL from/to system paths.
33 * Or from string to structural notations (e.g. com.sun.star.util.URL).
34 * And sometimes java had another notation then the office it has.
37 public class URLHelper
42 * Because the office need URLs for loading/saving documents
43 * we must convert used system paths.
44 * And java use another notation for file URLs ... correct it.
47 * represent the file in system notation
50 * a file url which represent the given system path
52 public static String
getFileURLFromSystemPath( File aSystemPath
)
54 String sFileURL
= null;
57 sFileURL
= aSystemPath
.toURI().toURL().toString();
59 catch( MalformedURLException exWrong
)
63 // problem of java: file URL's are coded with 1 slash instead of 2 or 3 ones!
64 // => correct this problem first, otherwise office can't use these URL's
66 (sFileURL
!= null ) &&
67 sFileURL
.startsWith("file:/") &&
68 !sFileURL
.startsWith("file://")
71 StringBuffer sWorkBuffer
= new StringBuffer(sFileURL
);
72 sWorkBuffer
.insert(6,"//");
73 sFileURL
= sWorkBuffer
.toString();
82 * The same as getFileURLFromSystemPath() before but uses string parameter instead
83 * of a File type. It exists to suppress converting of necessary parameters in the
84 * outside code. But of course getFileURLFromSystemPath(File) will be a little bit faster
85 * then this method ...
88 * represent the file in system notation
91 * a file url which represent the given system path
93 public static String
getFileURLFromSystemPath( String sSystemPath
)
95 return getFileURLFromSystemPath(new File(sSystemPath
));
101 * Return a name list of all available files of a directory.
102 * We filter pure sub directories names. All other files
103 * are returned as full qualified URL strings. So they can be
104 * used for further purposes. One parameter define the start directory,
105 * another one describe the url protocol, which the return URL names should have.
108 * the start directory, which should include all test files
111 * a filtered list of java File objects of all available files of the start dir
112 * and all accessible sub directories.
114 public static ArrayList
<File
> getSystemFilesFromDir(String sStartDir
)
116 File aRoot
= new File(sStartDir
);
118 if (! aRoot
.exists())
121 if (! aRoot
.isDirectory())
124 File
[] lAllFiles
= aRoot
.listFiles();
125 if (lAllFiles
== null )
128 ArrayList
<File
> lFilteredFiles
= new ArrayList
<File
>(lAllFiles
.length
);
130 for (int i
=0; i
<lAllFiles
.length
; ++i
)
132 if (lAllFiles
[i
].isFile())
133 lFilteredFiles
.add(lAllFiles
[i
]);
135 if (lAllFiles
[i
].isDirectory())
138 ArrayList
<File
> lSubFiles
= URLHelper
.getSystemFilesFromDir(lAllFiles
[i
].getPath());
139 if (lSubFiles
!= null)
141 Iterator
<File
> aSnapshot
= lSubFiles
.iterator();
142 while (aSnapshot
.hasNext()) {
143 lFilteredFiles
.add(aSnapshot
.next());
149 return lFilteredFiles
;