KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > localstore > FileStoreRoot


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.internal.localstore;
12
13 import java.io.File JavaDoc;
14 import java.net.URI JavaDoc;
15 import org.eclipse.core.filesystem.EFS;
16 import org.eclipse.core.filesystem.IFileStore;
17 import org.eclipse.core.internal.utils.FileUtil;
18 import org.eclipse.core.resources.IPathVariableManager;
19 import org.eclipse.core.resources.ResourcesPlugin;
20 import org.eclipse.core.runtime.*;
21
22 /**
23  * Represents the root of a file system that is connected to the workspace.
24  * A file system can be rooted on any resource.
25  */

26 public class FileStoreRoot {
27     private int chop;
28     /**
29      * When a root is changed, the old root object is marked invalid
30      * so that other resources with a cache of the root will know they need to update.
31      */

32     private boolean isValid = true;
33     /**
34      * If this root represents a resource in the local file system, this path
35      * represents the root location. This value is null if the root represents
36      * a non-local file system
37      */

38     private IPath localRoot = null;
39
40     private URI JavaDoc root;
41
42     private final IPathVariableManager variableManager;
43
44     /**
45      * Defines the root of a file system within the workspace tree.
46      * @param rootURI The virtual file representing the root of the file
47      * system that has been mounted
48      * @param workspacePath The workspace path at which this file
49      * system has been mounted
50      */

51     FileStoreRoot(URI JavaDoc rootURI, IPath workspacePath) {
52         Assert.isNotNull(rootURI);
53         Assert.isNotNull(workspacePath);
54         this.variableManager = ResourcesPlugin.getWorkspace().getPathVariableManager();
55         this.root = rootURI;
56         this.chop = workspacePath.segmentCount();
57         this.localRoot = toLocalPath(root);
58     }
59
60     /**
61      * Returns the resolved, absolute file system location of the resource
62      * corresponding to the given workspace path, or null if none could
63      * be computed.
64      */

65     public URI JavaDoc computeURI(IPath workspacePath) {
66         IPath childPath = workspacePath.removeFirstSegments(chop);
67         final URI JavaDoc rootURI = variableManager.resolveURI(root);
68         if (childPath.segmentCount() == 0)
69             return rootURI;
70         try {
71             return EFS.getStore(rootURI).getChild(childPath).toURI();
72         } catch (CoreException e) {
73             return null;
74         }
75     }
76
77     /**
78      * Creates an IFileStore for a given workspace path.
79      * @exception CoreException If the file system for that resource is undefined
80      */

81     IFileStore createStore(IPath workspacePath) throws CoreException {
82         IPath childPath = workspacePath.removeFirstSegments(chop);
83         IFileStore rootStore;
84         final URI JavaDoc uri = variableManager.resolveURI(root);
85         if (!uri.isAbsolute()) {
86             //handles case where resource location cannot be resolved
87
//such as unresolved path variable or invalid file system scheme
88
return EFS.getNullFileSystem().getStore(workspacePath);
89         }
90         rootStore = EFS.getStore(uri);
91         if (childPath.segmentCount() == 0)
92             return rootStore;
93         return rootStore.getChild(childPath);
94     }
95
96     boolean isValid() {
97         return isValid;
98     }
99
100     IPath localLocation(IPath workspacePath) {
101         if (localRoot == null)
102             return null;
103         IPath location;
104         if (workspacePath.segmentCount() <= chop)
105             location = localRoot;
106         else
107             location = localRoot.append(workspacePath.removeFirstSegments(chop));
108         location = variableManager.resolvePath(location);
109         //if path is still relative then path variable could not be resolved
110
if (!location.isAbsolute())
111             return null;
112         return location;
113     }
114
115     void setValid(boolean value) {
116         this.isValid = value;
117     }
118
119     /**
120      * Returns the local path for the given URI, or null if not possible.
121      */

122     private IPath toLocalPath(URI JavaDoc uri) {
123         try {
124             final File JavaDoc localFile = EFS.getStore(uri).toLocalFile(EFS.NONE, null);
125             return localFile == null ? null : new Path(localFile.getAbsolutePath());
126         } catch (CoreException e) {
127             return FileUtil.toPath(uri);
128         }
129     }
130 }
131
Popular Tags