Java Code Examples for org.eclipse.xtext.resource.XtextResource#getURI()

The following examples show how to use org.eclipse.xtext.resource.XtextResource#getURI() . These examples are extracted from open source projects. 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
public static String getCompleteContent(XtextResource xr) throws IOException, UnsupportedEncodingException {
	XtextResourceSet resourceSet = (XtextResourceSet) xr.getResourceSet();
	URIConverter uriConverter = resourceSet.getURIConverter();
	URI uri = xr.getURI();
	String encoding = xr.getEncoding();

	InputStream inputStream = null;

	try {
		inputStream = uriConverter.createInputStream(uri);

		return getCompleteContent(encoding, inputStream);
	} finally {
		tryClose(inputStream, null);
	}
}
 
Example 2
/**
 * Return the visible containers given a resource.
 *
 * @param resource
 *          the resource. Must not be {@code null}
 * @return The list of visible containers.
 */
protected List<IContainer> getVisibleContainers(final XtextResource resource) {
  URI uri = resource.getURI();
  List<IContainer> result = null;
  final ResourceCache<URI, List<IContainer>> cache = CacheManager.getInstance().getOrCreateResourceCache("AbstractPolymorphicScopeProvider#visibleContainers", resource); //$NON-NLS-1$
  if (cache != null) {
    result = cache.get(uri);
    if (result != null) {
      return result;
    }
  }

  final IResourceServiceProvider resourceServiceProvider = resource.getResourceServiceProvider();
  final IResourceDescription.Manager descriptionManager = resourceServiceProvider.getResourceDescriptionManager();
  final IContainer.Manager containerManager = resourceServiceProvider.getContainerManager();

  final IResourceDescription description = descriptionManager.getResourceDescription(resource);
  final IResourceDescriptions resourceDescriptions = getResourceDescriptions(resource);
  result = containerManager.getVisibleContainers(description, resourceDescriptions);
  if (cache != null) {
    cache.set(uri, result);
  }
  return result;
}
 
Example 3
Source Project: n4js   File: N4JSASTUtils.java    License: Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Computes an MD5 hash from the given resource's source code (the actual source text), as stored in
 * {@link TModule#getAstMD5()}. Will fail with an exception if the given resource does not have a valid
 * {@link XtextResource#getParseResult() parse result}, as created by Xtext during parsing.
 */
public static String md5Hex(XtextResource resource) {
	final IParseResult parseResult = resource.getParseResult();
	final INode rootNode = parseResult != null ? parseResult.getRootNode() : null;
	final String source = rootNode != null ? rootNode.getText() : null;
	if (source == null) {
		throw new IllegalStateException("resource does not have a valid parse result: " + resource.getURI());
	}
	return Hashing.murmur3_128(SEED).hashString(source, Charsets.UTF_8).toString();
}
 
Example 4
/**
 * Returns the {@link URI uri} of the associated {@link org.eclipse.emf.ecore.resource.Resource emf resource}.
 * May be null if no resource is available or its uri is <code>null</code>.
 * @return the resource uri if available.
 * @since 2.1
 */
@Override
public URI getResourceURI() {
	XtextResource resource = this.resource;
	if (resource != null)
		return resource.getURI();
	return null;
}
 
Example 5
protected IRenameElementContext createLocalRenameElementContext(EObject targetElement, XtextEditor editor,
		ITextSelection selection, XtextResource resource) {
	final URI targetElementURI = EcoreUtil2.getPlatformResourceOrNormalizedURI(targetElement);
	IRenameElementContext.Impl renameElementContext = new IRenameElementContext.Impl(targetElementURI,
			targetElement.eClass(), editor, selection, resource.getURI());
	return renameElementContext;
}
 
Example 6
protected IXtextDocument getDocument(final String currentModelToParse) {
	final XtextResource xtextResource = loadHelper.getResourceFor(new StringInputStream(Strings.emptyIfNull(currentModelToParse)));
	if (announceDirtyState) {
		dirtyResource = new IDirtyResource() {
			@Override
			public String getContents() {
				return currentModelToParse;
			}

			@Override
			public String getActualContents() {
				return currentModelToParse;
			}

			@Override
			public IResourceDescription getDescription() {
				return xtextResource.getResourceServiceProvider().getResourceDescriptionManager().getResourceDescription(xtextResource);
			}

			@Override
			public URI getURI() {
				return xtextResource.getURI();
			}
		};
		dirtyStateManager.manageDirtyState(dirtyResource);
	}
	return getDocument(xtextResource, currentModelToParse);
}