use of org.eclipse.xtext.resource.XtextResource in project dsl-devkit by dsldevkit.
the class AbstractXtextTestUtil method getResource.
/**
* Creates a resource with the given URI and content.
*
* @param uri
* to associate the model with
* @param content
* String representation of the create a resource from
* @return {@link XtextResource} created
* @throws IOException
* may be thrown when trying to load the given content
*/
protected final XtextResource getResource(final URI uri, final String content) throws IOException {
StringInputStream instanceStream = new StringInputStream(content);
XtextResourceSet rs = getResourceSet();
XtextResource resource = (XtextResource) rs.createResource(uri);
rs.getResources().add(resource);
resource.load(instanceStream, null);
EcoreUtil.resolveAll(resource);
return resource;
}
use of org.eclipse.xtext.resource.XtextResource in project dsl-devkit by dsldevkit.
the class ScopeLinkingService method getIncludedModel.
/**
* Returns the {@link ScopeModel#getIncludes() included scope model} corresponding to the given node from the node model.
*
* @param context
* context object
* @param node
* parser node corresponding to include reference
* @return referenced {@link ScopeModel} to include
*/
private List<EObject> getIncludedModel(final ScopeModel context, final INode node) {
// based on XtextLinkingService#getUsedGrammar()
try {
String scopeModelName = (String) valueConverterService.toValue("", grammarAccess.getDottedIDRule().getName(), node);
if (scopeModelName != null) {
final ResourceSet resourceSet = context.eResource().getResourceSet();
List<Resource> resources = resourceSet.getResources();
for (int i = 0; i < resources.size(); i++) {
Resource resource = resources.get(i);
EObject rootElement = null;
if (resource instanceof XtextResource && ((XtextResource) resource).getLanguageName().equals(languageName) && !resource.getContents().isEmpty()) {
rootElement = resource.getContents().get(0);
}
if (rootElement instanceof ScopeModel) {
ScopeModel otherModel = (ScopeModel) rootElement;
if (scopeModelName.equals(otherModel.getName())) {
return Collections.<EObject>singletonList(otherModel);
}
}
}
URI classpathURI = URI.createURI(ClasspathUriUtil.CLASSPATH_SCHEME + ":/" + scopeModelName.replace('.', '/') + "." + fileExtensionProvider.getPrimaryFileExtension());
final Resource resource = resourceSet.getResource(classpathURI, true);
if (!resource.getContents().isEmpty()) {
final ScopeModel otherModel = (ScopeModel) resource.getContents().get(0);
if (scopeModelName.equals(otherModel.getName())) {
return Collections.<EObject>singletonList(otherModel);
}
}
}
return Collections.emptyList();
} catch (ClasspathUriResolutionException e) {
LOG.warn("Cannot load included scope.", e);
return Collections.emptyList();
} catch (ValueConverterException e) {
LOG.warn("Cannot convert included scope name.", e);
return Collections.emptyList();
}
}
use of org.eclipse.xtext.resource.XtextResource in project dsl-devkit by dsldevkit.
the class AbstractFastLinkingService method getUsedGrammar.
/**
* Tries to find a grammar.
*
* @param resourceSet
* to use for loading
* @param grammarName
* qualified grammar name
* @return A singleton list containing the grammar, or an empty list if not found.
*/
protected List<EObject> getUsedGrammar(final ResourceSet resourceSet, final String grammarName) {
// copied from XtextLinkingService#getUsedGrammar()
try {
if (grammarName != null) {
List<Resource> resources = resourceSet.getResources();
for (int i = 0; i < resources.size(); i++) {
Resource resource = resources.get(i);
EObject rootElement = null;
if (resource instanceof XtextResource) {
IParseResult parseResult = ((XtextResource) resource).getParseResult();
if (parseResult != null) {
rootElement = parseResult.getRootASTElement();
}
} else if (!resource.getContents().isEmpty()) {
rootElement = resource.getContents().get(0);
}
if (rootElement instanceof Grammar) {
Grammar otherGrammar = (Grammar) rootElement;
if (grammarName.equals(otherGrammar.getName())) {
if (resource instanceof DerivedStateAwareResource) {
resource.getContents();
}
return Collections.<EObject>singletonList(otherGrammar);
}
}
}
// $NON-NLS-1$ //$NON-NLS-2$
URI classpathURI = URI.createURI(ClasspathUriUtil.CLASSPATH_SCHEME + ":/" + grammarName.replace('.', '/') + ".xtext");
URI normalizedURI = null;
if (resourceSet instanceof XtextResourceSet) {
XtextResourceSet set = (XtextResourceSet) resourceSet;
normalizedURI = set.getClasspathUriResolver().resolve(set.getClasspathURIContext(), classpathURI);
} else {
normalizedURI = resourceSet.getURIConverter().normalize(classpathURI);
}
final Resource resource = resourceSet.getResource(normalizedURI, true);
if (!resource.getContents().isEmpty()) {
final Grammar usedGrammar = (Grammar) resource.getContents().get(0);
if (grammarName.equals(usedGrammar.getName())) {
return Collections.<EObject>singletonList(usedGrammar);
}
}
}
return Collections.emptyList();
} catch (ClasspathUriResolutionException e) {
return Collections.emptyList();
} catch (ValueConverterException e) {
return Collections.emptyList();
}
}
use of org.eclipse.xtext.resource.XtextResource in project dsl-devkit by dsldevkit.
the class EObjectContentProvider method valuesForAttributes.
/**
* Retrieve the object's values for the given EAttributes.
*
* @param attributes
* the EAttributes
* @return List<Pair<EAttribute, Object>> the attribute+values - possibly containing null entries
*/
private List<AttributeValuePair> valuesForAttributes(final EList<EAttribute> attributes) {
final URI elementUri = xtextElementSelectionListener.getSelectedElementUri();
XtextEditor editor = xtextElementSelectionListener.getEditor();
if (editor != null && elementUri != null) {
return editor.getDocument().readOnly(new IUnitOfWork<List<AttributeValuePair>, XtextResource>() {
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
public List<AttributeValuePair> exec(final XtextResource state) throws Exception {
final EObject eObject = state.getEObject(elementUri.fragment());
List<AttributeValuePair> pairs = Lists.transform(attributes, new Function<EAttribute, AttributeValuePair>() {
public AttributeValuePair apply(final EAttribute from) {
return new AttributeValuePair(from, eObject.eGet(from));
}
});
return pairs;
}
});
}
return newArrayList();
}
use of org.eclipse.xtext.resource.XtextResource in project xtext-xtend by eclipse.
the class JavaDocTypeReferenceProviderTest method testComputation_3.
@Test
public void testComputation_3() {
try {
StringConcatenation _builder = new StringConcatenation();
_builder.append("package foo");
_builder.newLine();
_builder.newLine();
_builder.append("/**");
_builder.newLine();
_builder.append("* {@link java.util.ArrayList");
_builder.newLine();
_builder.append("*/");
_builder.newLine();
_builder.append("class Foo{}");
_builder.newLine();
final String input = _builder.toString();
Resource _eResource = this.clazz(input).eResource();
final XtextResource resource = ((XtextResource) _eResource);
final ICompositeNode rootNode = resource.getParseResult().getRootNode();
final List<ReplaceRegion> regions = this.javaDocTypeReferenceProvider.computeTypeRefRegions(rootNode);
Assert.assertEquals(0, regions.size());
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
Aggregations