KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > resources > ant > ConvertPath


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.resources.ant;
12
13 import java.io.File JavaDoc;
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 /**
23  * An Ant task which allows to switch from a file system path to a resource path,
24  * and vice versa, and store the result in a user property whose name is set by the user. If the
25  * resource does not exist, the property is set to <code>false</code>.
26  * <p>
27  * The attribute "property" must be specified, as well as only one of "fileSystemPath" or "resourcePath".
28  * <p><p>
29  * Example:<p>
30  * &lt;eclipse.convertPath fileSystemPath="D:\MyWork\MyProject" property="myProject.resourcePath"/&gt;
31  */

32 public class ConvertPath extends Task {
33
34     /**
35      * The file system path.
36      */

37     private IPath fileSystemPath = null;
38
39     /**
40      * The resource path.
41      */

42     private IPath resourcePath = null;
43
44     /**
45      * The name of the property where the result may be stored.
46      */

47     private String JavaDoc property = null;
48
49     /**
50      * The id of the new Path object that may be created.
51      */

52     private String JavaDoc pathID = null;
53
54     /**
55      * Constructs a new <code>ConvertPath</code> instance.
56      */

57     public ConvertPath() {
58         super();
59     }
60
61     /**
62      * Performs the path conversion operation.
63      *
64      * @exception BuildException thrown if a problem occurs during execution.
65      */

66     public void execute() throws BuildException {
67         validateAttributes();
68         if (fileSystemPath == null)
69             // here, resourcePath is not null
70
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())); //$NON-NLS-1$
83
}
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             // can occur if the first segment is not a project
107
throw new BuildException(Policy.bind("exception.pathNotValid", path.toString())); //$NON-NLS-1$
108

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     /**
118      * Sets the file system path.
119      *
120      * @param value the file corresponding to the path supplied by the user
121      */

122     public void setFileSystemPath(File JavaDoc value) {
123         if (resourcePath != null)
124             throw new BuildException(Policy.bind("exception.cantUseBoth")); //$NON-NLS-1$
125
fileSystemPath = new org.eclipse.core.runtime.Path(value.toString());
126     }
127
128     /**
129      * Sets the resource path.
130      *
131      * @param value the path
132      */

133     public void setResourcePath(String JavaDoc value) {
134         if (fileSystemPath != null)
135             throw new BuildException(Policy.bind("exception.cantUseBoth")); //$NON-NLS-1$
136
resourcePath = new org.eclipse.core.runtime.Path(value);
137     }
138
139     /**
140      * Sets the name of the property where the result may stored.
141      *
142      * @param value the name of the property
143      */

144     public void setProperty(String JavaDoc value) {
145         property = value;
146
147     }
148
149     /**
150      * Sets the id for the path where the result may be stored
151      *
152      * @param value the id of the path
153      */

154     public void setPathId(String JavaDoc value) {
155         pathID = value;
156     }
157
158     /**
159      * Performs a validation of the receiver.
160      *
161      * @exception BuildException thrown if a problem occurs during validation.
162      */

163     protected void validateAttributes() throws BuildException {
164         if (property == null && pathID == null)
165             throw new BuildException(Policy.bind("exception.propertyAndPathIdNotSpecified")); //$NON-NLS-1$
166

167         if (resourcePath != null && (!resourcePath.isValidPath(resourcePath.toString()) || resourcePath.isEmpty()))
168             throw new BuildException(Policy.bind("exception.invalidPath", resourcePath.toOSString())); //$NON-NLS-1$
169
else if (fileSystemPath != null && !fileSystemPath.isValidPath(fileSystemPath.toOSString()))
170             throw new BuildException(Policy.bind("exception.invalidPath", fileSystemPath.toOSString())); //$NON-NLS-1$
171

172         if (resourcePath == null && fileSystemPath == null)
173             throw new BuildException(Policy.bind("exception.mustHaveOneAttribute")); //$NON-NLS-1$
174
}
175 }
176
Popular Tags