29
29
import java.io.FileInputStream;
30
30
import java.io.FileNotFoundException;
31
31
import java.io.IOException;
32
import java.util.zip.GZIPInputStream;
35
36
* Abstract superclass for all file loaders.
37
38
* @author fracpete (fracpete at waikato dot ac dot nz)
38
* @version $Revision: 1.2 $
39
* @version $Revision: 1.5 $
40
41
public abstract class AbstractFileLoader
41
42
extends AbstractLoader
50
51
/** Holds the source of the data set. */
51
52
protected File m_sourceFile = null;
54
/** the extension for compressed files */
55
public static String FILE_EXTENSION_COMPRESSED = ".gz";
57
/** use relative file paths */
58
protected boolean m_useRelativePath = false;
54
61
* get the File specified as the source
87
94
* Resets the Loader object and sets the source of the data set to be
88
95
* the supplied File object.
97
* @param file the source file.
98
* @throws IOException if an error occurs
100
public void setSource(File file) throws IOException {
106
throw new IOException("Source file object is null!");
109
String fName = file.getPath();
111
fName = weka.core.Environment.substitute(fName);
112
} catch (Exception e) {
113
throw new IOException(e.getMessage());
115
file = new File(fName);
116
if (file.getName().endsWith(getFileExtension() + FILE_EXTENSION_COMPRESSED)) {
117
setSource(new GZIPInputStream(new FileInputStream(file)));
119
setSource(new FileInputStream(file));
122
catch (FileNotFoundException ex) {
123
throw new IOException("File not found");
126
if (m_useRelativePath) {
128
m_sourceFile = Utils.convertToRelativePath(file);
129
m_File = m_sourceFile.getPath();
130
} catch (Exception ex) {
131
// System.err.println("[AbstractFileLoader] can't convert path to relative path.");
133
m_File = file.getPath();
137
m_File = file.getAbsolutePath();
142
* Resets the Loader object and sets the source of the data set to be
143
* the supplied File object.
90
145
* @param file the source file.
91
146
* @exception IOException if an error occurs
93
148
public void setSource(File file) throws IOException {
94
149
m_structure = null;
95
150
setRetrieval(NONE);
108
163
m_sourceFile = file;
109
164
m_File = file.getAbsolutePath();
168
* Tip text suitable for displaying int the GUI
170
* @return a description of this property as a String
172
public String useRelativePathTipText() {
173
return "Use relative rather than absolute paths";
177
* Set whether to use relative rather than absolute paths
179
* @param rp true if relative paths are to be used
181
public void setUseRelativePath(boolean rp) {
182
m_useRelativePath = rp;
186
* Gets whether relative paths are to be used
188
* @return true if relative paths are to be used
190
public boolean getUseRelativePath() {
191
return m_useRelativePath;