KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > filebuffers > FileBuffers


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.filebuffers;
12
13
14 import java.io.File JavaDoc;
15 import java.net.URI JavaDoc;
16
17 import org.eclipse.core.filesystem.EFS;
18 import org.eclipse.core.filesystem.IFileStore;
19 import org.eclipse.core.internal.filebuffers.FileBuffersPlugin;
20
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IPath;
23
24 import org.eclipse.core.resources.IFile;
25 import org.eclipse.core.resources.IWorkspaceRoot;
26 import org.eclipse.core.resources.ResourcesPlugin;
27
28
29 /**
30  * Facade for the file buffers plug-in. Provides access to the text file buffer
31  * manager and helper methods for location handling. This facade is available
32  * independent from the activation status of the file buffers plug-in.
33  * <p>
34  * This class must not be used by clients that do not want to require
35  * <code>org.eclipse.core.resources</code>. Use <code>ITextFileBufferManager.DEFAULT</code>
36  * to get the default text file buffer manager.
37  * </p>
38  *
39  * @since 3.0
40  */

41 public final class FileBuffers {
42
43     /**
44      * The workspace root.
45      * @since 3.3
46      */

47     private static final IWorkspaceRoot WORKSPACE_ROOT= ResourcesPlugin.getWorkspace().getRoot();
48
49     /**
50      * Cannot be instantiated.
51      */

52     private FileBuffers() {
53     }
54
55     /**
56      * File buffer plug-in ID
57      * (value <code>"org.eclipse.core.filebuffers"</code>).
58      *
59      * @since 3.3.
60      */

61     public final static String JavaDoc PLUGIN_ID= FileBuffersPlugin.PLUGIN_ID;
62
63     /**
64      * Returns the text file buffer manager. May return <code>null</code> if
65      * the file buffers plug-in may no be activated. This is, for example, the
66      * case when the method is called on plug-in shutdown.
67      * <p>
68      * Use <code>ITextFileBufferManager.DEFAULT</code> to get the default text
69      * file buffer manager if you do not want to depend on
70      * <code>org.eclipse.core.resources</code>.
71
72      * </p>
73      *
74      * @return the text file buffer manager or <code>null</code>
75      */

76     public static ITextFileBufferManager getTextFileBufferManager() {
77         FileBuffersPlugin plugin= FileBuffersPlugin.getDefault();
78         return plugin != null ? plugin.getFileBufferManager() : null;
79     }
80
81     /**
82      * Returns the workspace file at the given location or <code>null</code> if
83      * the location is not a valid location in the workspace.
84      *
85      * @param location the location
86      * @return the workspace file at the location or <code>null</code>
87      */

88     public static IFile getWorkspaceFileAtLocation(IPath location) {
89         return getWorkspaceFileAtLocation(location, false);
90     }
91
92     /**
93      * Returns the workspace file at the given location or <code>null</code> if
94      * the location is not a valid location in the workspace.
95      *
96      * @param location the location
97      * @param isNormalized <code>true</code> if the given location is already normalized
98      * @return the workspace file at the location or <code>null</code>
99      * @since 3.3
100      */

101     public static IFile getWorkspaceFileAtLocation(IPath location, boolean isNormalized) {
102         IPath normalized;
103         if (isNormalized)
104             normalized= location;
105         else
106             normalized= normalizeLocation(location);
107         
108         if (normalized.segmentCount() >= 2) {
109             // @see IContainer#getFile for the required number of segments
110
IFile file= WORKSPACE_ROOT.getFile(normalized);
111             if (file != null && file.exists())
112                 return file;
113         }
114         return null;
115     }
116
117     /**
118      * Returns the normalized form of the given path or location.
119      * <p>
120      * The normalized form is defined as follows:
121      * </p>
122      * <ul>
123      * <li><b>Existing Workspace Files:</b> For a path or location for
124      * which there
125      * {@link org.eclipse.core.resources.IContainer#exists(org.eclipse.core.runtime.IPath) exists}
126      * a workspace file, the normalized form is that file's workspace
127      * relative, absolute path as returned by
128      * {@link IFile#getFullPath()}.</li>
129      * <li><b>Non-existing Workspace Files:</b> For a path to a
130      * non-existing workspace file, the normalized form is the
131      * {@link IPath#makeAbsolute() absolute} form of the path.</li>
132      * <li><b>External Files:</b> For a location for which there
133      * exists no workspace file, the normalized form is the
134      * {@link IPath#makeAbsolute() absolute} form of the location.</li>
135      * </ul>
136      *
137      * @param pathOrLocation the path or location to be normalized
138      * @return the normalized form of <code>pathOrLocation</code>
139      */

140     public static IPath normalizeLocation(IPath pathOrLocation) {
141         // existing workspace resources - this is the 93% case
142
if (WORKSPACE_ROOT.exists(pathOrLocation))
143             return pathOrLocation.makeAbsolute();
144
145         IFile file= WORKSPACE_ROOT.getFileForLocation(pathOrLocation);
146         // existing workspace resources referenced by their file system path
147
// files that do not exist (including non-accessible files) do not pass
148
if (file != null && file.exists())
149             return file.getFullPath();
150
151         // non-existing resources and external files
152
return pathOrLocation.makeAbsolute();
153     }
154
155     /**
156      * Returns the file in the local file system for the given location.
157      * <p>
158      * The location is either a full path of a workspace resource or an
159      * absolute path in the local file system.
160      * </p>
161      *
162      * @param location the location
163      * @return the {@link IFileStore} in the local file system for the given location
164      * @since 3.2
165      */

166     public static IFileStore getFileStoreAtLocation(IPath location) {
167         if (location == null)
168             return null;
169
170         IFile file= getWorkspaceFileAtLocation(location);
171         try {
172             if (file != null) {
173                 URI JavaDoc uri= file.getLocationURI();
174                 if (uri == null)
175                     return null;
176                 return EFS.getStore(uri);
177             }
178         } catch (CoreException e) {
179             //fall through and assume it is a local file
180
}
181         return EFS.getLocalFileSystem().getStore(location);
182     }
183     
184     /**
185      * Returns the file in the local file system for the given location.
186      * <p>
187      * The location is either a full path of a workspace resource or an
188      * absolute path in the local file system.
189      * </p>
190      *
191      * @param location the location
192      * @return the {@link File} in the local file system for the given location
193      * @deprecated As of 3.2, replaced by {@link #getFileStoreAtLocation(IPath)}
194      */

195     public static File JavaDoc getSystemFileAtLocation(IPath location) {
196         IFileStore store= getFileStoreAtLocation(location);
197         if (store != null) {
198             try {
199                 return store.toLocalFile(EFS.NONE, null);
200             } catch (CoreException e) {
201                 return null;
202             }
203         }
204         return null;
205     }
206 }
207
Popular Tags