Try using this file for loading resources.
Currently it loads only as properties and String but you may choose to
return anyway you want.


import java.util.Properties;
import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream; 
import java.io.ByteArrayOutputStream;

/**
 * Uses the classloader to load resources from the file system
 */
public class ResourceLoader 
{
        
        /**
         * Useful int to define a buffer size
         */
        private static final int aBuffSize = 123123;
        
        /**
         * inputstream reader int
         */
        private static int k; 
                
        /**
         * Useful array for copying data
         */
        private static byte[] buff = new byte[aBuffSize]; 
        
        /**
         * The XML String to return
         */
        private static String strXml;
        
        
        /**
         * Returns the resource as a java.util.Properties object
         * @param resourceBundleName String
         * @param strPath String
         * @throws IOException
         * @return Properties
         */
        public static final Properties getPropertiesFromResourceBundle( 
                String resourceBundleName,
                String strPath )
                throws IOException 
        {
                
                InputStream inputStream = null;
                Properties properties = null;
                
                try 
                {
                        
                        // fetch the properties file as a stream
                        
                        try 
                        {
                                inputStream = getResourceAsStream(
resourceBundleName,
        
strPath );
                        }
                        catch ( IOException objIoe ) 
                        {                               
                                throw objIoe;
                        }
                                
                        // if the resource was found, load it into the
result Properties object
                        if( inputStream != null )
                        {
                                try
                                {
                                        properties = new Properties();
                                        properties.load( inputStream );
                                }
                                catch( IOException e )
                                {
                                        StringBuffer strBuff = new
StringBuffer(
        
"CPSConfigurationManager.getPropertiesFromResourceBundle() caught
IOException when trying to load properties file " );
                                        strBuff.append( resourceBundleName
);
                                        strBuff.append( ": " );
                                        strBuff.append( e.getMessage() );

        
                                        throw new IOException(
strBuff.toString() );
                                }
                        }
                }
                catch(IOException ioe) 
                {                       
                        throw ioe;
                }
                finally {
                        try 
                        {
                                if(inputStream != null)
                                {                               
                                        inputStream.close();
                                }
                        }
                        catch( IOException ex ) 
                        {       
                                throw ex;
                        }
                }
                return properties;
        } // end of method getPropertiesFromResourceBundle()
        
        
        
        /**
         * Returns the resource as a String object
         * @param resourceBundleName String
         * @param strPath String
         * @throws IOException
         * @return String
         */
        public static final String getXmlFromResourceBundle( 
                String resourceBundleName, 
                String strPath ) throws IOException 
        {
                
                OutputStream outputStream = null;
                InputStream inputStream = null;         
                
        
                
                try {
                        
                        // fetch the xml file as a stream

                        try {
                                inputStream = getResourceAsStream(
resourceBundleName,
        
strPath );
                        }
                        catch ( IOException objIoe ) 
                        {                               
                                throw objIoe;
                        }
                                
                        // if the resource was found, load it 
                        if( inputStream != null )
                        {
                                try
                                {
                                          // I wish I could just do this:
strXml = inputStream.toString();
                                      outputStream = new
ByteArrayOutputStream(aBuffSize);
                                      
                                      synchronized ( outputStream ) 
                                      {
                                              while (
(k=inputStream.read(buff) ) != -1) 
                                              {
                                                        outputStream.write(
buff, 0, k);
                                              }
                                                
                                              // I can now grab the string I
want
                                              strXml =
outputStream.toString(); 
                                              outputStream.close();
                                              return strXml;
                                      } // end of synchronized block
                                      
                                      // That was a lot of work to pull a
String out of an inputStream.

                                }
                                catch( IOException e ) 
                                {

                                        throw new IOException(
"PSConfigurationManager.getPropertiesFromResourceBundle() caught IOException
when trying to load properties file " + e.getMessage() );
                                }               
                                
                        }
                }
                catch( IOException ioe ) 
                {                       
                        throw ioe;
                }
                finally 
                {
                        try 
                        {
                                inputStream.close();
                        }
                        catch( IOException ex ) 
                        {       
                                throw ex;                       
                        }
                        
                }
                return "";
        } // end of method getPropertiesFromResourceBundle()
        
        
        
        /**
         *  Returns the resource as a stream. Uses this class classloader to
find the resource
         *  relative to this class. Since this class is loaded by the
classloader of the 
         *  calling class (say servlet), the resource will be searched
realtive to the calling
         *  class
         */
        private static final InputStream getResourceAsStream( String
resourceBundleName,
String strPath )
                throws IOException 
        {
                InputStream inputStream = null;
                try {
                // null safety check
                        if( resourceBundleName == null || strPath == null)
                        {
                                throw new IOException(
        
"ResourceLoader.getResourceAsStream() called with null filename" );
                        }
                        
                        StringBuffer resourceBundlePath = new
StringBuffer(strPath);
                        resourceBundlePath.append("/");
                        resourceBundlePath.append( resourceBundleName );
                        
                        ClassLoader loader =
ResourceLoader.class.getClassLoader();          
                        
                        inputStream = loader.getResourceAsStream(
resourceBundlePath.toString() );
                }
                catch( IOException ioe ) 
                {                       
                        inputStream.close();
                        throw ioe;
                }                               
        
                return inputStream;
        } // end of method getResourceAsStream


}


-----Original Message-----
From: Rakesh Patel [mailto:[EMAIL PROTECTED]
Sent: Monday, January 20, 2003 11:30 AM
To: [EMAIL PROTECTED]
Subject: Solution for finding images in a WAR file

Hi,

I have found a solution that I think works (have not got access to a Unix
environment to confirm this) but I did not hard-code any window/unix
specific paths.

Here's a snippet of code I used:

ServletContext scntxt = this.getServletContext();
String pathToFile = scntxt.getRealPath("/WEB-INF"); // or whatever relative
to web app root eg /images

String imagePath = "file:" + pathToFile;

org.apache.fop.configuration.Configuration.put("baseDir",imagePath);

as you can see, all my images are in web_app_root/WEB-INF.

The corresponding xml data file looks like this:

<HTRelitiveRef HTRRKey="2" HTRRType="Picture" HTRRID="/centurion.jpg"
HTRRLink="" HTRRVisible="F">centurion</HTRelitiveRef>

I hope this saves others the problem of finding the solution across many
posts!

Thanks

Rakesh

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to