openModeller  Version 1.4.0
SerializedXmlOccurrences.cpp
Go to the documentation of this file.
00001 
00027 #include <openmodeller/occ_io/SerializedXmlOccurrences.hh>
00028 
00029 #include <openmodeller/Occurrences.hh>
00030 #include <openmodeller/Configuration.hh>
00031 
00032 #include <string.h>
00033 #include <fstream>
00034 
00035 /*****************************************/
00036 /*** create OccurrencesReader callback ***/
00037 OccurrencesReader * 
00038 SerializedXmlOccurrences::CreateOccurrencesReaderCallback( const char *source, const char *coordSystem )
00039 {
00040   return new SerializedXmlOccurrences( source, coordSystem );
00041 }
00042 
00043 
00044 /*******************/
00045 /*** Constructor ***/
00046 SerializedXmlOccurrences::SerializedXmlOccurrences( const char *source, const char *coordSystem )
00047 {
00048   _source = (char *) source;
00049 
00050   _coord_system = (char *) coordSystem;
00051 
00052   _loaded = false;
00053 }
00054 
00055 
00056 /******************/
00057 /*** Destructor ***/
00058 SerializedXmlOccurrences::~SerializedXmlOccurrences()
00059 {
00060 }
00061 
00062 
00063 /***********************/
00064 /*** load Ocurrences ***/
00065 bool
00066 SerializedXmlOccurrences::load()
00067 {
00068   if ( _loaded ) {
00069 
00070     return true;
00071   }
00072 
00073   // Try to open file and check if first char is "<". This is the simplest
00074   // way to avoid parsing and then having to handle exceptions for files
00075   // that are not om serialized XML.
00076   // note: in most cases, om serialized XML doesn't contain the XML header
00077   // and starts with a blank line.
00078   std::ifstream fs( _source, std::ios_base::in );
00079 
00080   if ( fs.fail() ) {
00081 
00082     return false;
00083   }
00084 
00085   std::string line("");
00086   bool not_found = true;
00087 
00088   while ( not_found && getline( fs, line ) ) {
00089 
00090     for ( unsigned int i = 0; i < line.size(); ++i ) {    
00091 
00092       // Skip carriage returns, line feeds and spaces
00093       if ( line[i] == '\r' || line[i] == '\n' || line[i] == ' ' ) {
00094 
00095         continue;
00096       }
00097 
00098       // It may be an XML file
00099       if ( line[i] == '<' ) {
00100 
00101         not_found = false;
00102         break;
00103       }
00104       // Definitely not an XML file
00105       else {
00106 
00107         fs.close();
00108         return false;
00109       }
00110     }
00111   }
00112 
00113   fs.close();
00114 
00115   Log::instance()->info( "Loading occurrences from file %s.\n", _source );
00116 
00117   ConfigurationPtr parent_conf = Configuration::readXml( _source );
00118 
00119   if ( ConfigurationPtr sampler_conf = parent_conf->getSubsection( "Sampler", false ) ) {
00120 
00121     parent_conf = sampler_conf;
00122   }
00123 
00124   if ( ConstConfigurationPtr presence_conf = parent_conf->getSubsection( "Presence", false ) ) {
00125 
00126     OccurrencesPtr presences( new OccurrencesImpl( 1.0 ) );
00127     presences->setConfiguration( presence_conf );
00128 
00129     _presences.push_back( presences );
00130 
00131     _loaded = true;
00132   }
00133 
00134   if ( ConstConfigurationPtr absence_conf = parent_conf->getSubsection( "Absence", false ) ) {
00135 
00136     OccurrencesPtr absences = new OccurrencesImpl( 0.0 );
00137     absences->setConfiguration( absence_conf );
00138 
00139     _absences.push_back( absences );
00140 
00141     _loaded = true;
00142   }
00143 
00144   return _loaded;
00145 }