RE: Flowscript - delete file

Robby Pelssers <[email protected]>
Newsgroups gmane.text.xml.cocoon.user
Message-ID <A5838A1302A3AC48B4A90FAD790B51AB054AC5@AMXPRD0410MB366.eurprd04.prod.outlook.com>
Hi Peter,

I did not test the script I wrote actually and I can tell you why the script runs into an error.

.delete() is also a javascript method on an object.  It will delete a property by the name you specify.  As you clearly want to call the delete method on the file you need to use the workaround you used.

Kind regards,
Robby

From: Peter Sparkes [mailto:[email protected]]
Sent: Saturday, December 29, 2012 8:58 AM
To: [email protected]
Subject: Re: Flowscript - delete file

Hi Martin and Robby,

Thank you both for your help, what I have ended up with is:

importClass(Packages.java.io.File);
importClass(Packages.java.io.FileNotFoundException);

function deletefile() {

    //var filePath = cocoon.parameters["file"];

    var filePath = "D:/cocoon-2.1.11-wl/build/webapp/B&B/xml/9999.xml";  //for testing
    var file = new File(filePath);
     try {
        if (!file.exists()) {
           throw new FileNotFoundException(filePath);
        }
        file.delete();
        cocoon.sendPage('deletefilesuccess',{file:filePath<file:///\\filePath>});
    } catch (e) {
        cocoon.sendPage('deletefileerror',{file:e<file:///\\e>});
    }
}

However, file.delete(); produces a syntax error.

If I comment out file.delete(), the rest of the script works correctly:

  *   If 9999.xml exists, deletefilesuccess is accessed
  *   If 9999.xml does not exist, deletefileerror is accessed and I get:

Error File java.io.FileNotFoundException: D:/cocoon-2.1.11-wl/build/webapp/B&B/xml/9999.xml Not Found
What's wrong with file.delete(); ?

Regards

Peter



On 27/12/2012 18:35, Martin Heiden wrote:

Hi Peter,



Ok, I missed something:



 var xml_file = Packages.java.io.File("D:\cocoon-2.1.11-wl\build\webapp\B&B\xml" + file + ".xml");



should be:



 var xml_file = new Packages.java.io.File("D:\cocoon-2.1.11-wl\build\webapp\B&B\xml" + file + ".xml");



But you should try the function Robby sent to the list. It's much

cleaner.



Regards,



  Martin.





Thursday, December 27, 2012, 6:12:08 PM, you wrote:



PS> Hi Martin



PS> Thanks for your suggestion, unfortunately I get the same  error. The error log gives:



PS> ERROR (2012-12-27) 15:30.46:723 [flow] (/office/deletefile_9999) PoolThread-4/JSErrorReporter:

PS> "file:///D:/cocoon-2.1.11-wl/build/webapp/office/flow/deletefile.js"<file:///D:\cocoon-2.1.11-wl\build\webapp\office\flow\deletefile.js>, line 13: missing name after .

PS> operator

PS> ERROR (2012-12-27) 15:30.46:723 [sitemap.handled-errors] (/office/deletefile_9999)

PS> PoolThread-4/ErrorHandlerHelper: Sitemap: error calling function 'deletefile'

PS>      at <map:call> -

PS> file:///D:/cocoon-2.1.11-wl/build/webapp/office/sitemap.xmap:230:37<file:///D:\cocoon-2.1.11-wl\build\webapp\office\sitemap.xmap:230:37>

PS>      at <map:mount> -

PS> file:///D:/cocoon-2.1.11-wl/build/webapp/sitemap.xmap:783:90<file:///D:\cocoon-2.1.11-wl\build\webapp\sitemap.xmap:783:90>

PS> org.mozilla.javascript.EvaluatorException:

PS> "file:///D:/cocoon-2.1.11-wl/build/webapp/office/flow/deletefile.js"<file:///D:\cocoon-2.1.11-wl\build\webapp\office\flow\deletefile.js>, line 1: Compilation produced 1

PS> syntax errors.

PS>      at

PS> org.apache.cocoon.components.flow.javascript.JSErrorReporter.runtimeError(JSErrorReporter.java:67)



PS> regards



PS> Peter





PS> On 27/12/2012 11:42, Martin Heiden wrote:

Hi Peter,



PS>              //Delete file

PS>              Packages.java.io.File.xml_file.delete();



Try



xml_file.delete();



You just have to add the full "path" to the class when you

construct it. After this, the variable contains a reference and you

can just use it.



Regards,



   Martin.





Thursday, December 27, 2012, 10:28:31 AM, you wrote:



PS> Hi,



PS> I am trying to delete xml files using the following flowscript:



PS>     function deletefile() {



PS>          //file to be deleted

PS>          var file =

PS> Packages.java.lang.String(cocoon.parameters["file"]);



PS>          try {

PS>              // creating a link to the file to be deleted

PS>              var xml_file =

PS> Packages.java.io.File("D:\cocoon-2.1.11-wl\build\webapp\B&B\xml" + file

PS>     + ".xml");



PS>              //Delete file

PS>              Packages.java.io.File.xml_file.delete();



PS>              //  .txt file with OK message

PS>              cocoon.sendPage("success.txt", null);

PS>          }

PS>          catch(ex){

PS>              cocoon.log.error(ex);

PS>              // Smth. went wrong. Sending a error.txt file to the browser

PS>            cocoon.sendPage("error.txt", null);

PS>          }



PS>     }



PS> and get "Compilation produced 1 syntax errors"



PS> The error log contains:



PS>     ERROR ............    deletefile.js", line 13: missing name after . operator



PS> line 13 is:



PS>     Packages.java.io.File.xml_file.delete();



PS> What I am doing wrong?



PS> Peter















---------------------------------------------------------------------

To unsubscribe, e-mail: [email protected]<mailto:[email protected]>

For additional commands, e-mail: [email protected]<mailto:[email protected]>













Ciao,

 Martin





---------------------------------------------------------------------

To unsubscribe, e-mail: [email protected]<mailto:[email protected]>

For additional commands, e-mail: [email protected]<mailto:[email protected]>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.