2012-07-09 20:32:02 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-03-19 01:02:29 +01:00
|
|
|
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
2012-07-09 20:32:02 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "config/configitembuilder.hpp"
|
|
|
|
#include "base/dynamictype.hpp"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <sstream>
|
|
|
|
#include <boost/foreach.hpp>
|
2012-07-06 14:33:10 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
ConfigItemBuilder::ConfigItemBuilder(void)
|
2014-03-23 11:27:40 +01:00
|
|
|
: m_Abstract(false), m_Expressions(make_shared<Array>())
|
2012-07-06 14:33:10 +02:00
|
|
|
{
|
|
|
|
m_DebugInfo.FirstLine = 0;
|
|
|
|
m_DebugInfo.FirstColumn = 0;
|
|
|
|
m_DebugInfo.LastLine = 0;
|
|
|
|
m_DebugInfo.LastColumn = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConfigItemBuilder::ConfigItemBuilder(const DebugInfo& debugInfo)
|
2014-03-23 11:27:40 +01:00
|
|
|
: m_Abstract(false), m_Expressions(make_shared<Array>())
|
2012-07-06 14:33:10 +02:00
|
|
|
{
|
|
|
|
m_DebugInfo = debugInfo;
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
void ConfigItemBuilder::SetType(const String& type)
|
2012-07-06 14:33:10 +02:00
|
|
|
{
|
|
|
|
m_Type = type;
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
void ConfigItemBuilder::SetName(const String& name)
|
2012-07-06 14:33:10 +02:00
|
|
|
{
|
|
|
|
m_Name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigItemBuilder::SetAbstract(bool abstract)
|
|
|
|
{
|
|
|
|
m_Abstract = abstract;
|
|
|
|
}
|
|
|
|
|
2014-03-24 11:23:47 +01:00
|
|
|
void ConfigItemBuilder::SetScope(const Dictionary::Ptr& scope)
|
|
|
|
{
|
|
|
|
m_Scope = scope;
|
|
|
|
}
|
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
void ConfigItemBuilder::SetZone(const String& zone)
|
2014-05-03 20:01:23 +02:00
|
|
|
{
|
2014-05-03 20:02:22 +02:00
|
|
|
m_Zone = zone;
|
2014-05-03 20:01:23 +02:00
|
|
|
}
|
|
|
|
|
2014-10-16 17:44:06 +02:00
|
|
|
void ConfigItemBuilder::AddExpression(const Expression::Ptr& expr)
|
2012-07-06 14:33:10 +02:00
|
|
|
{
|
2014-03-23 11:27:40 +01:00
|
|
|
m_Expressions->Add(expr);
|
2012-07-06 14:33:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ConfigItem::Ptr ConfigItemBuilder::Compile(void)
|
|
|
|
{
|
2013-02-01 14:45:55 +01:00
|
|
|
if (m_Type.IsEmpty()) {
|
2013-03-16 21:18:53 +01:00
|
|
|
std::ostringstream msgbuf;
|
2013-02-01 14:45:55 +01:00
|
|
|
msgbuf << "The type name of an object may not be empty: " << m_DebugInfo;
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument(msgbuf.str()));
|
2013-02-01 14:45:55 +01:00
|
|
|
}
|
|
|
|
|
2013-02-06 00:32:05 +01:00
|
|
|
if (!DynamicType::GetByName(m_Type)) {
|
2013-03-16 21:18:53 +01:00
|
|
|
std::ostringstream msgbuf;
|
2013-02-06 00:32:05 +01:00
|
|
|
msgbuf << "The type '" + m_Type + "' is unknown: " << m_DebugInfo;
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument(msgbuf.str()));
|
2013-02-06 00:32:05 +01:00
|
|
|
}
|
|
|
|
|
2013-02-01 14:45:55 +01:00
|
|
|
if (m_Name.IsEmpty()) {
|
2013-03-16 21:18:53 +01:00
|
|
|
std::ostringstream msgbuf;
|
2013-02-01 14:45:55 +01:00
|
|
|
msgbuf << "The name of an object may not be empty: " << m_DebugInfo;
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument(msgbuf.str()));
|
2013-02-01 14:45:55 +01:00
|
|
|
}
|
2012-07-06 14:33:10 +02:00
|
|
|
|
2014-03-23 11:27:40 +01:00
|
|
|
Array::Ptr exprs = make_shared<Array>();
|
2014-03-28 14:46:19 +01:00
|
|
|
Array::Ptr templateArray = make_shared<Array>();
|
|
|
|
templateArray->Add(m_Name);
|
2014-03-30 10:00:11 +02:00
|
|
|
|
2014-10-16 17:44:06 +02:00
|
|
|
exprs->Add(make_shared<Expression>(&Expression::OpSetPlus,
|
|
|
|
make_shared<Expression>(&Expression::OpLiteral, "templates", m_DebugInfo),
|
|
|
|
make_shared<Expression>(&Expression::OpLiteral, templateArray, m_DebugInfo),
|
2014-03-30 10:00:11 +02:00
|
|
|
m_DebugInfo));
|
|
|
|
|
2014-10-16 17:44:06 +02:00
|
|
|
exprs->Add(make_shared<Expression>(&Expression::OpDict, m_Expressions, true, m_DebugInfo));
|
2014-03-23 11:27:40 +01:00
|
|
|
|
2014-10-16 17:44:06 +02:00
|
|
|
Expression::Ptr exprl = make_shared<Expression>(&Expression::OpDict, exprs, true, m_DebugInfo);
|
2012-07-06 14:33:10 +02:00
|
|
|
|
2013-11-06 08:51:56 +01:00
|
|
|
return make_shared<ConfigItem>(m_Type, m_Name, m_Abstract, exprl,
|
2014-05-03 20:02:22 +02:00
|
|
|
m_DebugInfo, m_Scope, m_Zone);
|
2012-07-06 14:33:10 +02:00
|
|
|
}
|