001: /**
002: * Copyright (c) 2003-2006, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: */package org.pdfbox.util;
030:
031: import java.io.File;
032: import java.io.FileInputStream;
033: import java.io.InputStream;
034: import java.io.IOException;
035:
036: import java.util.Properties;
037:
038: /**
039: * This class will handle loading resource files(AFM/CMAP).
040: *
041: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
042: * @version $Revision: 1.8 $
043: */
044: public class ResourceLoader {
045:
046: /**
047: * private constructor for utility class.
048: */
049: private ResourceLoader() {
050: //private utility class
051: }
052:
053: /**
054: * This will attempt to load the resource given the resource name.
055: *
056: * @param resourceName The resource to try and load.
057: *
058: * @return The resource as a stream or null if it could not be found.
059: *
060: * @throws IOException If there is an error while attempting to load the resource.
061: */
062: public static InputStream loadResource(String resourceName)
063: throws IOException {
064: ClassLoader loader = ResourceLoader.class.getClassLoader();
065:
066: InputStream is = null;
067:
068: if (loader != null) {
069: is = loader.getResourceAsStream(resourceName);
070: }
071:
072: //see sourceforge bug 863053, this is a fix for a user that
073: //needed to have PDFBox loaded by the bootstrap classloader
074: if (is == null) {
075: loader = ClassLoader.getSystemClassLoader();
076: if (loader != null) {
077: is = loader.getResourceAsStream(resourceName);
078: }
079: }
080:
081: if (is == null) {
082: File f = new File(resourceName);
083: if (f.exists()) {
084: is = new FileInputStream(f);
085: }
086: }
087:
088: return is;
089: }
090:
091: /**
092: * This will attempt to load the resource given the resource name.
093: *
094: * @param resourceName The resource to try and load.
095: *
096: * @return The resource as a stream or null if it could not be found.
097: *
098: * @throws IOException If there is an error loading the properties.
099: */
100: public static Properties loadProperties(String resourceName)
101: throws IOException {
102: Properties properties = null;
103: InputStream is = null;
104: try {
105: is = loadResource(resourceName);
106: if (is != null) {
107: properties = new Properties();
108: properties.load(is);
109: }
110: } finally {
111: if (is != null) {
112: is.close();
113: }
114: }
115: return properties;
116: }
117:
118: /**
119: * This will attempt to load the resource given the resource name.
120: *
121: * @param resourceName The resource to try and load.
122: * @param defaults A stream of default properties.
123: *
124: * @return The resource as a stream or null if it could not be found.
125: *
126: * @throws IOException If there is an error loading the properties.
127: */
128: public static Properties loadProperties(String resourceName,
129: Properties defaults) throws IOException {
130: InputStream is = null;
131: try {
132: is = loadResource(resourceName);
133: if (is != null) {
134: defaults.load(is);
135: }
136: } finally {
137: if (is != null) {
138: is.close();
139: }
140: }
141: return defaults;
142: }
143: }
|