Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "FAQ What is the difference between a path and a location?"

m (Formatting)
m
Line 2: Line 2:
  
 
<tt>IPath</tt> is in fact an abstract data structure that represents a series of slash-separated strings. Many people assume that paths always correspond to a file-system location, but this is not always the case.
 
<tt>IPath</tt> is in fact an abstract data structure that represents a series of slash-separated strings. Many people assume that paths always correspond to a file-system location, but this is not always the case.
  In fact, <tt>IPath</tt> objects are used in a variety of contexts for locating an  
+
   
object within a tree-like hierarchy. For example, a resource pathreturned by
+
In fact, <tt>IPath</tt> objects are used in a variety of contexts for locating an  
<tt>IResource.getFullPath</tt>, represents the position of a resource within the
+
object within a tree-like hierarchy. For example, a resource pathreturned by
workspace tree.  The first segment is the name of the project, the last segment
+
<tt>IResource.getFullPath</tt>, represents the position of a resource within the
is the name of the file or folder, and the middle segments are the names of the
+
workspace tree.  The first segment is the name of the project, the last segment
parent folders in order between the project and the file or folder in question.
+
is the name of the file or folder, and the middle segments are the names of the
This doesn't always match the path of the resource in the file system!  
+
parent folders in order between the project and the file or folder in question.
 +
This doesn't always match the path of the resource in the file system!  
  
 
The file-system location of a resource, on the other hand, is returned by the method <tt>IResource.getLocation</tt>. Methods on <tt>IContainer</tt> and <tt>IWorkspaceRoot</tt> can help you convert from one to the other.  The following code converts from a path to a location:
 
The file-system location of a resource, on the other hand, is returned by the method <tt>IResource.getLocation</tt>. Methods on <tt>IContainer</tt> and <tt>IWorkspaceRoot</tt> can help you convert from one to the other.  The following code converts from a path to a location:

Revision as of 01:46, 22 October 2006

In general, the term location represents physical file system paths, and path represents a resource's logical position within the workspace. Many people are confused by this distinction, as both terms are represented by using the IPath data type.

IPath is in fact an abstract data structure that represents a series of slash-separated strings. Many people assume that paths always correspond to a file-system location, but this is not always the case.

In fact, IPath objects are used in a variety of contexts for locating an object within a tree-like hierarchy. For example, a resource pathreturned by IResource.getFullPath, represents the position of a resource within the workspace tree. The first segment is the name of the project, the last segment is the name of the file or folder, and the middle segments are the names of the parent folders in order between the project and the file or folder in question. This doesn't always match the path of the resource in the file system!

The file-system location of a resource, on the other hand, is returned by the method IResource.getLocation. Methods on IContainer and IWorkspaceRoot can help you convert from one to the other. The following code converts from a path to a location:

   IPath path = ...;
   IWorkspace workspace = ResourcesPlugin.getWorkspace();
   IWorkspaceRoot root = workspace.getRoot();
   IResource resource = root.findMember(path);
   if (resource != null) {
      location = resource.getLocation();
   }

Note that in some situations, a resource can have a null location. In particular, resources whose project doesn't exist and linked resources that are relative to a nonexistent path variable will have a null location.

Here is the converse code to convert from a location to the workspace paths that correspond to it:

   IPath location = ...;
   IFile[] files = root.findFilesForLocation(location);
   IFolder[] folders = root.findContainersForLocation(location);
   if (files.length > 0) {
      for (int i = 0; i < files.length; i++)
         path = files[i].getLocation();
   } else {
      for (int i = 0; i < folders.length; i++)
         path = folders[i].getLocation();
   }

As this snippet shows, a single file-system location can correspond to multiple resources. This is true because linked resources can point to locations inside other projects. Of course, the same file-system location can't correspond to both files and folders at the same time.

When using API from the resources plug-in that involve IPath objects, always read the API javadoc carefully to see whether it deals with paths or locations. These different types of paths must never be mixed or used interchangeably, as they represent completely different things.


This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.

Back to the top