src/terralib/binding/v8/jsi/dataaccess/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 DataSourceFactory.cpp
22 
23  \brief Utility functions to register the DataSourceFactory class into Google JavaScript V8 engine.
24  */
25 
26 // TerraLib
27 #include "../../../../common/Exception.h"
28 #include "../../../../core/translator/Translator.h"
29 #include "../../../../dataaccess/datasource/DataSourceFactory.h"
30 #include "../../common/Utils.h"
31 #include "DataAccess.h"
32 
33 // STL
34 #include <memory>
35 
36 ::v8::Handle<::v8::Value> DataSourceFactory_Make(const ::v8::Arguments& args)
37 {
38  ::v8::HandleScope hs;
39 
40  if((args.Length() != 1) || args[0].IsEmpty() || !args[0]->IsString())
41  return ::v8::ThrowException(::v8::String::New("Missing parameter for the TeDataSourceFactory. The correct syntax is: var my_ds = TeDataSourceFactory.make(dstype);"));
42 
43  v8::String::Utf8Value jdsType(args[0]->ToString());
44  std::string dsType = *jdsType;
45 
46  try
47  {
48  std::auto_ptr<te::da::DataSource> ds(te::da::DataSourceFactory::make(dsType));
49  ::v8::Local<::v8::Object> jsds = te::v8::common::Make(ds.get(), te::v8::jsi::GetDataSourceTemplate, true);
50  ds.release();
51  return hs.Close(jsds);
52  }
53  catch(const std::exception& e)
54  {
55  return ::v8::ThrowException(::v8::String::New(e.what()));
56  }
57  catch(...)
58  {
59  return ::v8::ThrowException(::v8::String::New(TR_V8JSI("Unexpected exception in make method!")));
60  }
61 }
62 
63 void te::v8::jsi::RegisterDataSourceFactory(::v8::Local<::v8::Object>& global)
64 {
65  ::v8::HandleScope hs;
66 
67  ::v8::Handle<::v8::ObjectTemplate> dsfactory = ::v8::ObjectTemplate::New();
68 
69  dsfactory->Set(::v8::String::NewSymbol("make"), ::v8::FunctionTemplate::New(DataSourceFactory_Make));
70 
71  global->Set(::v8::String::New("TeDataSourceFactory"), dsfactory->NewInstance());
72 }
73 
::v8::Local<::v8::Object > Make(T *obj, TF tfunc, const bool isOwner)
It creates a new JavaScript object from a C++ object (obj).
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
void RegisterDataSourceFactory(::v8::Local<::v8::Object > &global)
It registers the DataSourceFactory class.
JavaScript exporting routine for the TerraLib Data Access module.
static te::dt::Date ds(2010, 01, 01)
::v8::Persistent<::v8::FunctionTemplate > & GetDataSourceTemplate()
It returns a reference to the persistent template of a DataSource object.
#define TR_V8JSI(message)
It marks a string in order to get translated. This is a special mark used in the Vector Geometry modu...
::v8::Handle<::v8::Value > DataSourceFactory_Make(const ::v8::Arguments &args)
std::string ToString(const XMLCh *const value)
It converts the XML string to a standard C++ string.