KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > regression > ResourceReader


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: ResourceReader.java,v 1.4 2005/02/21 12:18:40 andy_seaborne Exp $
5 */

6
7 /*
8  * ResourceReader.java
9  *
10  * Created on June 29, 2001, 9:54 PM
11  */

12
13 package com.hp.hpl.jena.regression;
14
15 import java.io.InputStream JavaDoc;
16 import java.io.FileInputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18 /**
19  * read a data file stored on the class path.
20  * To use this, ensure your data is on the class path (e.g. in the
21  * program jar, or in a separate data.jar), and give a relative path
22  * name to the data.
23  * Not intended for an applet environment.
24  *
25  * @author jjc
26  * @version Release='$Name: $' Revision='$Revision: 1.4 $' Date='$Date: 2005/02/21 12:18:40 $'
27  */

28 class ResourceReader {
29     // If false use FileInputSDtream's assuming we are in the correct directory;
30
static boolean useClassLoader = false;
31     /** Creates new ResourceReader
32      * @param resource The filename of the data file relative to the Java classpath.
33      * @exception java.lang.SecurityException If cannot access the classloader, e.g. in applet.
34      * @exception java.lang.IllegalArgumentException If file not found.
35      */

36     private ResourceReader() {}
37     /*
38     public ResourceReader(String resource) throws IOException {
39         super(getInputStream(resource));
40     }
41     */

42     
43     static InputStream JavaDoc getInputStream(String JavaDoc prop) throws IOException JavaDoc {
44         if ( useClassLoader) {
45             ClassLoader JavaDoc loader = ResourceReader.class.getClassLoader();
46             if ( loader == null )
47                 throw new SecurityException JavaDoc("Cannot access class loader");
48             InputStream JavaDoc in = loader.getResourceAsStream(prop);
49             if ( in == null )
50                 throw new IllegalArgumentException JavaDoc("Resource: " + prop + " not found on class path.");
51             return in;
52         } else {
53             return new FileInputStream JavaDoc(prop);
54         }
55     }
56
57 }
58
59 /*
60     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
61     All rights reserved.
62
63     Redistribution and use in source and binary forms, with or without
64     modification, are permitted provided that the following conditions
65     are met:
66
67     1. Redistributions of source code must retain the above copyright
68        notice, this list of conditions and the following disclaimer.
69
70     2. Redistributions in binary form must reproduce the above copyright
71        notice, this list of conditions and the following disclaimer in the
72        documentation and/or other materials provided with the distribution.
73
74     3. The name of the author may not be used to endorse or promote products
75        derived from this software without specific prior written permission.
76
77     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
78     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
79     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
80     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
81     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
82     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
83     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
84     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
85     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
86     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
87 */

88
Popular Tags