Skip to main content
summaryrefslogtreecommitdiffstats
blob: 100c7fd6effc510ffe41e680abba290454ecc5c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*******************************************************************************
 * Copyright (c) 2002, 2015 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ua.tests.util;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;

/*
 * Utility methods for finding resources.
 */
public class ResourceFinder {

	/*
	 * Finds the specified file in the given plugin and returns a URL to it.
	 */
	public static URL findFile(Plugin plugin, String path) {
		return FileLocator.find(plugin.getBundle(),  new Path(path), null);
	}
	
	/*
	 * Finds and returns URLs to all files in the plugin directory under the given
	 * folder with the given suffix. Can also recursively traverse all subfolders.
	 */
	public static URL[] findFiles(Plugin plugin, String folder, String suffix, boolean recursive) {
		String fullLocation = plugin.getBundle().getLocation();
		String location = fullLocation.substring(fullLocation.indexOf('@') + 1);
		IPath path = new Path(location).append(folder);
		File file = path.toFile();

		/*
		 * If it's a relative path, append it to the install location.
		 */
		if (!file.exists()) {
			path = new Path(Platform.getInstallLocation().getURL().toString().substring("file:".length()) + path);
			file = path.toFile();
		}
		
		File[] files = path.toFile().listFiles();
		return findFiles(files, suffix, recursive);
	}
	
	/*
	 * Finds and returns URLs to all files in the given list that have the given suffix, and
	 * recursively traverses subdirectories if requested.
	 */
	private static URL[] findFiles(File[] files, String suffix, boolean recursive) {
		List<URL> list = new ArrayList<URL>();
		if (files != null) {
			for (File file : files) {
				if (file.isDirectory()) {
					if (recursive) {
						list.addAll(Arrays.asList(findFiles(file.listFiles(), suffix, recursive)));
					}
				}
				else {
					try {
						URL url = file.toURI().toURL();
						if (url.toString().endsWith(suffix)) {
							list.add(url);
						}
					}
					catch (MalformedURLException e) {
					}
				}
			}
		}
		
		URL[] array = new URL[list.size()];
		list.toArray(array);
		return array;
	}
	
}

Back to the top