001 /* 002 * Copyright 2007, 2012 Stephen Fisher and Junhyong Kim, University of 003 * Pennsylvania. 004 * 005 * This file is part of Glo-DB. 006 * 007 * Glo-DB is free software: you can redistribute it and/or modify it 008 * under the terms of the GNU General Public License as published by 009 * the Free Software Foundation, either version 3 of the License, or 010 * (at your option) any later version. 011 * 012 * Glo-DB is distributed in the hope that it will be useful, but 013 * WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * General Public License for more details. 016 * 017 * You should have received a copy of the GNU General Public License 018 * along with Glo-DB. If not, see <http://www.gnu.org/licenses/>. 019 * 020 * @(#)GloDBTrack.java 021 */ 022 023 package edu.upenn.gloDB.io; 024 025 import edu.upenn.gloDB.*; 026 import edu.upenn.gloDB.gui.GUIUtils; 027 import java.io.*; 028 import javax.swing.filechooser.FileFilter; 029 030 /** 031 * Import/Export Track data from/to GloDB files. 032 * 033 * XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 034 * THIS FILE IS A PLACE HOLDER 035 * xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 036 * 037 * @author Stephen Fisher 038 * @version $Id: GloDBTrack.java,v 1.1.2.6 2007/02/16 17:14:47 fisher Exp $ 039 */ 040 041 public class GloDBTrack implements TrackFile { 042 043 private final int ID = FileIO.GLODB; 044 private final String DESC = "Glo-DB files (*.glo)"; 045 private final String[] EXT = {".glo"}; 046 private final FileFilter fileFilter = new GloDBFilter(); 047 048 //-------------------------------------------------------------------------- 049 // Setters and Getters 050 051 public int getID() { return ID; } 052 053 public String getDesc() { return DESC; } 054 055 public String[] getExt() { return EXT; } 056 057 public FileFilter getFileFilter() { return fileFilter; } 058 059 //-------------------------------------------------------------------------- 060 // Miscellaneous Methods 061 062 /** 063 * Load all Features in the GloDB file into a single Track and 064 * return the resulting Track object. 065 */ 066 public Track load(String filename) { 067 return load(filename, ""); 068 } 069 070 /** 071 * Load all Features in the GloDB file into a single Track and 072 * return the resulting Track object. If a source isn't 073 * provided, then if appropriate, this will attempt to load source 074 * data from the file. 075 * 076 * @XXX need to throw FileIO exceptions, rather than just print 077 * errors. 078 */ 079 public Track load(String filename, String sourceID) { 080 File file = new File(filename); 081 // return null if the file doesn't exist 082 if (! file.exists()) { 083 GloDBUtils.printMsg("ERROR: file \"" + filename + "\" doesn't exist."); 084 return null; 085 } 086 087 Track track = null; 088 try { 089 // load the Track 090 FileInputStream fStream = new FileInputStream(file); 091 ObjectInputStream oStream = new ObjectInputStream(fStream); 092 track = (Track) oStream.readObject(); 093 oStream.close(); 094 095 // add track to trackPool 096 try { 097 ObjectHandles.addTrack(track); 098 } catch (InvalidIDException e) { 099 String id_new = Track.randomID("_T"); 100 String msg = "ID \"" + track.getID() + "\" already exists, using ID \"" + id_new + "\" instead."; 101 GloDBUtils.printWarning(msg); 102 103 // add self to set of all Tracks, using new ID 104 track.setID(id_new, false); 105 ObjectHandles.addTrack(track); 106 } 107 108 // add the Track to trackPool and return a reference 109 // to the Track 110 ObjectHandles.addTrack(track); 111 return track; 112 } catch (FileNotFoundException e) { 113 // problem with FileInputStream 114 GloDBUtils.printMsg("ERROR: file \"" + filename + "\" can not be opened."); 115 } catch (ClassNotFoundException e) { 116 // problem with ObjectInputStream.readObject(). XXX do we 117 // need to close 'oStream'? 118 GloDBUtils.printMsg("ERROR: input file \"" + filename + "\" does not" 119 + " contain a valid Track."); 120 } catch (IOException e) { 121 // problem with ObjectInputStream. XXX do we need to 122 // close 'oStream'? 123 GloDBUtils.printMsg("ERROR: reading input file \"" + filename + "\"."); 124 } catch (InvalidIDException e) { 125 GloDBUtils.printMsg("ERROR: ID \"" + track.getID() + "\" already exists."); 126 } 127 128 return null; 129 } 130 131 /** 132 * Save the Track to a file based on it's ID. This will 133 * overwrite any existing file and append ".glo" to the filename, 134 * if necessary. 135 */ 136 public void save(String id) { 137 // add ".glo" filename extension, if necessary 138 String filename = id; 139 if (! filename.endsWith(".glo")) filename += ".glo"; 140 141 save(id, filename, true); 142 } 143 144 /** 145 * Save all Features in a GloDB file. If the file already exists, 146 * then overwrite it if 'overwrite' is true. 147 * 148 * @XXX need to throw FileIO exceptions, rather than just print 149 * errors. 150 * @XXX Should probably throw an exception if the file exists and 151 * not supposed to overwrite the file. 152 * @XXX Should offer option to include Sequence data. 153 */ 154 public void save(String id, String filename, boolean overwrite) { 155 File file = new File(filename); 156 // if the file already exists and not supposed to overwrite 157 // it, then return on error. 158 if (file.exists() && (! overwrite)) { 159 GloDBUtils.printMsg("ERROR: file \"" + filename + "\" already exists."); 160 return; 161 } 162 163 try { 164 FileOutputStream fStream = new FileOutputStream(file); 165 ObjectOutputStream oStream = new ObjectOutputStream(fStream); 166 Track f = ObjectHandles.getTrack(id); 167 oStream.writeObject(f); 168 oStream.flush(); 169 oStream.close(); 170 } catch (FileNotFoundException e) { 171 // problem with FileOutputStream 172 GloDBUtils.printMsg("ERROR: file \"" + filename + "\" can not be opened."); 173 } catch (IOException e) { 174 // problem with ObjectOutputStream. XXX do we need to 175 // close 'oStream'? 176 GloDBUtils.printMsg("ERROR: writting output file \"" + filename + "\"."); 177 } 178 } 179 180 /** 181 * GloDB specific FileFilter. 182 * @XXX This should use EXT. 183 */ 184 private class GloDBFilter extends FileFilter { 185 public boolean accept(File f) { 186 // accept directories 187 if (f.isDirectory()) return true; 188 189 // if true, then don't filter by file extensions. 190 if (GUIUtils.showAllFiles()) return true; 191 192 // accept files ending in '.glo' 193 if ((f.getName()).endsWith(".glo")) return true; 194 195 return false; 196 } 197 198 // set the filter's description 199 public String getDescription() { return DESC; } 200 } 201 202 } // GloDBTrack.java 203 204