source: src/main/java/weka/gui/Loader.java @ 6

Last change on this file since 6 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 3.2 KB
Line 
1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 * Loader.java
19 * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.gui;
24
25import java.io.Reader;
26import java.io.InputStream;
27import java.io.InputStreamReader;
28import java.net.URL;
29
30/**
31 * This class is for loading resources from a JAR archive.
32 *
33 * @author FracPete (fracpete at waikato dot ac dot nz)
34 * @version $Revision: 1.2 $
35 */
36
37public class Loader {
38  /** the dir to use as prefix if filenames are w/o it, must have a slash
39   * at the end (the path separator is a slash!) */
40  private String          dir;
41 
42  /**
43   * initializes the object
44   */
45  public Loader(String dir) {
46    this.dir = dir;
47  }
48 
49  /**
50   * returns the dir prefix
51   */
52  public String getDir() {
53    return dir;
54  }
55 
56  /**
57   * returns the processed filename, i.e. with the dir-prefix if it's
58   * missing
59   */
60  public String processFilename(String filename) {
61    if (!filename.startsWith(getDir()))
62      filename = getDir() + filename;
63   
64    return filename;
65  }
66 
67  /**
68   * returns a URL for the given filename, can be NULL if it fails
69   */
70  public static URL getURL(String dir, String filename) {
71    Loader         loader;
72   
73    loader = new Loader(dir);
74    return loader.getURL(filename);
75  }
76 
77  /**
78   * returns a URL for the given filename, can be NULL if it fails
79   */
80  public URL getURL(String filename) {
81    filename = processFilename(filename);
82    return Loader.class.getClassLoader().getResource(filename);
83  }
84 
85  /**
86   * returns an InputStream for the given dir and filename, can be NULL if it
87   * fails
88   */
89  public static InputStream getInputStream(String dir, String filename) {
90    Loader         loader;
91   
92    loader = new Loader(dir);
93    return loader.getInputStream(filename);
94  }
95 
96  /**
97   * returns an InputStream for the given filename, can be NULL if it fails
98   */
99  public InputStream getInputStream(String filename) {
100    filename = processFilename(filename);
101    return Loader.class.getResourceAsStream(filename);
102  }
103 
104  /**
105   * returns a Reader for the given filename and dir, can be NULL if it fails
106   */
107  public static Reader getReader(String dir, String filename) {
108    Loader            loader;
109   
110    loader = new Loader(dir);
111    return loader.getReader(filename);
112  }
113 
114  /**
115   * returns a Reader for the given filename, can be NULL if it fails
116   */
117  public Reader getReader(String filename) {
118    InputStream          in;
119   
120    in = getInputStream(filename);
121   
122    if (in == null)
123      return null;
124    else
125      return new InputStreamReader(in);
126  }
127}
Note: See TracBrowser for help on using the repository browser.