blob: 42e684802bb89d944cd1ae13983621324e0be33f [file] [log] [blame]
cbridgha6887f4d2005-03-23 15:10:33 +00001/*******************************************************************************
2 * Copyright (c) 2003, 2004 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
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
13import java.util.Arrays;
14
cbridgha25501202005-03-30 23:45:41 +000015import org.eclipse.core.resources.IContainer;
16import org.eclipse.core.resources.IFolder;
cbridgha6887f4d2005-03-23 15:10:33 +000017import org.eclipse.core.resources.IProject;
cbridgha6887f4d2005-03-23 15:10:33 +000018import org.eclipse.core.resources.IResource;
cbridgha6887f4d2005-03-23 15:10:33 +000019import org.eclipse.core.runtime.CoreException;
20import org.eclipse.core.runtime.IPath;
21import org.eclipse.core.runtime.IProgressMonitor;
cbridgha6887f4d2005-03-23 15:10:33 +000022import org.eclipse.core.runtime.jobs.ISchedulingRule;
cbridgha44a17d72005-04-04 07:05:01 +000023import org.eclipse.wst.common.componentcore.ComponentCore;
cbridgha44a17d72005-04-04 07:05:01 +000024import org.eclipse.wst.common.componentcore.internal.ComponentResource;
vbhadrir58634cb2005-04-25 20:28:18 +000025import org.eclipse.wst.common.componentcore.internal.StructureEdit;
cbridgha44a17d72005-04-04 07:05:01 +000026import org.eclipse.wst.common.componentcore.internal.WorkbenchComponent;
cbridghab75aee92005-05-18 03:02:33 +000027import org.eclipse.wst.common.componentcore.internal.impl.ResourceTreeNode;
cbridgha44a17d72005-04-04 07:05:01 +000028import org.eclipse.wst.common.componentcore.internal.impl.ResourceTreeRoot;
cbridgha4bfee8b2005-04-25 20:00:22 +000029import org.eclipse.wst.common.componentcore.resources.ComponentHandle;
cbridgha44a17d72005-04-04 07:05:01 +000030import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;
31import org.eclipse.wst.common.componentcore.resources.IVirtualContainer;
32import org.eclipse.wst.common.componentcore.resources.IVirtualResource;
cbridgha6887f4d2005-03-23 15:10:33 +000033
cbridgha1c6a6ba2005-03-24 16:24:53 +000034public abstract class VirtualResource implements IVirtualResource {
cbridghaaf88ca62005-06-24 19:00:16 +000035
cbridgha25501202005-03-30 23:45:41 +000036 protected static final IResource[] NO_RESOURCES = null;
cbridgha6887f4d2005-03-23 15:10:33 +000037 private ComponentHandle componentHandle;
38 private IPath runtimePath;
39 private int hashCode;
40 private String toString;
cbridgha1686f442005-03-29 20:48:41 +000041 private static final String EMPTY_STRING = ""; //$NON-NLS-1$
cbridgha44a17d72005-04-04 07:05:01 +000042 private IVirtualComponent component;
cbridgha4da5ee72005-04-14 14:27:50 +000043 private String resourceType;
cbridghaaf88ca62005-06-24 19:00:16 +000044
45
cbridgha6887f4d2005-03-23 15:10:33 +000046 protected VirtualResource(ComponentHandle aComponentHandle, IPath aRuntimePath) {
cbridghaaf88ca62005-06-24 19:00:16 +000047 componentHandle = aComponentHandle;
cbridgha6887f4d2005-03-23 15:10:33 +000048 runtimePath = aRuntimePath;
49 }
cbridghaaf88ca62005-06-24 19:00:16 +000050
51
cbridgha6887f4d2005-03-23 15:10:33 +000052 protected VirtualResource(IProject aProject, String aComponentName, IPath aRuntimePath) {
cbridghaaf88ca62005-06-24 19:00:16 +000053 this(ComponentHandle.create(aProject, aComponentName), aRuntimePath);
cbridgha6887f4d2005-03-23 15:10:33 +000054 }
55
cbridghaaf88ca62005-06-24 19:00:16 +000056 public void delete(int updateFlags, IProgressMonitor monitor) throws CoreException {
57
58 if ((updateFlags & IVirtualResource.IGNORE_UNDERLYING_RESOURCE) == 0) {
59 doDeleteRealResources(updateFlags, monitor);
60 }
61
62 doDeleteMetaModel(updateFlags, monitor);
63 }
64
65 protected void doDeleteMetaModel(int updateFlags, IProgressMonitor monitor) {
66 StructureEdit moduleCore = null;
cbridgha6887f4d2005-03-23 15:10:33 +000067 try {
cbridgha44a17d72005-04-04 07:05:01 +000068 moduleCore = StructureEdit.getStructureEditForWrite(getComponentHandle().getProject());
69 WorkbenchComponent component = moduleCore.findComponentByName(getComponentHandle().getName());
cbridghafbd0abb2005-04-14 04:44:09 +000070 ComponentResource[] resources = component.findResourcesByRuntimePath(getRuntimePath());
cbridgha6887f4d2005-03-23 15:10:33 +000071 component.getResources().removeAll(Arrays.asList(resources));
72 } finally {
cbridghaaf88ca62005-06-24 19:00:16 +000073 if (moduleCore != null) {
cbridgha6887f4d2005-03-23 15:10:33 +000074 moduleCore.saveIfNecessary(monitor);
75 moduleCore.dispose();
76 }
77 }
78 }
79
80
81 protected abstract void doDeleteRealResources(int updateFlags, IProgressMonitor monitor) throws CoreException;
82
cbridgha6887f4d2005-03-23 15:10:33 +000083 public boolean exists() {
cbridgha338cd8f2005-04-19 20:03:12 +000084 IResource resource = getUnderlyingResource();
85 return resource != null && resource.exists();
cbridgha6887f4d2005-03-23 15:10:33 +000086 }
87
cbridgha6887f4d2005-03-23 15:10:33 +000088 public String getFileExtension() {
cbridgha1686f442005-03-29 20:48:41 +000089 String name = getName();
90 int dot = name.lastIndexOf('.');
91 if (dot == -1)
92 return null;
cbridghaaf88ca62005-06-24 19:00:16 +000093 if (dot == name.length() - 1)
94 return EMPTY_STRING;
95 return name.substring(dot + 1);
cbridgha6887f4d2005-03-23 15:10:33 +000096 }
cbridghaaf88ca62005-06-24 19:00:16 +000097
cbridgha10e82232005-03-28 14:33:24 +000098 public IPath getWorkspaceRelativePath() {
cbridgha1686f442005-03-29 20:48:41 +000099 return getProject().getFullPath().append(getProjectRelativePath());
cbridgha6887f4d2005-03-23 15:10:33 +0000100 }
cbridghaaf88ca62005-06-24 19:00:16 +0000101
cbridgha1686f442005-03-29 20:48:41 +0000102 public IPath getRuntimePath() {
103 return runtimePath;
cbridghaaf88ca62005-06-24 19:00:16 +0000104 }
105
cbridgha6887f4d2005-03-23 15:10:33 +0000106 public IPath getProjectRelativePath() {
cbridgha44a17d72005-04-04 07:05:01 +0000107 StructureEdit moduleCore = null;
cbridgha17f4e1d2005-03-28 19:41:04 +0000108 try {
cbridgha44a17d72005-04-04 07:05:01 +0000109 moduleCore = StructureEdit.getStructureEditForRead(getProject());
110 WorkbenchComponent component = moduleCore.findComponentByName(getComponentHandle().getName());
cbridghaaf88ca62005-06-24 19:00:16 +0000111 if (component != null) {
cbridghafe83b0c2005-04-19 19:54:55 +0000112 ResourceTreeRoot root = ResourceTreeRoot.getDeployResourceTreeRoot(component);
cbridgha4c640c52005-04-25 16:03:19 +0000113 // still need some sort of loop here to search subpieces of the runtime path.
114 ComponentResource[] componentResources = null;
cbridghaaf88ca62005-06-24 19:00:16 +0000115
cbridghad953dc72005-04-26 21:38:47 +0000116 IPath estimatedPath = null;
cbridgha4c640c52005-04-25 16:03:19 +0000117 IPath searchPath = null;
cbridghaaf88ca62005-06-24 19:00:16 +0000118 do {
119 searchPath = (searchPath == null) ? getRuntimePath() : searchPath.removeLastSegments(1);
cbridghab75aee92005-05-18 03:02:33 +0000120 componentResources = root.findModuleResources(searchPath, ResourceTreeNode.CREATE_NONE);
cbridghaaf88ca62005-06-24 19:00:16 +0000121 estimatedPath = findBestMatch(componentResources);
122 } while (estimatedPath == null && canSearchContinue(componentResources, searchPath));
cbridgha4c640c52005-04-25 16:03:19 +0000123 return estimatedPath;
cbridghafe83b0c2005-04-19 19:54:55 +0000124 }
cbridgha17f4e1d2005-03-28 19:41:04 +0000125 } finally {
cbridghaaf88ca62005-06-24 19:00:16 +0000126 if (moduleCore != null) {
cbridgha17f4e1d2005-03-28 19:41:04 +0000127 moduleCore.dispose();
128 }
129 }
130 return getRuntimePath();
cbridghad953dc72005-04-26 21:38:47 +0000131 }
cbridghaaf88ca62005-06-24 19:00:16 +0000132
cbridghad953dc72005-04-26 21:38:47 +0000133 private boolean canSearchContinue(ComponentResource[] componentResources, IPath searchPath) {
cbridgha07e7c5f2005-04-27 14:57:46 +0000134 return (searchPath.segmentCount() > 0);
cbridghaaf88ca62005-06-24 19:00:16 +0000135 }
136
cbridghad953dc72005-04-26 21:38:47 +0000137 private IPath findBestMatch(ComponentResource[] theComponentResources) {
138
139 int currentMatchLength = 0;
cbridgha07e7c5f2005-04-27 14:57:46 +0000140 int bestMatchLength = -1;
cbridghad953dc72005-04-26 21:38:47 +0000141 IPath estimatedPath = null;
142 IPath currentPath = null;
143 final IPath runtimePath = getRuntimePath();
cbridghaaf88ca62005-06-24 19:00:16 +0000144 for (int i = 0; i < theComponentResources.length; i++) {
cbridghad953dc72005-04-26 21:38:47 +0000145 currentPath = theComponentResources[i].getRuntimePath();
cbridghaaf88ca62005-06-24 19:00:16 +0000146
147 if (currentPath.isPrefixOf(runtimePath)) {
148 if (currentPath.segmentCount() == runtimePath.segmentCount())
cbridghad953dc72005-04-26 21:38:47 +0000149 return theComponentResources[i].getSourcePath();
cbridghaaf88ca62005-06-24 19:00:16 +0000150
151 currentMatchLength = currentPath.matchingFirstSegments(runtimePath);
152 if (currentMatchLength == currentPath.segmentCount() && currentMatchLength > bestMatchLength) {
cbridghad953dc72005-04-26 21:38:47 +0000153 bestMatchLength = currentMatchLength;
154 IPath sourcePath = theComponentResources[i].getSourcePath();
155 IPath subpath = runtimePath.removeFirstSegments(currentMatchLength);
cbridghaaf88ca62005-06-24 19:00:16 +0000156 estimatedPath = sourcePath.append(subpath);
157 }
cbridghad953dc72005-04-26 21:38:47 +0000158 }
cbridghaaf88ca62005-06-24 19:00:16 +0000159 }
cbridghad953dc72005-04-26 21:38:47 +0000160 return estimatedPath;
161 }
cbridghaaf88ca62005-06-24 19:00:16 +0000162
cbridgha1686f442005-03-29 20:48:41 +0000163 public String getName() {
164 return getRuntimePath().lastSegment();
165 }
cbridghaaf88ca62005-06-24 19:00:16 +0000166
cbridgha44a17d72005-04-04 07:05:01 +0000167 public IVirtualComponent getComponent() {
cbridghaaf88ca62005-06-24 19:00:16 +0000168 if (component == null)
cbridgha44a17d72005-04-04 07:05:01 +0000169 component = ComponentCore.createComponent(getProject(), getComponentHandle().getName());
170 return component;
cbridgha6887f4d2005-03-23 15:10:33 +0000171 }
vbhadrir35da0ba2005-06-27 20:02:56 +0000172
173 //returns null if the folder is already the root folder
cbridgha1686f442005-03-29 20:48:41 +0000174 public IVirtualContainer getParent() {
cbridghaaf88ca62005-06-24 19:00:16 +0000175 if (getRuntimePath().segmentCount() > 1)
cbridgha1686f442005-03-29 20:48:41 +0000176 return new VirtualFolder(getComponentHandle(), getRuntimePath().removeLastSegments(1));
vbhadrir35da0ba2005-06-27 20:02:56 +0000177 return null;
cbridghaaf88ca62005-06-24 19:00:16 +0000178 }
cbridgha1686f442005-03-29 20:48:41 +0000179
180 public IProject getProject() {
181 return getComponentHandle().getProject();
182 }
cbridgha6887f4d2005-03-23 15:10:33 +0000183
184 public boolean isAccessible() {
185 throw new UnsupportedOperationException("Method not supported"); //$NON-NLS-1$
cbridghaaf88ca62005-06-24 19:00:16 +0000186 // return false;
187 }
188
cbridgha6887f4d2005-03-23 15:10:33 +0000189 public Object getAdapter(Class adapter) {
190 throw new UnsupportedOperationException("Method not supported"); //$NON-NLS-1$
cbridghaaf88ca62005-06-24 19:00:16 +0000191 // return null;
cbridgha6887f4d2005-03-23 15:10:33 +0000192 }
193
194 public boolean contains(ISchedulingRule rule) {
195 throw new UnsupportedOperationException("Method not supported"); //$NON-NLS-1$
cbridghaaf88ca62005-06-24 19:00:16 +0000196 // return false;
cbridgha6887f4d2005-03-23 15:10:33 +0000197 }
198
199 public boolean isConflicting(ISchedulingRule rule) {
200 throw new UnsupportedOperationException("Method not supported"); //$NON-NLS-1$
cbridghaaf88ca62005-06-24 19:00:16 +0000201 // return false;
cbridgha6887f4d2005-03-23 15:10:33 +0000202 }
cbridghaaf88ca62005-06-24 19:00:16 +0000203
cbridgha6887f4d2005-03-23 15:10:33 +0000204 public String toString() {
cbridghaaf88ca62005-06-24 19:00:16 +0000205 if (toString == null)
206 toString = "[" + getComponentHandle() + ":" + getRuntimePath() + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
cbridgha6887f4d2005-03-23 15:10:33 +0000207 return toString;
208 }
cbridghaaf88ca62005-06-24 19:00:16 +0000209
cbridgha6887f4d2005-03-23 15:10:33 +0000210 public int hashCode() {
cbridghaaf88ca62005-06-24 19:00:16 +0000211 if (hashCode == 0)
cbridgha6887f4d2005-03-23 15:10:33 +0000212 hashCode = toString().hashCode();
213 return hashCode;
214 }
cbridghaaf88ca62005-06-24 19:00:16 +0000215
cbridgha6887f4d2005-03-23 15:10:33 +0000216 public boolean equals(Object anOther) {
cbridghaaf88ca62005-06-24 19:00:16 +0000217 return hashCode() == ((anOther != null && anOther instanceof VirtualResource) ? anOther.hashCode() : 0);
cbridgha4da5ee72005-04-14 14:27:50 +0000218 }
cbridghaaf88ca62005-06-24 19:00:16 +0000219
220 public void setResourceType(String aResourceType) {
221 resourceType = aResourceType;
222 StructureEdit moduleCore = null;
223 try {
224 moduleCore = StructureEdit.getStructureEditForRead(getProject());
225 WorkbenchComponent component = moduleCore.findComponentByName(getComponentHandle().getName());
226 ComponentResource[] resources = component.findResourcesByRuntimePath(getRuntimePath());
227 for (int i = 0; i < resources.length; i++) {
228 resources[i].setResourceType(aResourceType);
229 }
230 } finally {
231 if (moduleCore != null) {
232 moduleCore.dispose();
233 }
234 }
235 }
236
237 // TODO Fetch the resource type from the model.
cbridgha4da5ee72005-04-14 14:27:50 +0000238 public String getResourceType() {
cbridghaaf88ca62005-06-24 19:00:16 +0000239 if (null == resourceType) {
240 StructureEdit moduleCore = null;
241 try {
242 moduleCore = StructureEdit.getStructureEditForRead(getProject());
243 WorkbenchComponent component = moduleCore.findComponentByName(getComponentHandle().getName());
244 ComponentResource[] resources = component.findResourcesByRuntimePath(getRuntimePath());
245 for (int i = 0; i < resources.length; i++) {
246 resourceType = resources[i].getResourceType();
247 return resourceType;
248 }
249 } finally {
250 if (moduleCore != null) {
251 moduleCore.dispose();
252 }
253 }
254 }
255 resourceType = "";
cbridgha4da5ee72005-04-14 14:27:50 +0000256 return resourceType;
257 }
cbridgha17f4e1d2005-03-28 19:41:04 +0000258
cbridgha523a94a2005-05-20 16:40:44 +0000259 public ComponentHandle getComponentHandle() {
cbridgha17f4e1d2005-03-28 19:41:04 +0000260 return componentHandle;
cbridghaaf88ca62005-06-24 19:00:16 +0000261 }
262
cbridgha25501202005-03-30 23:45:41 +0000263 protected void createResource(IContainer resource, int updateFlags, IProgressMonitor monitor) throws CoreException {
cbridgha1686f442005-03-29 20:48:41 +0000264
cbridghaaf88ca62005-06-24 19:00:16 +0000265 if (resource.exists())
vbhadrird3841182005-06-07 17:57:08 +0000266 return;
cbridgha25501202005-03-30 23:45:41 +0000267 if (!resource.getParent().exists())
268 createResource(resource.getParent(), updateFlags, monitor);
cbridghaaf88ca62005-06-24 19:00:16 +0000269 if (!resource.exists() && resource.getType() == IResource.FOLDER) {
cbridgha25501202005-03-30 23:45:41 +0000270 ((IFolder) resource).create(updateFlags, true, monitor);
cbridghaaf88ca62005-06-24 19:00:16 +0000271 }
cbridgha25501202005-03-30 23:45:41 +0000272 }
273
274 protected boolean isPotentalMatch(IPath aRuntimePath) {
cbridgha1686f442005-03-29 20:48:41 +0000275 return aRuntimePath.isPrefixOf(getRuntimePath());
276 }
cbridghaaf88ca62005-06-24 19:00:16 +0000277
cbridgha6887f4d2005-03-23 15:10:33 +0000278}