1 11 package org.eclipse.core.resources.ant; 12 13 import java.io.File ; 14 import org.apache.tools.ant.BuildException; 15 import org.apache.tools.ant.Task; 16 import org.apache.tools.ant.types.Path; 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.resources.ResourcesPlugin; 19 import org.eclipse.core.runtime.IPath; 20 import org.eclipse.core.runtime.Platform; 21 22 32 public class ConvertPath extends Task { 33 34 37 private IPath fileSystemPath = null; 38 39 42 private IPath resourcePath = null; 43 44 47 private String property = null; 48 49 52 private String pathID = null; 53 54 57 public ConvertPath() { 58 super(); 59 } 60 61 66 public void execute() throws BuildException { 67 validateAttributes(); 68 if (fileSystemPath == null) 69 convertResourcePathToFileSystemPath(resourcePath); 71 else 72 convertFileSystemPathToResourcePath(fileSystemPath); 73 } 74 75 protected void convertFileSystemPathToResourcePath(IPath path) { 76 IResource resource; 77 if (Platform.getLocation().equals(path)) { 78 resource = ResourcesPlugin.getWorkspace().getRoot(); 79 } else { 80 resource = ResourcesPlugin.getWorkspace().getRoot().getContainerForLocation(path); 81 if (resource == null) 82 throw new BuildException(Policy.bind("exception.noProjectMatchThePath", fileSystemPath.toOSString())); } 84 if (property != null) 85 getProject().setUserProperty(property, resource.getFullPath().toString()); 86 if (pathID != null) { 87 Path newPath = new Path(getProject(), resource.getFullPath().toString()); 88 getProject().addReference(pathID, newPath); 89 } 90 } 91 92 protected void convertResourcePathToFileSystemPath(IPath path) { 93 IResource resource = null; 94 switch (path.segmentCount()) { 95 case 0 : 96 resource = ResourcesPlugin.getWorkspace().getRoot(); 97 break; 98 case 1 : 99 resource = ResourcesPlugin.getWorkspace().getRoot().getProject(path.lastSegment()); 100 break; 101 default : 102 resource = ResourcesPlugin.getWorkspace().getRoot().getFile(path); 103 } 104 105 if (resource.getLocation() == null) 106 throw new BuildException(Policy.bind("exception.pathNotValid", path.toString())); 109 if (property != null) 110 getProject().setUserProperty(property, resource.getLocation().toOSString()); 111 if (pathID != null) { 112 Path newPath = new Path(getProject(), resource.getLocation().toOSString()); 113 getProject().addReference(pathID, newPath); 114 } 115 } 116 117 122 public void setFileSystemPath(File value) { 123 if (resourcePath != null) 124 throw new BuildException(Policy.bind("exception.cantUseBoth")); fileSystemPath = new org.eclipse.core.runtime.Path(value.toString()); 126 } 127 128 133 public void setResourcePath(String value) { 134 if (fileSystemPath != null) 135 throw new BuildException(Policy.bind("exception.cantUseBoth")); resourcePath = new org.eclipse.core.runtime.Path(value); 137 } 138 139 144 public void setProperty(String value) { 145 property = value; 146 147 } 148 149 154 public void setPathId(String value) { 155 pathID = value; 156 } 157 158 163 protected void validateAttributes() throws BuildException { 164 if (property == null && pathID == null) 165 throw new BuildException(Policy.bind("exception.propertyAndPathIdNotSpecified")); 167 if (resourcePath != null && (!resourcePath.isValidPath(resourcePath.toString()) || resourcePath.isEmpty())) 168 throw new BuildException(Policy.bind("exception.invalidPath", resourcePath.toOSString())); else if (fileSystemPath != null && !fileSystemPath.isValidPath(fileSystemPath.toOSString())) 170 throw new BuildException(Policy.bind("exception.invalidPath", fileSystemPath.toOSString())); 172 if (resourcePath == null && fileSystemPath == null) 173 throw new BuildException(Policy.bind("exception.mustHaveOneAttribute")); } 175 } 176 | Popular Tags |