Skip to main content
summaryrefslogtreecommitdiffstats
blob: 116d9d2a3d5074e50b61e6248615b61819b3a27b (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*******************************************************************************
 * Copyright (c) 2005, 2007 committers of openArchitectureWare 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:
 *     committers of openArchitectureWare - initial API and implementation
 *******************************************************************************/
package org.eclipse.xtend.shared.ui.core.internal;

import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.debug.core.sourcelookup.containers.ZipEntryStorage;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.core.ExternalPackageFragmentRoot;
import org.eclipse.jdt.internal.core.JarPackageFragmentRoot;
import org.eclipse.xtend.shared.ui.internal.XtendLog;

public class JDTUtil {

	private static final Pattern patternNamespace = Pattern.compile("::");
	private static final Pattern patternSlash = Pattern.compile("/");

	/**
	 * find the path for the oaw name space and extension
	 * 
	 * @param project
	 *            - the javaproject
	 * @param oawns
	 *            - oaw name space (i.e. 'my::xtend::File')
	 * @param ext
	 *            - file extension (i.e. 'ext')
	 * @return
	 */
	@SuppressWarnings("restriction")
	public static IStorage findStorage(IJavaProject project, ResourceID id,
			boolean searchJars) {
		IPath p = path(id);
		try {
			IPackageFragmentRoot[] roots = project.getPackageFragmentRoots();
			for (int i = 0; i < roots.length; i++) {
				IPackageFragmentRoot root = roots[i];
				if (!root.isArchive()) {

					IFolder rootFolder = null;
					IResource correspondingResource = root
							.getCorrespondingResource();
					if (correspondingResource instanceof IFolder) {
						rootFolder = (IFolder) correspondingResource;
					} else if (root instanceof ExternalPackageFragmentRoot) {
						IResource resource = ((ExternalPackageFragmentRoot) root)
								.resource();
						if (resource instanceof IFolder) {
							rootFolder = (IFolder) resource;
							IStorage member = (IStorage) rootFolder.findMember(p);
							if(member != null) {
								return member;
							}
						}
					}
					if (rootFolder != null) {
						IResource r = project.getProject().findMember(
								rootFolder.getProjectRelativePath().append(p));
						if (r instanceof IFile)
							return (IFile) r;
					}
				} else if (searchJars) {
					IStorage storage = loadFromJar(id, root);
					if (storage != null)
						return storage;
				}
			}
		} catch (JavaModelException e) {
			XtendLog.logInfo(e);
		}
		return null;
	}

	@SuppressWarnings("restriction")
	public static IStorage loadFromJar(ResourceID id, IPackageFragmentRoot root)
			throws JavaModelException {
		if (root instanceof JarPackageFragmentRoot) {
			JarPackageFragmentRoot jar = (JarPackageFragmentRoot) root;
			ZipFile zipFile;
			try {
				zipFile = jar.getJar();
			} catch (CoreException e) {
				XtendLog.logError(e);
				return null;
			}
			ZipEntry entry = zipFile.getEntry(id.toFileName());
			if (entry != null && zipFile != null) {
				return new ZipEntryStorage(zipFile, entry);
			}
		}
		return null;
	}

	public static ResourceID findOawResourceID(IJavaProject project,
			IStorage file) {
		if (file == null)
			return null;
		try {
			IPackageFragmentRoot[] roots = project.getPackageFragmentRoots();
			for (int i = 0; i < roots.length; i++) {
				IPackageFragmentRoot root = roots[i];
				if (root.getPath().isPrefixOf(file.getFullPath())) {
					IPath shortOne = file.getFullPath().removeFirstSegments(
							root.getPath().segmentCount());
					String ns = shortOne.removeFileExtension().toString();
					ns = patternSlash.matcher(ns).replaceAll("::");
					return new ResourceID(ns, shortOne.getFileExtension());
				}
			}
		} catch (JavaModelException e1) {
			XtendLog.logInfo(e1);
		}
		return null;
	}

	public static IJavaProject getJProject(IStorage s) {
		if (s instanceof IFile) {
			return JavaCore.create(((IFile) s).getProject());
		} else {
			IProject[] projects = ResourcesPlugin.getWorkspace().getRoot()
					.getProjects();
			for (int i = 0; i < projects.length; i++) {
				IProject project = projects[i];
				IJavaProject p = JavaCore.create(project);
				if (p.exists()) {
					IPackageFragmentRoot[] roots;
					try {
						roots = p.getPackageFragmentRoots();
						for (int j = 0; j < roots.length; j++) {
							IPackageFragmentRoot root = roots[j];
							if (root.getPath().isPrefixOf(s.getFullPath()))
								return p;
						}
					} catch (JavaModelException e) {
						XtendLog.logError(e);
					}
				}
			}
		}
		return null;
	}

	private static IPath path(ResourceID id) {
		return new Path(patternNamespace.matcher(id.name).replaceAll("/") + "."
				+ id.extension);
	}

	public static String getQualifiedName(IStorage source) {
		ResourceID id = findOawResourceID(getJProject(source), source);
		if (id != null)
			return id.name;
		return null;
	}

}

Back to the top