KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > externaltools > internal > launchConfigurations > ExternalToolsUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 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  * Keith Seitz (keiths@redhat.com) - Bug 27243 (environment variables contribution)
11  *******************************************************************************/

12 package org.eclipse.ui.externaltools.internal.launchConfigurations;
13
14
15 import java.io.File JavaDoc;
16
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.core.variables.IStringVariableManager;
24 import org.eclipse.core.variables.VariablesPlugin;
25 import org.eclipse.debug.core.DebugPlugin;
26 import org.eclipse.debug.core.ILaunchConfiguration;
27 import org.eclipse.debug.ui.RefreshTab;
28 import org.eclipse.osgi.util.NLS;
29 import org.eclipse.ui.externaltools.internal.model.IExternalToolConstants;
30
31 /**
32  * Utilities for external tool launch configurations.
33  * <p>
34  * This class it not intended to be instantiated.
35  * </p>
36  */

37 public class ExternalToolsUtil {
38
39     /**
40      * Throws a core exception with an error status object built from
41      * the given message, lower level exception, and error code.
42      *
43      * @param message the status message
44      * @param exception lower level exception associated with the
45      * error, or <code>null</code> if none
46      * @param code error code
47      */

48     protected static void abort(String JavaDoc message, Throwable JavaDoc exception, int code) throws CoreException {
49         throw new CoreException(new Status(IStatus.ERROR, IExternalToolConstants.PLUGIN_ID, code, message, exception));
50     }
51     
52     /**
53      * Expands and returns the location attribute of the given launch
54      * configuration. The location is
55      * verified to point to an existing file, in the local file system.
56      *
57      * @param configuration launch configuration
58      * @return an absolute path to a file in the local file system
59      * @throws CoreException if unable to retrieve the associated launch
60      * configuration attribute, if unable to resolve any variables, or if the
61      * resolved location does not point to an existing file in the local file
62      * system
63      */

64     public static IPath getLocation(ILaunchConfiguration configuration) throws CoreException {
65         String JavaDoc location = configuration.getAttribute(IExternalToolConstants.ATTR_LOCATION, (String JavaDoc) null);
66         if (location == null) {
67             abort(NLS.bind(ExternalToolsLaunchConfigurationMessages.ExternalToolsUtil_Location_not_specified_by__0__1, new String JavaDoc[] { configuration.getName()}), null, 0);
68         } else {
69             String JavaDoc expandedLocation = getStringVariableManager().performStringSubstitution(location);
70             if (expandedLocation == null || expandedLocation.length() == 0) {
71                 String JavaDoc msg = NLS.bind(ExternalToolsLaunchConfigurationMessages.ExternalToolsUtil_invalidLocation__0_, new Object JavaDoc[] { configuration.getName()});
72                 abort(msg, null, 0);
73             } else {
74                 File JavaDoc file = new File JavaDoc(expandedLocation);
75                 if (file.isFile()) {
76                     return new Path(expandedLocation);
77                 }
78                 
79                 String JavaDoc msg = NLS.bind(ExternalToolsLaunchConfigurationMessages.ExternalToolsUtil_invalidLocation__0_, new Object JavaDoc[] { configuration.getName()});
80                 abort(msg, null, 0);
81             }
82         }
83         // execution will not reach here
84
return null;
85     }
86     
87     /**
88      * Returns a boolean specifying whether or not output should be captured for
89      * the given configuration
90      *
91      * @param configuration the configuration from which the value will be
92      * extracted
93      * @return boolean specifying whether or not output should be captured
94      * @throws CoreException if unable to access the associated attribute
95      */

96     public static boolean getCaptureOutput(ILaunchConfiguration configuration) throws CoreException {
97         return configuration.getAttribute(DebugPlugin.ATTR_CAPTURE_OUTPUT, true);
98     }
99
100     /**
101      * Expands and returns the working directory attribute of the given launch
102      * configuration. Returns <code>null</code> if a working directory is not
103      * specified. If specified, the working is verified to point to an existing
104      * directory in the local file system.
105      *
106      * @param configuration launch configuration
107      * @return an absolute path to a directory in the local file system, or
108      * <code>null</code> if unspecified
109      * @throws CoreException if unable to retrieve the associated launch
110      * configuration attribute, if unable to resolve any variables, or if the
111      * resolved location does not point to an existing directory in the local
112      * file system
113      */

114     public static IPath getWorkingDirectory(ILaunchConfiguration configuration) throws CoreException {
115         String JavaDoc location = configuration.getAttribute(IExternalToolConstants.ATTR_WORKING_DIRECTORY, (String JavaDoc) null);
116         if (location != null) {
117             String JavaDoc expandedLocation = getStringVariableManager().performStringSubstitution(location);
118             if (expandedLocation.length() > 0) {
119                 File JavaDoc path = new File JavaDoc(expandedLocation);
120                 if (path.isDirectory()) {
121                     return new Path(expandedLocation);
122                 }
123                 String JavaDoc msg = NLS.bind(ExternalToolsLaunchConfigurationMessages.ExternalToolsUtil_invalidDirectory__0_, new Object JavaDoc[] { expandedLocation, configuration.getName()});
124                 abort(msg, null, 0);
125             }
126         }
127         return null;
128     }
129
130     /**
131      * Expands and returns the arguments attribute of the given launch
132      * configuration. Returns <code>null</code> if arguments are not specified.
133      *
134      * @param configuration launch configuration
135      * @return an array of resolved arguments, or <code>null</code> if
136      * unspecified
137      * @throws CoreException if unable to retrieve the associated launch
138      * configuration attribute, or if unable to resolve any variables
139      */

140     public static String JavaDoc[] getArguments(ILaunchConfiguration configuration) throws CoreException {
141         String JavaDoc args = configuration.getAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, (String JavaDoc) null);
142         if (args != null) {
143             String JavaDoc expanded = getStringVariableManager().performStringSubstitution(args);
144             return parseStringIntoList(expanded);
145         }
146         return null;
147     }
148
149     private static IStringVariableManager getStringVariableManager() {
150         return VariablesPlugin.getDefault().getStringVariableManager();
151     }
152     
153     /**
154      * Returns whether the given launch configuration is enabled. This property
155      * is intended only to apply to external tool builder configurations and
156      * determines whether the project builder will launch the configuration
157      * when it builds.
158      *
159      * @param configuration the configuration for which the enabled state should
160      * be determined.
161      * @return whether the given configuration is enabled to be run when a build occurs.
162      * @throws CoreException if unable to access the associated attribute
163      */

164     public static boolean isBuilderEnabled(ILaunchConfiguration configuration) throws CoreException {
165         return configuration.getAttribute(IExternalToolConstants.ATTR_BUILDER_ENABLED, true);
166     }
167     
168     /**
169      * Returns the collection of resources for the build scope as specified by the given launch configuration.
170      *
171      * @param configuration launch configuration
172      * @throws CoreException if an exception occurs while retrieving the resources
173      */

174     public static IResource[] getResourcesForBuildScope(ILaunchConfiguration configuration) throws CoreException {
175         String JavaDoc scope = configuration.getAttribute(IExternalToolConstants.ATTR_BUILD_SCOPE, (String JavaDoc) null);
176         if (scope == null) {
177             return null;
178         }
179     
180         return RefreshTab.getRefreshResources(scope);
181     }
182     
183     /**
184      * Parses the argument text into an array of individual
185      * strings using the space character as the delimiter.
186      * An individual argument containing spaces must have a
187      * double quote (") at the start and end. Two double
188      * quotes together is taken to mean an embedded double
189      * quote in the argument text.
190      *
191      * @param arguments the arguments as one string
192      * @return the array of arguments
193      */

194     public static String JavaDoc[] parseStringIntoList(String JavaDoc arguments) {
195         if (arguments == null || arguments.length() == 0) {
196             return new String JavaDoc[0];
197         }
198         String JavaDoc[] res= DebugPlugin.parseArguments(arguments);
199         return res;
200     }
201     
202 }
203
Popular Tags