Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to accessing resources on the classpath
How to accessing resources on the classpath [message #1175331] Thu, 07 November 2013 17:40 Go to next message
Dieter Bogdoll is currently offline Dieter BogdollFriend
Messages: 5
Registered: November 2013
Junior Member
I have written a grammar with xtext and imported this as a plugin into a eclipse installation. A java project inside eclipse uses this plugin to edit a file
(using the generated grammar editor).

I would like to access the classpath of this Java project for validating the grammar (not the classpath of the grammar plugin). I need to resources which might be files or classes.

How would this work from inside of xtext?

Thanks,
Dieter
Re: How to accessing resources on the classpath [message #1175365 is a reply to message #1175331] Thu, 07 November 2013 18:08 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13525
Registered: July 2009
Senior Member
Hi you can ask a xtextresource for its classpathuricontext

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to accessing resources on the classpath [message #1175414 is a reply to message #1175365] Thu, 07 November 2013 18:54 Go to previous messageGo to next message
Dieter Bogdoll is currently offline Dieter BogdollFriend
Messages: 5
Registered: November 2013
Junior Member
Hi Christian,
I'm fairly new to xtext, could you be a bit more specific. Smile

E.g. here is my validation method:
@Check 
def checkIfBuildersExists(BuilderName aBuilderName) {
    println("Does '"+aBuilderName.name+"' exists, please check ...")
}


Whereas 'BuilderName' extends (indirectly) org.eclipse.emf.ecore.EObject and the class in which the check-method extends org.eclipse.xtext.validation.AbstractDeclarativeValidator.

So how do I access the xtextresource?
Re: How to accessing resources on the classpath [message #1175436 is a reply to message #1175414] Thu, 07 November 2013 19:12 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13525
Registered: July 2009
Senior Member
Hi,

you can ask an eObject for its eResource. this may be an XtextResource.

Btw is there a specific thing you want to do (you did not go into detail on that)


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to accessing resources on the classpath [message #1175450 is a reply to message #1175436] Thu, 07 November 2013 19:24 Go to previous messageGo to next message
Dieter Bogdoll is currently offline Dieter BogdollFriend
Messages: 5
Registered: November 2013
Junior Member
The 'BuilderName' contains a string. This is either a full qualified name for a java class. In this case I want to check if this class exists on the classpath of the java project. Or it is a key of a property file which contains again a java full qualified name. This property file (or several) are somewhere on the classpath, they have a specific location but could reside within several jars.

Re: How to accessing resources on the classpath [message #1176432 is a reply to message #1175450] Fri, 08 November 2013 10:28 Go to previous messageGo to next message
Dieter Bogdoll is currently offline Dieter BogdollFriend
Messages: 5
Registered: November 2013
Junior Member
Here is some pseudo-code describing what I want to do. In the end, I think,
I need to know how to access the classLoader which is used my the java project
using my grammar ui.

private def getClassLoader() {
	// get magically the classpathloader <- thats what I need how to do it
}

private def Properties loadProps(URL urlToProperties) {
	// ...
}

private def verifyClassExists(String className) {
	classLoader.loadClass(className)!=null
}

private def String findClassNameFor(String aName) {
	classLoader.getResources("builderNames.properties").mapFirst[ 
		// find the first element where I can apply the map and return the result
		val props = loadProps(it)
		props.getProperty(aName);
	]
}

@Check 
def checkIfBuildersExists(BuilderName aBuilderName) {
	val className = findClassNameFor(aBuilderName.name) ?:  aBuilderName.name
	if(!verifyClassExists(className)) {
		error("Builder '"+aBuilderName.name+"' does not exist", aBuilderName, OmniPropertiesPackage.Literals.BUILDER__NAME)	
	}
}
Re: How to accessing resources on the classpath [message #1178551 is a reply to message #1176432] Sat, 09 November 2013 18:46 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 1823
Registered: July 2009
Senior Member
I haven't tested but you should be able to use the classpath URI scheme here.

Here's some Java code, that should help get you started:

ResourceSet rs = someEObject.eResource().getResourceSet();
URI classPathURI = URI.createURI("classpath:/"+path);
if (rs.getURIConverter().exists(classPathURI, null)) {
InputStream contents = rs.getURIConverter().createInputStream(classPathURI);
//. ...
}
Re: How to accessing resources on the classpath [message #1179517 is a reply to message #1178551] Sun, 10 November 2013 10:28 Go to previous message
Dieter Bogdoll is currently offline Dieter BogdollFriend
Messages: 5
Registered: November 2013
Junior Member
Sven Efftinge wrote on Sat, 09 November 2013 13:46


ResourceSet rs = someEObject.eResource().getResourceSet();
URI classPathURI = URI.createURI("classpath:/"+path);
if (rs.getURIConverter().exists(classPathURI, null)) {
InputStream contents = rs.getURIConverter().createInputStream(classPathURI);
//. ...
}


Thanks, for this code fragment. I was able to load a non java resource with it.
I were not able to get a java resource, I then always get an exception.

But even the non java resource is not fully what I want. E.g. you could have
several jars on your classpath and each jar can contain the same named resource.
With getClass().getClassloader().getResources("resourcename") you get back
an Enumeration of all files in the jars. And that is what I would need here.
Also the loading of a java class would be relevant for me.

Is there no other xtext Project which needs to access the classpath to verify
the correctness of the text to translate?
Previous Topic:Editor cannot be initialized/Unresolved proxy
Next Topic:Error when trying to Export to Eclipse product
Goto Forum:
  


Current Time: Mon Aug 10 16:59:00 GMT 2020

Powered by FUDForum. Page generated in 0.02225 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top