Bump version to 24.04.3.4
[LibreOffice.git] / qadevOOo / runner / helper / URLHelper.java
blob6a46c3acdd6b04a94710445f45e3bf143f412601
1 /*
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 .
19 package helper;
21 // __________ Imports __________
23 // exceptions
24 import java.io.File;
25 import java.net.MalformedURLException;
26 import java.util.ArrayList;
27 import java.util.Iterator;
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 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
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.
46 * @param aSystemPath
47 * represent the file in system notation
49 * @return [String]
50 * a file url which represent the given system path
52 public static String getFileURLFromSystemPath( File aSystemPath )
54 String sFileURL = null;
55 try
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
65 if(
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();
76 return sFileURL;
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 ...
87 * @param sSystemPath
88 * represent the file in system notation
90 * @return [String]
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.
107 * @param sStartDir
108 * the start directory, which should include all test files
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.
114 public static ArrayList<File> getSystemFilesFromDir(String sStartDir)
116 File aRoot = new File(sStartDir);
118 if (! aRoot.exists())
119 return null;
121 if (! aRoot.isDirectory())
122 return null;
124 File[] lAllFiles = aRoot.listFiles();
125 if (lAllFiles == null )
126 return 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]);
134 else
135 if (lAllFiles[i].isDirectory())
137 // recursion!
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;