00001 ///////////////////////////////////////////////////////////////////////////// 00002 // File: gnSourceFactory.h 00003 // Purpose: Manage data sources 00004 // Description: Manages data sources by tracking open files/databases/URLs 00005 // Changes: 00006 // Version: libGenome 0.5.1 00007 // Author: Aaron Darling 00008 // Modified by: 00009 // Copyright: (c) Aaron Darling 00010 // Licenses: See COPYING file for details 00011 ///////////////////////////////////////////////////////////////////////////// 00012 #ifndef _gnSourceFactory_h_ 00013 #define _gnSourceFactory_h_ 00014 00015 #include "gn/gnDefs.h" 00016 00017 #include <string> 00018 #include <vector> 00019 #include <map> 00020 #include "gn/gnBaseSource.h" 00021 00022 /** 00023 * gnSourceFactory is the middle man when acessing a sequence data source 00024 * It tracks all data sources currently in use, ensuring that a particular 00025 * data source is only opened and parsed once. When opening a data source 00026 * it first tries to interpret the source location as a URL, opening the 00027 * specified file, or downloading it if necessary. If that fails, it will 00028 * attempt to open the source as a file on the local disk. 00029 * gnSourceFactory uses the file extension to determine file format, so a 00030 * file which ends with .fas will be opened by gnFASSource. Finally, 00031 * gnSourceFactory can be given directory paths to search when opening a 00032 * file whose path is not specified. 00033 * IMPORTANT: Do not try to instantiate this class. 00034 * To use this class do the following: 00035 * gnSourceFactory* mySourceFactory = gnSourceFactory::GetSourceFactory(); 00036 */ 00037 class GNDLLEXPORT gnSourceFactory{ 00038 public: 00039 ~gnSourceFactory(); 00040 00041 /** 00042 * Returns the current source factory. 00043 * @return The current source factory. 00044 */ 00045 static gnSourceFactory* GetSourceFactory(); 00046 // Plugin Sources 00047 /** 00048 * Returns the number of file extension to class mappings. 00049 * @return The list size. 00050 */ 00051 uint32 GetSourceClassListSize() const; 00052 /** 00053 * Deletes a file extension to class mapping. 00054 * @param ext The extension to delete. 00055 * @return True if successful. 00056 */ 00057 boolean DelSourceClass( const string& ext ); 00058 /** 00059 * Gets the source class which is mapped to the specified file extension. 00060 * @param ext The extension to delete. 00061 * @return The class associated with the extension. 00062 */ 00063 gnBaseSource* GetSourceClass( const string& ext ) const; 00064 /** 00065 * Gets the source class which would be mapped to the string. 00066 * @param sourceStr The string to check, usually a filename. 00067 * @return The class associated with the extension. 00068 */ 00069 gnBaseSource* MatchSourceClass( const string& sourceStr ) const; 00070 /** 00071 * Checks if the specified file extension is recognized. 00072 * @param ext The extension to check. 00073 * @return True if the extension has a class. 00074 */ 00075 boolean HasSourceClass( const string& ext ) const; 00076 /** 00077 * Maps the specified file extension to the given source class. 00078 * e.g. ".fas" to gnFASSource 00079 * @param ext The extension to map. 00080 * @param source The class to map 00081 * @return True if successful. 00082 */ 00083 boolean SetSourceClass( const string& ext, const gnBaseSource& source ); 00084 /** 00085 * Sets a source class to be the default class for unknown file extensions. 00086 * @param source The default class to map 00087 * @return True if successful. 00088 */ 00089 boolean SetDefaultSourceClass( const gnBaseSource* source ); 00090 /** 00091 * Gets the source class which is the default class for unknown file extensions. 00092 * @return The default class 00093 */ 00094 gnBaseSource* GetDefaultSourceClass() const; 00095 // Directory paths to search for sources 00096 /** 00097 * Returns the number of directory paths to search for files. 00098 * @return The list size. 00099 */ 00100 uint32 GetPathListSize() const; 00101 /** 00102 * Adds the directory to the search path. 00103 * @param path The path to add. 00104 * @return True if successful. 00105 */ 00106 boolean AddPath( const string& path ); 00107 /** 00108 * Deletes the directory path at index i from the search path list. 00109 * @param i The index of the path to delete. 00110 * @return True if successful, false if i is out of range. 00111 */ 00112 boolean DelPath( uint32 i ); 00113 /** 00114 * Inserts the directory path at index i in the search path list. 00115 * @param path The path to insert. 00116 * @param i The index of the path to insert before. 00117 * @return True if successful, false if i is out of range. 00118 */ 00119 boolean InsPath( const string& path, uint32 i ); 00120 /** 00121 * Gets the directory path at index i in the path list. 00122 * @param i The index of the path to get. 00123 * @return The path or an empty string if i is out of range. 00124 */ 00125 string GetPath( uint32 i ) const; 00126 /** 00127 * Checks the path list for the given path. 00128 * @param path The path to look for. 00129 * @return True if the path is in the path list. 00130 */ 00131 boolean HasPath( string path ) const; 00132 // Sources 00133 /** 00134 * Returns the number of open data sources. 00135 * @return The list size. 00136 */ 00137 uint32 GetSourceListSize() const; 00138 /** 00139 * Opens and returns a pointer to a source of genetic sequence data. 00140 * If the source has already been opened, AddSource() returns a copy of the existing source class. 00141 * @param sourceStr The file name or URL where the source is located. 00142 * @param searchPaths Should the path list be searched if the file can't be found. 00143 * @return A pointer to the source. 00144 */ 00145 gnBaseSource* AddSource( const string& sourceStr, boolean searchPaths = true ); 00146 /** 00147 * Gets the source at index i in the source list. 00148 * @param i The index of the source to get. 00149 * @return The source. 00150 */ 00151 gnBaseSource* GetSource( uint32 i ) const; 00152 /** 00153 * Deletes the source at index i in the source list. 00154 * This will close the associated file, network, or database connection. 00155 * @param i The index of the source to delete. 00156 * @throws IndexOutOfBounds if i is too large 00157 */ 00158 void DelSource( uint32 i ); 00159 /** 00160 * Deletes the given source from the source list. 00161 * This will close the associated file, network, or database connection. 00162 * @param source The source to close. 00163 * @return True if successful. 00164 */ 00165 boolean DelSource( const gnBaseSource* source ); 00166 /** 00167 * Gets the source if it has already been opened. 00168 * @param sourceStr The file name or URL where the source is located. 00169 * @param searchPaths Should the path list be searched if the file can't be found. 00170 * @return A pointer to the source. 00171 */ 00172 gnBaseSource* HasSource( string sourceStr, boolean searchPaths = true ) const; 00173 00174 private: 00175 gnSourceFactory(); 00176 gnSourceFactory(gnSourceFactory& gnsf); 00177 gnSourceFactory& operator=(gnSourceFactory& gnsf); 00178 00179 boolean PathExists( string path ) const; 00180 static boolean GetURL( const string& urlStr, string& localFile ); 00181 00182 vector< string > m_pathList; 00183 vector< gnBaseSource* > m_sourceList; 00184 map< string, gnBaseSource* > m_sourceClassList; 00185 gnBaseSource* m_pDefaultSourceClass; 00186 };//class gnSourceFactory 00187 00188 inline 00189 gnSourceFactory* gnSourceFactory::GetSourceFactory() 00190 { 00191 //use construct on first use method to avoid the static constructor 00192 //initialization fiasco... 00193 static gnSourceFactory* m_sSourceFactory = new gnSourceFactory(); 00194 return m_sSourceFactory; 00195 } 00196 00197 // Plugin Sources 00198 inline 00199 uint32 gnSourceFactory::GetSourceClassListSize() const 00200 { 00201 return m_sourceClassList.size(); 00202 } 00203 inline 00204 boolean gnSourceFactory::SetDefaultSourceClass( const gnBaseSource* source ) 00205 { 00206 if(m_pDefaultSourceClass != NULL){ 00207 delete m_pDefaultSourceClass; 00208 } 00209 m_pDefaultSourceClass = source->Clone(); 00210 return true; 00211 } 00212 inline 00213 gnBaseSource* gnSourceFactory::GetDefaultSourceClass() const 00214 { 00215 return m_pDefaultSourceClass; 00216 } 00217 00218 // Directory paths to search for sources 00219 inline 00220 uint32 gnSourceFactory::GetPathListSize() const 00221 { 00222 return m_pathList.size(); 00223 } 00224 inline 00225 uint32 gnSourceFactory::GetSourceListSize() const 00226 { 00227 return m_sourceList.size(); 00228 } 00229 00230 00231 #endif 00232 // _gnSourceFactory_h_