Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Absolute file path
Absolute file path [message #716734] Thu, 18 August 2011 09:18 Go to next message
Olaf  is currently offline Olaf Friend
Messages: 3
Registered: July 2011
Junior Member
Hi everyone!

How can I get the absolute path of an resource?
I want to parse a WSDL file in the same directory as my own DSL file but the WSDL parser I am forced to use only accepts an absolute file path.
In the classes Resource, ResourceSet, URI etc. I wasn't ably to find anything that returns an absolute path.

Olaf
Re: Absolute file path [message #716763 is a reply to message #716734] Thu, 18 August 2011 11:28 Go to previous messageGo to next message
Timo Missing name is currently offline Timo Missing nameFriend
Messages: 12
Registered: March 2011
Junior Member
I'm using the following method to get a java.io.File object from an URI object, perhaps you can reuse it.

private File getFile(URI uri) {

	if (uri != null) {

		/** to resolve this we need the workspace root */
		IWorkspaceRoot myWorkspaceRoot = ResourcesPlugin.getWorkspace()
				.getRoot();
		/** create an new IPath from the URI (decode blanks etc.) */
		IPath path = new Path(uri.toPlatformString(true));
		/** finally resolve the file with the workspace */
		IFile file = myWorkspaceRoot.getFile(path);

		if (file != null && file.getLocation() != null) {

			/** get java.io.File object */
			File f = file.getLocation().toFile();
			return f;
		}
	}
	return null;
}
Re: Absolute file path [message #717551 is a reply to message #716763] Sun, 21 August 2011 09:28 Go to previous messageGo to next message
Olaf  is currently offline Olaf Friend
Messages: 3
Registered: July 2011
Junior Member
Hi,

thank you, Timo, this will work for me!

But just as a matter of interest: Is there a way to get the absolute path without using the resources plugin? Xtext claims that it may be used without any eclipse. And there are for sure use cases where you need an absolute path.

Olaf
Re: Absolute file path [message #717558 is a reply to message #717551] Sun, 21 August 2011 10:20 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13876
Registered: July 2009
Senior Member
Hi,

why is using the resource plugin a problem???

can you elaborate a bit how you execute your stuff???
how do you get the xtext models?

Why don't you simply ask the models for a file string?
sure you may have to wrap this into a new File(….) to
get an absolute path. but this should be no problem.

public static void main(String[] args) throws IOException {
		Injector injector = new 
MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
		ResourceSet rs = injector.getInstance(ResourceSet.class);
		URI uri = URI.createURI("../org.xtext.example.mydsl/src/Test.mydsl");
		Resource r = rs.getResource(uri, true);
		Model m = (Model) r.getContents().get(0);
		String absolutePath = new 
File(m.eResource().getURI().toFileString()).getCanonicalPath();
		System.out.println(absolutePath);
	}


BTW. this is an EMF question and not an Xtext question.

~Christian

On 2011-08-21 09:28:50 +0000, Olaf said:

> Hi,
>
> thank you, Timo, this will work for me!
>
> But just as a matter of interest: Is there a way to get the absolute
> path without using the resources plugin? Xtext claims that it may be
> used without any eclipse. And there are for sure use cases where you
> need an absolute path.
>
> Olaf


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:QualifiedName to a computed scope
Next Topic:How to generate code using Xtext and Xtend?
Goto Forum:
  


Current Time: Sat Apr 10 23:22:46 GMT 2021

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

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

Back to the top