Hacking NetBeans #3 - Registering Files in Default Filesystem



Last time I blogged about executing ant tasks from the IDE. It was done in an ugly way - by generating a temporary ant script for execution. A nicer way is to use an existing ant script, which will be placed somewhere in the module's jar.

But the question is: how to access a xml file which is located somewhere inside my module? Or any other arbitrary file?

Well, you need to register this file in NetBeans layered filesystem. To achieve this, add something like this to the layer.xml file:

    <folder name="Services">
        <folder name="MyModule">
            <file name="my_script.xml" url="resources/copy_script.xml"/>                
        </folder>
    </folder>

By this I am declaring, that the export_script.xml file is located in resources/export_script.xml inside my module. This path is relative to the path of the layer.xml files. So these files are:

org/netbeans/modules/mymodule/layer.xml
org/netbeans/modules/mymodule/resources/copy_script.xml


I also declare that my module can find the xml file in Services/MyModule/copy_script.xml on the default NetBeans filesystem. To access the file, I use:

        FileObject sfo = Repository.getDefault().getDefaultFileSystem().findResource("Services/MyModule/my_script.xml");

Now that I have a reference to the FileObject of the xml file I can do something useful with it. I can e.g. convert it to a regular file by using FileUtil.toFile(sfo). To execute the ant script you can use my previous example but you will need to copy the ant script to the disk at first and call RunTarget() method on a FileObject which resides on disk.

Confused about NetBeans filesystems and the layer.xml file? I was, too. A good and short reading is here from Tim. You can also use the sysfs plug-in to explore the default filesystem. It's full of surprises :-)
Comments:

Post a Comment:
Comments are closed for this entry.
About

Roman Strobl

Search

Archives
« duben 2014
PoÚtStČtSoNe
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today