Skip to main content
summaryrefslogtreecommitdiffstats
blob: f433e9f85c1c0758e623ef9f5fae7ea2c67290f3 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*******************************************************************************
 * Copyright (c) 2004, 2006 Sybase, Inc. and others.
 *
 * 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:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsf.facesconfig.ui.util;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;


/**
 * This utility class is used to make a bridge between the relative path from
 * webroot and physical path in the project.
 * 
 * @author Xiao-guang Zhang
 * 
 */
public class WebrootUtil extends
            org.eclipse.jst.jsf.common.ui.internal.utils.WebrootUtil {

	/**
	 * get the the project path for webpath The project path is something like
	 * "/projectname/webroot/filename.jsp", or "/projectname/webroot/folder".
	 * The project information should be removed from project path, e.g,
	 * "/filename.jsp" or "/folder/*";
	 * 
	 * @param strPath -
	 *            the web path
	 * @return - project path from "/projectname/webroot"
	 */
	static public String getProjectPath(EObject model, String strPath) {

		IProject project = getProject(model);

		String strProjectPath = "";
		if (strPath != null) {
			IPath path = new Path(strPath);
			// jsp file
			if (path.getFileExtension() != null) {
				IPath webContentPath = getWebContentPath(project);
				if (webContentPath != null)
					strProjectPath = webContentPath.toString() + strPath;

			} else
			// jsp folder
			{
				strPath = new String(strPath.getBytes(), 0,
						strPath.length() - 1);
				IPath webContentPath = getWebContentPath(project);
				if (webContentPath != null)
					strProjectPath = webContentPath.toString() + strPath;
			}
		}
		return strProjectPath;
	}

	/**
	 * get the current resource according to EMF model
	 * 
	 * @param model -
	 *            emf model
	 * @return
	 */
	static public IResource getResource(EObject model) {
		IResource resource = null;

		if (model != null && model.eResource() != null) {
			URI uri = model.eResource().getURI();
			IPath path = new Path(URI.decode(uri.devicePath()));

			// since the uri format is "platform:/resource/..."
			// we will remove the first part
			path = path.removeFirstSegments(1);

			IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace()
					.getRoot();
			resource = workspaceRoot.findMember(path);
		}

		return resource;
	}

	/**
	 * get the current project according to EMF model
	 * 
	 * @param model -
	 *            emf model
	 * @return
	 */
	static public IProject getProject(EObject model) {
		IProject project = null;

		IResource resource = getResource(model);

		if (resource != null) {
			project = resource.getProject();
		}

		return project;
	}
}

Back to the top