KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > runtime > 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.core.internal.runtime;
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.*;
19 import org.osgi.framework.Bundle;
20
21 // This class provides implements the find* methods exposed on Platform.
22
// It does the lookup in bundles and fragments and does the variable replacement.
23
// Can only be used if OSGi is available.
24
public class FindSupport {
25     // OSGI system properties
26
public static final String JavaDoc PROP_NL = "osgi.nl"; //$NON-NLS-1$
27
public static final String JavaDoc PROP_OS = "osgi.os"; //$NON-NLS-1$
28
public static final String JavaDoc PROP_WS = "osgi.ws"; //$NON-NLS-1$
29
public static final String JavaDoc PROP_ARCH = "osgi.arch"; //$NON-NLS-1$
30

31     private static String JavaDoc[] NL_JAR_VARIANTS = buildNLVariants(Activator.getContext() == null ? System.getProperty(PROP_NL) : Activator.getContext().getProperty(PROP_NL));
32
33     private static String JavaDoc[] buildNLVariants(String JavaDoc nl) {
34         ArrayList JavaDoc result = new ArrayList JavaDoc();
35         IPath base = new Path("nl"); //$NON-NLS-1$
36

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

52     public static URL JavaDoc find(Bundle bundle, IPath path) {
53         return find(bundle, path, null);
54     }
55
56     /**
57      * See doc on {@link FileLocator#find(Bundle, IPath, Map)}
58      */

59     public static URL JavaDoc find(Bundle b, IPath path, Map JavaDoc override) {
60         return find(b, path, override, null);
61     }
62
63     /**
64      * See doc on {@link FileLocator#findEntries(Bundle, IPath)}
65      */

66     public static URL JavaDoc[] findEntries(Bundle bundle, IPath path) {
67         return findEntries(bundle, path, null);
68     }
69
70     /**
71      * See doc on {@link FileLocator#findEntries(Bundle, IPath, Map)}
72      */

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

263     public static final InputStream JavaDoc openStream(Bundle bundle, IPath file, boolean substituteArgs) throws IOException JavaDoc {
264         URL JavaDoc url = null;
265         if (!substituteArgs) {
266             url = findInPlugin(bundle, file, null);
267             if (url == null)
268                 url = findInFragments(bundle, file, null);
269         } else {
270             url = FindSupport.find(bundle, file);
271         }
272         if (url != null)
273             return url.openStream();
274         throw new IOException JavaDoc("Cannot find " + file.toString()); //$NON-NLS-1$
275
}
276 }
277
Popular Tags