org.openxma.dsl.common.jdt.JdtXTextResourceSetUtils Maven / Gradle / Ivy
package org.openxma.dsl.common.jdt;
import java.util.List;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.ContentHandler;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.xtext.resource.ClasspathUriUtil;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.eclipse.xtext.ui.util.JdtClasspathUriResolver;
import at.spardat.xma.guidesign.XMAComponent;
import at.spardat.xma.guidesign.plugin.GUIDesignerPlugin;
/**
* Utility to operate with XMAComponents, XTextResourceSets and Jdt
*
*/
public class JdtXTextResourceSetUtils {
/**
* Creates a XTextResourceSet for a IProject
*
* @param project
* @return
*/
public static XtextResourceSet createXTextResourceSetForProject(IProject project) {
XtextResourceSet xtextResourceSet = new XtextResourceSet();
xtextResourceSet.setClasspathUriResolver(new JdtClasspathUriResolver());
xtextResourceSet.setClasspathURIContext(JavaCore.create(project));
return xtextResourceSet;
}
/**
* Get the path for a resource relative to the XMA-sourcefolder.
*
* @param xmaFile
* @return
*/
public static String getSourceFolderRelativePath(IResource xmaFile) {
String projectRelativePath = xmaFile.getProjectRelativePath().toString();
String sourceFolder = GUIDesignerPlugin.getPlugin().getJavaSourceFolder(xmaFile.getProject());
sourceFolder = removeTrailingSlash(sourceFolder);
if (projectRelativePath.length() > sourceFolder.length() && projectRelativePath.indexOf(sourceFolder) == 0) {
return projectRelativePath.substring(sourceFolder.length()+1);
}
return null;
}
/**
* Get the folder-path for a resource relative to the XMA-sourcefolder.
*
* @param resource
* @return
*/
public static String getSourceFolderRelativeFolderPath(IResource resource) {
IContainer container = (resource instanceof IContainer) ? (IContainer)resource : resource.getParent();
return getSourceFolderRelativePath(container);
}
public static String removeTrailingSlash(String string) {
if (string.endsWith("/")) {
return string.substring(0,string.length()-1);
}
return string;
}
/**
* Get the classpath uri for a XMA-file.
*
* @param xmaFile
* @return
*/
public static String getClassPathUriForXMAFile(IFile xmaFile) {
String sourceFolderRelativePath = getSourceFolderRelativePath(xmaFile);
return ClasspathUriUtil.CLASSPATH_SCHEME + ":/" + sourceFolderRelativePath;
}
/**
* Get the resource for a XMA-file in a given ResourceSet.
*
* @param resourceSet
* @param xmaFile
* @return
*/
public static Resource getResourceForXMAFile(ResourceSet resourceSet, IFile xmaFile) {
String uriString = getClassPathUriForXMAFile(xmaFile);
return resourceSet.getResource(URI.createURI(uriString), true);
}
/**
* Get the resource for a XMA-file within a new created XTextResourceSet
*
* @param xmaFile
* @return
*/
public static Resource getResourceForXMAFile(IFile xmaFile) {
XtextResourceSet xtextResourceSet = createXTextResourceSetForProject(xmaFile.getProject());
return getResourceForXMAFile(xtextResourceSet,xmaFile);
}
/**
* Get the XMAComponent from a Resource.
*
* @param xmaResource
* @return null in case there is no XMAComponent in the Resource
*/
public static XMAComponent getXMAComponentForResource(Resource xmaResource) {
if (xmaResource == null) {
throw new IllegalArgumentException("The XMA resource must not be null");
}
List content = xmaResource.getContents();
if (content != null && content.size()>0) {
EObject firstElement = xmaResource.getContents().get(0);
if (firstElement instanceof XMAComponent) {
return (XMAComponent)firstElement;
}
}
return null;
}
/**
* Get the XMAComponent from a file.
*
* The resource for the file is created within a XTextResource.
*
* @param xmaFile
* @return null in case there is no XMAComponent in the file
*/
public static XMAComponent getXMAComponent(IFile xmaFile) {
Resource xmaResource = getResourceForXMAFile(xmaFile);
return getXMAComponentForResource(xmaResource);
}
/**
* Create a new XtextResource in a given XtextResourceSet
*
* @param xtextResourceSet
* @param file
* @return
*/
public static XtextResource createXtextResource(XtextResourceSet xtextResourceSet, IFile file) {
URI pmlFileURI = URI.createPlatformResourceURI(file.getFullPath().toString(),true);
return (XtextResource) xtextResourceSet.createResource(pmlFileURI, ContentHandler.UNSPECIFIED_CONTENT_TYPE);
}
/**
* Create a new XtextResource in a new XtextResourceSet
*
* @param file
* @return
*/
public static XtextResource createXtextResource(IFile file) {
XtextResourceSet xtextResourceSet = JdtXTextResourceSetUtils.createXTextResourceSetForProject(file.getProject());
return createXtextResource(xtextResourceSet,file);
}
/**
* Create a new standard emf Resource in a new standard emf ResourceSet.
*
* @param file
* @return
*/
public static Resource createStandardEmfResource(IFile file) {
URI xmaFileURI = URI.createPlatformResourceURI(file.getFullPath().toString(),true);
ResourceSet resourceSet = new ResourceSetImpl();
return resourceSet.createResource(xmaFileURI);
}
}