from text file to InputSource (another how to get there from here question)

W

Wendy S

I need to pick up a file sitting in WEB-INF/classes, which I usually do like
this:
ClassLoader cl = MyClassName.class.getClassLoader();
InputStream rules = cl.getResourceAsStream( XML_RULES );

But an InputStream won't do, the createDigester method takes either
org.xml.sax.InputSource or java.net.URL .

Initially, I had this:
ClassLoader cl = MyClassName.class.getClassLoader();
InputStream rules = cl.getResourceAsStream( XML_RULES );
Digester digester = DigesterLoader.createDigester( new InputSource(
rules) );

And then I got it down to:
ClassLoader cl = MyClassName.class.getClassLoader();
Digester digester = DigesterLoader.createDigester( cl.getResource(
XML_RULES ) );
(ClassLoader.getResource returns a URL.)

There's probably a more direct path, I just don't see it.

What methods other than ClassLoader.getResource and getResourceAsStream can
find things on the classpath? InputSource has a constructor that takes a
'system identifier' but I haven't been able to figure out exactly what that
is.

Thanks once again!
 
A

anonymous

Wendy said:
I need to pick up a file sitting in WEB-INF/classes, which I usually do like
this:
ClassLoader cl = MyClassName.class.getClassLoader();
InputStream rules = cl.getResourceAsStream( XML_RULES );

But an InputStream won't do, the createDigester method takes either
org.xml.sax.InputSource or java.net.URL .

Initially, I had this:
ClassLoader cl = MyClassName.class.getClassLoader();
InputStream rules = cl.getResourceAsStream( XML_RULES );
Digester digester = DigesterLoader.createDigester( new InputSource(
rules) );

And then I got it down to:
ClassLoader cl = MyClassName.class.getClassLoader();
Digester digester = DigesterLoader.createDigester( cl.getResource(
XML_RULES ) );
(ClassLoader.getResource returns a URL.)

There's probably a more direct path, I just don't see it.

What methods other than ClassLoader.getResource and getResourceAsStream can
find things on the classpath? InputSource has a constructor that takes a
'system identifier' but I haven't been able to figure out exactly what that
is.

Thanks once again!

Here is one method which I often employ:
Put the name of the file and its path relative to WEB-INF into web.xml.
Like this:

<web-app>

<context-param>
<param-name>DATA_DIR</param-name>
<param-value>/WEB-INF/data</param-value>
</context-param>
...
</web-app>

Then get the DATA_DIR:

private File getDataDir() throws ServletException {
ServletContext application = getServletContext();
String dataDir = application.getInitParameter(DATA_DIR_PARAM);
if (dataDir == null || dataDir.length() == 0)
error(FILE_COMP_ID, MISSING_DATA_DIR_PARAM_ERROR,
FacesMessage.SEVERITY_FATAL);
String realDir = application.getRealPath(dataDir);
if (realDir == null)
error(FILE_COMP_ID, CANNOT_ACCESS_DATA_DIR_ERROR,
FacesMessage.SEVERITY_FATAL);
return new File(realDir);
}

etc. etc. etc. ad nauseam
 
W

Wendy S

anonymous said:
Here is one method which I often employ:
Put the name of the file and its path relative to WEB-INF into web.xml.

This code is going into the DAO layer, so although I know the file is
sitting in WEB-INF/classes, the code can only know that the file is be
visible on the classpath.

It's interesting to see Faces code start to appear, though. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top