blob: fb58e5583dae8dc839914bf4776dfa157d2c099f [file] [log] [blame]
cbridgha6887f4d2005-03-23 15:10:33 +00001/*******************************************************************************
jlanutifed82ab2005-12-01 22:03:10 +00002 * Copyright (c) 2003, 2005 IBM Corporation and others.
cbridgha6887f4d2005-03-23 15:10:33 +00003 * 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
jlanutifed82ab2005-12-01 22:03:10 +00007 *
cbridgha6887f4d2005-03-23 15:10:33 +00008 * Contributors:
9 * IBM Corporation - initial API and implementation
cbridghaaf88ca62005-06-24 19:00:16 +000010 *******************************************************************************/
cbridgha44a17d72005-04-04 07:05:01 +000011package org.eclipse.wst.common.componentcore.internal.resources;
cbridgha6887f4d2005-03-23 15:10:33 +000012
vbhadrir9dd55d92005-09-08 21:46:35 +000013import java.util.ArrayList;
cbridgha6887f4d2005-03-23 15:10:33 +000014import java.util.Arrays;
vbhadrir9dd55d92005-09-08 21:46:35 +000015import java.util.List;
cbridgha6887f4d2005-03-23 15:10:33 +000016
cbridgha25501202005-03-30 23:45:41 +000017import org.eclipse.core.resources.IContainer;
18import org.eclipse.core.resources.IFolder;
cbridgha6887f4d2005-03-23 15:10:33 +000019import org.eclipse.core.resources.IProject;
cbridgha6887f4d2005-03-23 15:10:33 +000020import org.eclipse.core.resources.IResource;
cbridgha6887f4d2005-03-23 15:10:33 +000021import org.eclipse.core.runtime.CoreException;
22import org.eclipse.core.runtime.IPath;
23import org.eclipse.core.runtime.IProgressMonitor;
cbridgha6887f4d2005-03-23 15:10:33 +000024import org.eclipse.core.runtime.jobs.ISchedulingRule;
cbridgha44a17d72005-04-04 07:05:01 +000025import org.eclipse.wst.common.componentcore.ComponentCore;
cbridgha44a17d72005-04-04 07:05:01 +000026import org.eclipse.wst.common.componentcore.internal.ComponentResource;
vbhadrir58634cb2005-04-25 20:28:18 +000027import org.eclipse.wst.common.componentcore.internal.StructureEdit;
cbridgha44a17d72005-04-04 07:05:01 +000028import org.eclipse.wst.common.componentcore.internal.WorkbenchComponent;
cbridgha632c3872005-10-03 13:27:37 +000029import org.eclipse.wst.common.componentcore.internal.impl.ModuleURIUtil;
cbridghab75aee92005-05-18 03:02:33 +000030import org.eclipse.wst.common.componentcore.internal.impl.ResourceTreeNode;
cbridgha44a17d72005-04-04 07:05:01 +000031import org.eclipse.wst.common.componentcore.internal.impl.ResourceTreeRoot;
32import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;
33import org.eclipse.wst.common.componentcore.resources.IVirtualContainer;
34import org.eclipse.wst.common.componentcore.resources.IVirtualResource;
cbridgha6887f4d2005-03-23 15:10:33 +000035
cbridgha1c6a6ba2005-03-24 16:24:53 +000036public abstract class VirtualResource implements IVirtualResource {
cbridghaaf88ca62005-06-24 19:00:16 +000037
cbridgha25501202005-03-30 23:45:41 +000038 protected static final IResource[] NO_RESOURCES = null;
jlanuti35b628c2006-01-04 16:44:24 +000039 private final IProject componentProject;
40 private final IPath runtimePath;
cbridgha6887f4d2005-03-23 15:10:33 +000041 private int hashCode;
42 private String toString;
cbridgha1686f442005-03-29 20:48:41 +000043 private static final String EMPTY_STRING = ""; //$NON-NLS-1$
cbridgha44a17d72005-04-04 07:05:01 +000044 private IVirtualComponent component;
cbridgha4da5ee72005-04-14 14:27:50 +000045 private String resourceType;
cbridghaaf88ca62005-06-24 19:00:16 +000046
47
cbridgha632c3872005-10-03 13:27:37 +000048 protected VirtualResource(IProject aComponentProject, IPath aRuntimePath) {
49 componentProject = aComponentProject;
cbridgha6887f4d2005-03-23 15:10:33 +000050 runtimePath = aRuntimePath;
51 }
cbridghaaf88ca62005-06-24 19:00:16 +000052
cbridghaaf88ca62005-06-24 19:00:16 +000053 public void delete(int updateFlags, IProgressMonitor monitor) throws CoreException {
54
55 if ((updateFlags & IVirtualResource.IGNORE_UNDERLYING_RESOURCE) == 0) {
56 doDeleteRealResources(updateFlags, monitor);
57 }
58
59 doDeleteMetaModel(updateFlags, monitor);
60 }
61
62 protected void doDeleteMetaModel(int updateFlags, IProgressMonitor monitor) {
63 StructureEdit moduleCore = null;
cbridgha6887f4d2005-03-23 15:10:33 +000064 try {
cbridgha632c3872005-10-03 13:27:37 +000065 moduleCore = StructureEdit.getStructureEditForWrite(getProject());
66 WorkbenchComponent aComponent = moduleCore.getComponent();
vbhadrir9dd55d92005-09-08 21:46:35 +000067 ComponentResource[] resources = aComponent.findResourcesByRuntimePath(getRuntimePath());
68 aComponent.getResources().removeAll(Arrays.asList(resources));
cbridgha6887f4d2005-03-23 15:10:33 +000069 } finally {
cbridghaaf88ca62005-06-24 19:00:16 +000070 if (moduleCore != null) {
cbridgha6887f4d2005-03-23 15:10:33 +000071 moduleCore.saveIfNecessary(monitor);
72 moduleCore.dispose();
73 }
74 }
75 }
76
77
78 protected abstract void doDeleteRealResources(int updateFlags, IProgressMonitor monitor) throws CoreException;
79
cbridgha6887f4d2005-03-23 15:10:33 +000080 public boolean exists() {
vbhadrir9dd55d92005-09-08 21:46:35 +000081 // verify all underlying resources exist for the virtual resource to exist
82 IResource[] resources = getUnderlyingResources();
vbhadrirff852bb2005-09-22 19:52:21 +000083 if (resources==null || resources.length==0)
84 return false;
vbhadrir9dd55d92005-09-08 21:46:35 +000085 for (int i=0; i<resources.length; i++) {
86 if (resources[i]==null || !resources[i].exists())
87 return false;
88 }
89 return true;
cbridgha6887f4d2005-03-23 15:10:33 +000090 }
91
cbridgha6887f4d2005-03-23 15:10:33 +000092 public String getFileExtension() {
cbridgha1686f442005-03-29 20:48:41 +000093 String name = getName();
94 int dot = name.lastIndexOf('.');
95 if (dot == -1)
96 return null;
cbridghaaf88ca62005-06-24 19:00:16 +000097 if (dot == name.length() - 1)
98 return EMPTY_STRING;
99 return name.substring(dot + 1);
cbridgha6887f4d2005-03-23 15:10:33 +0000100 }
cbridghaaf88ca62005-06-24 19:00:16 +0000101
cbridgha10e82232005-03-28 14:33:24 +0000102 public IPath getWorkspaceRelativePath() {
cbridgha1686f442005-03-29 20:48:41 +0000103 return getProject().getFullPath().append(getProjectRelativePath());
cbridgha6887f4d2005-03-23 15:10:33 +0000104 }
cbridghaaf88ca62005-06-24 19:00:16 +0000105
cbridgha1686f442005-03-29 20:48:41 +0000106 public IPath getRuntimePath() {
107 return runtimePath;
cbridghaaf88ca62005-06-24 19:00:16 +0000108 }
109
vbhadrir9dd55d92005-09-08 21:46:35 +0000110 public IPath[] getProjectRelativePaths() {
cbridgha44a17d72005-04-04 07:05:01 +0000111 StructureEdit moduleCore = null;
cbridgha17f4e1d2005-03-28 19:41:04 +0000112 try {
cbridgha44a17d72005-04-04 07:05:01 +0000113 moduleCore = StructureEdit.getStructureEditForRead(getProject());
vbhadrirf1b80462005-10-21 15:44:00 +0000114 if (moduleCore !=null) {
115 WorkbenchComponent aComponent = moduleCore.getComponent();
116 if (aComponent != null) {
117 ResourceTreeRoot root = ResourceTreeRoot.getDeployResourceTreeRoot(aComponent);
118 // still need some sort of loop here to search subpieces of the runtime path.
119 ComponentResource[] componentResources = null;
120
121 if (root != null) {
122 IPath[] estimatedPaths = null;
123 IPath searchPath = null;
124 do {
125 searchPath = (searchPath == null) ? getRuntimePath() : searchPath.removeLastSegments(1);
126 componentResources = root.findModuleResources(searchPath, ResourceTreeNode.CREATE_NONE);
127 estimatedPaths = findBestMatches(componentResources);
128 } while (estimatedPaths.length==0 && canSearchContinue(componentResources, searchPath));
129 if (estimatedPaths==null || estimatedPaths.length==0)
130 return new IPath[] {getRuntimePath()};
131 return estimatedPaths;
132 }
133 }
cbridghafe83b0c2005-04-19 19:54:55 +0000134 }
cbridgha17f4e1d2005-03-28 19:41:04 +0000135 } finally {
cbridghaaf88ca62005-06-24 19:00:16 +0000136 if (moduleCore != null) {
cbridgha17f4e1d2005-03-28 19:41:04 +0000137 moduleCore.dispose();
138 }
139 }
vbhadrir9dd55d92005-09-08 21:46:35 +0000140 return new IPath[] {getRuntimePath()};
cbridghad953dc72005-04-26 21:38:47 +0000141 }
cbridghaaf88ca62005-06-24 19:00:16 +0000142
vbhadrir9dd55d92005-09-08 21:46:35 +0000143 public IPath getProjectRelativePath() {
144 return getProjectRelativePaths()[0];
145 }
146
cbridghad953dc72005-04-26 21:38:47 +0000147 private boolean canSearchContinue(ComponentResource[] componentResources, IPath searchPath) {
cbridgha07e7c5f2005-04-27 14:57:46 +0000148 return (searchPath.segmentCount() > 0);
cbridghaaf88ca62005-06-24 19:00:16 +0000149 }
150
vbhadrir9dd55d92005-09-08 21:46:35 +0000151 private IPath[] findBestMatches(ComponentResource[] theComponentResources) {
152 List result = new ArrayList();
cbridghad953dc72005-04-26 21:38:47 +0000153 int currentMatchLength = 0;
cbridgha07e7c5f2005-04-27 14:57:46 +0000154 int bestMatchLength = -1;
cbridghad953dc72005-04-26 21:38:47 +0000155 IPath estimatedPath = null;
156 IPath currentPath = null;
vbhadrir9dd55d92005-09-08 21:46:35 +0000157 final IPath aRuntimePath = getRuntimePath();
cbridghaaf88ca62005-06-24 19:00:16 +0000158 for (int i = 0; i < theComponentResources.length; i++) {
cbridghad953dc72005-04-26 21:38:47 +0000159 currentPath = theComponentResources[i].getRuntimePath();
vbhadrir9dd55d92005-09-08 21:46:35 +0000160 if (currentPath.isPrefixOf(aRuntimePath)) {
161 if (currentPath.segmentCount() == aRuntimePath.segmentCount()) {
162 result.add(theComponentResources[i].getSourcePath());
163 continue;
164 }
165 currentMatchLength = currentPath.matchingFirstSegments(aRuntimePath);
cbridghaaf88ca62005-06-24 19:00:16 +0000166 if (currentMatchLength == currentPath.segmentCount() && currentMatchLength > bestMatchLength) {
cbridghad953dc72005-04-26 21:38:47 +0000167 bestMatchLength = currentMatchLength;
168 IPath sourcePath = theComponentResources[i].getSourcePath();
vbhadrir9dd55d92005-09-08 21:46:35 +0000169 IPath subpath = aRuntimePath.removeFirstSegments(currentMatchLength);
cbridghaaf88ca62005-06-24 19:00:16 +0000170 estimatedPath = sourcePath.append(subpath);
171 }
cbridghad953dc72005-04-26 21:38:47 +0000172 }
cbridghaaf88ca62005-06-24 19:00:16 +0000173 }
vbhadrir9dd55d92005-09-08 21:46:35 +0000174 if (result.size()>0)
175 return (IPath[]) result.toArray(new IPath[result.size()]);
vbhadrirc269ec32005-09-09 16:11:46 +0000176 if (estimatedPath == null)
177 return new IPath[] {};
vbhadrir9dd55d92005-09-08 21:46:35 +0000178 return new IPath[] {estimatedPath};
cbridghad953dc72005-04-26 21:38:47 +0000179 }
cbridghaaf88ca62005-06-24 19:00:16 +0000180
cbridgha1686f442005-03-29 20:48:41 +0000181 public String getName() {
vbhadrirdc0f5ee2005-08-17 20:16:35 +0000182 if (getRuntimePath().segmentCount()>0)
183 return getRuntimePath().lastSegment();
184 return getRuntimePath().toString();
cbridgha1686f442005-03-29 20:48:41 +0000185 }
cbridghaaf88ca62005-06-24 19:00:16 +0000186
cbridgha44a17d72005-04-04 07:05:01 +0000187 public IVirtualComponent getComponent() {
cbridghaaf88ca62005-06-24 19:00:16 +0000188 if (component == null)
cbridgha632c3872005-10-03 13:27:37 +0000189 component = ComponentCore.createComponent(getProject());
cbridgha44a17d72005-04-04 07:05:01 +0000190 return component;
cbridgha6887f4d2005-03-23 15:10:33 +0000191 }
vbhadrir35da0ba2005-06-27 20:02:56 +0000192
193 //returns null if the folder is already the root folder
cbridgha1686f442005-03-29 20:48:41 +0000194 public IVirtualContainer getParent() {
cbridgha796d6a82005-06-28 00:23:26 +0000195 if (getRuntimePath().segmentCount() >= 1)
cbridgha632c3872005-10-03 13:27:37 +0000196 return new VirtualFolder(getProject(), getRuntimePath().removeLastSegments(1));
vbhadrir35da0ba2005-06-27 20:02:56 +0000197 return null;
cbridghaaf88ca62005-06-24 19:00:16 +0000198 }
cbridgha1686f442005-03-29 20:48:41 +0000199
200 public IProject getProject() {
cbridgha632c3872005-10-03 13:27:37 +0000201 return componentProject;
cbridgha1686f442005-03-29 20:48:41 +0000202 }
cbridgha6887f4d2005-03-23 15:10:33 +0000203
204 public boolean isAccessible() {
205 throw new UnsupportedOperationException("Method not supported"); //$NON-NLS-1$
cbridghaaf88ca62005-06-24 19:00:16 +0000206 // return false;
207 }
208
cbridgha6887f4d2005-03-23 15:10:33 +0000209 public Object getAdapter(Class adapter) {
210 throw new UnsupportedOperationException("Method not supported"); //$NON-NLS-1$
cbridghaaf88ca62005-06-24 19:00:16 +0000211 // return null;
cbridgha6887f4d2005-03-23 15:10:33 +0000212 }
213
214 public boolean contains(ISchedulingRule rule) {
215 throw new UnsupportedOperationException("Method not supported"); //$NON-NLS-1$
cbridghaaf88ca62005-06-24 19:00:16 +0000216 // return false;
cbridgha6887f4d2005-03-23 15:10:33 +0000217 }
218
219 public boolean isConflicting(ISchedulingRule rule) {
220 throw new UnsupportedOperationException("Method not supported"); //$NON-NLS-1$
cbridghaaf88ca62005-06-24 19:00:16 +0000221 // return false;
cbridgha6887f4d2005-03-23 15:10:33 +0000222 }
cbridghaaf88ca62005-06-24 19:00:16 +0000223
cbridgha6887f4d2005-03-23 15:10:33 +0000224 public String toString() {
cbridghaaf88ca62005-06-24 19:00:16 +0000225 if (toString == null)
cbridgha9be0d352005-10-03 16:01:21 +0000226 toString = "[" + ModuleURIUtil.getHandleString(getComponent()) + ":" + getRuntimePath() + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
cbridgha6887f4d2005-03-23 15:10:33 +0000227 return toString;
228 }
cbridghaaf88ca62005-06-24 19:00:16 +0000229
cbridgha6887f4d2005-03-23 15:10:33 +0000230 public int hashCode() {
cbridghaaf88ca62005-06-24 19:00:16 +0000231 if (hashCode == 0)
cbridgha6887f4d2005-03-23 15:10:33 +0000232 hashCode = toString().hashCode();
233 return hashCode;
234 }
cbridghaaf88ca62005-06-24 19:00:16 +0000235
cbridgha6887f4d2005-03-23 15:10:33 +0000236 public boolean equals(Object anOther) {
cbridghaaf88ca62005-06-24 19:00:16 +0000237 return hashCode() == ((anOther != null && anOther instanceof VirtualResource) ? anOther.hashCode() : 0);
cbridgha4da5ee72005-04-14 14:27:50 +0000238 }
cbridghaaf88ca62005-06-24 19:00:16 +0000239
240 public void setResourceType(String aResourceType) {
241 resourceType = aResourceType;
242 StructureEdit moduleCore = null;
243 try {
244 moduleCore = StructureEdit.getStructureEditForRead(getProject());
cbridgha632c3872005-10-03 13:27:37 +0000245 WorkbenchComponent aComponent = moduleCore.getComponent();
vbhadrir9dd55d92005-09-08 21:46:35 +0000246 ComponentResource[] resources = aComponent.findResourcesByRuntimePath(getRuntimePath());
cbridghaaf88ca62005-06-24 19:00:16 +0000247 for (int i = 0; i < resources.length; i++) {
248 resources[i].setResourceType(aResourceType);
249 }
250 } finally {
251 if (moduleCore != null) {
252 moduleCore.dispose();
253 }
254 }
255 }
256
257 // TODO Fetch the resource type from the model.
cbridgha4da5ee72005-04-14 14:27:50 +0000258 public String getResourceType() {
cbridghaaf88ca62005-06-24 19:00:16 +0000259 if (null == resourceType) {
260 StructureEdit moduleCore = null;
261 try {
262 moduleCore = StructureEdit.getStructureEditForRead(getProject());
cbridgha632c3872005-10-03 13:27:37 +0000263 WorkbenchComponent aComponent = moduleCore.getComponent();
vbhadrir9dd55d92005-09-08 21:46:35 +0000264 ComponentResource[] resources = aComponent.findResourcesByRuntimePath(getRuntimePath());
cbridghaaf88ca62005-06-24 19:00:16 +0000265 for (int i = 0; i < resources.length; i++) {
266 resourceType = resources[i].getResourceType();
267 return resourceType;
268 }
269 } finally {
270 if (moduleCore != null) {
271 moduleCore.dispose();
272 }
273 }
274 }
vbhadrir9dd55d92005-09-08 21:46:35 +0000275 resourceType = ""; //$NON-NLS-1$
cbridgha4da5ee72005-04-14 14:27:50 +0000276 return resourceType;
277 }
cbridgha17f4e1d2005-03-28 19:41:04 +0000278
cbridghaaf88ca62005-06-24 19:00:16 +0000279
cbridgha25501202005-03-30 23:45:41 +0000280 protected void createResource(IContainer resource, int updateFlags, IProgressMonitor monitor) throws CoreException {
cbridgha1686f442005-03-29 20:48:41 +0000281
cbridghaaf88ca62005-06-24 19:00:16 +0000282 if (resource.exists())
vbhadrird3841182005-06-07 17:57:08 +0000283 return;
cbridgha25501202005-03-30 23:45:41 +0000284 if (!resource.getParent().exists())
285 createResource(resource.getParent(), updateFlags, monitor);
cbridghaaf88ca62005-06-24 19:00:16 +0000286 if (!resource.exists() && resource.getType() == IResource.FOLDER) {
cbridgha25501202005-03-30 23:45:41 +0000287 ((IFolder) resource).create(updateFlags, true, monitor);
cbridghaaf88ca62005-06-24 19:00:16 +0000288 }
cbridgha25501202005-03-30 23:45:41 +0000289 }
290
291 protected boolean isPotentalMatch(IPath aRuntimePath) {
cbridgha1686f442005-03-29 20:48:41 +0000292 return aRuntimePath.isPrefixOf(getRuntimePath());
293 }
cbridghaaf88ca62005-06-24 19:00:16 +0000294
cbridghae26159c2005-12-06 04:14:47 +0000295 /* (non-Javadoc)
296 * @see org.eclipse.wst.common.componentcore.resources.IVirtualResource#removeLink(org.eclipse.core.runtime.IPath, int, org.eclipse.core.runtime.IProgressMonitor)
297 */
298 public void removeLink(IPath aProjectRelativeLocation, int updateFlags, IProgressMonitor monitor) throws CoreException {
299 StructureEdit moduleCore = null;
300 try {
301 moduleCore = StructureEdit.getStructureEditForWrite(getProject());
302 WorkbenchComponent component = moduleCore.getComponent();
303 ResourceTreeRoot root = ResourceTreeRoot.getDeployResourceTreeRoot(component);
304 ComponentResource[] resources = root.findModuleResources(getRuntimePath(), ResourceTreeNode.CREATE_NONE);
305 if (resources.length > 0) {
306 for (int resourceIndx = 0; resourceIndx < resources.length; resourceIndx++) {
307 if (aProjectRelativeLocation.makeAbsolute().equals(resources[resourceIndx].getSourcePath())) {
308 component.getResources().remove(resources[resourceIndx]);
309 }
310 }
311 }
312 } finally {
313 if (moduleCore != null) {
314 moduleCore.saveIfNecessary(monitor);
315 moduleCore.dispose();
316 }
317 }
318 }
cbridgha6887f4d2005-03-23 15:10:33 +0000319}