src/terralib/dataaccess/datasource/DataSourceFactory.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/dataaccess/core/datasource/DataSourceFactory.cpp
22 
23  \brief A factory of data sources.
24 */
25 
26 // TerraLib
27 #include "../../core/translator/Translator.h"
28 #include "../../core/uri/URI.h"
29 #include "DataSource.h"
30 #include "DataSourceFactory.h"
31 
32 // Boost
33 #include <boost/format.hpp>
34 
35 std::map<std::string, te::da::DataSourceFactory::FactoryFnctType>
37 
38 std::unique_ptr<te::da::DataSource> te::da::DataSourceFactory::make(const std::string& driver, const te::core::URI& connInfo)
39 {
40  std::unique_ptr<DataSource> ds;
41  if (connInfo.isValid())
42  {
43  std::map<std::string, FactoryFnctType>::const_iterator it = sm_factories.find(driver);
44 
45  if (it == sm_factories.end())
46  throw Exception((boost::format(TE_TR("Could not find a data source factory named: %1!")) % connInfo.path()).str());
47 
48  ds.reset(sm_factories[driver](connInfo));
49  }
50 
51  return ds;
52 }
53 
54 std::unique_ptr<te::da::DataSource> te::da::DataSourceFactory::make(const std::string& driver, const std::string& connInfo)
55 {
56  const te::core::URI dsURI(connInfo);
57  std::unique_ptr<DataSource> ds;
58 
59  if(dsURI.isValid())
60  {
61  ds = make(driver, dsURI);
62  }
63 
64  return ds;
65 }
66 
67 bool te::da::DataSourceFactory::find(const std::string& dsType)
68 {
69  std::map<std::string, FactoryFnctType>::const_iterator it = sm_factories.find(dsType);
70 
71  return (it != sm_factories.end());
72 }
73 
74 void te::da::DataSourceFactory::add(const std::string& dsType, FactoryFnctType f)
75 {
76  std::map<std::string, FactoryFnctType>::const_iterator it = sm_factories.find(dsType);
77 
78  if(it != sm_factories.end())
79  throw Exception((boost::format(TE_TR("A data source factory with name '%1' is already registered.")) % dsType).str());
80 
81  sm_factories[dsType] = f;
82 }
83 
84 void te::da::DataSourceFactory::remove(const std::string& dsType)
85 {
86  std::map<std::string, FactoryFnctType>::iterator it = sm_factories.find(dsType);
87 
88  if(it == sm_factories.end())
89  throw Exception((boost::format(TE_TR("There is no registered data source factory named '%1'.")) % dsType).str());
90 
91  sm_factories.erase(it);
92 }
std::string path() const
Retrieving the path.
Definition: URI.cpp:118
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
static std::map< std::string, FactoryFnctType > sm_factories
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
Base exception class for plugin module.
bool isValid() const
Return if the given URI is valid or not.
Definition: URI.cpp:133
static te::dt::Date ds(2010, 01, 01)
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
static void remove(const std::string &dsType)
it finalizes the GDAL factory support.
A factory for data sources.
boost::function< DataSource *(const te::core::URI &uri)> FactoryFnctType
A class for representing an Uniform Resource Identifier (URI).
Definition: URI.h:49
static void add(const std::string &dsType, FactoryFnctType f)