We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpIntegration › Applets and files...
Page Index Toggle Pages: 1
Applets and files... (Read 2447 times)
Applets and files...
Jun 11th, 2009, 12:01pm
 
This is probably a very basic question, but I am stumped. I have a processing app which loads an XML file and then parses this file within the app. All is fine running locally, but I cannot get it to run as an applet. I have signed the resulting jar file, but it always fails to load the file (java.security.AccessControlException: access denied (java.io.FilePermission /data/HoCMP.xml read).

Could someone explain to me how you write code which loads a file and parses it within the app and works as an applet (the bit I am stuck on)? Is it possible? From what I have been reading on the web I am now not so sure now.

Or a webpage/article that explains it. I have been stuck on this for days now, so frustrated as my app is good to go bar this huge hurdle.

I can provide code if you want, I think this could confuse my question though?

Thanks for any help.
Re: Applets and files...
Reply #1 - Jun 11th, 2009, 12:19pm
 
Apologies, let me add a comment to my question..

I have just read that an Applet's default limitations are:

- An Applet cannot have access to file on the local computer
- An Applet cannot invoke any other program on the local computer
- An Applet cannot communicate with any computer other than the computer from which the HTML page containing the applet was downloaded.

So, I believe my question was rather basic, as my understanding of what Applet can and cannot do was limited.

Do you agree that the only way to read in a file for an Applet to use is to host the file on a website?

Thank you.
Re: Applets and files...
Reply #2 - Jun 11th, 2009, 1:05pm
 
SmeeZee wrote on Jun 11th, 2009, 12:19pm:
Do you agree that the only way to read in a file for an Applet to use is to host the file on a website

I haven't tried it yet, but I believe the applet can also read a file (resource) located in its jar file (automatically packed by Processing if it is in the data folder, from what I have read).

Note that I have read that with recent Java releases, unsigned applets can read data from other URLs, as long as these are identified as service providers (Web APIs, like Twitter or Flickr, etc.).
Not sure what it takes for this to work...
Re: Applets and files...
Reply #3 - Jun 11th, 2009, 1:29pm
 
"I believe the applet can also read a file (resource?) located in its jar file" - Interesting, this is what I was thinking. Within my app/applet I have a class which takes the filename (optionally including the location) as a string for its constructor. I cannot work out how I would reference the XML file if it is located in the jar file. I have tried all manner of different options.

If you think it is possible I will continue to research, I really would prefer to load from file rather than resolving a hosted file as this slows what is already a heavy app.

If you find anything in the meantime I would appreciate it.

Thank you.
Re: Applets and files...
Reply #4 - Jun 11th, 2009, 1:45pm
 
In theory, you have nothing to do: you put the file in the data dir, and use stuff like loadImage, loadFont or loadString (etc.) with just the filename. When exporting, the file should be in the jar and Processing will continue to read it from there automatically.
If it doesn't work for you, I might experiment and see if it works...
Re: Applets and files...
Reply #5 - Jun 11th, 2009, 3:40pm
 
I will play with it some more, just to discuss has helped. Will update this with my progress.
Re: Applets and files...
Reply #6 - Jun 12th, 2009, 2:40am
 
So I have been playing and found the following:

An applet which loads a text file which resides in the data directory using loadStrings() works perfectly. However if I try to load the file into object it fails to find it, example code:

public MyParser(String fileName)
{
       data = loadStrings(fileName);

       try
       {
             dbf = DocumentBuilderFactory.newInstance();
             db = dbf.newDocumentBuilder();
             myDom = db.parse(fileName);
       }
       catch (Exception e)
       {
         System.out.println("There has been an error!");  
         System.err.println(e.toString());
       }
}

So the loadStrings() works perfectly, however the db.parse(fileName) throws the error "There has been an error!
java.io.FileNotFoundException: /home/AntMan/HoCMP.xml (No such file or directory)".

In this case it is trying to load the file from my local home directory.

I believe I need the equivalent to loadStrings() but loadXML() which returns a Document rather than String[]. I have tried to find the source code for loadStrings() to see if I can code this function based on this code, but to no avail?

Can anyone advise on either the location of the code or if this function already exists?
Re: Applets and files...
Reply #7 - Jun 12th, 2009, 5:55am
 
Yes, the loadXxx() functions of Processing does some magic, getting the data folder path and prepending it to the file name, while you use Java methods which know nothing of this folder...
Fortunately, you have also access to this path with functions like dataPath() or dataFile().

Source is at PApplet.java (look at the Contribute section of the site) but you can also just use the developer's reference if you prefer to avoid the gory details...
Re: Applets and files...
Reply #8 - Jun 12th, 2009, 8:13am
 
After some research into Java applets and reading files within (see: http://www.java-tips.org/java-se-tips/java.applet/how-can-i-read-a-file-from-the-jar-file-of-an-a.html) I managed to get it working!!!

Example code showing how to do this, based on previous examples (in this example fileName contains the prefix data/):

   protected Document myDom = null;
   protected DocumentBuilderFactory dbf = null;
   protected DocumentBuilder db = null;
   protected InputStream in = null;
   protected String[] data;

   public MyParser(String fileName)
   {
       data = loadStrings(fileName);
       System.out.println("Starting this applet jar reader stuff");
       System.out.println("fileName is "+fileName);

       dbf = DocumentBuilderFactory.newInstance();
       
       try
       {
            in = getClass().getResourceAsStream(fileName);
           
            System.out.println("created input stream");    
            db = dbf.newDocumentBuilder();
            myDom = db.parse(in);    
            System.out.println("Finished loading");
       }
       catch (Exception e)
       {
            System.out.println("Problems!!!");
            System.err.println(e.toString());
       }


That was rather epic, but thankfully I have done it. Thank you for your support. I need a lie down now!
Page Index Toggle Pages: 1