You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by "Chintalapaty, Sreedhar " <sc...@ptc.com> on 2002/12/18 16:00:06 UTC

How do I read files inside an Action?

Hi,

Can some one point me to an example of how we can access an xml file on the file system from a custom Action?

TIA,

Sreedhar

---------------------------------------------------------------------
Please check that your question  has not already been answered in the
FAQ before posting.     <http://xml.apache.org/cocoon/faq/index.html>

To unsubscribe, e-mail:     <co...@xml.apache.org>
For additional commands, e-mail:   <co...@xml.apache.org>


Re: How do I read files inside an Action?

Posted by Konstantin Piroumian <kp...@apache.org>.
You can do it like this:

Source source = null;
        SourceResolver resolver = null;
        try {
            resolver = (SourceResolver) manager.lookup(
SourceResolver.ROLE );
            source = resolver.resolveURI( fileName );
            return builder.parse( new InputSource(
source.getInputStream() ));
        }
        catch (Exception x) {
            logger.warn("XMLREsourceBundle: No excalibur-source " +
fileName,
x);
        }
        finally {
            resolver.release( source );
            manager.release( resolver );
        }

Take a look at the ResourceExistsAction or latest (from CVS)
XMLResourceBundle for details.

Konstantin


From: "Chintalapaty, Sreedhar " <sc...@ptc.com>


Hi,

Can some one point me to an example of how we can access an xml file on the
file system from a custom Action?

TIA,

Sreedhar

---------------------------------------------------------------------
Please check that your question  has not already been answered in the
FAQ before posting.     <http://xml.apache.org/cocoon/faq/index.html>

To unsubscribe, e-mail:     <co...@xml.apache.org>
For additional commands, e-mail:   <co...@xml.apache.org>



---------------------------------------------------------------------
Please check that your question  has not already been answered in the
FAQ before posting.     <http://xml.apache.org/cocoon/faq/index.html>

To unsubscribe, e-mail:     <co...@xml.apache.org>
For additional commands, e-mail:   <co...@xml.apache.org>


RE: How do I read files inside an Action?

Posted by Ray Martin <ra...@aci-hq.com>.
Sir,

Just like any other file called from java.

In the action class you would need:

import java.io.File; // cocoon has some defaults - this could be one of them

//nothing special here, just an example.  however, this directory is where
cocoon places uploaded files by default.  make yours wherever your xml file
is located.
String currentPath = "C:/Program Files/Apache Group/Tomcat
4.1/work/Standalone/localhost/cocoon/cocoon-files/upload-dir/";

// next three lines checks the directory for files and sets pointer to the
first file in the directory (should put some checks around this)(could
iterate through the list)
File fileDir = new File(currentPath);
File[] listOfFiles = fileDir.listFiles();
String filename = listOfFiles[0].toString();

File uploadDir = new File(currentPath);
File theXMLfile = new File(filename);
System.out.println("The director is: " + theResume.getParent());
System.out.println("The file name: " + theResume.getName());
long fileLen = theXMLfile.length();
Long testThis = new Long(fileLen);
int fileLength = testThis.intValue();
FileInputStream fis = new FileInputStream(theResume);
byte[] byteArray = new byte[fileLength];
int cnt = fis.read(byteArray, 0, fileLength);
fis.close();

byteArray now contains the contents of theXMLfile.  this only works for
ASCII files.  i do not have an example at my fingertips for Unicode.


these are snippets from an action that i have performing.  if you need
anything else, holler.

Ray

> -----Original Message-----
> From: Chintalapaty, Sreedhar [mailto:schintalapaty@ptc.com]
> Sent: Wednesday, December 18, 2002 10:00 AM
> To: cocoon-users@xml.apache.org
> Subject: How do I read files inside an Action?
>
>
> Hi,
>
> Can some one point me to an example of how we can access an xml
> file on the file system from a custom Action?
>
> TIA,
>
> Sreedhar
>
> ---------------------------------------------------------------------
> Please check that your question  has not already been answered in the
> FAQ before posting.     <http://xml.apache.org/cocoon/faq/index.html>
>
> To unsubscribe, e-mail:     <co...@xml.apache.org>
> For additional commands, e-mail:   <co...@xml.apache.org>
>
>



---------------------------------------------------------------------
Please check that your question  has not already been answered in the
FAQ before posting.     <http://xml.apache.org/cocoon/faq/index.html>

To unsubscribe, e-mail:     <co...@xml.apache.org>
For additional commands, e-mail:   <co...@xml.apache.org>