Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 104081ec572242c0fa37600539a14d1209170759 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*******************************************************************************
 *  Copyright (c) 2010  Oracle. All rights reserved.
 *  This program and the accompanying materials are made available under the
 *  terms of the Eclipse Public License v1.0, which accompanies this distribution
 *  and is available at http://www.eclipse.org/legal/epl-v10.html
 *  
 *  Contributors: 
 *  	Oracle - initial API and implementation
 *******************************************************************************/
package org.eclipse.jpt.common.core.internal.resource;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jpt.common.core.JptCommonCorePlugin;
import org.eclipse.jpt.common.core.internal.utility.PlatformTools;
import org.eclipse.wst.common.componentcore.ComponentCore;
import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;
import org.eclipse.wst.common.componentcore.resources.IVirtualFile;
import org.eclipse.wst.common.componentcore.resources.IVirtualFolder;
import org.eclipse.wst.common.componentcore.resources.IVirtualResource;

public class ModuleResourceLocator
		extends SimpleJavaResourceLocator {
	
	/**
	 * Return the folder representing the "META-INF" runtime location
	 */
	@Override
	public IContainer getDefaultResourceLocation(IProject project) {
		IVirtualComponent component = ComponentCore.createComponent(project);
		return component.getRootFolder().getFolder(META_INF_PATH).getUnderlyingFolder();
	}
	
	@Override
	public IPath getResourcePath(IProject project, IPath runtimePath) {
		IVirtualComponent component = ComponentCore.createComponent(project);
		return component.getRootFolder().getFile(runtimePath).getWorkspaceRelativePath();
	}
	
	@Override
	public IPath getRuntimePath(IProject project, IPath resourcePath) {
		IFile file = PlatformTools.getFile(resourcePath);
		IVirtualComponent component = ComponentCore.createComponent(project);
		IVirtualFolder root = component.getRootFolder();
		IVirtualFile vFile = findVirtualFile(root, file);
		if (vFile != null) {
			return vFile.getRuntimePath().makeRelative();
		}
		// couldn't find it.  try the super-case
		return super.getRuntimePath(project, resourcePath);
	}
	
	private IVirtualFile findVirtualFile(IVirtualFolder vFolder, IFile file) {
		try {
			for (IVirtualResource vResource : vFolder.members()) {
				if (vResource.getType() == IVirtualResource.FILE) {
					IVirtualFile vFile = (IVirtualFile) vResource;
					if (file.equals(vFile.getUnderlyingResource())) {
						return vFile;
					}
				}
				else if (vResource.getType() == IVirtualResource.FOLDER) {
					IVirtualFile vFile = findVirtualFile((IVirtualFolder) vResource, file);
					if (vFile != null) {
						return vFile;
					}
				}
			}
		}
		catch (CoreException ce) {
			// fall through
			JptCommonCorePlugin.log(ce);
		}
		return null;
	}
}

Back to the top