org.eclipse.emf.mwe.core.resources.ResourceLoader Java Examples

The following examples show how to use org.eclipse.emf.mwe.core.resources.ResourceLoader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProjectAwareXtendXtext2EcorePostProcessor.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected synchronized ResourceLoader getResourceLoader(GeneratedMetamodel metamodel) {
	if (resourceLoader != null)
		return resourceLoader;

	URI uri = metamodel.eResource().getURI();
	if (ClasspathUriUtil.isClasspathUri(uri)) {
		ResourceSet resourceSet = metamodel.eResource().getResourceSet();
		uri = resourceSet.getURIConverter().normalize(uri);
	}
	IFile grammarFile = getWorkspace().getRoot().getFile(new Path(uri.toPlatformString(true)));
	IJavaProject javaProject = JavaCore.create(grammarFile.getProject());
	try {
		if (javaProject.exists()) {
			ClassLoader classLoader = createClassLoader(javaProject);
			resourceLoader = new ResourceLoaderImpl(classLoader);
			return resourceLoader;
		}
	} catch (CoreException e) {
		logger.error("Error creating execution context for java project '" + grammarFile.getProject().getName()
				+ "'", e);
	}
	return super.getResourceLoader(metamodel);
}
 
Example #2
Source File: XtendXtext2EcorePostProcessor.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void process(GeneratedMetamodel metamodel) {
	Resource xtendFile = loadXtendFile(metamodel);
	if (xtendFile != null) {
		logger.warn("You are using an old xtend(1)-based IXtext2EcorePostProcessor. This features is deprecated and will be dropped in a future release of Xtext.");
		ExecutionContext ctx = getExecutionContext(metamodel);
		ctx = ctx.cloneWithResource(xtendFile);
		ResourceLoader currentThreadResourceLoader = null;
		try {
			currentThreadResourceLoader = getCurrentThreadResourceLoader();
			setCurrentThreadResourceLoader(getResourceLoader(metamodel));
			final Object[] params = new Object[] { metamodel };
			Extension extension = ctx.getExtension("process", params);
			if (extension != null) {
				extension.evaluate(params, ctx);
			}
		}
		catch (EvaluationException e) {
			logger.error("Error encountered processing metamodel " + metamodel.getName() + " with "
					+ xtendFile.getFullyQualifiedName(), e);
		}
		finally {
			setCurrentThreadResourceLoader(currentThreadResourceLoader);
		}
	}
}
 
Example #3
Source File: GeneratorSupport.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Executes a given operation using a custom resource loader which will load resources using the classpath of the given project, provided that it is a Java
 * project.
 *
 * @param project
 *          context project, can also be {@code null}
 * @param runnable
 *          operation to run
 */
public void executeWithProjectResourceLoader(final IProject project, final Runnable runnable) {
  ResourceLoader oldResourceLoader = ResourceLoaderFactory.getCurrentThreadResourceLoader();
  ResourceLoader resourceLoader = createResourceLoader(project);
  try {
    ResourceLoaderFactory.setCurrentThreadResourceLoader(resourceLoader);
    runnable.run();
  } finally {
    ResourceLoaderFactory.setCurrentThreadResourceLoader(oldResourceLoader);
    if (resourceLoader instanceof CustomResourceLoader) {
      ((CustomResourceLoader) resourceLoader).close();
    }
  }
}
 
Example #4
Source File: GeneratorSupport.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
private ResourceLoader createResourceLoader(final IProject project) {
  if (project != null) {
    IJavaProject javaProject = JavaCore.create(project);
    if (javaProject != null) {
      IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
      try {
        IClasspathEntry[] classPathEntries = javaProject.getResolvedClasspath(true);
        URL[] urls = new URL[classPathEntries.length];
        for (int i = 0; i < classPathEntries.length; i++) {
          IClasspathEntry entry = classPathEntries[i];
          IPath path = null;
          switch (entry.getEntryKind()) {
          case IClasspathEntry.CPE_PROJECT:
            IJavaProject requiredProject = JavaCore.create((IProject) workspaceRoot.findMember(entry.getPath()));
            if (requiredProject != null) {
              path = workspaceRoot.findMember(requiredProject.getOutputLocation()).getLocation();
            }
            break;
          case IClasspathEntry.CPE_SOURCE:
            path = workspaceRoot.findMember(entry.getPath()).getLocation();
            break;
          default:
            path = entry.getPath();
            break;
          }
          if (path != null) {
            urls[i] = path.toFile().toURI().toURL();
          }
        }
        ClassLoader parentClassLoader = javaProject.getClass().getClassLoader();
        URLClassLoader classLoader = new URLClassLoader(urls, parentClassLoader);
        return new CustomResourceLoader(classLoader);
      } catch (MalformedURLException | CoreException e) {
        LOG.warn("Failed to create class loader for project " + project.getName(), e);
      }
    }
  }
  return new ResourceLoaderImpl(GeneratorSupport.class.getClassLoader());
}
 
Example #5
Source File: XtendXtext2EcorePostProcessor.java    From xtext-core with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * @param metamodel
 *            the metamodel to augment
 * @return the resource loader used to load resources during the xtend processing
 */
protected ResourceLoader getResourceLoader(GeneratedMetamodel metamodel) {
	return ResourceLoaderFactory.createResourceLoader();
}