/****************************************************************************** * Icinga 2 * * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) * * * * 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. * ******************************************************************************/ #include "config/expression.h" #include "config/expressionlist.h" #include "base/objectlock.h" #include "base/debug.h" #include "base/array.h" #include #include using namespace icinga; Expression::Expression(const String& key, ExpressionOperator op, const Value& value, const DebugInfo& debuginfo) : m_Key(key), m_Operator(op), m_Value(value), m_DebugInfo(debuginfo) { ASSERT(op != OperatorExecute || value.IsObjectType()); } Value Expression::DeepClone(const Value& value) { if (value.IsObjectType()) { Array::Ptr array = value; Array::Ptr result = make_shared(); ObjectLock olock(array); BOOST_FOREACH(const Value& item, array) { result->Add(DeepClone(item)); } return result; } else if (value.IsObjectType()) { Dictionary::Ptr dict = value; Dictionary::Ptr result = make_shared(); ObjectLock olock(dict); BOOST_FOREACH(const Dictionary::Pair& kv, dict) { result->Set(kv.first, DeepClone(kv.second)); } return result; } return value; } void Expression::Execute(const Dictionary::Ptr& dictionary) const { Value oldValue, newValue; ExpressionList::Ptr valueExprl; Dictionary::Ptr valueDict; Array::Ptr valueArray; if (m_Value.IsObjectType()) valueExprl = m_Value; if (m_Value.IsObjectType()) valueDict = m_Value; if (m_Value.IsObjectType()) valueArray = m_Value; newValue = m_Value; Dictionary::Ptr dict; Array::Ptr array; switch (m_Operator) { case OperatorNop: /* Nothing to do here. */ return; case OperatorExecute: if (!valueExprl) BOOST_THROW_EXCEPTION(std::invalid_argument("Operand for OperatorExecute must be an ExpressionList.")); valueExprl->Execute(dictionary); return; case OperatorSet: if (valueExprl) { dict = make_shared(); valueExprl->Execute(dict); newValue = dict; } else { newValue = DeepClone(newValue); } break; case OperatorPlus: oldValue = dictionary->Get(m_Key); if (oldValue.IsObjectType()) dict = oldValue; if (oldValue.IsObjectType()) array = oldValue; if (valueExprl) { if (!dict) dict = make_shared(); valueExprl->Execute(dict); newValue = dict; } else if (valueDict) { if (!dict) dict = make_shared(); ObjectLock olock(valueDict); String key; Value value; BOOST_FOREACH(const Dictionary::Pair& kv, valueDict) { dict->Set(kv.first, DeepClone(kv.second)); } newValue = dict; } else if (valueArray) { if (!array) array = make_shared(); ObjectLock olock(valueArray); BOOST_FOREACH(const Value& value, valueArray) { array->Add(DeepClone(value)); } newValue = array; } else { std::ostringstream message; message << "+= only works for dictionaries and arrays (" << m_DebugInfo << ")"; BOOST_THROW_EXCEPTION(std::invalid_argument(message.str())); } break; default: BOOST_THROW_EXCEPTION(std::runtime_error("Not yet implemented.")); } dictionary->Set(m_Key, newValue); } void Expression::ExtractPath(const std::vector& path, const ExpressionList::Ptr& result) const { ASSERT(!path.empty()); if (path[0] == m_Key) { if (!m_Value.IsObjectType()) BOOST_THROW_EXCEPTION(std::invalid_argument("Specified path does not exist.")); ExpressionList::Ptr exprl = m_Value; if (path.size() == 1) { result->AddExpression(Expression("", OperatorExecute, exprl, m_DebugInfo)); return; } std::vector sub_path(path.begin() + 1, path.end()); exprl->ExtractPath(sub_path, result); } else if (m_Operator == OperatorExecute) { ExpressionList::Ptr exprl = m_Value; exprl->ExtractPath(path, result); } } void Expression::ExtractFiltered(const std::set& keys, const shared_ptr& result) const { if (keys.find(m_Key) != keys.end()) { result->AddExpression(*this); } else if (m_Operator == OperatorExecute) { ExpressionList::Ptr exprl = m_Value; exprl->ExtractFiltered(keys, result); } } void Expression::ErasePath(const std::vector& path) { ASSERT(!path.empty()); if (path[0] == m_Key) { if (path.size() == 1) { m_Operator = OperatorNop; } else if (m_Value.IsObjectType()) { ExpressionList::Ptr exprl = m_Value; std::vector sub_path(path.begin() + 1, path.end()); exprl->ErasePath(sub_path); } } else if (m_Operator == OperatorExecute) { ExpressionList::Ptr exprl = m_Value; exprl->ErasePath(path); } } void Expression::FindDebugInfoPath(const std::vector& path, DebugInfo& result) const { ASSERT(!path.empty()); if (path[0] == m_Key) { if (path.size() == 1) { result = m_DebugInfo; } else if (m_Value.IsObjectType()) { ExpressionList::Ptr exprl = m_Value; std::vector sub_path(path.begin() + 1, path.end()); exprl->FindDebugInfoPath(sub_path, result); } } else if (m_Operator == OperatorExecute) { ExpressionList::Ptr exprl = m_Value; exprl->FindDebugInfoPath(path, result); } }