KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > impl > model > util > FindSupport


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.intro.impl.model.util;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Map JavaDoc;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.Path;
20 import org.eclipse.core.runtime.Platform;
21 import org.osgi.framework.Bundle;
22
23 // This class provides implements the find* methods exposed on Platform.
24
// It does the lookup in bundles and fragments and does the variable replacement.
25
public class FindSupport {
26     private static String JavaDoc[] NL_JAR_VARIANTS = buildNLVariants(Platform.getNL());
27
28     private static String JavaDoc[] buildNLVariants(String JavaDoc nl) {
29         ArrayList JavaDoc result = new ArrayList JavaDoc();
30         IPath base = new Path("nl"); //$NON-NLS-1$
31

32         IPath path = new Path(nl.replace('_', '/'));
33         while (path.segmentCount() > 0) {
34             result.add(base.append(path).toString());
35             // for backwards compatibility only, don't replace the slashes
36
if (path.segmentCount() > 1)
37                 result.add(base.append(path.toString().replace('/', '_')).toString());
38             path = path.removeLastSegments(1);
39         }
40
41         return (String JavaDoc[]) result.toArray(new String JavaDoc[result.size()]);
42     }
43
44     /**
45      * See doc on @link Platform#find(Bundle, IPath) Platform#find(Bundle, IPath)
46      */

47     public static URL JavaDoc find(Bundle bundle, IPath path) {
48         return find(bundle, path, null);
49     }
50
51     /**
52      * Proposed API for Platform in Eclispe 3.2.
53      * Same as @link #find(Bundle, IPath) except multiple entries can be
54      * returned if more than one entry matches the path in the host and
55      * any of its fragments.
56      *
57      * @param bundle
58      * @param path
59      * @return an array of entries which match the given path. An empty
60      * array is returned if no matches are found.
61      */

62     public static URL JavaDoc[] findEntries(Bundle bundle, IPath path) {
63         return findEntries(bundle, path, null);
64     }
65
66     /**
67      * Proposed API for Platform in Eclispe 3.2.
68      * Same as @link #find(Bundle, IPath) except multiple entries can be
69      * returned if more than one entry matches the path in the host and
70      * any of its fragments.
71      *
72      * @param bundle
73      * @param path
74      * @param override
75      * @returnan array of entries which match the given path. An empty
76      * array is returned if no matches are found.
77      */

78     public static URL JavaDoc[] findEntries(Bundle bundle, IPath path, Map JavaDoc override) {
79         ArrayList JavaDoc results = new ArrayList JavaDoc(1);
80         find(bundle, path, override, results);
81         return (URL JavaDoc[]) results.toArray(new URL JavaDoc[results.size()]);
82     }
83
84     /**
85      * See doc on @link Platform#find(Bundle, IPath, Map) Platform#find(Bundle, IPath, Map)
86      */

87     public static URL JavaDoc find(Bundle b, IPath path, Map JavaDoc override) {
88         return find(b, path, override, null);
89     }
90  
91     private static URL JavaDoc find(Bundle b, IPath path, Map JavaDoc override, ArrayList JavaDoc multiple) {
92         if (path == null)
93             return null;
94
95         URL JavaDoc result = null;
96
97         // Check for the empty or root case first
98
if (path.isEmpty() || path.isRoot()) {
99             // Watch for the root case. It will produce a new
100
// URL which is only the root directory (and not the
101
// root of this plugin).
102
result = findInPlugin(b, Path.EMPTY, multiple);
103             if (result == null || multiple != null)
104                 result = findInFragments(b, Path.EMPTY, multiple);
105             return result;
106         }
107
108         // Now check for paths without variable substitution
109
String JavaDoc first = path.segment(0);
110         if (first.charAt(0) != '$') {
111             result = findInPlugin(b, path, multiple);
112             if (result == null || multiple != null)
113                 result = findInFragments(b, path, multiple);
114             return result;
115         }
116
117         // Worry about variable substitution
118
IPath rest = path.removeFirstSegments(1);
119         if (first.equalsIgnoreCase("$nl$")) //$NON-NLS-1$
120
return findNL(b, rest, override, multiple);
121         if (first.equalsIgnoreCase("$os$")) //$NON-NLS-1$
122
return findOS(b, rest, override, multiple);
123         if (first.equalsIgnoreCase("$ws$")) //$NON-NLS-1$
124
return findWS(b, rest, override, multiple);
125         if (first.equalsIgnoreCase("$files$")) //$NON-NLS-1$
126
return null;
127
128         return null;
129     }
130
131     private static URL JavaDoc findOS(Bundle b, IPath path, Map JavaDoc override, ArrayList JavaDoc multiple) {
132         String JavaDoc os = null;
133         if (override != null)
134             try {
135                 // check for override
136
os = (String JavaDoc) override.get("$os$"); //$NON-NLS-1$
137
} catch (ClassCastException JavaDoc e) {
138                 // just in case
139
}
140         if (os == null)
141             // use default
142
os = Platform.getOS();
143         if (os.length() == 0)
144             return null;
145
146         // Now do the same for osarch
147
String JavaDoc osArch = null;
148         if (override != null)
149             try {
150                 // check for override
151
osArch = (String JavaDoc) override.get("$arch$"); //$NON-NLS-1$
152
} catch (ClassCastException JavaDoc e) {
153                 // just in case
154
}
155         if (osArch == null)
156             // use default
157
osArch = Platform.getOSArch();
158         if (osArch.length() == 0)
159             return null;
160
161         URL JavaDoc result = null;
162         IPath base = new Path("os").append(os).append(osArch); //$NON-NLS-1$
163
// Keep doing this until all you have left is "os" as a path
164
while (base.segmentCount() != 1) {
165             IPath filePath = base.append(path);
166             result = findInPlugin(b, filePath, multiple);
167             if (result != null && multiple == null)
168                 return result;
169             result = findInFragments(b, filePath, multiple);
170             if (result != null && multiple == null)
171                 return result;
172             base = base.removeLastSegments(1);
173         }
174         // If we get to this point, we haven't found it yet.
175
// Look in the plugin and fragment root directories
176
result = findInPlugin(b, path, multiple);
177         if (result != null && multiple == null)
178             return result;
179         return findInFragments(b, path, multiple);
180     }
181
182     private static URL JavaDoc findWS(Bundle b, IPath path, Map JavaDoc override, ArrayList JavaDoc multiple) {
183         String JavaDoc ws = null;
184         if (override != null)
185             try {
186                 // check for override
187
ws = (String JavaDoc) override.get("$ws$"); //$NON-NLS-1$
188
} catch (ClassCastException JavaDoc e) {
189                 // just in case
190
}
191         if (ws == null)
192             // use default
193
ws = Platform.getWS();
194         IPath filePath = new Path("ws").append(ws).append(path); //$NON-NLS-1$
195
// We know that there is only one segment to the ws path
196
// e.g. ws/win32
197
URL JavaDoc result = findInPlugin(b, filePath, multiple);
198         if (result != null && multiple == null)
199             return result;
200         result = findInFragments(b, filePath, multiple);
201         if (result != null && multiple == null)
202             return result;
203         // If we get to this point, we haven't found it yet.
204
// Look in the plugin and fragment root directories
205
result = findInPlugin(b, path, multiple);
206         if (result != null && multiple == null)
207             return result;
208         return findInFragments(b, path, multiple);
209     }
210
211     private static URL JavaDoc findNL(Bundle b, IPath path, Map JavaDoc override, ArrayList JavaDoc multiple) {
212         String JavaDoc nl = null;
213         String JavaDoc[] nlVariants = null;
214         if (override != null)
215             try {
216                 // check for override
217
nl = (String JavaDoc) override.get("$nl$"); //$NON-NLS-1$
218
} catch (ClassCastException JavaDoc e) {
219                 // just in case
220
}
221         nlVariants = nl == null ? NL_JAR_VARIANTS : buildNLVariants(nl);
222         if (nl != null && nl.length() == 0)
223             return null;
224
225         URL JavaDoc result = null;
226         for (int i = 0; i < nlVariants.length; i++) {
227             IPath filePath = new Path(nlVariants[i]).append(path);
228             result = findInPlugin(b, filePath, multiple);
229             if (result != null && multiple == null)
230                 return result;
231             result = findInFragments(b, filePath, multiple);
232             if (result != null && multiple == null)
233                 return result;
234         }
235         // If we get to this point, we haven't found it yet.
236
// Look in the plugin and fragment root directories
237
result = findInPlugin(b, path, multiple);
238         if (result != null && multiple == null)
239             return result;
240         return findInFragments(b, path, multiple);
241     }
242
243     private static URL JavaDoc findInPlugin(Bundle b, IPath filePath, ArrayList JavaDoc multiple) {
244         URL JavaDoc result = b.getEntry(filePath.toString());
245         if (result != null && multiple != null)
246             multiple.add(result);
247         return result;
248     }
249
250     private static URL JavaDoc findInFragments(Bundle b, IPath filePath, ArrayList JavaDoc multiple) {
251         Bundle[] fragments = Platform.getFragments(b);
252         if (fragments == null)
253             return null;
254
255         multiple.ensureCapacity(fragments.length + 1);
256         URL JavaDoc fileURL = null;
257         int i = 0;
258         while (i < fragments.length && fileURL == null) {
259             fileURL = fragments[i].getEntry(filePath.toString());
260             if (fileURL != null && multiple != null) {
261                 multiple.add(fileURL);
262                 fileURL = null;
263             }
264             i++;
265         }
266         return fileURL;
267     }
268
269     /**
270      * See doc on @link Platform#openStream(Bundle, IPath, boolean) Platform#Platform#openStream(Bundle, IPath, boolean)
271      */

272     public static final InputStream JavaDoc openStream(Bundle bundle, IPath file, boolean localized) throws IOException JavaDoc {
273         URL JavaDoc url = null;
274         if (!localized) {
275             url = findInPlugin(bundle, file, null);
276             if (url == null)
277                 url = findInFragments(bundle, file, null);
278         } else {
279             url = FindSupport.find(bundle, file);
280         }
281         if (url != null)
282             return url.openStream();
283         throw new IOException JavaDoc("Cannot find " + file.toString()); //$NON-NLS-1$
284
}
285
286 }
287
Popular Tags