KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > util > ResourceLoader


1 /**
2  * Copyright (c) 2003-2006, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  */

30 package org.pdfbox.util;
31
32 import java.io.File JavaDoc;
33 import java.io.FileInputStream JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.io.IOException JavaDoc;
36
37 import java.util.Properties JavaDoc;
38
39 /**
40  * This class will handle loading resource files(AFM/CMAP).
41  *
42  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
43  * @version $Revision: 1.8 $
44  */

45 public class ResourceLoader
46 {
47     
48     /**
49      * private constructor for utility class.
50      */

51     private ResourceLoader()
52     {
53         //private utility class
54
}
55
56     /**
57      * This will attempt to load the resource given the resource name.
58      *
59      * @param resourceName The resource to try and load.
60      *
61      * @return The resource as a stream or null if it could not be found.
62      *
63      * @throws IOException If there is an error while attempting to load the resource.
64      */

65     public static InputStream JavaDoc loadResource( String JavaDoc resourceName ) throws IOException JavaDoc
66     {
67         ClassLoader JavaDoc loader = ResourceLoader.class.getClassLoader();
68
69         InputStream JavaDoc is = null;
70         
71         if( loader != null )
72         {
73             is = loader.getResourceAsStream( resourceName );
74         }
75         
76         //see sourceforge bug 863053, this is a fix for a user that
77
//needed to have PDFBox loaded by the bootstrap classloader
78
if( is == null )
79         {
80             loader = ClassLoader.getSystemClassLoader();
81             if( loader != null )
82             {
83                 is = loader.getResourceAsStream( resourceName );
84             }
85         }
86         
87         if( is == null )
88         {
89             File JavaDoc f = new File JavaDoc( resourceName );
90             if( f.exists() )
91             {
92                 is = new FileInputStream JavaDoc( f );
93             }
94         }
95
96         return is;
97     }
98     
99     /**
100      * This will attempt to load the resource given the resource name.
101      *
102      * @param resourceName The resource to try and load.
103      *
104      * @return The resource as a stream or null if it could not be found.
105      *
106      * @throws IOException If there is an error loading the properties.
107      */

108     public static Properties JavaDoc loadProperties( String JavaDoc resourceName ) throws IOException JavaDoc
109     {
110         Properties JavaDoc properties = null;
111         InputStream JavaDoc is = null;
112         try
113         {
114             is = loadResource( resourceName );
115             if( is != null )
116             {
117                 properties = new Properties JavaDoc();
118                 properties.load( is );
119             }
120         }
121         finally
122         {
123             if( is != null )
124             {
125                 is.close();
126             }
127         }
128         return properties;
129     }
130     
131     /**
132      * This will attempt to load the resource given the resource name.
133      *
134      * @param resourceName The resource to try and load.
135      * @param defaults A stream of default properties.
136      *
137      * @return The resource as a stream or null if it could not be found.
138      *
139      * @throws IOException If there is an error loading the properties.
140      */

141     public static Properties JavaDoc loadProperties( String JavaDoc resourceName, Properties JavaDoc defaults ) throws IOException JavaDoc
142     {
143         InputStream JavaDoc is = null;
144         try
145         {
146             is = loadResource( resourceName );
147             if( is != null )
148             {
149                 defaults.load( is );
150             }
151         }
152         finally
153         {
154             if( is != null )
155             {
156                 is.close();
157             }
158         }
159         return defaults;
160     }
161 }
Popular Tags