Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Resource doesn't exist
Resource doesn't exist [message #495391] Wed, 04 November 2009 17:14 Go to next message
Eclipse UserFriend
Originally posted by: formatzeh.gmx.de

I want to open editor with a given model file as input. I do this
programmatically because I don't know the meta-model before. But in the
following line I get an exception:

org.eclipse.emf.ecore.resource.Resource loadedResource =
resourceSet.getResource(uri, true);

The exception is a ResourceException saying that the specified file
can't be found. The uri will be created from an IFile object and does
exist because the file will be created just before intending to open it.
Does anybody has an idea or isn't it an EMF concern?
Re: Resource doesn't exist [message #495402 is a reply to message #495391] Wed, 04 November 2009 17:30 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
Gilbert,

Comments below.

Gilbert Mirenque wrote:
> I want to open editor with a given model file as input. I do this
> programmatically because I don't know the meta-model before. But in the
> following line I get an exception:
>
> org.eclipse.emf.ecore.resource.Resource loadedResource =
> resourceSet.getResource(uri, true);
>
> The exception is a ResourceException saying that the specified file
> can't be found. The uri will be created from an IFile object and does
> exist because the file will be created just before intending to open it.
> Does anybody has an idea or isn't it an EMF concern?
>
You should use resourceSet.createResource if you're creating something new.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Resource doesn't exist [message #495534 is a reply to message #495402] Thu, 05 November 2009 09:38 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: formatzeh.gmx.de

Hi Ed,
> You should use resourceSet.createResource if you're creating something new.
I looked into the sources of ResourceSetImpl.getResource and found out
that internally a new Resource will be created. The other point is that
I can't modify the code of the part where the resource will be loaded.
That's why I tried creating a new resource before, hoping that the
resource will be registered in the registry and found after that. But it
wasn't successfull. The following is the code of creating the new resource:

IPath path = getPathFromCodeFile(codeFile);
File file = new File(path.toOSString());
String[] parts = codeFile.getName().split("\\.");
String extension = parts[parts.length - 1];
IPath ext = path.addFileExtension(extension);
File tempFile = new File(ext.toOSString());
if(tempFile.exists()) {
boolean deleted = tempFile.delete();
System.out.println("deleted: " + deleted);
}
boolean created = tempFile.createNewFile();
if(created) {
FileOutputStream fos = new FileOutputStream(tempFile);
FileInputStream fis = new FileInputStream(file);
while(fis.available() > 0) {
fos.write(fis.read());
}
fis.close();
fos.close();
ResourceSet resourceSet = new ResourceSetImpl();
URI uri = URI.createFileURI(tempFile.getAbsolutePath());
resourceSet.createResource(uri);
Resource resource2 = resourceSet.getResource(uri, true);
IFile iFile = getFileFromPath(ext);
return iFile;
}

codeFile is a representation of files in my RCP app. Physically it has
no extension but it is represented as a normal file with extension in my
workspace. But as the editor needs an extension I create a copy of the
file just before trying to open it and add the appropriate extension
that the editor can open it. The editor internally calls

org.eclipse.emf.ecore.resource.Resource loadedResource =
resourceSet.getResource(uri, true);

And there the exception is thrown that the resource doesn't exist. But
when I do the same with resource2 in the code above no exception is
thrown. I don't understand this. Besides I don't know if it has
something to do with EMF or if it is a problem if I get in trouble with
the IFile semantics because the returned iFile will be passed to the
constructor of a new extension of FileEditorInput.
Re: Resource doesn't exist [message #495570 is a reply to message #495534] Thu, 05 November 2009 11:22 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------070306010001090405040200
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

Gilbert,

Comments below.

Gilbert Mirenque wrote:
> Hi Ed,
>
>> You should use resourceSet.createResource if you're creating something new.
>>
> I looked into the sources of ResourceSetImpl.getResource and found out
> that internally a new Resource will be created.
Yep.
> The other point is that
> I can't modify the code of the part where the resource will be loaded.
>
Why not?
> That's why I tried creating a new resource before, hoping that the
> resource will be registered in the registry and found after that. But it
> wasn't successfull. The following is the code of creating the new resource:
>
> IPath path = getPathFromCodeFile(codeFile);
> File file = new File(path.toOSString());
> String[] parts = codeFile.getName().split("\\.");
> String extension = parts[parts.length - 1];
> IPath ext = path.addFileExtension(extension);
> File tempFile = new File(ext.toOSString());
> if(tempFile.exists()) {
> boolean deleted = tempFile.delete();
> System.out.println("deleted: " + deleted);
> }
> boolean created = tempFile.createNewFile();
> if(created) {
> FileOutputStream fos = new FileOutputStream(tempFile);
> FileInputStream fis = new FileInputStream(file);
> while(fis.available() > 0) {
> fos.write(fis.read());
> }
> fis.close();
> fos.close();
> ResourceSet resourceSet = new ResourceSetImpl();
> URI uri = URI.createFileURI(tempFile.getAbsolutePath());
> resourceSet.createResource(uri);
> Resource resource2 = resourceSet.getResource(uri, true);
> IFile iFile = getFileFromPath(ext);
> return iFile;
> }
>
This all looks a bit tortuous. I'm not sure what you're trying to
accomplish.
> codeFile is a representation of files in my RCP app. Physically it has
> no extension but it is represented as a normal file with extension in my
> workspace. But as the editor needs an extension I create a copy of the
> file just before trying to open it and add the appropriate extension
> that the editor can open it. The editor internally calls
>
> org.eclipse.emf.ecore.resource.Resource loadedResource =
> resourceSet.getResource(uri, true);
>
> And there the exception is thrown that the resource doesn't exist. But
> when I do the same with resource2 in the code above no exception is
> thrown. I don't understand this. Besides I don't know if it has
> something to do with EMF or if it is a problem if I get in trouble with
> the IFile semantics because the returned iFile will be passed to the
> constructor of a new extension of FileEditorInput.
>
Keep in mind that creating a file in the file system doesn't
automatically update the workspace to know about it. You should be
using platform:/resource URIs for workspace access, not directly
accessing the file system, which will also result in the workspace not
being properly in sync and will bypass things like the workspace
history. If you want to ignore all that, you can always catch the
exception and call resourceSet.getResource(uri, false) to access the
resource that failed to load.

--------------070306010001090405040200
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Gilbert,<br>
<br>
Comments below.<br>
<br>
Gilbert Mirenque wrote:
<blockquote cite="mid:hcu6fp$44c$1@build.eclipse.org" type="cite">
<pre wrap="">Hi Ed,
</pre>
<blockquote type="cite">
<pre wrap="">You should use resourceSet.createResource if you're creating something new.
</pre>
</blockquote>
<pre wrap=""><!---->I looked into the sources of ResourceSetImpl.getResource and found out
that internally a new Resource will be created.</pre>
</blockquote>
Yep.<br>
<blockquote cite="mid:hcu6fp$44c$1@build.eclipse.org" type="cite">
<pre wrap=""> The other point is that
I can't modify the code of the part where the resource will be loaded.
</pre>
</blockquote>
Why not?<br>
<blockquote cite="mid:hcu6fp$44c$1@build.eclipse.org" type="cite">
<pre wrap="">That's why I tried creating a new resource before, hoping that the
resource will be registered in the registry and found after that. But it
wasn't successfull. The following is the code of creating the new resource:

IPath path = getPathFromCodeFile(codeFile);
File file = new File(path.toOSString());
String[] parts = codeFile.getName().split("\\.");
String extension = parts[parts.length - 1];
IPath ext = path.addFileExtension(extension);
File tempFile = new File(ext.toOSString());
if(tempFile.exists()) {
boolean deleted = tempFile.delete();
System.out.println("deleted: " + deleted);
}
boolean created = tempFile.createNewFile();
if(created) {
FileOutputStream fos = new FileOutputStream(tempFile);
FileInputStream fis = new FileInputStream(file);
while(fis.available() &gt; 0) {
fos.write(fis.read());
}
fis.close();
fos.close();
ResourceSet resourceSet = new ResourceSetImpl();
URI uri = URI.createFileURI(tempFile.getAbsolutePath());
resourceSet.createResource(uri);
Resource resource2 = resourceSet.getResource(uri, true);
IFile iFile = getFileFromPath(ext);
return iFile;
}
</pre>
</blockquote>
This all looks a bit tortuous. I'm not sure what you're trying to
accomplish. <br>
<blockquote cite="mid:hcu6fp$44c$1@build.eclipse.org" type="cite">
<pre wrap="">
codeFile is a representation of files in my RCP app. Physically it has
no extension but it is represented as a normal file with extension in my
workspace. But as the editor needs an extension I create a copy of the
file just before trying to open it and add the appropriate extension
that the editor can open it. The editor internally calls

org.eclipse.emf.ecore.resource.Resource loadedResource =
resourceSet.getResource(uri, true);

And there the exception is thrown that the resource doesn't exist. But
when I do the same with resource2 in the code above no exception is
thrown. I don't understand this. Besides I don't know if it has
something to do with EMF or if it is a problem if I get in trouble with
the IFile semantics because the returned iFile will be passed to the
constructor of a new extension of FileEditorInput.
</pre>
</blockquote>
Keep in mind that creating a file in the file system doesn't
automatically update the workspace to know about it.  You should be
using platform:/resource URIs for workspace access, not directly
accessing the file system, which will also result in the workspace not
being properly in sync and will bypass things like the workspace
history.  If you want to ignore all that, you can always catch the
exception and call resourceSet.getResource(uri, false) to access the
resource that failed to load.<br>
</body>
</html>

--------------070306010001090405040200--


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Resource doesn't exist [message #495680 is a reply to message #495570] Thu, 05 November 2009 18:01 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: formatzeh.gmx.de

Hi Ed,
now I found the copy function from the IResource interface and tried to
solve it that way because it is nearer to the Eclipse "way":

IPath path = getPathFromCodeFile(codeFile);
IFile original = getFileFromPath(path);
String[] parts = codeFile.getName().split("\\.");
String extension = parts[parts.length - 1];
IPath ext = path.addFileExtension(extension);
try {
original.copy(ext, true, null);
} catch (CoreException e) {
e.printStackTrace();
}
IFile iFile = getFileFromPath(ext);
return iFile;

But when invoking "copy" I get the exception that the colon : isn't
allowed. So I tried it relative to the workspace path of my RCP app but
then the "Resource doesn't exist" exception is thrown again.

> Keep in mind that creating a file in the file system doesn't
> automatically update the workspace to know about it. You should be
> using platform:/resource URIs for workspace access, not directly
> accessing the file system, which will also result in the workspace not
> being properly in sync and will bypass things like the workspace
> history. If you want to ignore all that, you can always catch the
> exception and call resourceSet.getResource(uri, false) to access the
> resource that failed to load.
>

So how can I do file operations with platform:/resource URI's? Or how
can I register a file in the workspace? Another strange fact is that the
second parameter of "copy" is the flag for controlling whether resources
that are not in sync with the local file system will be tolerated.
Re: Resource doesn't exist [message #495690 is a reply to message #495680] Thu, 05 November 2009 18:51 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: formatzeh.gmx.de

I now wrapped the copy operation into a WorkspaceModifyOperation. The
documentation says that this class should be used when the workspace
will be modified. But no success :( The same exception that the resource
doesn't exist.
Re: Resource doesn't exist [message #495702 is a reply to message #495680] Thu, 05 November 2009 19:33 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------030606010709050305010505
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

Gilbert,

Comments below.

Gilbert Mirenque wrote:
> Hi Ed,
> now I found the copy function from the IResource interface and tried to
> solve it that way because it is nearer to the Eclipse "way":
>
> IPath path = getPathFromCodeFile(codeFile);
> IFile original = getFileFromPath(path);
>
Of course I don't know what these do exactly.
> String[] parts = codeFile.getName().split("\\.");
> String extension = parts[parts.length - 1];
> IPath ext = path.addFileExtension(extension);
> try {
> original.copy(ext, true, null);
> } catch (CoreException e) {
> e.printStackTrace();
> }
> IFile iFile = getFileFromPath(ext);
> return iFile;
>
> But when invoking "copy" I get the exception that the colon : isn't
> allowed.
Sounds like you're using a OS-dependent file name where a workspace
relative path is perhaps required.
> So I tried it relative to the workspace path of my RCP app but
> then the "Resource doesn't exist" exception is thrown again.
>
>
>> Keep in mind that creating a file in the file system doesn't
>> automatically update the workspace to know about it. You should be
>> using platform:/resource URIs for workspace access, not directly
>> accessing the file system, which will also result in the workspace not
>> being properly in sync and will bypass things like the workspace
>> history. If you want to ignore all that, you can always catch the
>> exception and call resourceSet.getResource(uri, false) to access the
>> resource that failed to load.
>>
>>
>
> So how can I do file operations with platform:/resource URI's? Or how
> can I register a file in the workspace? Another strange fact is that the
> second parameter of "copy" is the flag for controlling whether resources
> that are not in sync with the local file system will be tolerated.
>
These aren't really EMF questions. Look carefully at the APIs you're
using and be sure to use workspace relative paths rather than
OS-dependent paths. Also look at the EMF FAQ for converting between
IFile/IResource and URIs used in Resources.

--------------030606010709050305010505
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Gilbert,<br>
<br>
Comments below.<br>
<br>
Gilbert Mirenque wrote:
<blockquote cite="mid:hcv3u6$uk9$1@build.eclipse.org" type="cite">
<pre wrap="">Hi Ed,
now I found the copy function from the IResource interface and tried to
solve it that way because it is nearer to the Eclipse "way":

IPath path = getPathFromCodeFile(codeFile);
IFile original = getFileFromPath(path);
</pre>
</blockquote>
Of course I don't know what these do exactly.<br>
<blockquote cite="mid:hcv3u6$uk9$1@build.eclipse.org" type="cite">
<pre wrap="">String[] parts = codeFile.getName().split("\\.");
String extension = parts[parts.length - 1];
IPath ext = path.addFileExtension(extension);
try {
original.copy(ext, true, null);
} catch (CoreException e) {
e.printStackTrace();
}
IFile iFile = getFileFromPath(ext);
return iFile;

But when invoking "copy" I get the exception that the colon : isn't
allowed. </pre>
</blockquote>
Sounds like you're using a OS-dependent file name where a workspace
relative path is perhaps required.<br>
<blockquote cite="mid:hcv3u6$uk9$1@build.eclipse.org" type="cite">
<pre wrap="">So I tried it relative to the workspace path of my RCP app but
then the "Resource doesn't exist" exception is thrown again.

</pre>
<blockquote type="cite">
<pre wrap="">Keep in mind that creating a file in the file system doesn't
automatically update the workspace to know about it. You should be
using platform:/resource URIs for workspace access, not directly
accessing the file system, which will also result in the workspace not
being properly in sync and will bypass things like the workspace
history. If you want to ignore all that, you can always catch the
exception and call resourceSet.getResource(uri, false) to access the
resource that failed to load.

</pre>
</blockquote>
<pre wrap=""><!---->
So how can I do file operations with platform:/resource URI's? Or how
can I register a file in the workspace? Another strange fact is that the
second parameter of "copy" is the flag for controlling whether resources
that are not in sync with the local file system will be tolerated.
</pre>
</blockquote>
These aren't really EMF questions.  Look carefully at the APIs you're
using and be sure to use workspace relative paths rather than
OS-dependent paths.  Also look at the EMF FAQ for converting between
IFile/IResource and URIs used in Resources.<br>
</body>
</html>

--------------030606010709050305010505--


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Hash codes for eObjects
Next Topic:Ecore model from a XML model - not a XML Schema!
Goto Forum:
  


Current Time: Fri Apr 26 03:12:27 GMT 2024

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

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

Back to the top