xref: /AOO41X/main/qadevOOo/runner/helper/URLHelper.java (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 package helper;
29 
30 // __________ Imports __________
31 
32 // exceptions
33 import java.net.MalformedURLException;
34 
35 // interfaces
36 import com.sun.star.util.XURLTransformer;
37 
38 // others
39 import java.io.File;
40 import java.util.Vector;
41 import java.util.Enumeration;
42 
43 
44 /**
45  * It collects some static helper functons to handle URLs.
46  * Sometimes it's neccessary to convert URL from/to system pathes.
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.
49  *
50  */
51 public class URLHelper
52 {
53     // ____________________
54 
55     /**
56      * Because the office need URLs for loading/saving documents
57      * we must convert used system pathes.
58      * And java use another notation for file URLs ... correct it.
59      *
60      * @param aSystemPath
61      *          represent the file in system notation
62      *
63      * @return [String]
64      *          a file url which represent the given system path
65      */
66     public static String getFileURLFromSystemPath( File aSystemPath )
67     {
68         String sFileURL = null;
69         try
70         {
71             //sFileURL = aSystemPath.toURI().toURL().toString();
72             sFileURL = aSystemPath.toURL().toString();
73         }
74         catch( MalformedURLException exWrong )
75         {
76             sFileURL = null;
77         }
78 
79         // problem of java: file URL's are coded with 1 slash instead of 2 or 3 ones!
80         // => correct this problem first, otherwise office can't use these URL's
81         if(
82             (sFileURL                       != null ) &&
83             (sFileURL.startsWith("file:/")  == true ) &&
84             (sFileURL.startsWith("file://") == false)
85           )
86         {
87             StringBuffer sWorkBuffer = new StringBuffer(sFileURL);
88             sWorkBuffer.insert(6,"//");
89             sFileURL = sWorkBuffer.toString();
90         }
91 
92         return sFileURL;
93     }
94 
95     // ____________________
96 
97     /**
98      * The same as getFileURLFromSystemPath() before but uses string parameter instead
99      * of a File type. It exist to supress converting of neccessary parameters in the
100      * outside code. But of course getFileURLFromSystemPath(File) will be a little bit faster
101      * then this method ...
102      *
103      * @param sSystemPath
104      *          represent the file in system notation
105      *
106      * @return [String]
107      *          a file url which represent the given system path
108      */
109     public static String getFileURLFromSystemPath( String sSystemPath )
110     {
111         return getFileURLFromSystemPath(new File(sSystemPath));
112     }
113 
114     // ____________________
115 
116     /**
117      * Does the same as getFileURLFromSystemPath() before ... but uses
118      * the given protocol string (e.g."http://") insted of "file:///".
119      *
120      * @param aSystemPath
121      *          represent the file in system notation
122      *
123      * @param aBasePath
124      *          define the base path of the aSystemPath value,
125      *          which must be replaced with the value of "sServerPath".
126      *
127      * @param sServerURL
128      *          Will be used to replace sBasePath.
129      *
130      * @example
131      *          System Path = "d:\test\file.txt"
132      *          Base Path   = "d:\test"
133      *          Server Path = "http://alaska:8000"
134      *          => "http://alaska:8000/file.txt"
135      *
136      * @return [String]
137      *          an url which represent the given system path
138      *          and uses the given protocol
139      */
140     public static String getURLWithProtocolFromSystemPath( File aSystemPath, File aBasePath, String sServerURL )
141     {
142         String sFileURL = URLHelper.getFileURLFromSystemPath(aSystemPath);
143         String sBaseURL = URLHelper.getFileURLFromSystemPath(aBasePath  );
144 
145         // cut last '/'!
146         if (sBaseURL.lastIndexOf('/')==(sBaseURL.length()-1))
147             sBaseURL = sBaseURL.substring(0,sBaseURL.length()-1);
148 
149         // cut last '/'!
150         if (sServerURL.lastIndexOf('/')==(sServerURL.length()-1))
151             sServerURL = sServerURL.substring(0,sServerURL.length()-1);
152 
153         int index = sFileURL.indexOf(sBaseURL);
154         String sURL = sFileURL.substring(0,index) + sServerURL +
155                                 sFileURL.substring(index+sBaseURL.length());
156         //String sURL = sFileURL.replaceFirst(sBaseURL,sServerURL);
157         return sURL;
158     }
159 
160     // ____________________
161 
162     /**
163      * The same as getURLWithProtocolFromSystemPath() before but uses string parameter instead
164      * of a File types. It exist to supress converting of neccessary parameters in the
165      * outside code. But of course getURLWithProtocolFromSystemPath(File,File,String) will be
166      * a little bit faster then this method ...
167      *
168      * @param sSystemPath
169      *          represent the file in system notation
170      *
171      * @param sBasePath
172      *          define the base path of the aSystemPath value,
173      *          which must be replaced with the value of "sServerPath".
174      *
175      * @param sServerPath
176      *          Will be used to replace sBasePath.
177      *
178      * @example
179      *          System Path = "d:\test\file.txt"
180      *          Base Path   = "d:\test"
181      *          Server Path = "http://alaska:8000"
182      *          => "http://alaska:8000/file.txt"
183      *
184      * @return [String]
185      *          an url which represent the given system path
186      *          and uses the given protocol
187      */
188     public static String getURLWithProtocolFromSystemPath( String sSystemPath, String sBasePath, String sServerPath )
189     {
190         return getURLWithProtocolFromSystemPath(new File(sSystemPath), new File(sBasePath), sServerPath);
191     }
192 
193     // ____________________
194 
195     /**
196      * This convert an URL (formated as a string) to a struct com.sun.star.util.URL.
197      * It use a special service to do that: the URLTransformer.
198      * Because some API calls need it and it's not allowed to set "Complete"
199      * part of the util struct only. The URL must be parsed.
200      *
201      * @param sURL
202      *          URL for parsing in string notation
203      *
204      * @return [com.sun.star.util.URL]
205      *              URL in UNO struct notation
206      */
207     public static com.sun.star.util.URL parseURL(XURLTransformer xParser, String sURL)
208     {
209         com.sun.star.util.URL aURL = null;
210 
211         if (sURL==null || sURL.equals(""))
212             return null;
213 
214         try
215         {
216             // Create special service for parsing of given URL.
217 /*            com.sun.star.util.XURLTransformer xParser = (com.sun.star.util.XURLTransformer)OfficeConnect.createRemoteInstance(
218                                                             com.sun.star.util.XURLTransformer.class,
219                                                             "com.sun.star.util.URLTransformer");
220 */
221             // Because it's an in/out parameter we must use an array of URL objects.
222             com.sun.star.util.URL[] aParseURL = new com.sun.star.util.URL[1];
223             aParseURL[0]          = new com.sun.star.util.URL();
224             aParseURL[0].Complete = sURL;
225 
226             // Parse the URL
227             xParser.parseStrict(aParseURL);
228 
229             aURL = aParseURL[0];
230         }
231         catch(com.sun.star.uno.RuntimeException exRuntime)
232         {
233             // Any UNO method of this scope can throw this exception.
234             // Reset the return value only.
235             aURL = null;
236         }
237 
238         return aURL;
239     }
240 
241     //_________________________________
242     /**
243      * Return a name list of all available files of a directory.
244      * We filter pure sub directories names. All other files
245      * are returned as full qualified URL strings. So they can be
246      * used for further purposes. One parameter define the start directory,
247      * another one describe the url protocol, which the return URL names should have.
248      *
249      * @param   sDir
250      *              the start directory, which should include all test files
251      *
252      * @return  [Vector]
253      *              a filtered list of java File objects of all available files of the start dir
254      *              and all accessable sub directories.
255      */
256     public static Vector getSystemFilesFromDir(String sStartDir)
257     {
258         File aRoot = new File(sStartDir);
259 
260         if (! aRoot.exists())
261             return null;
262 
263         if (! aRoot.isDirectory())
264             return null;
265 
266         File[] lAllFiles = aRoot.listFiles();
267         if (lAllFiles == null )
268             return null;
269 
270         Vector lFilteredFiles = new Vector(lAllFiles.length);
271 
272         for (int i=0; i<lAllFiles.length; ++i)
273         {
274             if (lAllFiles[i].isFile())
275                 lFilteredFiles.add(lAllFiles[i]);
276             else
277             if (lAllFiles[i].isDirectory())
278             {
279                 // recursion!
280                 Vector lSubFiles = URLHelper.getSystemFilesFromDir(lAllFiles[i].getPath());
281                 if (lSubFiles != null)
282                 {
283                     Enumeration aSnapshot = lSubFiles.elements();
284                     while (aSnapshot.hasMoreElements())
285                         lFilteredFiles.add(aSnapshot.nextElement());
286                 }
287             }
288         }
289 
290         return lFilteredFiles;
291     }
292 }
293