1 /*
2  * This file is part of the LibreOffice project.
3  *
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/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
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 .
17  */
18 
19 package helper;
20 
21 // __________ Imports __________
22 
23 // exceptions
24 import java.io.File;
25 import java.net.MalformedURLException;
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 
29 
30 /**
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 strutural notations (e.g. com.sun.star.util.URL).
34  * And sometimes java had another notation then the office it has.
35  *
36  */
37 public class URLHelper
38 {
39 
40 
41     /**
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.
45      *
46      * @param aSystemPath
47      *          represent the file in system notation
48      *
49      * @return [String]
50      *          a file url which represent the given system path
51      */
getFileURLFromSystemPath( File aSystemPath )52     public static String getFileURLFromSystemPath( File aSystemPath )
53     {
54         String sFileURL = null;
55         try
56         {
57             sFileURL = aSystemPath.toURI().toURL().toString();
58         }
59         catch( MalformedURLException exWrong )
60         {
61         }
62 
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
65         if(
66             (sFileURL                       != null ) &&
67             sFileURL.startsWith("file:/") &&
68             !sFileURL.startsWith("file://")
69           )
70         {
71             StringBuffer sWorkBuffer = new StringBuffer(sFileURL);
72             sWorkBuffer.insert(6,"//");
73             sFileURL = sWorkBuffer.toString();
74         }
75 
76         return sFileURL;
77     }
78 
79 
80 
81     /**
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 ...
86      *
87      * @param sSystemPath
88      *          represent the file in system notation
89      *
90      * @return [String]
91      *          a file url which represent the given system path
92      */
getFileURLFromSystemPath( String sSystemPath )93     public static String getFileURLFromSystemPath( String sSystemPath )
94     {
95         return getFileURLFromSystemPath(new File(sSystemPath));
96     }
97 
98 
99 
100     /**
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.
106      *
107      * @param   sStartDir
108      *              the start directory, which should include all test files
109      *
110      * @return  [Vector]
111      *              a filtered list of java File objects of all available files of the start dir
112      *              and all accessible sub directories.
113      */
getSystemFilesFromDir(String sStartDir)114     public static ArrayList<File> getSystemFilesFromDir(String sStartDir)
115     {
116         File aRoot = new File(sStartDir);
117 
118         if (! aRoot.exists())
119             return null;
120 
121         if (! aRoot.isDirectory())
122             return null;
123 
124         File[] lAllFiles = aRoot.listFiles();
125         if (lAllFiles == null )
126             return null;
127 
128         ArrayList<File> lFilteredFiles = new ArrayList<File>(lAllFiles.length);
129 
130         for (int i=0; i<lAllFiles.length; ++i)
131         {
132             if (lAllFiles[i].isFile())
133                 lFilteredFiles.add(lAllFiles[i]);
134             else
135             if (lAllFiles[i].isDirectory())
136             {
137                 // recursion!
138                 ArrayList<File> lSubFiles = URLHelper.getSystemFilesFromDir(lAllFiles[i].getPath());
139                 if (lSubFiles != null)
140                 {
141                     Iterator<File> aSnapshot = lSubFiles.iterator();
142                     while (aSnapshot.hasNext()) {
143                         lFilteredFiles.add(aSnapshot.next());
144                     }
145                 }
146             }
147         }
148 
149         return lFilteredFiles;
150     }
151 }
152