org.openxma.dsl.common.jdt.JdtProjectAndClassPathUtils Maven / Gradle / Ivy
package org.openxma.dsl.common.jdt;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.common.util.WrappedException;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.openxma.dsl.common.DslConstants;
/**
* @deprecated provides backward compatibilty to the formerly supported (and since version 3.6 deprecated) classpathUri
* mechanism
*/
public class JdtProjectAndClassPathUtils {
public static String RESOURCE_PREFIX = "/resource";
public static String getNamespace(IResource resource) {
return getNamespaceInternal(resource);
}
public static String getNamespace(URI plattformUri) {
if (plattformUri.toString().endsWith(DslConstants.STANDARD_LIBRARY)) {
return DslConstants.STANDARD_LIBRARY_NAMESPACE;
} else if (plattformUri.toString().endsWith(DslConstants.STANDARD_STYLES)) {
return DslConstants.STANDARD_STYLES_NAMESPACE;
} else if (plattformUri.scheme().equals(DslConstants.CLASSPATH_SCHEME)) {
String classPathUri = plattformUri.toString();
String packageName = classPathUri.substring(
DslConstants.CLASSPATH_SCHEME.length() + 2,
classPathUri.indexOf('/', DslConstants.CLASSPATH_SCHEME.length() + 2) != -1 ? classPathUri
.lastIndexOf('/') : classPathUri.length()).replace('/', '.');
return packageName;
} else if (!plattformUri.isPlatformResource()) {
throw new RuntimeException("Only platform or classpath scheme uris supported not '" + plattformUri + "'");
}
String platformString = plattformUri.toPlatformString(true);
IResource resource = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformString));
return getNamespaceInternal(resource);
}
private static String getNamespaceInternal(IResource resource) {
IProject project = resource.getProject();
IJavaProject javaProject = JavaCore.create(project);
try {
IPackageFragmentRoot[] packageFragmentRoots = javaProject.getPackageFragmentRoots();
for (int i = 0; i < packageFragmentRoots.length; i++) {
IPackageFragmentRoot packageFragmentRoot = packageFragmentRoots[i];
if (packageFragmentRoot.getKind() == IPackageFragmentRoot.K_SOURCE) {
if (packageFragmentRoot.getUnderlyingResource().contains(resource)) {
String workspacePath = resource.getFullPath().toString();
String packageFragementsPath = workspacePath.substring(packageFragmentRoot.getPath().toString()
.length() + 1);
int lastIndexOf = packageFragementsPath.lastIndexOf("/");
if (lastIndexOf != -1) {
return packageFragementsPath.substring(0, lastIndexOf).replace('/', '.');
}
return "";
}
}
}
} catch (Exception exception) {
throw new WrappedException(exception);
}
throw new IllegalStateException("Dsl file '" + resource + "' not found in build path.");
}
}