1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 import java.io.File;
25 import java.net.MalformedURLException;
26 
27 public class URLHelper
28 {
29     /**
30      * Because the office need URLs for loading/saving documents
31      * we must convert used system pathes.
32      * And java use another notation for file URLs ... correct it.
33      *
34      * @param aSystemPath
35      *          represent the file in system notation
36      *
37      * @return [String]
38      *          a file url which represent the given system path
39      */
40     @SuppressWarnings("deprecation")
41     public static String getFileURLFromSystemPath( File aSystemPath )
42     {
43         String sFileURL = null;
44         try
45         {
46             sFileURL = aSystemPath.toURL().toString();
47         }
48         catch( MalformedURLException exWrong )
49         {
50             sFileURL = null;
51         }
52 
53         // problem of java: file URL's are coded with 1 slash instead of 2 or 3 ones!
54         // => correct this problem first, otherwise office can't use these URL's
55         if(
56             (sFileURL                       != null ) &&
57             (sFileURL.startsWith("file:/")  == true ) &&
58             (sFileURL.startsWith("file://") == false)
59           )
60         {
61             StringBuffer sWorkBuffer = new StringBuffer(sFileURL);
62             sWorkBuffer.insert(6,"//");
63             sFileURL = sWorkBuffer.toString();
64         }
65 
66         return sFileURL;
67     }
68 }
69