How do I get the path of my application.xgapp?


Jacky Miu
 

I have created an application using Gate Developer. After I have saved the application, I got the usual resources including application.xgapp, application-resources folder, etc.

For initialization, I have to read the content of a file stored within the application-resources folder. Now, my question is how I can get the path of this application (or alternatively the path of the application.xgapp file) when I run this application. If I can get the path of the application.xgapp, I can create a relative path to easily access the content of the said file. This is helpful since the application can be saved to any other location. 

I have tried to use the following code to obtain that path but it was unsuccessful:

import java.nio.file.Paths;
String currentDir = System.getProperty("user.dir");

Because the currentDir above only provides the path of my Gate program, not the path of the application.xgapp. Thank you in advance of your help! 

Regards,
Jacky Miu


Ian Roberts
 

"For initialization" of which component?  If this is in your own custom ProcessingResource then the standard way to handle this sort of thing is to give your PR an init parameter of type URL or ResourceReference that points to the file that you want to load.  In your init() method you just use the normal .openStream() method (implemented by both URL and ResourceReference) to read the data from the file, or if you need to access other files in the same folder then you can create new URLs relative to that one

URL otherFile = new URL(paramValue, "fileName.txt");

When you save the application as an xgapp any URL or ResourceReference parameter values will be stored in the xgapp as a path relative to the xgapp location and resolved back to an absolute URL when you re-load, so you can move the xgapp and the target file to any other location and as long as they remain in the same relative locations it'll all still work.

Ian

On 24/02/2022 06:06, Jacky Miu via groups.io wrote:
I have created an application using Gate Developer. After I have saved the application, I got the usual resources including application.xgapp, application-resources folder, etc.

For initialization, I have to read the content of a file stored within the application-resources folder. Now, my question is how I can get the path of this application (or alternatively the path of the application.xgapp file) when I run this application. If I can get the path of the application.xgapp, I can create a relative path to easily access the content of the said file. This is helpful since the application can be saved to any other location. 

I have tried to use the following code to obtain that path but it was unsuccessful:

import java.nio.file.Paths;
String currentDir = System.getProperty("user.dir");

Because the currentDir above only provides the path of my Gate program, not the path of the application.xgapp. Thank you in advance of your help! 

Regards,
Jacky Miu


-- 
Ian Roberts               | Department of Computer Science
i.roberts@...  | University of Sheffield, UK


Jacky Miu
 

Hi Ian,

Thank you for your advice.

I actually want to read a text from a file (e.g. input.txt), then designate the text as a document feature.  Following code is used in a jape file:

import java.nio.file.Files;
import java.nio.file.Paths;

data = new String(Files.readAllBytes(Paths.get("C:/Users/input.txt"))); 
doc.getFeatures().put("input", data);

It works fine while I am using Gate Developer. However, if I save the application as an xgapp, the reference to C:/Users/input.txt is no longer correct because the application can be unzipped to any location. Any advice on how I can avoid changing the file path even after the application has been saved as an xgapp? Thanks.

Regards,
Jacky Miu


On Thursday, February 24, 2022, 05:55:08 PM GMT+8, Ian Roberts <i.roberts@...> wrote:


"For initialization" of which component?  If this is in your own custom ProcessingResource then the standard way to handle this sort of thing is to give your PR an init parameter of type URL or ResourceReference that points to the file that you want to load.  In your init() method you just use the normal .openStream() method (implemented by both URL and ResourceReference) to read the data from the file, or if you need to access other files in the same folder then you can create new URLs relative to that one

URL otherFile = new URL(paramValue, "fileName.txt");

When you save the application as an xgapp any URL or ResourceReference parameter values will be stored in the xgapp as a path relative to the xgapp location and resolved back to an absolute URL when you re-load, so you can move the xgapp and the target file to any other location and as long as they remain in the same relative locations it'll all still work.

Ian

On 24/02/2022 06:06, Jacky Miu via groups.io wrote:
I have created an application using Gate Developer. After I have saved the application, I got the usual resources including application.xgapp, application-resources folder, etc.

For initialization, I have to read the content of a file stored within the application-resources folder. Now, my question is how I can get the path of this application (or alternatively the path of the application.xgapp file) when I run this application. If I can get the path of the application.xgapp, I can create a relative path to easily access the content of the said file. This is helpful since the application can be saved to any other location. 

I have tried to use the following code to obtain that path but it was unsuccessful:

import java.nio.file.Paths;
String currentDir = System.getProperty("user.dir");

Because the currentDir above only provides the path of my Gate program, not the path of the application.xgapp. Thank you in advance of your help! 

Regards,
Jacky Miu


-- 
Ian Roberts               | Department of Computer Science
i.roberts@...  | University of Sheffield, UK


Ian Roberts
 

To be honest, I think your best bet in this situation is still to turn this logic into a proper PR rather than a JAPE.  This would let you take advantage of the URL relativisation trick in xgapps and would also mean that you could make it read the file from disk just once (at PR init time) and store the string in a variable that could then be assigned to every document, rather than having to read the file off disk repeatedly for every individual document.  I've made an example of how you could do this at https://github.com/ianroberts/filereader

Ian

On 25/02/2022 07:35, Jacky Miu via groups.io wrote:
Hi Ian,

Thank you for your advice.

I actually want to read a text from a file (e.g. input.txt), then designate the text as a document feature.  Following code is used in a jape file:

import java.nio.file.Files;
import java.nio.file.Paths;

data = new String(Files.readAllBytes(Paths.get("C:/Users/input.txt"))); 
doc.getFeatures().put("input", data);

It works fine while I am using Gate Developer. However, if I save the application as an xgapp, the reference to C:/Users/input.txt is no longer correct because the application can be unzipped to any location. Any advice on how I can avoid changing the file path even after the application has been saved as an xgapp? Thanks.

Regards,
Jacky Miu


On Thursday, February 24, 2022, 05:55:08 PM GMT+8, Ian Roberts <i.roberts@...> wrote:


"For initialization" of which component?  If this is in your own custom ProcessingResource then the standard way to handle this sort of thing is to give your PR an init parameter of type URL or ResourceReference that points to the file that you want to load.  In your init() method you just use the normal .openStream() method (implemented by both URL and ResourceReference) to read the data from the file, or if you need to access other files in the same folder then you can create new URLs relative to that one

URL otherFile = new URL(paramValue, "fileName.txt");

When you save the application as an xgapp any URL or ResourceReference parameter values will be stored in the xgapp as a path relative to the xgapp location and resolved back to an absolute URL when you re-load, so you can move the xgapp and the target file to any other location and as long as they remain in the same relative locations it'll all still work.

Ian

On 24/02/2022 06:06, Jacky Miu via groups.io wrote:
I have created an application using Gate Developer. After I have saved the application, I got the usual resources including application.xgapp, application-resources folder, etc.

For initialization, I have to read the content of a file stored within the application-resources folder. Now, my question is how I can get the path of this application (or alternatively the path of the application.xgapp file) when I run this application. If I can get the path of the application.xgapp, I can create a relative path to easily access the content of the said file. This is helpful since the application can be saved to any other location. 

I have tried to use the following code to obtain that path but it was unsuccessful:

import java.nio.file.Paths;
String currentDir = System.getProperty("user.dir");

Because the currentDir above only provides the path of my Gate program, not the path of the application.xgapp. Thank you in advance of your help! 

Regards,
Jacky Miu


-- 
Ian Roberts               | Department of Computer Science
i.roberts@...  | University of Sheffield, UK


-- 
Ian Roberts               | Department of Computer Science
i.roberts@...  | University of Sheffield, UK


Jacky Miu
 

Dear Ian,

The filereader works fine, and fully accomplishes my task! Thank you so much for your help!

Best regards,
Jacky


On Friday, February 25, 2022, 06:33:24 PM GMT+8, Ian Roberts <i.roberts@...> wrote:


To be honest, I think your best bet in this situation is still to turn this logic into a proper PR rather than a JAPE.  This would let you take advantage of the URL relativisation trick in xgapps and would also mean that you could make it read the file from disk just once (at PR init time) and store the string in a variable that could then be assigned to every document, rather than having to read the file off disk repeatedly for every individual document.  I've made an example of how you could do this at https://github.com/ianroberts/filereader

Ian

On 25/02/2022 07:35, Jacky Miu via groups.io wrote:
Hi Ian,

Thank you for your advice.

I actually want to read a text from a file (e.g. input.txt), then designate the text as a document feature.  Following code is used in a jape file:

import java.nio.file.Files;
import java.nio.file.Paths;

data = new String(Files.readAllBytes(Paths.get("C:/Users/input.txt"))); 
doc.getFeatures().put("input", data);

It works fine while I am using Gate Developer. However, if I save the application as an xgapp, the reference to C:/Users/input.txt is no longer correct because the application can be unzipped to any location. Any advice on how I can avoid changing the file path even after the application has been saved as an xgapp? Thanks.

Regards,
Jacky Miu


On Thursday, February 24, 2022, 05:55:08 PM GMT+8, Ian Roberts <i.roberts@...> wrote:


"For initialization" of which component?  If this is in your own custom ProcessingResource then the standard way to handle this sort of thing is to give your PR an init parameter of type URL or ResourceReference that points to the file that you want to load.  In your init() method you just use the normal .openStream() method (implemented by both URL and ResourceReference) to read the data from the file, or if you need to access other files in the same folder then you can create new URLs relative to that one

URL otherFile = new URL(paramValue, "fileName.txt");

When you save the application as an xgapp any URL or ResourceReference parameter values will be stored in the xgapp as a path relative to the xgapp location and resolved back to an absolute URL when you re-load, so you can move the xgapp and the target file to any other location and as long as they remain in the same relative locations it'll all still work.

Ian

On 24/02/2022 06:06, Jacky Miu via groups.io wrote:
I have created an application using Gate Developer. After I have saved the application, I got the usual resources including application.xgapp, application-resources folder, etc.

For initialization, I have to read the content of a file stored within the application-resources folder. Now, my question is how I can get the path of this application (or alternatively the path of the application.xgapp file) when I run this application. If I can get the path of the application.xgapp, I can create a relative path to easily access the content of the said file. This is helpful since the application can be saved to any other location. 

I have tried to use the following code to obtain that path but it was unsuccessful:

import java.nio.file.Paths;
String currentDir = System.getProperty("user.dir");

Because the currentDir above only provides the path of my Gate program, not the path of the application.xgapp. Thank you in advance of your help! 

Regards,
Jacky Miu


-- 
Ian Roberts               | Department of Computer Science
i.roberts@...  | University of Sheffield, UK


-- 
Ian Roberts               | Department of Computer Science
i.roberts@...  | University of Sheffield, UK