KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > common > ResourceReader


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * ResourceReader.java
20  *
21  * This class is responsible for reading in string based resources stored within
22  * a loaded class path.
23  */

24
25 // package name
26
package com.rift.coad.lib.common;
27
28 // imports
29
import com.rift.coad.lib.common.*;
30 import java.lang.ClassLoader JavaDoc;
31 import java.io.*;
32 import java.net.URL JavaDoc;
33
34
35 /**
36  * This object is responsible for loading in text resources stored within the
37  * the class path.
38  *
39  * @author Brett Chaldecott
40  */

41 public class ResourceReader {
42     
43     // the classes private member variables
44
private String JavaDoc path = null;
45     private String JavaDoc document = null;
46     
47     /**
48      * Creates a new instance of ResourceReader
49      *
50      * @param path The path to the resource to read.
51      */

52     public ResourceReader(String JavaDoc path) throws Exception JavaDoc {
53         this.path = path;
54         loadDocument(getClass().getClassLoader());
55     }
56     
57     
58     /**
59      * Creates a new instance of ResourceReader
60      *
61      * @param path The path to the resource to read.
62      */

63     public ResourceReader(String JavaDoc path,ClassLoader JavaDoc classLoader)
64             throws Exception JavaDoc {
65         this.path = path;
66         loadDocument(classLoader);
67     }
68     
69     
70     /**
71      * This method returns the path to the resource that has been loaded by this
72      * class.
73      *
74      * @return The string containing the path to load in.
75      */

76     public String JavaDoc getPath() {
77         return path;
78     }
79     
80     
81     /**
82      * This method will return the document that has been loaded in by this
83      * object.
84      *
85      * @return The string containing the loaded document.
86      */

87     public String JavaDoc getDocument() {
88         return document;
89     }
90     
91     
92     /**
93      * This method the the resource into memory.
94      *
95      * @param classLoader The class loader reference.
96      */

97     private void loadDocument(ClassLoader JavaDoc classLoader) throws Exception JavaDoc {
98         try {
99             // set up the file object
100
InputStreamReader reader = new InputStreamReader(classLoader.
101                     getResourceAsStream(path));
102             BufferedReader buffReader = new BufferedReader(reader);
103             StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
104             char[] buffer = new char[1024];
105             int length = 0;
106             while (-1 != (length = buffReader.read(buffer))) {
107                 stringBuffer.append(buffer,0,length);
108             }
109             reader.close();
110             document = stringBuffer.toString();
111         } catch (Exception JavaDoc ex){
112             throw new Exception JavaDoc(
113                     "Failed to load the file [" + path + "]",ex);
114         }
115     }
116 }
117
Popular Tags