Java Code Examples for org.eclipse.xtext.resource.XtextResourceSet#getResource()

The following examples show how to use org.eclipse.xtext.resource.XtextResourceSet#getResource() . 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: XtextFormatterTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testXtextFormatting() throws IOException {
	String path = getClass().getPackage().getName().replace('.', '/');
	URI u = URI.createURI("classpath:/" + path + "/XtextFormatterMessy.xtext");
	XtextResourceSet resourceSet = new XtextResourceSet();
	resourceSet.setClasspathURIContext(getClass());
	Resource r = resourceSet.getResource(u, true);
	// System.out.println(r.getWarnings());
	// System.out.println(r.getErrors());
	ByteArrayOutputStream formatted = new ByteArrayOutputStream();
	r.save(formatted, SaveOptions.newBuilder().format().getOptions().toOptionsMap());
	// System.out.println(EmfFormatter.listToStr(r.getContents()));
	// System.out.println(formatted.toString());

	URI expectedURI = URI.createURI("classpath:/" + path + "/XtextFormatterExpected.xtext");
	XtextResource expectedResource = (XtextResource) resourceSet.getResource(expectedURI, true);
	String expected = expectedResource.getParseResult().getRootNode().getText();
	assertEquals(expected.replaceAll(System.lineSeparator(), "\n"), 
	    formatted.toString().replaceAll(System.lineSeparator(), "\n"));
}
 
Example 2
Source File: FindReferencesTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testFindReferencesTypeLiteral() throws Exception {
	createFile("find.references.test/src/Test." + fileExtension, "com.acme.OtherwiseUnused::m()");
	waitForBuild();
	
	XtextResourceSet set = get(XtextResourceSet.class);
	set.setClasspathURIContext(JavaCore.create(project));
	Resource resource = set.getResource(URI.createPlatformResourceURI("find.references.test/src/Test." + fileExtension, true), true);

	// obtain reference to type
	XMemberFeatureCall expression = (XMemberFeatureCall) resource.getContents().get(0);
	JvmType lookup = (JvmType) ((XAbstractFeatureCall) expression.getMemberCallTarget()).getFeature();

	final MockAcceptor mockAcceptor = new MockAcceptor();
	mockAcceptor.expect(expression.getMemberCallTarget(), lookup, XABSTRACT_FEATURE_CALL__FEATURE);
	findReferencesTester.checkFindReferences(lookup, "Java References to com.acme.OtherwiseUnused", mockAcceptor);
}
 
Example 3
Source File: Main.java    From ArduinoML-kernel with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static void ArduinoML2xmi(String modelPath, String destinationPath) throws IOException{
       // register ArduinoML
	ResourceSet resources = new ResourceSetImpl();
    Map<String, Object> packageRegistry = resources.getPackageRegistry();
       packageRegistry.put(arduinoML.ArduinoMLPackage.eNS_URI, arduinoML.ArduinoMLPackage.eINSTANCE);
	
	// load ArduinoML dependencies 
	Injector injector = new ArduinoMLStandaloneSetup().createInjectorAndDoEMFRegistration();
	XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
	
	// load the dsl file and parse it
	URI uri = URI.createURI(modelPath);
	Resource xtextResource = resourceSet.getResource(uri, true);
	EcoreUtil.resolveAll(xtextResource);
	Resource xmiResource = resourceSet.createResource(URI.createURI(destinationPath));
	// add the root (often forgotten)
	xmiResource.getContents().add(xtextResource.getContents().get(0));
	
	xmiResource.save(null);
}
 
Example 4
Source File: BaseEPackageAccess.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
public static Resource loadResource(String string, XtextResourceSet resourceSet) {
	URI uri = URI.createURI(string);
	Resource resource;
	try {
		resource = resourceSet.getResource(uri, true);
		if (resource == null) {
			throw new IllegalArgumentException("Couldn't create resource for URI : " + uri);
		}
	} catch (Exception e) {
		throw new WrappedException(e);
	}
	EList<EObject> contents = resource.getContents();
	if (contents.size() < 1) {
		throw new IllegalStateException("loading classpath:" + string + " : Expected at least root element but found "
				+ contents.size());
	}
	return resource;
}
 
Example 5
Source File: ClusteringStorageAwareResourceLoader.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Execute the given operation in a clustered fashion.
 */
public <T> Iterable<T> executeClustered(Iterable<URI> uris, Function1<? super Resource, ? extends T> operation) {
	int loadedURIsCount = 0;
	Set<URI> sourceLevelURIs = new HashSet<>();
	List<Resource> resources = new ArrayList<>();
	List<T> result = new ArrayList<>();
	Iterator<URI> iter = uris.iterator();
	while (iter.hasNext()) {
		URI uri = iter.next();
		XtextResourceSet resourceSet = context.getResourceSet();
		if (!context.getClusteringPolicy().continueProcessing(resourceSet, uri, loadedURIsCount)) {
			FluentIterable.from(resources).transform(operation::apply).copyInto(result);
			clearResourceSet();
			resources.clear();
			loadedURIsCount = 0;
		}
		loadedURIsCount++;
		if (isSource(uri)) {
			sourceLevelURIs.add(uri);
			Resource existingResource = resourceSet.getResource(uri, false);
			if (existingResource instanceof StorageAwareResource) {
				if (((StorageAwareResource) existingResource).isLoadedFromStorage()) {
					existingResource.unload();
				}
			}
			SourceLevelURIsAdapter.setSourceLevelUrisWithoutCopy(resourceSet, sourceLevelURIs);
		}
		resources.add(resourceSet.getResource(uri, true));
	}
	FluentIterable.from(resources).transform(operation::apply).copyInto(result);
	return result;
}
 
Example 6
Source File: Bug266082Test.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testRedundantImport() {
	XtextResourceSet resourceSet = get(XtextResourceSet.class);
	resourceSet.setClasspathURIContext(getClass().getClassLoader());
	URI uri = URI.createURI("classpath:/org/eclipse/xtext/linking/03.importuritestlanguage");
	Resource res = resourceSet.getResource(uri, true);
	assertNotNull("res", res);
	EcoreUtil.resolveAll(res);
	assertFalse(res.getErrors().toString(), res.getErrors().isEmpty());
}
 
Example 7
Source File: Bug266082Test.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testCircularImports() {
	XtextResourceSet resourceSet = get(XtextResourceSet.class);
	resourceSet.setClasspathURIContext(getClass().getClassLoader());
	URI uri = URI.createURI("classpath:/org/eclipse/xtext/linking/01.importuritestlanguage");
	Resource res = resourceSet.getResource(uri, true);
	EcoreUtil.resolveAll(res);
	assertNotNull("res", res);
	assertFalse(res.getErrors().toString(), res.getErrors().isEmpty());
}
 
Example 8
Source File: Bug437669Test.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected Type getContext() {
	XtextResourceSet resourceSet = get(XtextResourceSet.class);
	resourceSet.setClasspathURIContext(getClass().getClassLoader());

	URI uri = URI.createURI("classpath:/org/eclipse/xtext/linking/02.importuritestlanguage");
	Resource resource = resourceSet.getResource(uri, true);
	Main main = (Main) resource.getContents().get(0);
	return main.getTypes().get(0);
}
 
Example 9
Source File: ToEcoreTrafoTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private XtextResource getResource(String uri) {
	XtextResourceSet set = get(XtextResourceSet.class);
	URI nsURI = URI.createURI(EcorePackage.eNS_URI);
	URI platformURI = URI.createURI("platform:/resource/org.eclipse.emf.ecore/model/Ecore.ecore");
	set.getURIConverter().getURIMap().put(platformURI, nsURI);
	set.getURIResourceMap().put(platformURI, set.getResource(nsURI, true));
	set.setClasspathURIContext(getClass().getClassLoader());
	// if(AllTests.isPluginContext) {
	// set.setClasspathUriResolver(new BundleClasspathUriResolver());
	// set.setClasspathURIContext(Activator.getDefault());
	// }
	XtextResource r = (XtextResource) set.getResource(URI.createURI(uri), true);
	return r;
}
 
Example 10
Source File: XtextScopingTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void setUp() throws Exception {
	super.setUp();
	with(XtextStandaloneSetup.class);
	XtextResourceSet resourceSet = get(XtextResourceSet.class);
	resourceSet.setClasspathURIContext(getClass().getClassLoader());
	Resource resource = resourceSet.getResource(
			URI.createURI("classpath:/org/eclipse/xtext/grammarinheritance/ConcreteTestLanguage.xtext"), true);
	grammar = (Grammar) resource.getContents().get(0);
}
 
Example 11
Source File: ElementFinderTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private AbstractGrammarElementFinder grammarFinder() {
	final String file = "classpath:/org/eclipse/xtext/generator/grammarAccess/ElementFinderTestLanguage.xtext";
	XtextResourceSet rs = get(XtextResourceSet.class);
	rs.setClasspathURIContext(getClass());
	final Resource r = rs.getResource(URI.createURI(file), true);
	return new AbstractGrammarElementFinder() {
		@Override
		public Grammar getGrammar() {
			return (Grammar) r.getContents().get(0);
		}
	};
}
 
Example 12
Source File: EMFGeneratorFragment2.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Create a clone of the original grammar. The clone will not refer to a node model.
 */
private Grammar cloneGrammarIntoNewResourceSet(final Grammar original) {
  final Resource originalResource = original.eResource();
  XtextResourceSet _xtextResourceSet = new XtextResourceSet();
  final XtextResourceSet clonedResourceSet = EcoreUtil2.<XtextResourceSet>clone(_xtextResourceSet, originalResource.getResourceSet());
  final Resource clonedResource = clonedResourceSet.getResource(originalResource.getURI(), false);
  EObject _head = IterableExtensions.<EObject>head(clonedResource.getContents());
  return ((Grammar) _head);
}
 
Example 13
Source File: XClusteringStorageAwareResourceLoader.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Execute the given operation in a clustered fashion.
 */
public <T> List<T> executeClustered(Iterable<URI> uris, Function1<? super Resource, ? extends T> operation) {
	int loadedURIsCount = 0;
	Set<URI> sourceLevelURIs = new HashSet<>();
	List<Resource> resources = new ArrayList<>();
	List<T> result = new ArrayList<>();
	Iterator<URI> iter = uris.iterator();
	while (iter.hasNext()) {
		URI uri = iter.next();
		XtextResourceSet resourceSet = context.getResourceSet();
		if (!context.getClusteringPolicy().continueProcessing(resourceSet, uri, loadedURIsCount)) {
			result.addAll(ListExtensions.map(resources, operation::apply));
			this.clearResourceSet();
			resources.clear();
			loadedURIsCount = 0;
		}
		loadedURIsCount++;
		if (this.isSource(uri)) {
			sourceLevelURIs.add(uri);
			Resource existingResource = resourceSet.getResource(uri, false);
			if (existingResource instanceof StorageAwareResource) {
				if (((StorageAwareResource) existingResource).isLoadedFromStorage()) {
					existingResource.unload();
				}
			}
			SourceLevelURIsAdapter.setSourceLevelUrisWithoutCopy(resourceSet, sourceLevelURIs);
		}
		resources.add(resourceSet.getResource(uri, true));
	}
	result.addAll(ListExtensions.map(resources, operation::apply));
	return result;
}
 
Example 14
Source File: IncrementalBuilder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected void unloadResource(URI uri) {
	XtextResourceSet resourceSet = request.getResourceSet();
	Resource resource = resourceSet.getResource(uri, false);
	if (resource != null) {
		resourceSet.getResources().remove(resource);
		// proxify
		resource.unload();
	}
}
 
Example 15
Source File: Bug266082Test.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public void _testRedundantImportLarge() {
	XtextResourceSet resourceSet = get(XtextResourceSet.class);
	resourceSet.setClasspathURIContext(getClass().getClassLoader());
	URI uri = URI.createURI("classpath:/org/eclipse/xtext/linking/04.importuritestlanguage");
	Resource res = resourceSet.getResource(uri, true);
	assertNotNull("res", res);
	EcoreUtil.resolveAll(res);
	assertFalse(res.getErrors().toString(), res.getErrors().isEmpty());
}
 
Example 16
Source File: Xtext2EcoreTransformerTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testEcoreReference_01() throws Exception {
	XtextResourceSet resourceSet = new XtextResourceSet();
	Map<URI, URI> uriMap = resourceSet.getURIConverter().getURIMap();
	uriMap.put(URI.createPlatformPluginURI("org.eclipse.xtext/", false), URI.createURI("classpath:/"));
	uriMap.put(URI.createPlatformPluginURI("org.eclipse.xtext.xbase/", false), URI.createURI("classpath:/"));
	uriMap.put(URI.createPlatformPluginURI("org.eclipse.xtext.common.types/", false), URI.createURI("classpath:/"));
	uriMap.put(URI.createPlatformPluginURI("org.eclipse.emf.ecore/", false), URI.createURI("classpath:/"));
	uriMap.put(URI.createPlatformPluginURI("org.eclipse.xtext.tests/src/", false), URI.createURI("classpath:/"));
	uriMap.put(URI.createPlatformResourceURI("org.eclipse.xtext.xbase/", false), URI.createURI("classpath:/"));
	uriMap.put(URI.createPlatformResourceURI("org.eclipse.emf.ecore/", false), URI.createURI("classpath:/"));
	uriMap.put(URI.createPlatformResourceURI("org.eclipse.xtext.common.types/", false),
			URI.createURI("classpath:/"));
	uriMap.put(URI.createPlatformResourceURI("org.eclipse.xtext.tests/src/", false), URI.createURI("classpath:/"));
	resourceSet.setClasspathURIContext(this.getClass());
	uriMap.put(URI.createURI("platform:/resource/org.eclipse.emf.ecore/model/Ecore.ecore"),
			URI.createURI("platform:/plugin/org.eclipse.emf.ecore/model/Ecore.ecore"));
	Assert.assertFalse(
			resourceSet.getResource(URI.createURI("platform:/plugin/org.eclipse.emf.ecore/model/Ecore.ecore"), true)
					.getContents().isEmpty());
	Assert.assertFalse(resourceSet.getResource(URI.createURI(
			"platform:/plugin/org.eclipse.xtext.tests/src/org/eclipse/xtext/metamodelreferencing/tests/EcorePerNsURI.ecore"),
			true).getContents().isEmpty());
	Assert.assertFalse(resourceSet.getResource(URI.createURI(
			"platform:/plugin/org.eclipse.xtext.tests/src/org/eclipse/xtext/metamodelreferencing/tests/EcorePerPlatformResource.ecore"),
			true).getContents().isEmpty());
	Assert.assertFalse(resourceSet.getResource(URI.createURI(
			"platform:/plugin/org.eclipse.xtext.tests/src/org/eclipse/xtext/metamodelreferencing/tests/EcorePerPlatformPlugin.ecore"),
			true).getContents().isEmpty());
	Resource resource = resourceSet.getResource(
			URI.createURI(
					"classpath:/org/eclipse/xtext/metamodelreferencing/tests/EcoreReferenceTestLanguage.xtext"),
			true);
	XtextResource xtextResource = (XtextResource) resource;
	Assert.assertTrue(Joiner.on("\n").join(xtextResource.getErrors()), xtextResource.getErrors().isEmpty());
}
 
Example 17
Source File: Bug378261Test.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testReplaceResourceURIs() {
	XtextResourceSet xtextResourceSet = get(XtextResourceSet.class);
	xtextResourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ecore", new EcoreResourceFactoryImpl());
	xtextResourceSet.setClasspathURIContext(this);
	Resource grammarResource = xtextResourceSet.getResource(URI.createURI("classpath:/org/eclipse/xtext/generator/grammarAccess/GrammarAccessTestLanguage.xtext"), true);
	EcoreUtil.resolveAll(grammarResource);
	Grammar grammar = (Grammar) grammarResource.getContents().get(0);
	EPackage ePackage = grammar.getMetamodelDeclarations().get(0).getEPackage();
	assertFalse(ePackage.eResource().getURI().equals(URI.createURI(ePackage.getNsURI())));
	fragment.replaceResourceURIsWithNsURIs(grammar, xtextResourceSet);
	assertEquals(ePackage.eResource().getURI(), URI.createURI(ePackage.getNsURI()));
}
 
Example 18
Source File: FragmentFakingEcoreResourceTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void setUp() throws Exception {
	super.setUp();
	XtextResourceSet set = new XtextResourceSet();
	set.setClasspathURIContext(getClass());
	Resource resource = set.getResource(URI.createURI("classpath:/org/eclipse/xtext/generator/grammarAccess/ametamodel.ecore"), true);
	metamodel = (EPackage) resource.getContents().get(0);
}
 
Example 19
Source File: SubPackageAwareGrammarAccessFragmentTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void setUp() throws Exception {
	super.setUp();
	XtextResourceSet set = new XtextResourceSet();
	set.setClasspathURIContext(getClass());
	Resource resource = set.getResource(URI.createURI("classpath:/org/eclipse/xtext/generator/grammarAccess/ametamodel.ecore"), true);
	metamodel = (EPackage) resource.getContents().get(0);
	fragment = new GrammarAccessFragment();
}
 
Example 20
Source File: XStatefulIncrementalBuilder.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/** Unload a resource with the given URI. */
protected void unloadResource(URI uri) {
	XtextResourceSet resourceSet = this.request.getResourceSet();
	Resource resource = resourceSet.getResource(uri, false);
	if (resource != null) {
		resourceSet.getResources().remove(resource);
		// proxify
		resource.unload();
	}
}