Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Absolute file path
Absolute file path [message #716734] Thu, 18 August 2011 05:18 Go to next message
Eclipse UserFriend
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 07:28 Go to previous messageGo to next message
Eclipse UserFriend
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 05:28 Go to previous messageGo to next message
Eclipse UserFriend
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 06:20 Go to previous message
Eclipse UserFriend
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
Previous Topic:QualifiedName to a computed scope
Next Topic:How to generate code using Xtext and Xtend?
Goto Forum:
  


Current Time: Wed Jul 23 09:53:11 EDT 2025

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

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

Back to the top