org.eclipse.xtext.resource.ClasspathUriUtil Java Examples

The following examples show how to use org.eclipse.xtext.resource.ClasspathUriUtil. 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: BuiltInSchemeRegistrar.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Convert the given n4scheme-URI to a classpath-URI.
 */
private URI toClasspathURI(URI uriWithN4Scheme) {
	String[] allSegments = new String[uriWithN4Scheme.segmentCount() + 1];
	allSegments[0] = "env";
	for (int i = 0; i < uriWithN4Scheme.segmentCount(); i++) {
		allSegments[i + 1] = uriWithN4Scheme.segment(i);
	}
	URI classpathURI = URI.createHierarchicalURI(
			ClasspathUriUtil.CLASSPATH_SCHEME,
			uriWithN4Scheme.authority(),
			uriWithN4Scheme.device(),
			allSegments,
			uriWithN4Scheme.query(),
			uriWithN4Scheme.fragment());
	return classpathURI;
}
 
Example #2
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 #3
Source File: BundleClasspathUriResolver.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public URI resolve(Object context, URI classpathUri) {
       if (context instanceof Plugin) {
           context = ((Plugin) context).getBundle();
       }
       if (!(context instanceof Bundle)) {
           throw new IllegalArgumentException("Context must implement Bundle");
       }
       Bundle bundle = (Bundle) context;
       try {
           if (ClasspathUriUtil.isClasspathUri(classpathUri)) {
               URI result = findResourceInBundle(bundle, classpathUri);
				if (classpathUri.fragment() != null)
					result = result.appendFragment(classpathUri.fragment());
				return result;
           }
       } catch (Exception exc) {
           throw new ClasspathUriResolutionException(exc);
       }
       return classpathUri;
   }
 
Example #4
Source File: WorkspaceClasspathUriResolver.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public URI resolve(Object context, URI classpathUri) {
    if(!(context instanceof IResource)) {
        throw new IllegalArgumentException("Context must implement IResource");
    }
    IResource resource = (IResource) context;
    try {
        if (ClasspathUriUtil.isClasspathUri(classpathUri)) {
            IProject project = resource.getProject();
            IJavaProject javaProject = JavaCore.create(project);
            URI result = findResourceInWorkspace(javaProject, classpathUri);
	if (classpathUri.fragment() != null)
		result = result.appendFragment(classpathUri.fragment());
	return result;
        }
    } catch (Exception exc) {
        throw new ClasspathUriResolutionException(exc);
    }
    return classpathUri;
}
 
Example #5
Source File: JdtClasspathUriResolver.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public URI resolve(Object context, URI classpathUri) {
	if (!(context instanceof IJavaElement)) {
		throw new IllegalArgumentException("Context must implement IResource");
	}
	javaElement = (IJavaElement) context;
	try {
		if (ClasspathUriUtil.isClasspathUri(classpathUri)) {
			IJavaProject javaProject = javaElement.getJavaProject();
			URI result = findResourceInWorkspace(javaProject, classpathUri);
			if (classpathUri.fragment() != null)
				result = result.appendFragment(classpathUri.fragment());
			return result;
		}
	}
	catch (Exception exc) {
		throw new ClasspathUriResolutionException(exc);
	}
	return classpathUri;
}
 
Example #6
Source File: ClasspathBasedChecks.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Verifies that a given catalog file has the same name as the name given in the model.
 * Also verifies that the given package exists and that the file is in that package.
 * 
 * @param catalog
 *          a check catalog
 */
@Check
public void checkFileNamingConventions(final CheckCatalog catalog) {
  Resource resource = catalog.eResource();
  URI resourceURI = resource.getURI();
  String packageName = catalog.getPackageName();
  StringBuilder classpathURIBuilder = new StringBuilder(ClasspathUriUtil.CLASSPATH_SCHEME);
  classpathURIBuilder.append(":/");
  if (packageName != null) {
    classpathURIBuilder.append(packageName.replace(DOT, SLASH)).append(SLASH);
  }
  classpathURIBuilder.append(resourceURI.lastSegment());
  URI classpathURI = URI.createURI(classpathURIBuilder.toString());
  URIConverter uriConverter = resource.getResourceSet().getURIConverter();
  try {
    URI normalizedClasspathURI = uriConverter.normalize(classpathURI);
    URI normalizedResourceURI = uriConverter.normalize(resourceURI);
    // Must normalize both URIs... however, pre-Xtext 2.4.3 we only normalized the classpath URI, and it worked?!
    // Just to be sure we don't break anything, leave that earlier behavior in.
    if (!normalizedResourceURI.equals(normalizedClasspathURI) && !resourceURI.equals(normalizedClasspathURI)) {
      reportInvalidPackage(catalog, packageName, null);
    }
  } catch (ClasspathUriResolutionException e) {
    reportInvalidPackage(catalog, packageName, null);
  }
  String catalogName = catalog.getName();
  if (catalogName != null && !equal(resourceURI.trimFileExtension().lastSegment(), catalogName)) {
    error("The catalog '" + (packageName != null ? notNull(packageName) + DOT : "") + catalogName + "' must be defined in its own file", catalog, CheckPackage.Literals.CHECK_CATALOG__NAME, ValidationMessageAcceptor.INSIGNIFICANT_INDEX, IssueCodes.WRONG_FILE);
  }
}