Skip to main content
summaryrefslogtreecommitdiffstats
blob: 18e0c6873dd4869bad3ce59147958a5ba8459e07 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*******************************************************************************
 * Copyright (c) 2004 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.jst.jsp.tests.encoding;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;

/**
 * The main plugin class to be used in the desktop.
 */
public class JSPEncodingTestsPlugin extends Plugin {
	// The shared instance.
	private static JSPEncodingTestsPlugin plugin;

	public static List getAllTestFiles(String topDirName) {
		List result = null;
		URL installURL = getInstallLocation();
		// String scheme = installURL.getProtocol();
		String path = installURL.getPath();
		String location = path + topDirName;
		File topDir = new File(location);
		if (!topDir.isDirectory()) {
			throw new IllegalArgumentException(topDirName + " is not a directory");
		}

		result = getFilesInDir(topDir);
		return result;
	}

	/**
	 * Returns the shared instance.
	 */
	public static JSPEncodingTestsPlugin getDefault() {
		return plugin;
	}

	private static List getFilesInDir(File topDir) {
		List files = new ArrayList();
		File[] topFiles = topDir.listFiles();
		for (int i = 0; i < topFiles.length; i++) {
			File file = topFiles[i];
			if (file.isFile()) {
				files.add(file);
			}
			else if (file.isDirectory() && !file.getName().endsWith("CVS")) {
				List innerFiles = getFilesInDir(file);
				files.addAll(innerFiles);
			}
		}
		return files;
	}

	public static URL getInstallLocation() {
		URL installLocation = Platform.getBundle("org.eclipse.jst.jsp.tests.encoding").getEntry("/");
		URL resolvedLocation = null;
		try {
			resolvedLocation = FileLocator.resolve(installLocation);
		}
		catch (IOException e) {
			// impossible
			throw new Error(e);
		}
		return resolvedLocation;
	}

	/**
	 * Returns the string from the plugin's resource bundle, or 'key' if not
	 * found.
	 */
	public static String getResourceString(String key) {
		ResourceBundle bundle = JSPEncodingTestsPlugin.getDefault().getResourceBundle();
		try {
			return (bundle != null ? bundle.getString(key) : key);
		}
		catch (MissingResourceException e) {
			return key;
		}
	}

	public static File getTestFile(String filepath) {
		URL installURL = getInstallLocation();
		// String scheme = installURL.getProtocol();
		String path = installURL.getPath();
		String location = path + filepath;
		File result = new File(location);
		return result;
	}

	public static Reader getTestReader(String filepath) throws FileNotFoundException {
		URL installURL = getInstallLocation();
		// String scheme = installURL.getProtocol();
		String path = installURL.getPath();
		String location = path + filepath;
		Reader result = new FileReader(location);
		return result;
	}

	/**
	 * Returns the workspace instance.
	 */
	public static IWorkspace getWorkspace() {
		return ResourcesPlugin.getWorkspace();
	}

	/**
	 * The constructor.
	 */
	public JSPEncodingTestsPlugin() {
		super();
		plugin = this;
	}

	/**
	 * Returns the plugin's resource bundle,
	 */
	public ResourceBundle getResourceBundle() {
		return null;
	}

}

Back to the top