How to create a VirtualFile

Hi,

how can I create a virtual file? I have the byte[] and the name/path
elemnt from any source and like to create a file (class source or byte
code or a simple tex file. The only things I found are all PSI based. Is
there another way to bring a file into the project? And the question is
how do I determine the possible source path(s)?

Sven

11 comments

Hi,
LocalFileSystem.getInstance().findFileByPath

"Sven Krause" <no.spam@public-files.de> wrote in message
news:bmlj9g$nap$1@is.intellij.net...

Hi,

>

how can I create a virtual file? I have the byte[] and the name/path
elemnt from any source and like to create a file (class source or byte
code or a simple tex file. The only things I found are all PSI based. Is
there another way to bring a file into the project? And the question is
how do I determine the possible source path(s)?

>

Sven

>


0

Alexey Efimov wrote:

Hi,
LocalFileSystem.getInstance().findFileByPath

Thanks, but it assumes the file within the source path.. and I don't
know how to determine the potential source path..

Sven


"Sven Krause" <no.spam@public-files.de> wrote in message
news:bmlj9g$nap$1@is.intellij.net...

>>Hi,
>>
>>how can I create a virtual file? I have the byte[] and the name/path
>>elemnt from any source and like to create a file (class source or byte
>>code or a simple tex file. The only things I found are all PSI based. Is
>>there another way to bring a file into the project? And the question is
>>how do I determine the possible source path(s)?
>>
>>Sven
>>



0

Oh, sorry, i just hink that you have name/path.
So, if you have file content - just implement VirtualFile interface, i think
it must be work.

"Sven Krause" <no.spam@public-files.de> wrote in message
news:bmlked$srr$1@is.intellij.net...

Alexey Efimov wrote:

Hi,
LocalFileSystem.getInstance().findFileByPath

Thanks, but it assumes the file within the source path.. and I don't
know how to determine the potential source path..

>

Sven

>

>

"Sven Krause" <no.spam@public-files.de> wrote in message
news:bmlj9g$nap$1@is.intellij.net...

>
>>Hi,
>>
>>how can I create a virtual file? I have the byte[] and the name/path
>>elemnt from any source and like to create a file (class source or byte
>>code or a simple tex file. The only things I found are all PSI based. Is
>>there another way to bring a file into the project? And the question is
>>how do I determine the possible source path(s)?
>>
>>Sven
>>
>
>
>

>


0

Alexey Efimov wrote:

Oh, sorry, i just hink that you have name/path.
So, if you have file content - just implement VirtualFile interface, i think
it must be work.


Thnak you, I will give them a try.

Sven


"Sven Krause" <no.spam@public-files.de> wrote in message
news:bmlked$srr$1@is.intellij.net...

>>Alexey Efimov wrote:
>>
>>>Hi,
>>>LocalFileSystem.getInstance().findFileByPath
>>
>>Thanks, but it assumes the file within the source path.. and I don't
>>know how to determine the potential source path..
>>
>>Sven
>>
>>
>>>"Sven Krause" <no.spam@public-files.de> wrote in message
>>>news:bmlj9g$nap$1@is.intellij.net...
>>>
>>>
>>>>Hi,
>>>>
>>>>how can I create a virtual file? I have the byte[] and the name/path
>>>>elemnt from any source and like to create a file (class source or byte
>>>>code or a simple tex file. The only things I found are all PSI based. Is
>>>>there another way to bring a file into the project? And the question is
>>>>how do I determine the possible source path(s)?
>>>>
>>>>Sven
>>>>
>>>
>>>
>>>



0

Could you please formulate more precisly what you want to do? Do you want to
create a file on disk and obtain VirtualFile instance corresponding to it?
Or you already have a file on the disk and only need a VirtualFile instance?

P.S. And no matter what you need it's 99% probability that you DON'T NEED to
implement VirtualFile.

--
Valentin Kipiatkov
JetBrains, Inc
http://www.intellij.com
"Develop with pleasure!"

"Sven Krause" <no.spam@public-files.de> wrote in message
news:bmlj9g$nap$1@is.intellij.net...

Hi,

>

how can I create a virtual file? I have the byte[] and the name/path
elemnt from any source and like to create a file (class source or byte
code or a simple tex file. The only things I found are all PSI based. Is
there another way to bring a file into the project? And the question is
how do I determine the possible source path(s)?

>

Sven

>


0

Valentin Kipiatkov (JetBrains) wrote:

Could you please formulate more precisly what you want to do? Do you want to
create a file on disk and obtain VirtualFile instance corresponding to it?
Or you already have a file on the disk and only need a VirtualFile instance?

P.S. And no matter what you need it's 99% probability that you DON'T NEED to
implement VirtualFile.

Ok. I'm extracting a JarEntry from a JarFile/JarInputStream and like to
open this file within IDEA. So i need a) to figure out where to place
the file that is fits into the current project and b) launch the proper
editor for it.

What I tried was:

String content = read();


Document document = EditorFactory.getInstance().createDocument(content);
System.out.println("document = " + document);
VirtualFile file = FileDocumentManager.getInstance().getFile(document);
if( file != null ) {
System.out.println("file = " + file);
FileEditor[] editors =
FileEditorManager.getInstance(project).openFile(file, true);
System.out.println("editors = " + Arrays.asList(editors));
}

So the way to create the VF is bad, since it is not registered yet in
the project tree.

Help is welcome.

Sven

0

a) to figure out where to place
the file that is fits into the current project


So is it true that you have some jar which is NOT YET added to the libraries
of the project?

--
Valentin Kipiatkov
JetBrains, Inc
http://www.intellij.com
"Develop with pleasure!"


"Sven Krause" <no.spam@public-files.de> wrote in message
news:bmlsua$e2n$1@is.intellij.net...

Valentin Kipiatkov (JetBrains) wrote:

Could you please formulate more precisly what you want to do? Do you

want to

create a file on disk and obtain VirtualFile instance corresponding to

it?

Or you already have a file on the disk and only need a VirtualFile

instance?

>

P.S. And no matter what you need it's 99% probability that you DON'T

NEED to

implement VirtualFile.

>
Ok. I'm extracting a JarEntry from a JarFile/JarInputStream and like to
open this file within IDEA. So i need a) to figure out where to place
the file that is fits into the current project and b) launch the proper
editor for it.

>

What I tried was:

>

String content = read();

>
>

Document document = EditorFactory.getInstance().createDocument(content);
System.out.println("document = " + document);
VirtualFile file = FileDocumentManager.getInstance().getFile(document);
if( file != null ) {
System.out.println("file = " + file);
FileEditor[] editors =
FileEditorManager.getInstance(project).openFile(file, true);
System.out.println("editors = " + Arrays.asList(editors));
}

>

So the way to create the VF is bad, since it is not registered yet in
the project tree.

>

Help is welcome.

>

Sven

>


0

Valentin Kipiatkov (JetBrains) wrote:
>>a) to figure out where to place
>>the file that is fits into the current project


So is it true that you have some jar which is NOT YET added to the libraries
of the project?

Correct. What I'm doing/planning is a archive editor (a la winzip)
allowing to exctract/add/modify jars/zips.

Sven

0

You should use RootManager to add your jar to the project. Here is some code
sample:

ModifiableRootModel rootModel = rootManager.getModifiableModel();
Library.ModifyableModel libraryModel =
rootModel.getModuleLibraryTable().createLibrary().getModifyableModel();
libraryModel.addRoot(yourJarVirtualFile, OrderRootType.CLASSES);
libraryModel.commit();
rootModel.commit();

--
Valentin Kipiatkov
JetBrains, Inc
http://www.intellij.com
"Develop with pleasure!"


"Sven Krause" <no.spam@public-files.de> wrote in message
news:bmlv2m$682$1@is.intellij.net...

Valentin Kipiatkov (JetBrains) wrote:
>>a) to figure out where to place
>>the file that is fits into the current project
>
>

So is it true that you have some jar which is NOT YET added to the

libraries

of the project?

>
Correct. What I'm doing/planning is a archive editor (a la winzip)
allowing to exctract/add/modify jars/zips.

>

Sven

>


0

Wow.
So, if the model can have multiple roots added to it,
why is it createLibrary? Isn't a library by definition a single jar file?

Valentin Kipiatkov (JetBrains) wrote:

You should use RootManager to add your jar to the project. Here is some code
sample:

ModifiableRootModel rootModel = rootManager.getModifiableModel();
Library.ModifyableModel libraryModel =
rootModel.getModuleLibraryTable().createLibrary().getModifyableModel();
libraryModel.addRoot(yourJarVirtualFile, OrderRootType.CLASSES);
libraryModel.commit();
rootModel.commit();


--

Erb

==============================================================
"Most of you are familiar with the virtues of a programmer.
There are three, of course: laziness, impatience, and hubris."
- Larry Wall
==============================================================

0

why is it createLibrary? Isn't a library by definition a single jar file?


No, you can create libraries containing mulitple jars. In the current UI
it's available by "Create Named Library".

--
Valentin Kipiatkov
JetBrains, Inc
http://www.intellij.com
"Develop with pleasure!"


"Erb" <dont@evenbother.com> wrote in message
news:bn6fj5$3i4$1@is.intellij.net...

Wow.
So, if the model can have multiple roots added to it,
why is it createLibrary? Isn't a library by definition a single jar file?

>

Valentin Kipiatkov (JetBrains) wrote:

>

You should use RootManager to add your jar to the project. Here is some

code

sample:

>

ModifiableRootModel rootModel = rootManager.getModifiableModel();
Library.ModifyableModel libraryModel =
rootModel.getModuleLibraryTable().createLibrary().getModifyableModel();
libraryModel.addRoot(yourJarVirtualFile, OrderRootType.CLASSES);
libraryModel.commit();
rootModel.commit();

>

>

--

>

Erb

>

==============================================================
"Most of you are familiar with the virtues of a programmer.
There are three, of course: laziness, impatience, and hubris."
- Larry Wall
==============================================================

>


0

Please sign in to leave a comment.