openModeller  Version 1.5.0
SerializedXmlOccurrences.cpp
Go to the documentation of this file.
1 
28 
31 
32 #include <string.h>
33 #include <fstream>
34 
35 /*****************************************/
36 /*** create OccurrencesReader callback ***/
38 SerializedXmlOccurrences::CreateOccurrencesReaderCallback( const char *source, const char *coordSystem )
39 {
40  return new SerializedXmlOccurrences( source, coordSystem );
41 }
42 
43 
44 /*******************/
45 /*** Constructor ***/
46 SerializedXmlOccurrences::SerializedXmlOccurrences( const char *source, const char *coordSystem )
47 {
48  _source = (char *) source;
49 
50  _coord_system = (char *) coordSystem;
51 
52  _loaded = false;
53 }
54 
55 
56 /******************/
57 /*** Destructor ***/
59 {
60 }
61 
62 
63 /***********************/
64 /*** load Ocurrences ***/
65 bool
67 {
68  if ( _loaded ) {
69 
70  return true;
71  }
72 
73  // Try to open file and check if first char is "<". This is the simplest
74  // way to avoid parsing and then having to handle exceptions for files
75  // that are not om serialized XML.
76  // note: in most cases, om serialized XML doesn't contain the XML header
77  // and starts with a blank line.
78  std::ifstream fs( _source, std::ios_base::in );
79 
80  if ( fs.fail() ) {
81 
82  return false;
83  }
84 
85  std::string line("");
86  bool not_found = true;
87 
88  while ( not_found && getline( fs, line ) ) {
89 
90  for ( unsigned int i = 0; i < line.size(); ++i ) {
91 
92  // Skip carriage returns, line feeds and spaces
93  if ( line[i] == '\r' || line[i] == '\n' || line[i] == ' ' ) {
94 
95  continue;
96  }
97 
98  // It may be an XML file
99  if ( line[i] == '<' ) {
100 
101  not_found = false;
102  break;
103  }
104  // Definitely not an XML file
105  else {
106 
107  fs.close();
108  return false;
109  }
110  }
111  }
112 
113  fs.close();
114 
115  Log::instance()->info( "Loading occurrences from file %s.\n", _source );
116 
118 
119  if ( ConfigurationPtr sampler_conf = parent_conf->getSubsection( "Sampler", false ) ) {
120 
121  parent_conf = sampler_conf;
122  }
123 
124  if ( ConstConfigurationPtr presence_conf = parent_conf->getSubsection( "Presence", false ) ) {
125 
126  OccurrencesPtr presences( new OccurrencesImpl( 1.0 ) );
127  presences->setConfiguration( presence_conf );
128 
129  _presences.push_back( presences );
130 
131  _loaded = true;
132  }
133 
134  if ( ConstConfigurationPtr absence_conf = parent_conf->getSubsection( "Absence", false ) ) {
135 
136  OccurrencesPtr absences = new OccurrencesImpl( 0.0 );
137  absences->setConfiguration( absence_conf );
138 
139  _absences.push_back( absences );
140 
141  _loaded = true;
142  }
143 
144  return _loaded;
145 }
LstOccurrences _presences
static ConfigurationPtr readXml(char const *filename)
static OccurrencesReader * CreateOccurrencesReaderCallback(const char *source, const char *coordSystem)
static Log * instance()
Returns the instance pointer, creating the object on the first call.
Definition: Log.cpp:45
LstOccurrences _absences
SerializedXmlOccurrences(const char *source, const char *coordSystem)
void info(const char *format,...)
'Info' level.
Definition: Log.cpp:256