1 11 package org.eclipse.ant.internal.ui.model; 12 13 14 import java.io.File ; 15 import java.io.IOException ; 16 import java.net.MalformedURLException ; 17 import java.net.URL ; 18 import java.text.MessageFormat ; 19 import java.util.ArrayList ; 20 import java.util.Arrays ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.StringTokenizer ; 24 25 import org.apache.tools.ant.BuildException; 26 import org.apache.tools.ant.util.FileUtils; 27 import org.eclipse.ant.core.AntCorePlugin; 28 import org.eclipse.ant.core.AntCorePreferences; 29 import org.eclipse.ant.core.AntRunner; 30 import org.eclipse.ant.core.TargetInfo; 31 import org.eclipse.ant.internal.core.AntClasspathEntry; 32 import org.eclipse.ant.internal.ui.launchConfigurations.AntHomeClasspathEntry; 33 import org.eclipse.ant.internal.ui.launchConfigurations.IAntLaunchConfigurationConstants; 34 import org.eclipse.ant.internal.ui.views.AntView; 35 import org.eclipse.core.resources.IFile; 36 import org.eclipse.core.resources.IWorkspaceRoot; 37 import org.eclipse.core.resources.ResourcesPlugin; 38 import org.eclipse.core.runtime.CoreException; 39 import org.eclipse.core.runtime.IPath; 40 import org.eclipse.core.runtime.IStatus; 41 import org.eclipse.core.runtime.Path; 42 import org.eclipse.core.runtime.Status; 43 import org.eclipse.core.variables.VariablesPlugin; 44 import org.eclipse.debug.core.ILaunchConfiguration; 45 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 46 import org.eclipse.debug.ui.console.FileLink; 47 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 48 import org.eclipse.jdt.launching.IRuntimeClasspathEntry; 49 import org.eclipse.jdt.launching.IRuntimeClasspathEntry2; 50 import org.eclipse.jdt.launching.JavaRuntime; 51 import org.eclipse.ui.IWorkbenchPage; 52 import org.eclipse.ui.IWorkbenchWindow; 53 import org.eclipse.ui.PlatformUI; 54 55 58 public final class AntUtil { 59 public static final String ATTRIBUTE_SEPARATOR = ","; public static final char ANT_CLASSPATH_DELIMITER= '*'; 61 public static final String ANT_HOME_CLASSPATH_PLACEHOLDER= "G"; public static final String ANT_GLOBAL_USER_CLASSPATH_PLACEHOLDER= "UG"; 66 private AntUtil() { 67 super(); 68 } 69 70 77 public static String combineStrings(String [] strings) { 78 if (strings.length == 0) 79 return null; 80 81 if (strings.length == 1) 82 return strings[0]; 83 84 StringBuffer buf = new StringBuffer (); 85 for (int i = 0; i < strings.length - 1; i++) { 86 buf.append(strings[i]); 87 buf.append(ATTRIBUTE_SEPARATOR); 88 } 89 buf.append(strings[strings.length - 1]); 90 return buf.toString(); 91 } 92 93 101 public static String [] getTargetsFromConfig(ILaunchConfiguration configuration) throws CoreException { 102 String attribute = configuration.getAttribute(IAntLaunchConfigurationConstants.ATTR_ANT_TARGETS, (String ) null); 103 if (attribute == null) { 104 return null; 105 } 106 return AntUtil.parseRunTargets(attribute); 107 } 108 109 118 public static Map getProperties(ILaunchConfiguration configuration) throws CoreException { 119 Map map = configuration.getAttribute(IAntLaunchConfigurationConstants.ATTR_ANT_PROPERTIES, (Map ) null); 120 return map; 121 } 122 123 131 public static String getAntHome(ILaunchConfiguration configuration) throws CoreException { 132 IRuntimeClasspathEntry[] entries = JavaRuntime.computeUnresolvedRuntimeClasspath(configuration); 133 for (int i = 0; i < entries.length; i++) { 134 IRuntimeClasspathEntry entry = entries[i]; 135 if (entry.getType() == IRuntimeClasspathEntry.OTHER) { 136 IRuntimeClasspathEntry2 entry2 = (IRuntimeClasspathEntry2)entry; 137 if (entry2.getTypeId().equals(AntHomeClasspathEntry.TYPE_ID)) { 138 return ((AntHomeClasspathEntry)entry2).getAntHome(); 139 } 140 } 141 } 142 return null; 143 } 144 145 154 public static String [] getPropertyFiles(ILaunchConfiguration configuration) throws CoreException { 155 String attribute = configuration.getAttribute(IAntLaunchConfigurationConstants.ATTR_ANT_PROPERTY_FILES, (String ) null); 156 if (attribute == null) { 157 return null; 158 } 159 String [] propertyFiles= AntUtil.parseString(attribute, ","); for (int i = 0; i < propertyFiles.length; i++) { 161 String propertyFile = propertyFiles[i]; 162 propertyFile= expandVariableString(propertyFile, AntUIModelMessages.getString("AntUtil.6")); propertyFiles[i]= propertyFile; 164 } 165 return propertyFiles; 166 } 167 168 177 public static TargetInfo[] getTargets(String path) throws CoreException { 178 AntRunner runner = new AntRunner(); 179 runner.setBuildFileLocation(path); 180 return runner.getAvailableTargets(); 181 } 182 183 194 public static TargetInfo[] getTargets(String path, String [] arguments, ILaunchConfiguration config) throws CoreException { 195 Map properties=getProperties(config); 196 String [] propertyFiles= getPropertyFiles(config); 197 AntRunner runner = new AntRunner(); 198 runner.setBuildFileLocation(path); 199 if (properties != null){ 200 runner.addUserProperties(properties); 201 } 202 if (propertyFiles != null && propertyFiles.length > 0) { 203 runner.setPropertyFiles(propertyFiles); 204 } 205 if (arguments != null && arguments.length > 0) { 206 runner.setArguments(arguments); 207 } 208 runner.setCustomClasspath(getCustomClasspath(config)); 209 210 String antHome= getAntHome(config); 211 if (antHome != null) { 212 runner.setAntHome(antHome); 213 } 214 return runner.getAvailableTargets(); 215 } 216 217 226 public static URL [] getCustomClasspath(ILaunchConfiguration config) throws CoreException { 227 boolean useDefault = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_DEFAULT_CLASSPATH, true); 228 if (useDefault) { 229 return null; 230 } 231 IRuntimeClasspathEntry[] unresolved = JavaRuntime.computeUnresolvedRuntimeClasspath(config); 232 List userEntries = new ArrayList (unresolved.length); 234 for (int i = 0; i < unresolved.length; i++) { 235 IRuntimeClasspathEntry entry = unresolved[i]; 236 if (entry.getClasspathProperty() == IRuntimeClasspathEntry.USER_CLASSES) { 237 userEntries.add(entry); 238 } 239 } 240 IRuntimeClasspathEntry[] entries = JavaRuntime.resolveRuntimeClasspath((IRuntimeClasspathEntry[])userEntries.toArray(new IRuntimeClasspathEntry[userEntries.size()]), config); 241 URL [] urls = new URL [entries.length]; 242 for (int i = 0; i < entries.length; i++) { 243 IRuntimeClasspathEntry entry = entries[i]; 244 try { 245 urls[i] = new URL ("file:"+entry.getLocation()); } catch (MalformedURLException e) { 247 throw new CoreException(new Status(IStatus.ERROR, AntUIPlugin.getUniqueIdentifier(), AntUIPlugin.INTERNAL_ERROR, AntUIModelMessages.getString("AntUtil.7"), e)); } 249 } 250 return urls; 251 } 252 253 263 public static void getCustomClasspaths(ILaunchConfiguration config, List antHomeEntries, List additionalEntries) { 264 String classpathString= null; 265 try { 266 classpathString = config.getAttribute(IAntLaunchConfigurationConstants.ATTR_ANT_CUSTOM_CLASSPATH, (String ) null); 267 } catch (CoreException e) { 268 } 269 if (classpathString == null) { 270 return; 271 } 272 String antString= null; 273 String userString= null; 274 int delim= classpathString.indexOf(ANT_CLASSPATH_DELIMITER); 275 276 if (delim == -1) { 277 antString= classpathString; 278 } else { 279 antString= classpathString.substring(0, delim); 280 userString= classpathString.substring(delim+1); 281 } 282 283 getEntries(antHomeEntries, antString); 284 285 if (userString != null) { 286 getEntries(additionalEntries, userString); 287 } 288 } 289 290 private static void getEntries(List entries, String urlString) { 291 String [] entryStrings= AntUtil.parseString(urlString, AntUtil.ATTRIBUTE_SEPARATOR); 292 AntCorePreferences prefs= AntCorePlugin.getPlugin().getPreferences(); 293 for (int i = 0; i < entryStrings.length; i++) { 294 String string = entryStrings[i]; 295 if (string.equals(ANT_HOME_CLASSPATH_PLACEHOLDER)) { 296 entries.addAll(Arrays.asList(prefs.getAntHomeClasspathEntries())); 297 } else if (string.equals(ANT_GLOBAL_USER_CLASSPATH_PLACEHOLDER)) { 298 entries.addAll(Arrays.asList(prefs.getAdditionalClasspathEntries())); 299 } else { 300 if (string.charAt(0) == '?') { 301 string= string.substring(1); 302 } 303 entries.add(new AntClasspathEntry(string)); 304 } 305 } 306 } 307 308 private static String expandVariableString(String variableString, String invalidMessage) throws CoreException { 309 String expandedString = VariablesPlugin.getDefault().getStringVariableManager().performStringSubstitution(variableString); 310 if (expandedString == null || expandedString.length() == 0) { 311 String msg = MessageFormat.format(invalidMessage, new String [] {variableString}); 312 throw new CoreException(new Status(IStatus.ERROR, IAntUIConstants.PLUGIN_ID, 0, msg, null)); 313 } 314 315 return expandedString; 316 } 317 318 324 public static AntView getAntView() { 325 IWorkbenchWindow window= PlatformUI.getWorkbench().getActiveWorkbenchWindow(); 326 if (window != null) { 327 IWorkbenchPage page= window.getActivePage(); 328 if (page != null) { 329 return (AntView) page.findView(IAntUIConstants.ANT_VIEW_ID); 330 } 331 } 332 return null; 333 } 334 335 342 public static String [] parseRunTargets(String extraAttibuteValue) { 343 return parseString(extraAttibuteValue, ATTRIBUTE_SEPARATOR); 344 } 345 346 352 public static String [] parseString(String delimString, String delim) { 353 if (delimString == null) { 354 return new String [0]; 355 } 356 357 StringTokenizer tokenizer = new StringTokenizer (delimString, delim); 360 String [] results = new String [tokenizer.countTokens()]; 361 for (int i = 0; i < results.length; i++) { 362 results[i] = tokenizer.nextToken(); 363 } 364 365 return results; 366 } 367 368 372 public static IFile getFile(String fullPath) { 373 IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot(); 374 return root.getFile(new Path(fullPath)); 375 } 376 377 public static FileLink getTaskLink(String path, File buildFileParent) { 378 path = path.trim(); 379 if (path.length() == 0) { 380 return null; 381 } 382 if (path.startsWith("file:")) { path= path.substring(5, path.length()); 385 } 386 int index = path.lastIndexOf(':'); 388 if (index == path.length() - 1) { 389 path = path.substring(0, index); 391 index = path.lastIndexOf(':'); 392 } 393 String fileName = path.substring(0, index); 395 IFile file = getFileForLocation(fileName, buildFileParent); 396 if (file != null) { 397 try { 398 String lineNumber = path.substring(index + 1); 399 int line = Integer.parseInt(lineNumber); 400 return new FileLink(file, null, -1, -1, line); 401 } catch (NumberFormatException e) { 402 } 403 } 404 return null; 405 } 406 407 421 public static IFile getFileForLocation(String path, File buildFileParent) { 422 IPath filePath= new Path(path); 423 IFile file = null; 424 IFile[] files= ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(filePath); 425 if (files.length > 0) { 426 file= files[0]; 427 } 428 if (file == null) { 429 File relativeFile= null; 431 try { 432 relativeFile= FileUtils.newFileUtils().resolveFile(buildFileParent, path); 434 filePath= new Path(relativeFile.getAbsolutePath()); 435 files= ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(filePath); 436 if (files.length > 0) { 437 file= files[0]; 438 } else { 439 return null; 440 } 441 } catch (BuildException be) { 442 return null; 443 } 444 } 445 446 if (file.exists()) { 447 return file; 448 } 449 File ioFile= file.getLocation().toFile(); 450 if (ioFile.exists()) { try { 452 files= ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(new Path(ioFile.getCanonicalPath())); 453 if (files.length > 0) { 454 return files[0]; 455 } 456 } catch (IOException e) { 457 } 458 } 459 460 return null; 461 } 462 463 472 public static void migrateToNewClasspathFormat(ILaunchConfiguration configuration) throws CoreException { 473 String oldClasspath = configuration.getAttribute(IAntLaunchConfigurationConstants.ATTR_ANT_CUSTOM_CLASSPATH, (String )null); 474 String oldAntHome = configuration.getAttribute(IAntLaunchConfigurationConstants.ATTR_ANT_HOME, (String )null); 475 String provider = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_CLASSPATH_PROVIDER, (String )null); 476 if (oldClasspath != null || oldAntHome != null || provider == null) { 477 ILaunchConfigurationWorkingCopy workingCopy = null; 478 if (configuration.isWorkingCopy()) { 479 workingCopy = (ILaunchConfigurationWorkingCopy) configuration; 480 } else { 481 workingCopy = configuration.getWorkingCopy(); 482 } 483 workingCopy.setAttribute(IAntLaunchConfigurationConstants.ATTR_ANT_CUSTOM_CLASSPATH, (String )null); 484 workingCopy.setAttribute(IAntLaunchConfigurationConstants.ATTR_ANT_HOME, (String )null); 485 workingCopy.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CLASSPATH_PROVIDER, "org.eclipse.ant.ui.AntClasspathProvider"); workingCopy.setAttribute(IJavaLaunchConfigurationConstants.ATTR_DEFAULT_CLASSPATH, true); 487 if (oldAntHome != null) { 488 IRuntimeClasspathEntry[] entries = JavaRuntime.computeUnresolvedRuntimeClasspath(workingCopy); 489 List mementos = new ArrayList (entries.length); 490 for (int i = 0; i < entries.length; i++) { 491 IRuntimeClasspathEntry entry = entries[i]; 492 if (entry.getType() == IRuntimeClasspathEntry.OTHER) { 493 IRuntimeClasspathEntry2 entry2 = (IRuntimeClasspathEntry2) entry; 494 if (entry2.getTypeId().equals(AntHomeClasspathEntry.TYPE_ID)) { 495 AntHomeClasspathEntry homeEntry = new AntHomeClasspathEntry(oldAntHome); 496 mementos.add(homeEntry.getMemento()); 497 continue; 498 } 499 } 500 mementos.add(entry.getMemento()); 501 } 502 workingCopy.setAttribute(IJavaLaunchConfigurationConstants.ATTR_DEFAULT_CLASSPATH, false); 503 workingCopy.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CLASSPATH, mementos); 504 } 505 workingCopy.doSave(); 506 } 507 } 508 } | Popular Tags |