org.eclipse.xtext.resource.ClasspathUriResolutionException Java Examples

The following examples show how to use org.eclipse.xtext.resource.ClasspathUriResolutionException. 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: 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 #2
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 #3
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 #4
Source File: XtendXtext2EcorePostProcessor.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Expects an Xtend file named <code>MyDsl</code>PostProcessor.ext with an extension with signature
 * process(xtext::GeneratedMetamodel) in the same folder as the grammar file.
 * 
 * @param metamodel
 *            the metamodel to augment
 * @return the xtendFile to execute
 */
protected Resource loadXtendFile(GeneratedMetamodel metamodel) {
	if (xtendFile == null) {
		final String extension = getExtensionName(metamodel);
		try {
			URI uri = getXtendFileLocation(metamodel);
			if (uri != null) {
				URIConverter uriConverter = metamodel.eResource().getResourceSet().getURIConverter();
				if (uriConverter.exists(uri, null)) {
					InputStream in = uriConverter.createInputStream(uri);
					try {
						XtendResourceParser parser = new XtendResourceParser();
						xtendFile = parser.parse(new InputStreamReader(in), extension + '.'
								+ XtendFile.FILE_EXTENSION);
						fireXtendFileLoaded();
					} finally {
						if (in != null)
							in.close();
					}
				}
			}
		} catch (ClasspathUriResolutionException ignored) {
			// no xtend file found
		} catch (Exception e) {
			logger.error("Could not parse " + extension, e);
		}
	}
	return xtendFile;
}
 
Example #5
Source File: ImportUriGlobalScopeProvider.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public URI resolve(String uriAsString) throws IllegalArgumentException {
	URI uri = URI.createURI(uriAsString);
	if (uriResolver != null) {
		try {
			return uriResolver.resolve(uriContext, uri);
		} catch(ClasspathUriResolutionException e) {
			return uri;
		}
	}
	return uri;
}
 
Example #6
Source File: FormatFragmentUtil.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Retrieve the format model associated with a given grammar.
 * <p>
 * <em>Note</em>: Expected to either be in same folder with the same name (except for the extension) or in the SRC outlet.
 * </p>
 *
 * @param grammar
 *          the grammar, must not be {@code null}
 * @param context
 *          xpand execution context, must not be {@code null}
 * @return the format model, or {@code null} if the resource could not be loaded
 * @throws FileNotFoundException
 *           thrown if the format file could not be found
 */
@SuppressWarnings("PMD.NPathComplexity")
public static FormatConfiguration getFormatModel(final Grammar grammar, final XpandExecutionContext context) throws FileNotFoundException {
  Variable resourceUriVariable = context.getVariable("resourceUri");
  if (resourceUriVariable == null) {
    return null;
  }
  URI uri = (URI) resourceUriVariable.getValue();
  final Resource grammarResource = grammar.eResource();
  final ResourceSet resourceSet = grammarResource.getResourceSet();
  Resource formatResource = null;
  try {
    formatResource = resourceSet.getResource(uri, true);
  } catch (final ClasspathUriResolutionException e) {
    // make another attempt
    uri = getDefaultFormatLocation(grammar, context);
    try {
      formatResource = resourceSet.getResource(uri, true);
    } catch (WrappedException e1) {
      formatResource = resourceSet.getResource(uri, false);
      if (formatResource != null) {
        resourceSet.getResources().remove(formatResource);
      }
      throw new FileNotFoundException(uri.toString()); // NOPMD
    }
  }
  if (formatResource == null) {
    throw new FileNotFoundException(uri.toString());
  }

  final List<Issue> issues = getModelValidator().validate(formatResource, LOG);

  for (final Issue issue : issues) {
    if (issue.isSyntaxError() || issue.getSeverity() == Severity.ERROR) {
      throw new WorkflowInterruptedException("Errors found in " + uri.toString() + ": " + issue.getMessage());
    }
  }

  return formatResource.getContents().size() == 0 ? null : (FormatConfiguration) formatResource.getContents().get(0);
}
 
Example #7
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);
  }
}
 
Example #8
Source File: ExportFragment.java    From dsl-devkit with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Get the export model that we have to process.
 *
 * @param grammar
 *          The grammar
 * @return The model
 */
private synchronized ExportModel getModel(final Grammar grammar) { // NOPMD NPathComplexity by wth on 24.11.10 08:22
  if (modelLoaded) {
    return model;
  }
  modelLoaded = true;
  Resource resource = grammar.eResource();
  if (resource == null) {
    return null;
  }

  final ResourceSet resourceSet = resource.getResourceSet();
  URI uri = null;
  if (getExportFileURI() != null) {
    uri = URI.createURI(getExportFileURI());
  } else {
    uri = grammar.eResource().getURI().trimFileExtension().appendFileExtension(EXPORT_FILE_EXTENSION);
  }
  try {
    resource = resourceSet.getResource(uri, true);
    final List<Issue> issues = VALIDATOR.validate(resource, LOGGER);

    for (final Issue issue : issues) {
      if (issue.isSyntaxError() || issue.getSeverity() == Severity.ERROR) {
        throw new WorkflowInterruptedException(NLS.bind(Messages.ExportFragment_EXPORT_ERRORS, uri));
      }
    }
    if (resource.getContents().size() == 0) {
      return null;
    }
    model = (ExportModel) resource.getContents().get(0);
    return model;
  } catch (final ClasspathUriResolutionException e) {
    // Resource does not exist.
    if (getExportFileURI() != null) {
      // Explicit file specified, but not found: stop the workflow.
      throw new WorkflowInterruptedException(NLS.bind(Messages.ExportFragment_NO_EXPORT_FILE, uri)); // NOPMD PreserveStackTrace by wth on 24.11.10 08:27
    }
    // No file found at implicit location: work with a null model, generating code that implements the default behavior.
    return null;
  }
}