openModeller  Version 1.5.0
OccurrencesFactory.cpp
Go to the documentation of this file.
1 
31 
32 #include <openmodeller/Log.hh>
34 
36 
37 /****************/
38 /*** instance ***/
41 {
42  static OccurrencesFactory _instance;
43 
44  if ( ! _initiated ) {
45 
48 
49 #ifdef TERRALIB_FOUND
51 #endif
52 
53 #ifdef CURL_FOUND
56 #endif
57 
58  _initiated = true;
59  }
60 
61  return _instance;
62 }
63 
64 /***********************/
65 /*** register driver ***/
66 bool
67 OccurrencesFactory::registerDriver( const string& driverId, CreateOccurrencesReaderCallback builder )
68 {
69  return _drivers.insert( DriversMap::value_type( driverId, builder ) ).second;
70 }
71 
72 /*************************/
73 /*** unregister driver ***/
74 bool
75 OccurrencesFactory::unregisterDriver( const string& driverId )
76 {
77  return _drivers.erase( driverId ) != 0;
78 }
79 
80 /******************************/
81 /*** get registered drivers ***/
82 vector<string>
84 {
85  vector<string> driver_ids;
86 
87  DriversMap::const_iterator d = _drivers.begin();
88  DriversMap::const_iterator end = _drivers.end();
89 
90  while ( d != end ) {
91 
92  driver_ids.push_back( d->first );
93 
94  ++d;
95  }
96 
97  return driver_ids;
98 }
99 
100 /**************/
101 /*** create ***/
103 OccurrencesFactory::create( const char * source, const char * coordSystem )
104 {
105  string source_str( source );
106 
107  // It must be TerraLib point data if source starts with "terralib>"
108  int i = source_str.find( "terralib>" );
109 
110  DriversMap::const_iterator di;
111 
112  if ( i == 0 ) {
113 
114  di = _drivers.find( "TerraLib" );
115 
116  if ( di != _drivers.end() ) {
117 
118  OccurrencesReader * te_driver = (di->second)( source, coordSystem );
119  te_driver->load();
120 
121  return te_driver;
122  }
123  else {
124 
125  throw OccurrencesReaderException( "TerraLib driver not found" );
126  }
127  }
128 
129  // It must be GBIF REST if source is the GBIF REST URL for occurrences
130  if ( source_str == "http://data.gbif.org/ws/rest/occurrence/list" ) {
131 
132  di = _drivers.find( "GBIF" );
133 
134  if ( di != _drivers.end() ) {
135 
136  OccurrencesReader * gbif_driver = (di->second)( source, coordSystem );
137  gbif_driver->load();
138 
139  return gbif_driver;
140  }
141  else {
142 
143  throw OccurrencesReaderException( "GBIF driver not found" );
144  }
145  }
146 
147  i = source_str.find( "http://" );
148 
149  if ( i == 0 ) {
150 
151  // Try TAPIR driver first
152  di = _drivers.find( "TAPIR" );
153 
154  if ( di != _drivers.end() ) {
155 
156  OccurrencesReader * tapir_driver = (di->second)( source, coordSystem );
157 
158  if ( tapir_driver->load() ) {
159 
160  return tapir_driver;
161  }
162  }
163 
164  // Then try GBIF (in case they changed the URL or if someone else decided to
165  // implement a service using the GBIF REST protocol)
166  di = _drivers.find( "GBIF" );
167 
168  if ( di != _drivers.end() ) {
169 
170  OccurrencesReader * gbif_driver = (di->second)( source, coordSystem );
171 
172  if ( gbif_driver->load() ) {
173 
174  return gbif_driver;
175  }
176  }
177  }
178 
179  // Try to open as XML file
180  try {
181 
182  Log::instance()->debug( "Trying to open occurrences source as XML file.\n" );
183 
184  OccurrencesReader * xml_driver = new SerializedXmlOccurrences( source, coordSystem );
185 
186  if ( xml_driver->load() ) {
187 
188  return xml_driver;
189  }
190 
191  delete xml_driver;
192  }
193  catch ( OmException& e ) {
194 
195  Log::instance()->debug( "XML occurrences reader exception: %s\n", e.what() );
196  }
197 
198  Log::instance()->debug( "Trying to open occurrences source as TXT file.\n" );
199 
200  // Default driver for delimited text
201  OccurrencesReader * file_driver = new DelimitedTextOccurrences( source, coordSystem );
202 
203  file_driver->load();
204 
205  return file_driver;
206 }
static OccurrencesReader * CreateOccurrencesReaderCallback(const char *source, const char *coordSystem)
static OccurrencesReader * CreateOccurrencesReaderCallback(const char *source, const char *coordSystem)
static OccurrencesReader * CreateOccurrencesReaderCallback(const char *source, const char *coordSystem)
bool unregisterDriver(const string &driverId)
vector< string > getRegisteredDrivers()
static Log * instance()
Returns the instance pointer, creating the object on the first call.
Definition: Log.cpp:45
static OccurrencesFactory & instance()
virtual bool load()=0
bool registerDriver(const string &driverId, CreateOccurrencesReaderCallback builder)
static OccurrencesReader * CreateOccurrencesReaderCallback(const char *source, const char *coordSystem)
static OccurrencesReader * CreateOccurrencesReaderCallback(const char *source, const char *coordSystem)
void debug(const char *format,...)
'Debug' level.
Definition: Log.cpp:237
OccurrencesReader * create(const char *source, const char *coordSystem)