001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * ResourceReader.java
020: *
021: * This class is responsible for reading in string based resources stored within
022: * a loaded class path.
023: */
024:
025: // package name
026: package com.rift.coad.lib.common;
027:
028: // imports
029: import com.rift.coad.lib.common.*;
030: import java.lang.ClassLoader;
031: import java.io.*;
032: import java.net.URL;
033:
034: /**
035: * This object is responsible for loading in text resources stored within the
036: * the class path.
037: *
038: * @author Brett Chaldecott
039: */
040: public class ResourceReader {
041:
042: // the classes private member variables
043: private String path = null;
044: private String document = null;
045:
046: /**
047: * Creates a new instance of ResourceReader
048: *
049: * @param path The path to the resource to read.
050: */
051: public ResourceReader(String path) throws Exception {
052: this .path = path;
053: loadDocument(getClass().getClassLoader());
054: }
055:
056: /**
057: * Creates a new instance of ResourceReader
058: *
059: * @param path The path to the resource to read.
060: */
061: public ResourceReader(String path, ClassLoader classLoader)
062: throws Exception {
063: this .path = path;
064: loadDocument(classLoader);
065: }
066:
067: /**
068: * This method returns the path to the resource that has been loaded by this
069: * class.
070: *
071: * @return The string containing the path to load in.
072: */
073: public String getPath() {
074: return path;
075: }
076:
077: /**
078: * This method will return the document that has been loaded in by this
079: * object.
080: *
081: * @return The string containing the loaded document.
082: */
083: public String getDocument() {
084: return document;
085: }
086:
087: /**
088: * This method the the resource into memory.
089: *
090: * @param classLoader The class loader reference.
091: */
092: private void loadDocument(ClassLoader classLoader) throws Exception {
093: try {
094: // set up the file object
095: InputStreamReader reader = new InputStreamReader(
096: classLoader.getResourceAsStream(path));
097: BufferedReader buffReader = new BufferedReader(reader);
098: StringBuffer stringBuffer = new StringBuffer();
099: char[] buffer = new char[1024];
100: int length = 0;
101: while (-1 != (length = buffReader.read(buffer))) {
102: stringBuffer.append(buffer, 0, length);
103: }
104: reader.close();
105: document = stringBuffer.toString();
106: } catch (Exception ex) {
107: throw new Exception("Failed to load the file [" + path
108: + "]", ex);
109: }
110: }
111: }
|