KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > externaltools > internal > variables > SystemPathResolver


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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  * Wieant (wieant@tasking.com) - Bug 138007
11  *******************************************************************************/

12 package org.eclipse.ui.externaltools.internal.variables;
13
14 import java.io.File JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.StringTokenizer JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.core.variables.IDynamicVariable;
22 import org.eclipse.core.variables.IDynamicVariableResolver;
23 import org.eclipse.debug.core.DebugPlugin;
24 import org.eclipse.ui.externaltools.internal.model.IExternalToolConstants;
25
26 public class SystemPathResolver implements IDynamicVariableResolver {
27     
28     /* (non-Javadoc)
29      * @see org.eclipse.core.variables.IDynamicVariableResolver#resolveValue(org.eclipse.core.variables.IDynamicVariable, java.lang.String)
30      */

31     public String JavaDoc resolveValue(IDynamicVariable variable, String JavaDoc argument) throws CoreException {
32         if (argument == null) {
33             throw new CoreException(new Status(IStatus.ERROR, IExternalToolConstants.PLUGIN_ID, IExternalToolConstants.ERR_INTERNAL_ERROR, VariableMessages.SystemPathResolver_0, null));
34         }
35         Map JavaDoc map= DebugPlugin.getDefault().getLaunchManager().getNativeEnvironment();
36         String JavaDoc path= (String JavaDoc) map.get("PATH"); //$NON-NLS-1$
37
if (path == null) {
38             return argument;
39         }
40         // On MS Windows the PATHEXT environment variable defines which file extensions
41
// mark files that are executable (e.g. .EXE, .COM, .BAT)
42
String JavaDoc pathext = (String JavaDoc) map.get("PATHEXT"); //$NON-NLS-1$
43
StringTokenizer JavaDoc tokenizer= new StringTokenizer JavaDoc(path, File.pathSeparator);
44         while (tokenizer.hasMoreTokens()) {
45             String JavaDoc pathElement= tokenizer.nextToken();
46             File JavaDoc pathElementFile= new File JavaDoc(pathElement);
47             if (pathElementFile.isDirectory()) {
48                 File JavaDoc toolFile= new File JavaDoc(pathElementFile, argument);
49                 if (toolFile.exists()) {
50                     return toolFile.getAbsolutePath();
51                 }
52                 if ( pathext != null ) {
53                     StringTokenizer JavaDoc pathextTokenizer = new StringTokenizer JavaDoc(pathext, File.pathSeparator);
54                     while (pathextTokenizer.hasMoreTokens()) {
55                         String JavaDoc pathextElement = pathextTokenizer.nextToken();
56                         toolFile = new File JavaDoc(pathElementFile, argument + pathextElement);
57                         if (toolFile.exists()) {
58                             return toolFile.getAbsolutePath();
59                         }
60                     }
61                 }
62             }
63         }
64         return argument;
65     }
66 }
67
Popular Tags