| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2002 Jürgen Riegel <[email protected]> * |
| 5 |
* * |
| 6 |
* This file is part of the FreeCAD CAx development system. * |
| 7 |
* * |
| 8 |
* This program is free software; you can redistribute it and/or modify * |
| 9 |
* it under the terms of the GNU Library General Public License (LGPL) * |
| 10 |
* as published by the Free Software Foundation; either version 2 of * |
| 11 |
* the License, or (at your option) any later version. * |
| 12 |
* for detail see the LICENCE text file. * |
| 13 |
* * |
| 14 |
* FreeCAD is distributed in the hope that it will be useful, * |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 17 |
* GNU Library General Public License for more details. * |
| 18 |
* * |
| 19 |
* You should have received a copy of the GNU Library General Public * |
| 20 |
* License along with FreeCAD; if not, write to the Free Software * |
| 21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * |
| 22 |
* USA * |
| 23 |
* * |
| 24 |
***************************************************************************/ |
| 25 |
|
| 26 |
|
| 27 |
#include <algorithm> |
| 28 |
#include <iostream> |
| 29 |
#include <memory> |
| 30 |
#include <xercesc/dom/DOM.hpp> |
| 31 |
#include <xercesc/framework/LocalFileFormatTarget.hpp> |
| 32 |
#include <xercesc/framework/LocalFileInputSource.hpp> |
| 33 |
#include <xercesc/framework/MemBufFormatTarget.hpp> |
| 34 |
#include <xercesc/framework/MemBufInputSource.hpp> |
| 35 |
#include <xercesc/parsers/XercesDOMParser.hpp> |
| 36 |
#include <xercesc/sax/EntityResolver.hpp> |
| 37 |
#include <xercesc/sax/ErrorHandler.hpp> |
| 38 |
#include <xercesc/sax/SAXParseException.hpp> |
| 39 |
#include <sstream> |
| 40 |
#include <string> |
| 41 |
#include <utility> |
| 42 |
|
| 43 |
#include <QFileInfo> |
| 44 |
#include <QLockFile> |
| 45 |
#include <QDir> |
| 46 |
|
| 47 |
#include <FCConfig.h> |
| 48 |
|
| 49 |
#ifdef FC_OS_LINUX |
| 50 |
# include <unistd.h> |
| 51 |
#endif |
| 52 |
|
| 53 |
#include <boost/algorithm/string.hpp> |
| 54 |
#include <fmt/printf.h> |
| 55 |
|
| 56 |
#include "Parameter.h" |
| 57 |
#include "Parameter.inl" |
| 58 |
#include "Console.h" |
| 59 |
#include "Exception.h" |
| 60 |
#include "Tools.h" |
| 61 |
|
| 62 |
FC_LOG_LEVEL_INIT("Parameter", true, true) |
| 63 |
|
| 64 |
#ifndef XERCES_CPP_NAMESPACE_BEGIN |
| 65 |
# define XERCES_CPP_NAMESPACE_QUALIFIER |
| 66 |
using namespace XERCES_CPP_NAMESPACE; |
| 67 |
#else |
| 68 |
XERCES_CPP_NAMESPACE_USE |
| 69 |
#endif |
| 70 |
using namespace Base; |
| 71 |
|
| 72 |
|
| 73 |
#include "XMLTools.h" |
| 74 |
|
| 75 |
//************************************************************************** |
| 76 |
//************************************************************************** |
| 77 |
// private classes declaration: |
| 78 |
// - DOMTreeErrorReporter |
| 79 |
// - StrX |
| 80 |
// - DOMPrintFilter |
| 81 |
// - DOMPrintErrorHandler |
| 82 |
// - XStr |
| 83 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 84 |
|
| 85 |
class DOMTreeErrorReporter: public ErrorHandler |
| 86 |
{ |
| 87 |
public: |
| 88 |
// ----------------------------------------------------------------------- |
| 89 |
// Implementation of the error handler interface |
| 90 |
// ----------------------------------------------------------------------- |
| 91 |
void warning(const SAXParseException& toCatch) override; |
| 92 |
void error(const SAXParseException& toCatch) override; |
| 93 |
void fatalError(const SAXParseException& toCatch) override; |
| 94 |
void resetErrors() override; |
| 95 |
|
| 96 |
// ----------------------------------------------------------------------- |
| 97 |
// Getter methods |
| 98 |
// ----------------------------------------------------------------------- |
| 99 |
bool getSawErrors() const; |
| 100 |
|
| 101 |
// ----------------------------------------------------------------------- |
| 102 |
// Private data members |
| 103 |
// |
| 104 |
// fSawErrors |
| 105 |
// This is set if we get any errors, and is queryable via a getter |
| 106 |
// method. Its used by the main code to suppress output if there are |
| 107 |
// errors. |
| 108 |
// ----------------------------------------------------------------------- |
| 109 |
bool fSawErrors {false}; |
| 110 |
}; |
| 111 |
|
| 112 |
|
| 113 |
class DOMPrintFilter: public DOMLSSerializerFilter |
| 114 |
{ |
| 115 |
public: |
| 116 |
/** @name Constructors */ |
| 117 |
explicit DOMPrintFilter(ShowType whatToShow = DOMNodeFilter::SHOW_ALL); |
| 118 |
//@{ |
| 119 |
|
| 120 |
/** @name Destructors */ |
| 121 |
~DOMPrintFilter() override = default; |
| 122 |
//@{ |
| 123 |
|
| 124 |
/** @ interface from DOMWriterFilter */ |
| 125 |
FilterAction acceptNode(const XERCES_CPP_NAMESPACE_QUALIFIER DOMNode* node) const override; |
| 126 |
//@{ |
| 127 |
|
| 128 |
ShowType getWhatToShow() const override |
| 129 |
{ |
| 130 |
return fWhatToShow; |
| 131 |
} |
| 132 |
|
| 133 |
// unimplemented copy ctor and assignment operator |
| 134 |
DOMPrintFilter(const DOMPrintFilter&) = delete; |
| 135 |
DOMPrintFilter(DOMPrintFilter&&) = delete; |
| 136 |
DOMPrintFilter& operator=(const DOMPrintFilter&) = delete; |
| 137 |
DOMPrintFilter& operator=(DOMPrintFilter&&) = delete; |
| 138 |
|
| 139 |
ShowType fWhatToShow; |
| 140 |
}; |
| 141 |
|
| 142 |
class DOMPrintErrorHandler: public DOMErrorHandler |
| 143 |
{ |
| 144 |
public: |
| 145 |
DOMPrintErrorHandler() = default; |
| 146 |
~DOMPrintErrorHandler() override = default; |
| 147 |
|
| 148 |
/** @name The error handler interface */ |
| 149 |
bool handleError(const DOMError& domError) override; |
| 150 |
void resetErrors() |
| 151 |
{} |
| 152 |
|
| 153 |
/* Unimplemented constructors and operators */ |
| 154 |
DOMPrintErrorHandler(const DOMPrintErrorHandler&) = delete; |
| 155 |
DOMPrintErrorHandler(DOMPrintErrorHandler&&) = delete; |
| 156 |
void operator=(const DOMPrintErrorHandler&) = delete; |
| 157 |
void operator=(DOMPrintErrorHandler&&) = delete; |
| 158 |
}; |
| 159 |
|
| 160 |
|
| 161 |
inline bool DOMTreeErrorReporter::getSawErrors() const |
| 162 |
{ |
| 163 |
return fSawErrors; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
//************************************************************************** |
| 168 |
//************************************************************************** |
| 169 |
// ParameterManager |
| 170 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 171 |
|
| 172 |
|
| 173 |
//************************************************************************** |
| 174 |
// Construction/Destruction |
| 175 |
|
| 176 |
|
| 177 |
/** Default construction |
| 178 |
*/ |
| 179 |
ParameterGrp::ParameterGrp( |
| 180 |
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* GroupNode, |
| 181 |
const char* sName, |
| 182 |
ParameterGrp* Parent |
| 183 |
) |
| 184 |
: _pGroupNode(GroupNode) |
| 185 |
, _Parent(Parent) |
| 186 |
{ |
| 187 |
if (sName) { |
| 188 |
_cName = sName; |
| 189 |
} |
| 190 |
if (_Parent) { |
| 191 |
_Manager = _Parent->_Manager; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
/** Destruction |
| 197 |
* complete destruction of the object |
| 198 |
*/ |
| 199 |
ParameterGrp::~ParameterGrp() |
| 200 |
{ |
| 201 |
for (auto& v : _GroupMap) { |
| 202 |
v.second->_Parent = nullptr; |
| 203 |
v.second->_Manager = nullptr; |
| 204 |
} |
| 205 |
if (_Detached && _pGroupNode) { |
| 206 |
_pGroupNode->release(); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
//************************************************************************** |
| 211 |
// Access methods |
| 212 |
|
| 213 |
void ParameterGrp::copyTo(const Base::Reference<ParameterGrp>& Grp) |
| 214 |
{ |
| 215 |
if (Grp == this) { |
| 216 |
return; |
| 217 |
} |
| 218 |
|
| 219 |
// delete previous content |
| 220 |
Grp->Clear(true); |
| 221 |
|
| 222 |
// copy all |
| 223 |
insertTo(Grp); |
| 224 |
} |
| 225 |
|
| 226 |
void ParameterGrp::insertTo(const Base::Reference<ParameterGrp>& Grp) |
| 227 |
{ |
| 228 |
if (Grp == this) { |
| 229 |
return; |
| 230 |
} |
| 231 |
|
| 232 |
// copy group |
| 233 |
std::vector<Base::Reference<ParameterGrp>> Grps = GetGroups(); |
| 234 |
std::vector<Base::Reference<ParameterGrp>>::iterator It1; |
| 235 |
for (It1 = Grps.begin(); It1 != Grps.end(); ++It1) { |
| 236 |
(*It1)->insertTo(Grp->GetGroup((*It1)->GetGroupName())); |
| 237 |
} |
| 238 |
|
| 239 |
// copy strings |
| 240 |
std::vector<std::pair<std::string, std::string>> StringMap = GetASCIIMap(); |
| 241 |
std::vector<std::pair<std::string, std::string>>::iterator It2; |
| 242 |
for (It2 = StringMap.begin(); It2 != StringMap.end(); ++It2) { |
| 243 |
Grp->SetASCII(It2->first.c_str(), It2->second.c_str()); |
| 244 |
} |
| 245 |
|
| 246 |
// copy bool |
| 247 |
std::vector<std::pair<std::string, bool>> BoolMap = GetBoolMap(); |
| 248 |
std::vector<std::pair<std::string, bool>>::iterator It3; |
| 249 |
for (It3 = BoolMap.begin(); It3 != BoolMap.end(); ++It3) { |
| 250 |
Grp->SetBool(It3->first.c_str(), It3->second); |
| 251 |
} |
| 252 |
|
| 253 |
// copy int |
| 254 |
std::vector<std::pair<std::string, long>> IntMap = GetIntMap(); |
| 255 |
std::vector<std::pair<std::string, long>>::iterator It4; |
| 256 |
for (It4 = IntMap.begin(); It4 != IntMap.end(); ++It4) { |
| 257 |
Grp->SetInt(It4->first.c_str(), It4->second); |
| 258 |
} |
| 259 |
|
| 260 |
// copy float |
| 261 |
std::vector<std::pair<std::string, double>> FloatMap = GetFloatMap(); |
| 262 |
std::vector<std::pair<std::string, double>>::iterator It5; |
| 263 |
for (It5 = FloatMap.begin(); It5 != FloatMap.end(); ++It5) { |
| 264 |
Grp->SetFloat(It5->first.c_str(), It5->second); |
| 265 |
} |
| 266 |
|
| 267 |
// copy uint |
| 268 |
std::vector<std::pair<std::string, unsigned long>> UIntMap = GetUnsignedMap(); |
| 269 |
std::vector<std::pair<std::string, unsigned long>>::iterator It6; |
| 270 |
for (It6 = UIntMap.begin(); It6 != UIntMap.end(); ++It6) { |
| 271 |
Grp->SetUnsigned(It6->first.c_str(), It6->second); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
void ParameterGrp::exportTo(const char* FileName) |
| 276 |
{ |
| 277 |
auto Mngr = ParameterManager::Create(); |
| 278 |
|
| 279 |
Mngr->CreateDocument(); |
| 280 |
|
| 281 |
// copy all into the new document |
| 282 |
insertTo(Base::Reference<ParameterGrp>(Mngr)); |
| 283 |
|
| 284 |
Mngr->SaveDocument(FileName); |
| 285 |
} |
| 286 |
|
| 287 |
void ParameterGrp::importFrom(const char* FileName) |
| 288 |
{ |
| 289 |
auto Mngr = ParameterManager::Create(); |
| 290 |
|
| 291 |
if (Mngr->LoadDocument(FileName) != 1) { |
| 292 |
throw FileException("ParameterGrp::import() cannot load document", FileName); |
| 293 |
} |
| 294 |
|
| 295 |
Mngr->copyTo(Base::Reference<ParameterGrp>(this)); |
| 296 |
} |
| 297 |
|
| 298 |
void ParameterGrp::insert(const char* FileName) |
| 299 |
{ |
| 300 |
auto Mngr = ParameterManager::Create(); |
| 301 |
|
| 302 |
if (Mngr->LoadDocument(FileName) != 1) { |
| 303 |
throw FileException("ParameterGrp::import() cannot load document", FileName); |
| 304 |
} |
| 305 |
|
| 306 |
Mngr->insertTo(Base::Reference<ParameterGrp>(this)); |
| 307 |
} |
| 308 |
|
| 309 |
void ParameterGrp::revert(const char* FileName) |
| 310 |
{ |
| 311 |
auto Mngr = ParameterManager::Create(); |
| 312 |
|
| 313 |
if (Mngr->LoadDocument(FileName) != 1) { |
| 314 |
throw FileException("ParameterGrp::revert() cannot load document", FileName); |
| 315 |
} |
| 316 |
|
| 317 |
revert(Base::Reference<ParameterGrp>(Mngr)); |
| 318 |
} |
| 319 |
|
| 320 |
void ParameterGrp::revert(const Base::Reference<ParameterGrp>& Grp) |
| 321 |
{ |
| 322 |
if (Grp == this) { |
| 323 |
return; |
| 324 |
} |
| 325 |
|
| 326 |
for (auto& grp : Grp->GetGroups()) { |
| 327 |
if (HasGroup(grp->GetGroupName())) { |
| 328 |
GetGroup(grp->GetGroupName())->revert(grp); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
for (const auto& v : Grp->GetASCIIMap()) { |
| 333 |
if (GetASCII(v.first.c_str(), v.second.c_str()) == v.second) { |
| 334 |
RemoveASCII(v.first.c_str()); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
for (const auto& v : Grp->GetBoolMap()) { |
| 339 |
if (GetBool(v.first.c_str(), v.second) == v.second) { |
| 340 |
RemoveBool(v.first.c_str()); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
for (const auto& v : Grp->GetIntMap()) { |
| 345 |
if (GetInt(v.first.c_str(), v.second) == v.second) { |
| 346 |
RemoveInt(v.first.c_str()); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
for (const auto& v : Grp->GetUnsignedMap()) { |
| 351 |
if (GetUnsigned(v.first.c_str(), v.second) == v.second) { |
| 352 |
RemoveUnsigned(v.first.c_str()); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
for (const auto& v : Grp->GetFloatMap()) { |
| 357 |
if (GetFloat(v.first.c_str(), v.second) == v.second) { |
| 358 |
RemoveFloat(v.first.c_str()); |
| 359 |
} |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* ParameterGrp::CreateElement( |
| 364 |
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* Start, |
| 365 |
const char* Type, |
| 366 |
const char* Name |
| 367 |
) |
| 368 |
{ |
| 369 |
if (XMLString::compareString(Start->getNodeName(), XStrLiteral("FCParamGroup").unicodeForm()) != 0 |
| 370 |
&& XMLString::compareString(Start->getNodeName(), XStrLiteral("FCParameters").unicodeForm()) |
| 371 |
!= 0) { |
| 372 |
Base::Console().warning( |
| 373 |
"CreateElement: %s cannot have the element %s of type %s\n", |
| 374 |
StrX(Start->getNodeName()).c_str(), |
| 375 |
Name, |
| 376 |
Type |
| 377 |
); |
| 378 |
return nullptr; |
| 379 |
} |
| 380 |
|
| 381 |
if (_Detached && _Parent) { |
| 382 |
// re-attach the group |
| 383 |
_Parent->_GetGroup(_cName.c_str()); |
| 384 |
} |
| 385 |
|
| 386 |
XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument* pDocument = Start->getOwnerDocument(); |
| 387 |
|
| 388 |
auto pcElem = pDocument->createElement(XStr(Type).unicodeForm()); |
| 389 |
pcElem->setAttribute(XStrLiteral("Name").unicodeForm(), XStr(Name).unicodeForm()); |
| 390 |
Start->appendChild(pcElem); |
| 391 |
|
| 392 |
return pcElem; |
| 393 |
} |
| 394 |
|
| 395 |
Base::Reference<ParameterGrp> ParameterGrp::GetGroup(const char* Name) |
| 396 |
{ |
| 397 |
if (!Name) { |
| 398 |
throw Base::ValueError("Empty group name"); |
| 399 |
} |
| 400 |
|
| 401 |
Base::Reference<ParameterGrp> hGrp = this; |
| 402 |
std::vector<std::string> tokens; |
| 403 |
boost::split(tokens, Name, boost::is_any_of("/")); |
| 404 |
for (auto& token : tokens) { |
| 405 |
boost::trim(token); |
| 406 |
if (token.empty()) { |
| 407 |
continue; |
| 408 |
} |
| 409 |
hGrp = hGrp->_GetGroup(token.c_str()); |
| 410 |
if (!hGrp) { |
| 411 |
// The group is clearing. Return some dummy group to avoid caller |
| 412 |
// crashing for backward compatibility. |
| 413 |
hGrp = new ParameterGrp(); |
| 414 |
hGrp->_cName = Name; |
| 415 |
break; |
| 416 |
} |
| 417 |
} |
| 418 |
if (hGrp == this) { |
| 419 |
throw Base::ValueError("Empty group name"); |
| 420 |
} |
| 421 |
return hGrp; |
| 422 |
} |
| 423 |
|
| 424 |
Base::Reference<ParameterGrp> ParameterGrp::_GetGroup(const char* Name) |
| 425 |
{ |
| 426 |
Base::Reference<ParameterGrp> rParamGrp; |
| 427 |
if (!_pGroupNode) { |
| 428 |
if (FC_LOG_INSTANCE.isEnabled(FC_LOGLEVEL_LOG)) { |
| 429 |
FC_WARN("Adding group " << Name << " in an orphan group " << _cName); |
| 430 |
} |
| 431 |
return rParamGrp; |
| 432 |
} |
| 433 |
if (_Clearing) { |
| 434 |
if (FC_LOG_INSTANCE.isEnabled(FC_LOGLEVEL_LOG)) { |
| 435 |
FC_WARN("Adding group " << Name << " while clearing " << GetPath()); |
| 436 |
} |
| 437 |
return rParamGrp; |
| 438 |
} |
| 439 |
|
| 440 |
DOMElement* pcTemp {}; |
| 441 |
|
| 442 |
// search if Group node already there |
| 443 |
pcTemp = FindElement(_pGroupNode, "FCParamGroup", Name); |
| 444 |
|
| 445 |
// already created? |
| 446 |
if (!(rParamGrp = _GroupMap[Name]).isValid()) { |
| 447 |
if (!pcTemp) { |
| 448 |
pcTemp = CreateElement(_pGroupNode, "FCParamGroup", Name); |
| 449 |
} |
| 450 |
// create and register handle |
| 451 |
rParamGrp = Base::Reference<ParameterGrp>(new ParameterGrp(pcTemp, Name, this)); |
| 452 |
_GroupMap[Name] = rParamGrp; |
| 453 |
} |
| 454 |
else if (!pcTemp) { |
| 455 |
_pGroupNode->appendChild(rParamGrp->_pGroupNode); |
| 456 |
rParamGrp->_Detached = false; |
| 457 |
if (this->_Detached && this->_Parent) { |
| 458 |
// Re-attach the group. Note that this may fail if the parent is |
| 459 |
// clearing. That's why we check this->_Detached below. |
| 460 |
this->_Parent->_GetGroup(_cName.c_str()); |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
if (!pcTemp && !this->_Detached) { |
| 465 |
_Notify(ParamType::FCGroup, Name, Name); |
| 466 |
} |
| 467 |
|
| 468 |
return rParamGrp; |
| 469 |
} |
| 470 |
|
| 471 |
std::string ParameterGrp::GetPath() const |
| 472 |
{ |
| 473 |
std::string path; |
| 474 |
if (_Parent && _Parent != _Manager) { |
| 475 |
path = _Parent->GetPath(); |
| 476 |
} |
| 477 |
if (!path.empty() && !_cName.empty()) { |
| 478 |
path += "/"; |
| 479 |
} |
| 480 |
path += _cName; |
| 481 |
return path; |
| 482 |
} |
| 483 |
|
| 484 |
std::vector<Base::Reference<ParameterGrp>> ParameterGrp::GetGroups() |
| 485 |
{ |
| 486 |
Base::Reference<ParameterGrp> rParamGrp; |
| 487 |
std::vector<Base::Reference<ParameterGrp>> vrParamGrp; |
| 488 |
|
| 489 |
if (!_pGroupNode) { |
| 490 |
return vrParamGrp; |
| 491 |
} |
| 492 |
|
| 493 |
std::string Name; |
| 494 |
|
| 495 |
DOMElement* pcTemp = FindElement(_pGroupNode, "FCParamGroup"); |
| 496 |
while (pcTemp) { |
| 497 |
Name = StrX(pcTemp->getAttributes() |
| 498 |
->getNamedItem(XStrLiteral("Name").unicodeForm()) |
| 499 |
->getNodeValue()) |
| 500 |
.c_str(); |
| 501 |
// already created? |
| 502 |
if (!(rParamGrp = _GroupMap[Name]).isValid()) { |
| 503 |
rParamGrp = Base::Reference<ParameterGrp>(new ParameterGrp(pcTemp, Name.c_str(), this)); |
| 504 |
_GroupMap[Name] = rParamGrp; |
| 505 |
} |
| 506 |
vrParamGrp.push_back(rParamGrp); |
| 507 |
// go to next |
| 508 |
pcTemp = FindNextElement(pcTemp, "FCParamGroup"); |
| 509 |
} |
| 510 |
|
| 511 |
return vrParamGrp; |
| 512 |
} |
| 513 |
|
| 514 |
/// test if this group is empty |
| 515 |
bool ParameterGrp::IsEmpty() const |
| 516 |
{ |
| 517 |
return !(_pGroupNode && _pGroupNode->getFirstChild()); |
| 518 |
} |
| 519 |
|
| 520 |
/// test if a special sub group is in this group |
| 521 |
bool ParameterGrp::HasGroup(const char* Name) const |
| 522 |
{ |
| 523 |
if (_GroupMap.find(Name) != _GroupMap.end()) { |
| 524 |
return true; |
| 525 |
} |
| 526 |
|
| 527 |
if (_pGroupNode && FindElement(_pGroupNode, "FCParamGroup", Name) != nullptr) { |
| 528 |
return true; |
| 529 |
} |
| 530 |
|
| 531 |
return false; |
| 532 |
} |
| 533 |
|
| 534 |
const char* ParameterGrp::TypeName(ParamType Type) |
| 535 |
{ |
| 536 |
switch (Type) { |
| 537 |
case ParamType::FCBool: |
| 538 |
return "FCBool"; |
| 539 |
case ParamType::FCInt: |
| 540 |
return "FCInt"; |
| 541 |
case ParamType::FCUInt: |
| 542 |
return "FCUInt"; |
| 543 |
case ParamType::FCText: |
| 544 |
return "FCText"; |
| 545 |
case ParamType::FCFloat: |
| 546 |
return "FCFloat"; |
| 547 |
case ParamType::FCGroup: |
| 548 |
return "FCParamGroup"; |
| 549 |
default: |
| 550 |
return nullptr; |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
ParameterGrp::ParamType ParameterGrp::TypeValue(const char* Name) |
| 555 |
{ |
| 556 |
if (Name) { |
| 557 |
if (boost::equals(Name, "FCBool")) { |
| 558 |
return ParamType::FCBool; |
| 559 |
} |
| 560 |
if (boost::equals(Name, "FCInt")) { |
| 561 |
return ParamType::FCInt; |
| 562 |
} |
| 563 |
if (boost::equals(Name, "FCUInt")) { |
| 564 |
return ParamType::FCUInt; |
| 565 |
} |
| 566 |
if (boost::equals(Name, "FCText")) { |
| 567 |
return ParamType::FCText; |
| 568 |
} |
| 569 |
if (boost::equals(Name, "FCFloat")) { |
| 570 |
return ParamType::FCFloat; |
| 571 |
} |
| 572 |
if (boost::equals(Name, "FCParamGroup")) { |
| 573 |
return ParamType::FCGroup; |
| 574 |
} |
| 575 |
} |
| 576 |
return ParamType::FCInvalid; |
| 577 |
} |
| 578 |
|
| 579 |
void ParameterGrp::SetAttribute(ParamType Type, const char* Name, const char* Value) |
| 580 |
{ |
| 581 |
switch (Type) { |
| 582 |
case ParamType::FCBool: |
| 583 |
case ParamType::FCInt: |
| 584 |
case ParamType::FCUInt: |
| 585 |
case ParamType::FCFloat: |
| 586 |
return _SetAttribute(Type, Name, Value); |
| 587 |
case ParamType::FCText: |
| 588 |
return SetASCII(Name, Value); |
| 589 |
case ParamType::FCGroup: |
| 590 |
RenameGrp(Name, Value); |
| 591 |
break; |
| 592 |
default: |
| 593 |
break; |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
void ParameterGrp::RemoveAttribute(ParamType Type, const char* Name) |
| 598 |
{ |
| 599 |
switch (Type) { |
| 600 |
case ParamType::FCBool: |
| 601 |
return RemoveBool(Name); |
| 602 |
case ParamType::FCInt: |
| 603 |
return RemoveInt(Name); |
| 604 |
case ParamType::FCUInt: |
| 605 |
return RemoveUnsigned(Name); |
| 606 |
case ParamType::FCText: |
| 607 |
return RemoveASCII(Name); |
| 608 |
case ParamType::FCFloat: |
| 609 |
return RemoveFloat(Name); |
| 610 |
case ParamType::FCGroup: |
| 611 |
return RemoveGrp(Name); |
| 612 |
default: |
| 613 |
break; |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
const char* ParameterGrp::GetAttribute( |
| 618 |
ParamType Type, |
| 619 |
const char* Name, |
| 620 |
std::string& Value, |
| 621 |
const char* Default |
| 622 |
) const |
| 623 |
{ |
| 624 |
if (!_pGroupNode) { |
| 625 |
return Default; |
| 626 |
} |
| 627 |
|
| 628 |
const char* T = TypeName(Type); |
| 629 |
if (!T) { |
| 630 |
return Default; |
| 631 |
} |
| 632 |
|
| 633 |
DOMElement* pcElem = FindElement(_pGroupNode, T, Name); |
| 634 |
if (!pcElem) { |
| 635 |
return Default; |
| 636 |
} |
| 637 |
|
| 638 |
if (Type == ParamType::FCText) { |
| 639 |
Value = GetASCII(Name, Default); |
| 640 |
} |
| 641 |
else if (Type != ParamType::FCGroup) { |
| 642 |
Value = StrX(pcElem->getAttribute(XStrLiteral("Value").unicodeForm())).c_str(); |
| 643 |
} |
| 644 |
return Value.c_str(); |
| 645 |
} |
| 646 |
|
| 647 |
std::vector<std::pair<std::string, std::string>> ParameterGrp::GetAttributeMap( |
| 648 |
ParamType Type, |
| 649 |
const char* sFilter |
| 650 |
) const |
| 651 |
{ |
| 652 |
std::vector<std::pair<std::string, std::string>> res; |
| 653 |
if (!_pGroupNode) { |
| 654 |
return res; |
| 655 |
} |
| 656 |
|
| 657 |
const char* T = TypeName(Type); |
| 658 |
if (!T) { |
| 659 |
return res; |
| 660 |
} |
| 661 |
|
| 662 |
std::string Name; |
| 663 |
|
| 664 |
DOMElement* pcTemp = FindElement(_pGroupNode, T); |
| 665 |
while (pcTemp) { |
| 666 |
Name = StrX( |
| 667 |
static_cast<DOMElement*>(pcTemp) |
| 668 |
->getAttributes() |
| 669 |
->getNamedItem(XStrLiteral("Name").unicodeForm()) |
| 670 |
->getNodeValue() |
| 671 |
) |
| 672 |
.c_str(); |
| 673 |
// check on filter condition |
| 674 |
if (!sFilter || Name.find(sFilter) != std::string::npos) { |
| 675 |
if (Type == ParamType::FCGroup) { |
| 676 |
res.emplace_back(Name, std::string()); |
| 677 |
} |
| 678 |
else { |
| 679 |
res.emplace_back( |
| 680 |
Name, |
| 681 |
StrX(static_cast<DOMElement*>(pcTemp)->getAttribute(XStrLiteral("Value").unicodeForm())) |
| 682 |
.c_str() |
| 683 |
); |
| 684 |
} |
| 685 |
} |
| 686 |
pcTemp = FindNextElement(pcTemp, T); |
| 687 |
} |
| 688 |
return res; |
| 689 |
} |
| 690 |
|
| 691 |
void ParameterGrp::_Notify(ParamType Type, const char* Name, const char* Value) |
| 692 |
{ |
| 693 |
if (_Manager) { |
| 694 |
_Manager->signalParamChanged(this, Type, Name, Value); |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
void ParameterGrp::_SetAttribute(ParamType T, const char* Name, const char* Value) |
| 699 |
{ |
| 700 |
const char* Type = TypeName(T); |
| 701 |
if (!Type) { |
| 702 |
return; |
| 703 |
} |
| 704 |
if (!_pGroupNode) { |
| 705 |
if (FC_LOG_INSTANCE.isEnabled(FC_LOGLEVEL_LOG)) { |
| 706 |
FC_WARN("Setting attribute " << Type << ":" << Name << " in an orphan group " << _cName); |
| 707 |
} |
| 708 |
return; |
| 709 |
} |
| 710 |
if (_Clearing) { |
| 711 |
if (FC_LOG_INSTANCE.isEnabled(FC_LOGLEVEL_LOG)) { |
| 712 |
FC_WARN("Adding attribute " << Type << ":" << Name << " while clearing " << GetPath()); |
| 713 |
} |
| 714 |
return; |
| 715 |
} |
| 716 |
|
| 717 |
// find or create the Element |
| 718 |
DOMElement* pcElem = FindOrCreateElement(_pGroupNode, Type, Name); |
| 719 |
if (pcElem) { |
| 720 |
XStr attr("Value"); |
| 721 |
// set the value only if different |
| 722 |
if (strcmp(StrX(pcElem->getAttribute(attr.unicodeForm())).c_str(), Value) != 0) { |
| 723 |
pcElem->setAttribute(attr.unicodeForm(), XStr(Value).unicodeForm()); |
| 724 |
// trigger observer |
| 725 |
_Notify(T, Name, Value); |
| 726 |
} |
| 727 |
// For backward compatibility, old observer gets notified regardless of |
| 728 |
// value changes or not. |
| 729 |
Notify(Name); |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
bool ParameterGrp::GetBool(const char* Name, bool bPreset) const |
| 734 |
{ |
| 735 |
if (!_pGroupNode) { |
| 736 |
return bPreset; |
| 737 |
} |
| 738 |
|
| 739 |
// check if Element in group |
| 740 |
DOMElement* pcElem = FindElement(_pGroupNode, "FCBool", Name); |
| 741 |
// if not return preset |
| 742 |
if (!pcElem) { |
| 743 |
return bPreset; |
| 744 |
} |
| 745 |
|
| 746 |
// if yes check the value and return |
| 747 |
return (strcmp(StrX(pcElem->getAttribute(XStrLiteral("Value").unicodeForm())).c_str(), "1") == 0); |
| 748 |
} |
| 749 |
|
| 750 |
void ParameterGrp::SetBool(const char* Name, bool bValue) |
| 751 |
{ |
| 752 |
_SetAttribute(ParamType::FCBool, Name, bValue ? "1" : "0"); |
| 753 |
} |
| 754 |
|
| 755 |
std::vector<bool> ParameterGrp::GetBools(const char* sFilter) const |
| 756 |
{ |
| 757 |
std::vector<bool> vrValues; |
| 758 |
if (!_pGroupNode) { |
| 759 |
return vrValues; |
| 760 |
} |
| 761 |
|
| 762 |
std::string Name; |
| 763 |
|
| 764 |
DOMElement* pcTemp = FindElement(_pGroupNode, "FCBool"); |
| 765 |
while (pcTemp) { |
| 766 |
Name = StrX(pcTemp->getAttribute(XStrLiteral("Name").unicodeForm())).c_str(); |
| 767 |
// check on filter condition |
| 768 |
if (!sFilter || Name.find(sFilter) != std::string::npos) { |
| 769 |
if (strcmp(StrX(pcTemp->getAttribute(XStrLiteral("Value").unicodeForm())).c_str(), "1") |
| 770 |
!= 0) { |
| 771 |
vrValues.push_back(false); |
| 772 |
} |
| 773 |
else { |
| 774 |
vrValues.push_back(true); |
| 775 |
} |
| 776 |
} |
| 777 |
pcTemp = FindNextElement(pcTemp, "FCBool"); |
| 778 |
} |
| 779 |
|
| 780 |
return vrValues; |
| 781 |
} |
| 782 |
|
| 783 |
std::vector<std::pair<std::string, bool>> ParameterGrp::GetBoolMap(const char* sFilter) const |
| 784 |
{ |
| 785 |
std::vector<std::pair<std::string, bool>> vrValues; |
| 786 |
if (!_pGroupNode) { |
| 787 |
return vrValues; |
| 788 |
} |
| 789 |
|
| 790 |
std::string Name; |
| 791 |
|
| 792 |
DOMElement* pcTemp = FindElement(_pGroupNode, "FCBool"); |
| 793 |
while (pcTemp) { |
| 794 |
Name = StrX(pcTemp->getAttribute(XStrLiteral("Name").unicodeForm())).c_str(); |
| 795 |
// check on filter condition |
| 796 |
if (!sFilter || Name.find(sFilter) != std::string::npos) { |
| 797 |
if (strcmp(StrX(pcTemp->getAttribute(XStrLiteral("Value").unicodeForm())).c_str(), "1") |
| 798 |
!= 0) { |
| 799 |
vrValues.emplace_back(Name, false); |
| 800 |
} |
| 801 |
else { |
| 802 |
vrValues.emplace_back(Name, true); |
| 803 |
} |
| 804 |
} |
| 805 |
pcTemp = FindNextElement(pcTemp, "FCBool"); |
| 806 |
} |
| 807 |
|
| 808 |
return vrValues; |
| 809 |
} |
| 810 |
|
| 811 |
long ParameterGrp::GetInt(const char* Name, long lPreset) const |
| 812 |
{ |
| 813 |
if (!_pGroupNode) { |
| 814 |
return lPreset; |
| 815 |
} |
| 816 |
|
| 817 |
// check if Element in group |
| 818 |
DOMElement* pcElem = FindElement(_pGroupNode, "FCInt", Name); |
| 819 |
// if not return preset |
| 820 |
if (!pcElem) { |
| 821 |
return lPreset; |
| 822 |
} |
| 823 |
// if yes check the value and return |
| 824 |
return atol(StrX(pcElem->getAttribute(XStrLiteral("Value").unicodeForm())).c_str()); |
| 825 |
} |
| 826 |
|
| 827 |
void ParameterGrp::SetInt(const char* Name, long lValue) |
| 828 |
{ |
| 829 |
std::string buf = fmt::sprintf("%li", lValue); |
| 830 |
_SetAttribute(ParamType::FCInt, Name, buf.c_str()); |
| 831 |
} |
| 832 |
|
| 833 |
std::vector<long> ParameterGrp::GetInts(const char* sFilter) const |
| 834 |
{ |
| 835 |
std::vector<long> vrValues; |
| 836 |
if (!_pGroupNode) { |
| 837 |
return vrValues; |
| 838 |
} |
| 839 |
|
| 840 |
std::string Name; |
| 841 |
|
| 842 |
DOMElement* pcTemp = FindElement(_pGroupNode, "FCInt"); |
| 843 |
while (pcTemp) { |
| 844 |
Name = StrX(pcTemp->getAttribute(XStrLiteral("Name").unicodeForm())).c_str(); |
| 845 |
// check on filter condition |
| 846 |
if (!sFilter || Name.find(sFilter) != std::string::npos) { |
| 847 |
vrValues.push_back( |
| 848 |
atol(StrX(pcTemp->getAttribute(XStrLiteral("Value").unicodeForm())).c_str()) |
| 849 |
); |
| 850 |
} |
| 851 |
pcTemp = FindNextElement(pcTemp, "FCInt"); |
| 852 |
} |
| 853 |
|
| 854 |
return vrValues; |
| 855 |
} |
| 856 |
|
| 857 |
std::vector<std::pair<std::string, long>> ParameterGrp::GetIntMap(const char* sFilter) const |
| 858 |
{ |
| 859 |
std::vector<std::pair<std::string, long>> vrValues; |
| 860 |
if (!_pGroupNode) { |
| 861 |
return vrValues; |
| 862 |
} |
| 863 |
|
| 864 |
std::string Name; |
| 865 |
|
| 866 |
DOMElement* pcTemp = FindElement(_pGroupNode, "FCInt"); |
| 867 |
while (pcTemp) { |
| 868 |
Name = StrX(pcTemp->getAttribute(XStrLiteral("Name").unicodeForm())).c_str(); |
| 869 |
// check on filter condition |
| 870 |
if (!sFilter || Name.find(sFilter) != std::string::npos) { |
| 871 |
vrValues.emplace_back( |
| 872 |
Name, |
| 873 |
(atol(StrX(pcTemp->getAttribute(XStrLiteral("Value").unicodeForm())).c_str())) |
| 874 |
); |
| 875 |
} |
| 876 |
pcTemp = FindNextElement(pcTemp, "FCInt"); |
| 877 |
} |
| 878 |
|
| 879 |
return vrValues; |
| 880 |
} |
| 881 |
|
| 882 |
unsigned long ParameterGrp::GetUnsigned(const char* Name, unsigned long lPreset) const |
| 883 |
{ |
| 884 |
if (!_pGroupNode) { |
| 885 |
return lPreset; |
| 886 |
} |
| 887 |
|
| 888 |
// check if Element in group |
| 889 |
DOMElement* pcElem = FindElement(_pGroupNode, "FCUInt", Name); |
| 890 |
// if not return preset |
| 891 |
if (!pcElem) { |
| 892 |
return lPreset; |
| 893 |
} |
| 894 |
|
| 895 |
// if yes check the value and return |
| 896 |
const int base = 10; |
| 897 |
return strtoul(StrX(pcElem->getAttribute(XStrLiteral("Value").unicodeForm())).c_str(), nullptr, base); |
| 898 |
} |
| 899 |
|
| 900 |
void ParameterGrp::SetUnsigned(const char* Name, unsigned long lValue) |
| 901 |
{ |
| 902 |
std::string buf = fmt::sprintf("%lu", lValue); |
| 903 |
_SetAttribute(ParamType::FCUInt, Name, buf.c_str()); |
| 904 |
} |
| 905 |
|
| 906 |
std::vector<unsigned long> ParameterGrp::GetUnsigneds(const char* sFilter) const |
| 907 |
{ |
| 908 |
std::vector<unsigned long> vrValues; |
| 909 |
if (!_pGroupNode) { |
| 910 |
return vrValues; |
| 911 |
} |
| 912 |
|
| 913 |
std::string Name; |
| 914 |
const int base = 10; |
| 915 |
|
| 916 |
DOMElement* pcTemp = FindElement(_pGroupNode, "FCUInt"); |
| 917 |
while (pcTemp) { |
| 918 |
Name = StrX(pcTemp->getAttribute(XStrLiteral("Name").unicodeForm())).c_str(); |
| 919 |
// check on filter condition |
| 920 |
if (!sFilter || Name.find(sFilter) != std::string::npos) { |
| 921 |
vrValues.push_back(strtoul( |
| 922 |
StrX(pcTemp->getAttribute(XStrLiteral("Value").unicodeForm())).c_str(), |
| 923 |
nullptr, |
| 924 |
base |
| 925 |
)); |
| 926 |
} |
| 927 |
pcTemp = FindNextElement(pcTemp, "FCUInt"); |
| 928 |
} |
| 929 |
|
| 930 |
return vrValues; |
| 931 |
} |
| 932 |
|
| 933 |
std::vector<std::pair<std::string, unsigned long>> ParameterGrp::GetUnsignedMap(const char* sFilter) const |
| 934 |
{ |
| 935 |
std::vector<std::pair<std::string, unsigned long>> vrValues; |
| 936 |
if (!_pGroupNode) { |
| 937 |
return vrValues; |
| 938 |
} |
| 939 |
|
| 940 |
std::string Name; |
| 941 |
const int base = 10; |
| 942 |
|
| 943 |
DOMElement* pcTemp = FindElement(_pGroupNode, "FCUInt"); |
| 944 |
while (pcTemp) { |
| 945 |
Name = StrX(pcTemp->getAttribute(XStrLiteral("Name").unicodeForm())).c_str(); |
| 946 |
// check on filter condition |
| 947 |
if (!sFilter || Name.find(sFilter) != std::string::npos) { |
| 948 |
vrValues.emplace_back( |
| 949 |
Name, |
| 950 |
(strtoul( |
| 951 |
StrX(pcTemp->getAttribute(XStrLiteral("Value").unicodeForm())).c_str(), |
| 952 |
nullptr, |
| 953 |
base |
| 954 |
)) |
| 955 |
); |
| 956 |
} |
| 957 |
pcTemp = FindNextElement(pcTemp, "FCUInt"); |
| 958 |
} |
| 959 |
|
| 960 |
return vrValues; |
| 961 |
} |
| 962 |
|
| 963 |
double ParameterGrp::GetFloat(const char* Name, double dPreset) const |
| 964 |
{ |
| 965 |
if (!_pGroupNode) { |
| 966 |
return dPreset; |
| 967 |
} |
| 968 |
|
| 969 |
// check if Element in group |
| 970 |
DOMElement* pcElem = FindElement(_pGroupNode, "FCFloat", Name); |
| 971 |
// if not return preset |
| 972 |
if (!pcElem) { |
| 973 |
return dPreset; |
| 974 |
} |
| 975 |
// if yes check the value and return |
| 976 |
return atof(StrX(pcElem->getAttribute(XStrLiteral("Value").unicodeForm())).c_str()); |
| 977 |
} |
| 978 |
|
| 979 |
void ParameterGrp::SetFloat(const char* Name, double dValue) |
| 980 |
{ |
| 981 |
// use %.12f instead of %f to handle values < 1.0e-6 |
| 982 |
std::string buf = fmt::sprintf("%.12f", dValue); |
| 983 |
_SetAttribute(ParamType::FCFloat, Name, buf.c_str()); |
| 984 |
} |
| 985 |
|
| 986 |
std::vector<double> ParameterGrp::GetFloats(const char* sFilter) const |
| 987 |
{ |
| 988 |
std::vector<double> vrValues; |
| 989 |
if (!_pGroupNode) { |
| 990 |
return vrValues; |
| 991 |
} |
| 992 |
|
| 993 |
std::string Name; |
| 994 |
|
| 995 |
DOMElement* pcTemp = FindElement(_pGroupNode, "FCFloat"); |
| 996 |
while (pcTemp) { |
| 997 |
Name = StrX(pcTemp->getAttribute(XStrLiteral("Name").unicodeForm())).c_str(); |
| 998 |
// check on filter condition |
| 999 |
if (!sFilter || Name.find(sFilter) != std::string::npos) { |
| 1000 |
vrValues.push_back( |
| 1001 |
atof(StrX(pcTemp->getAttribute(XStrLiteral("Value").unicodeForm())).c_str()) |
| 1002 |
); |
| 1003 |
} |
| 1004 |
pcTemp = FindNextElement(pcTemp, "FCFloat"); |
| 1005 |
} |
| 1006 |
|
| 1007 |
return vrValues; |
| 1008 |
} |
| 1009 |
|
| 1010 |
std::vector<std::pair<std::string, double>> ParameterGrp::GetFloatMap(const char* sFilter) const |
| 1011 |
{ |
| 1012 |
std::vector<std::pair<std::string, double>> vrValues; |
| 1013 |
if (!_pGroupNode) { |
| 1014 |
return vrValues; |
| 1015 |
} |
| 1016 |
|
| 1017 |
std::string Name; |
| 1018 |
|
| 1019 |
DOMElement* pcTemp = FindElement(_pGroupNode, "FCFloat"); |
| 1020 |
while (pcTemp) { |
| 1021 |
Name = StrX(pcTemp->getAttribute(XStrLiteral("Name").unicodeForm())).c_str(); |
| 1022 |
// check on filter condition |
| 1023 |
if (!sFilter || Name.find(sFilter) != std::string::npos) { |
| 1024 |
vrValues.emplace_back( |
| 1025 |
Name, |
| 1026 |
(atof(StrX(pcTemp->getAttribute(XStrLiteral("Value").unicodeForm())).c_str())) |
| 1027 |
); |
| 1028 |
} |
| 1029 |
pcTemp = FindNextElement(pcTemp, "FCFloat"); |
| 1030 |
} |
| 1031 |
|
| 1032 |
return vrValues; |
| 1033 |
} |
| 1034 |
|
| 1035 |
|
| 1036 |
void ParameterGrp::SetASCII(const char* Name, const char* sValue) |
| 1037 |
{ |
| 1038 |
if (!_pGroupNode) { |
| 1039 |
if (FC_LOG_INSTANCE.isEnabled(FC_LOGLEVEL_LOG)) { |
| 1040 |
FC_WARN("Setting attribute " << "FCText:" << Name << " in an orphan group " << _cName); |
| 1041 |
} |
| 1042 |
return; |
| 1043 |
} |
| 1044 |
if (_Clearing) { |
| 1045 |
if (FC_LOG_INSTANCE.isEnabled(FC_LOGLEVEL_LOG)) { |
| 1046 |
FC_WARN("Adding attribute " << "FCText:" << Name << " while clearing " << GetPath()); |
| 1047 |
} |
| 1048 |
return; |
| 1049 |
} |
| 1050 |
|
| 1051 |
bool isNew = false; |
| 1052 |
DOMElement* pcElem = FindElement(_pGroupNode, "FCText", Name); |
| 1053 |
if (!pcElem) { |
| 1054 |
pcElem = CreateElement(_pGroupNode, "FCText", Name); |
| 1055 |
isNew = true; |
| 1056 |
} |
| 1057 |
if (pcElem) { |
| 1058 |
// and set the value |
| 1059 |
DOMNode* pcElem2 = pcElem->getFirstChild(); |
| 1060 |
if (!pcElem2) { |
| 1061 |
XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument* pDocument = _pGroupNode->getOwnerDocument(); |
| 1062 |
DOMText* pText = pDocument->createTextNode(XUTF8Str(sValue).unicodeForm()); |
| 1063 |
pcElem->appendChild(pText); |
| 1064 |
if (isNew || sValue[0] != 0) { |
| 1065 |
_Notify(ParamType::FCText, Name, sValue); |
| 1066 |
} |
| 1067 |
} |
| 1068 |
else if (strcmp(StrXUTF8(pcElem2->getNodeValue()).c_str(), sValue) != 0) { |
| 1069 |
pcElem2->setNodeValue(XUTF8Str(sValue).unicodeForm()); |
| 1070 |
_Notify(ParamType::FCText, Name, sValue); |
| 1071 |
} |
| 1072 |
// trigger observer |
| 1073 |
Notify(Name); |
| 1074 |
} |
| 1075 |
} |
| 1076 |
|
| 1077 |
std::string ParameterGrp::GetASCII(const char* Name, const char* pPreset) const |
| 1078 |
{ |
| 1079 |
if (!_pGroupNode) { |
| 1080 |
return pPreset ? pPreset : ""; |
| 1081 |
} |
| 1082 |
|
| 1083 |
// check if Element in group |
| 1084 |
DOMElement* pcElem = FindElement(_pGroupNode, "FCText", Name); |
| 1085 |
// if not return preset |
| 1086 |
if (!pcElem) { |
| 1087 |
if (!pPreset) { |
| 1088 |
return {}; |
| 1089 |
} |
| 1090 |
return {pPreset}; |
| 1091 |
} |
| 1092 |
// if yes check the value and return |
| 1093 |
DOMNode* pcElem2 = pcElem->getFirstChild(); |
| 1094 |
if (pcElem2) { |
| 1095 |
return {StrXUTF8(pcElem2->getNodeValue()).c_str()}; |
| 1096 |
} |
| 1097 |
return {}; |
| 1098 |
} |
| 1099 |
|
| 1100 |
std::vector<std::string> ParameterGrp::GetASCIIs(const char* sFilter) const |
| 1101 |
{ |
| 1102 |
std::vector<std::string> vrValues; |
| 1103 |
if (!_pGroupNode) { |
| 1104 |
return vrValues; |
| 1105 |
} |
| 1106 |
|
| 1107 |
std::string Name; |
| 1108 |
|
| 1109 |
DOMElement* pcTemp = FindElement(_pGroupNode, "FCText"); |
| 1110 |
while (pcTemp) { |
| 1111 |
Name = StrXUTF8(pcTemp->getAttribute(XStrLiteral("Name").unicodeForm())).c_str(); |
| 1112 |
// check on filter condition |
| 1113 |
if (!sFilter || Name.find(sFilter) != std::string::npos) { |
| 1114 |
// retrieve the text element |
| 1115 |
DOMNode* pcElem2 = pcTemp->getFirstChild(); |
| 1116 |
if (pcElem2) { |
| 1117 |
vrValues.emplace_back(StrXUTF8(pcElem2->getNodeValue()).c_str()); |
| 1118 |
} |
| 1119 |
else { |
| 1120 |
vrValues.emplace_back(""); // For a string, an empty value is possible and allowed |
| 1121 |
} |
| 1122 |
} |
| 1123 |
pcTemp = FindNextElement(pcTemp, "FCText"); |
| 1124 |
} |
| 1125 |
|
| 1126 |
return vrValues; |
| 1127 |
} |
| 1128 |
|
| 1129 |
std::vector<std::pair<std::string, std::string>> ParameterGrp::GetASCIIMap(const char* sFilter) const |
| 1130 |
{ |
| 1131 |
std::vector<std::pair<std::string, std::string>> vrValues; |
| 1132 |
if (!_pGroupNode) { |
| 1133 |
return vrValues; |
| 1134 |
} |
| 1135 |
|
| 1136 |
std::string Name; |
| 1137 |
|
| 1138 |
DOMElement* pcTemp = FindElement(_pGroupNode, "FCText"); |
| 1139 |
while (pcTemp) { |
| 1140 |
Name = StrXUTF8(pcTemp->getAttribute(XStrLiteral("Name").unicodeForm())).c_str(); |
| 1141 |
// check on filter condition |
| 1142 |
if (!sFilter || Name.find(sFilter) != std::string::npos) { |
| 1143 |
// retrieve the text element |
| 1144 |
DOMNode* pcElem2 = pcTemp->getFirstChild(); |
| 1145 |
if (pcElem2) { |
| 1146 |
vrValues.emplace_back(Name, std::string(StrXUTF8(pcElem2->getNodeValue()).c_str())); |
| 1147 |
} |
| 1148 |
else { |
| 1149 |
vrValues.emplace_back( |
| 1150 |
Name, |
| 1151 |
std::string() |
| 1152 |
); // For a string, an empty value is possible and allowed |
| 1153 |
} |
| 1154 |
} |
| 1155 |
pcTemp = FindNextElement(pcTemp, "FCText"); |
| 1156 |
} |
| 1157 |
|
| 1158 |
return vrValues; |
| 1159 |
} |
| 1160 |
|
| 1161 |
//************************************************************************** |
| 1162 |
// Access methods |
| 1163 |
|
| 1164 |
void ParameterGrp::RemoveASCII(const char* Name) |
| 1165 |
{ |
| 1166 |
if (!_pGroupNode) { |
| 1167 |
return; |
| 1168 |
} |
| 1169 |
|
| 1170 |
// check if Element in group |
| 1171 |
DOMElement* pcElem = FindElement(_pGroupNode, "FCText", Name); |
| 1172 |
// if not return |
| 1173 |
if (!pcElem) { |
| 1174 |
return; |
| 1175 |
} |
| 1176 |
|
| 1177 |
DOMNode* node = _pGroupNode->removeChild(pcElem); |
| 1178 |
node->release(); |
| 1179 |
|
| 1180 |
// trigger observer |
| 1181 |
_Notify(ParamType::FCText, Name, nullptr); |
| 1182 |
Notify(Name); |
| 1183 |
} |
| 1184 |
|
| 1185 |
void ParameterGrp::RemoveBool(const char* Name) |
| 1186 |
{ |
| 1187 |
if (!_pGroupNode) { |
| 1188 |
return; |
| 1189 |
} |
| 1190 |
|
| 1191 |
// check if Element in group |
| 1192 |
DOMElement* pcElem = FindElement(_pGroupNode, "FCBool", Name); |
| 1193 |
// if not return |
| 1194 |
if (!pcElem) { |
| 1195 |
return; |
| 1196 |
} |
| 1197 |
|
| 1198 |
DOMNode* node = _pGroupNode->removeChild(pcElem); |
| 1199 |
node->release(); |
| 1200 |
|
| 1201 |
// trigger observer |
| 1202 |
_Notify(ParamType::FCBool, Name, nullptr); |
| 1203 |
Notify(Name); |
| 1204 |
} |
| 1205 |
|
| 1206 |
|
| 1207 |
void ParameterGrp::RemoveFloat(const char* Name) |
| 1208 |
{ |
| 1209 |
if (!_pGroupNode) { |
| 1210 |
return; |
| 1211 |
} |
| 1212 |
|
| 1213 |
// check if Element in group |
| 1214 |
DOMElement* pcElem = FindElement(_pGroupNode, "FCFloat", Name); |
| 1215 |
// if not return |
| 1216 |
if (!pcElem) { |
| 1217 |
return; |
| 1218 |
} |
| 1219 |
|
| 1220 |
DOMNode* node = _pGroupNode->removeChild(pcElem); |
| 1221 |
node->release(); |
| 1222 |
|
| 1223 |
// trigger observer |
| 1224 |
_Notify(ParamType::FCFloat, Name, nullptr); |
| 1225 |
Notify(Name); |
| 1226 |
} |
| 1227 |
|
| 1228 |
void ParameterGrp::RemoveInt(const char* Name) |
| 1229 |
{ |
| 1230 |
if (!_pGroupNode) { |
| 1231 |
return; |
| 1232 |
} |
| 1233 |
|
| 1234 |
// check if Element in group |
| 1235 |
DOMElement* pcElem = FindElement(_pGroupNode, "FCInt", Name); |
| 1236 |
// if not return |
| 1237 |
if (!pcElem) { |
| 1238 |
return; |
| 1239 |
} |
| 1240 |
|
| 1241 |
DOMNode* node = _pGroupNode->removeChild(pcElem); |
| 1242 |
node->release(); |
| 1243 |
|
| 1244 |
// trigger observer |
| 1245 |
_Notify(ParamType::FCInt, Name, nullptr); |
| 1246 |
Notify(Name); |
| 1247 |
} |
| 1248 |
|
| 1249 |
void ParameterGrp::RemoveUnsigned(const char* Name) |
| 1250 |
{ |
| 1251 |
if (!_pGroupNode) { |
| 1252 |
return; |
| 1253 |
} |
| 1254 |
|
| 1255 |
// check if Element in group |
| 1256 |
DOMElement* pcElem = FindElement(_pGroupNode, "FCUInt", Name); |
| 1257 |
// if not return |
| 1258 |
if (!pcElem) { |
| 1259 |
return; |
| 1260 |
} |
| 1261 |
|
| 1262 |
DOMNode* node = _pGroupNode->removeChild(pcElem); |
| 1263 |
node->release(); |
| 1264 |
|
| 1265 |
// trigger observer |
| 1266 |
_Notify(ParamType::FCUInt, Name, nullptr); |
| 1267 |
Notify(Name); |
| 1268 |
} |
| 1269 |
|
| 1270 |
Base::Color ParameterGrp::GetColor(const char* Name, Base::Color lPreset) const |
| 1271 |
{ |
| 1272 |
auto packed = GetUnsigned(Name, lPreset.getPackedValue()); |
| 1273 |
|
| 1274 |
return Color(static_cast<uint32_t>(packed)); |
| 1275 |
} |
| 1276 |
|
| 1277 |
void ParameterGrp::SetColor(const char* Name, Base::Color lValue) |
| 1278 |
{ |
| 1279 |
SetUnsigned(Name, lValue.getPackedValue()); |
| 1280 |
} |
| 1281 |
|
| 1282 |
std::vector<Base::Color> ParameterGrp::GetColors(const char* sFilter) const |
| 1283 |
{ |
| 1284 |
auto packed = GetUnsigneds(sFilter); |
| 1285 |
std::vector<Base::Color> result; |
| 1286 |
|
| 1287 |
std::transform( |
| 1288 |
packed.begin(), |
| 1289 |
packed.end(), |
| 1290 |
std::back_inserter(result), |
| 1291 |
[](const unsigned long lValue) { return Color(static_cast<uint32_t>(lValue)); } |
| 1292 |
); |
| 1293 |
|
| 1294 |
return result; |
| 1295 |
} |
| 1296 |
|
| 1297 |
std::vector<std::pair<std::string, Base::Color>> ParameterGrp::GetColorMap(const char* sFilter) const |
| 1298 |
{ |
| 1299 |
auto packed = GetUnsignedMap(sFilter); |
| 1300 |
std::vector<std::pair<std::string, Base::Color>> result; |
| 1301 |
|
| 1302 |
std::transform( |
| 1303 |
packed.begin(), |
| 1304 |
packed.end(), |
| 1305 |
std::back_inserter(result), |
| 1306 |
[](const std::pair<std::string, unsigned long>& lValue) { |
| 1307 |
return std::make_pair(lValue.first, Color(static_cast<uint32_t>(lValue.second))); |
| 1308 |
} |
| 1309 |
); |
| 1310 |
|
| 1311 |
return result; |
| 1312 |
} |
| 1313 |
|
| 1314 |
void ParameterGrp::RemoveColor(const char* Name) |
| 1315 |
{ |
| 1316 |
RemoveUnsigned(Name); |
| 1317 |
} |
| 1318 |
|
| 1319 |
void ParameterGrp::RemoveGrp(const char* Name) |
| 1320 |
{ |
| 1321 |
if (!_pGroupNode) { |
| 1322 |
return; |
| 1323 |
} |
| 1324 |
|
| 1325 |
auto it = _GroupMap.find(Name); |
| 1326 |
if (it == _GroupMap.end()) { |
| 1327 |
return; |
| 1328 |
} |
| 1329 |
|
| 1330 |
// If this or any of its children is referenced by an observer we do not |
| 1331 |
// delete the handle, just in case the group is later added again, or else |
| 1332 |
// those existing observer won't get any notification. BUT, we DO delete |
| 1333 |
// the underlying xml elements, so that we don't save the empty group |
| 1334 |
// later. |
| 1335 |
it->second->Clear(false); |
| 1336 |
if (!it->second->_Detached) { |
| 1337 |
it->second->_Detached = true; |
| 1338 |
_pGroupNode->removeChild(it->second->_pGroupNode); |
| 1339 |
} |
| 1340 |
if (it->second->ShouldRemove()) { |
| 1341 |
it->second->_Parent = nullptr; |
| 1342 |
it->second->_Manager = nullptr; |
| 1343 |
_GroupMap.erase(it); |
| 1344 |
} |
| 1345 |
|
| 1346 |
// trigger observer |
| 1347 |
Notify(Name); |
| 1348 |
} |
| 1349 |
|
| 1350 |
bool ParameterGrp::RenameGrp(const char* OldName, const char* NewName) |
| 1351 |
{ |
| 1352 |
if (!_pGroupNode) { |
| 1353 |
return false; |
| 1354 |
} |
| 1355 |
|
| 1356 |
auto it = _GroupMap.find(OldName); |
| 1357 |
if (it == _GroupMap.end()) { |
| 1358 |
return false; |
| 1359 |
} |
| 1360 |
auto jt = _GroupMap.find(NewName); |
| 1361 |
if (jt != _GroupMap.end()) { |
| 1362 |
return false; |
| 1363 |
} |
| 1364 |
|
| 1365 |
// rename group handle |
| 1366 |
_GroupMap[NewName] = _GroupMap[OldName]; |
| 1367 |
_GroupMap.erase(OldName); |
| 1368 |
_GroupMap[NewName]->_cName = NewName; |
| 1369 |
|
| 1370 |
// check if Element in group |
| 1371 |
DOMElement* pcElem = FindElement(_pGroupNode, "FCParamGroup", OldName); |
| 1372 |
if (pcElem) { |
| 1373 |
pcElem->setAttribute(XStrLiteral("Name").unicodeForm(), XStr(NewName).unicodeForm()); |
| 1374 |
} |
| 1375 |
|
| 1376 |
_Notify(ParamType::FCGroup, NewName, OldName); |
| 1377 |
return true; |
| 1378 |
} |
| 1379 |
|
| 1380 |
void ParameterGrp::Clear(bool notify) |
| 1381 |
{ |
| 1382 |
if (!_pGroupNode) { |
| 1383 |
return; |
| 1384 |
} |
| 1385 |
|
| 1386 |
Base::StateLocker guard(_Clearing); |
| 1387 |
|
| 1388 |
// early trigger notification of group removal when all its children |
| 1389 |
// hierarchies are intact. |
| 1390 |
_Notify(ParamType::FCGroup, nullptr, nullptr); |
| 1391 |
|
| 1392 |
// checking on references |
| 1393 |
for (auto it = _GroupMap.begin(); it != _GroupMap.end();) { |
| 1394 |
// If a group handle is referenced by some observer, then do not remove |
| 1395 |
// it but clear it, so that any existing observer can still get |
| 1396 |
// notification if the group is later on add back. We do remove the |
| 1397 |
// underlying xml element from its parent so that we won't save this |
| 1398 |
// empty group. |
| 1399 |
it->second->Clear(notify); |
| 1400 |
if (!it->second->_Detached) { |
| 1401 |
it->second->_Detached = true; |
| 1402 |
_pGroupNode->removeChild(it->second->_pGroupNode); |
| 1403 |
} |
| 1404 |
if (!it->second->ShouldRemove()) { |
| 1405 |
++it; |
| 1406 |
} |
| 1407 |
else { |
| 1408 |
it->second->_Parent = nullptr; |
| 1409 |
it->second->_Manager = nullptr; |
| 1410 |
it = _GroupMap.erase(it); |
| 1411 |
} |
| 1412 |
} |
| 1413 |
|
| 1414 |
// Remove the rest of non-group nodes; |
| 1415 |
std::vector<std::pair<ParamType, std::string>> params; |
| 1416 |
for (DOMNode *child = _pGroupNode->getFirstChild(), *next = child; child != nullptr; |
| 1417 |
child = next) { |
| 1418 |
next = next->getNextSibling(); |
| 1419 |
ParamType type = TypeValue(StrX(child->getNodeName()).c_str()); |
| 1420 |
if (type != ParamType::FCInvalid && type != ParamType::FCGroup) { |
| 1421 |
params.emplace_back( |
| 1422 |
type, |
| 1423 |
StrX(child->getAttributes()->getNamedItem(XStrLiteral("Name").unicodeForm())->getNodeValue()) |
| 1424 |
.c_str() |
| 1425 |
); |
| 1426 |
} |
| 1427 |
DOMNode* node = _pGroupNode->removeChild(child); |
| 1428 |
node->release(); |
| 1429 |
} |
| 1430 |
|
| 1431 |
for (auto& v : params) { |
| 1432 |
_Notify(v.first, v.second.c_str(), nullptr); |
| 1433 |
if (notify) { |
| 1434 |
Notify(v.second.c_str()); |
| 1435 |
} |
| 1436 |
} |
| 1437 |
|
| 1438 |
// trigger observer |
| 1439 |
Notify(""); |
| 1440 |
} |
| 1441 |
|
| 1442 |
//************************************************************************** |
| 1443 |
// Access methods |
| 1444 |
|
| 1445 |
bool ParameterGrp::ShouldRemove() const |
| 1446 |
{ |
| 1447 |
if (this->getRefCount() > 1) { |
| 1448 |
return false; |
| 1449 |
} |
| 1450 |
|
| 1451 |
return std::all_of(_GroupMap.cbegin(), _GroupMap.cend(), [](const auto& it) { |
| 1452 |
return it.second->ShouldRemove(); |
| 1453 |
}); |
| 1454 |
} |
| 1455 |
|
| 1456 |
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* ParameterGrp::FindElement( |
| 1457 |
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* Start, |
| 1458 |
const char* Type, |
| 1459 |
const char* Name |
| 1460 |
) const |
| 1461 |
{ |
| 1462 |
if (XMLString::compareString(Start->getNodeName(), XStrLiteral("FCParamGroup").unicodeForm()) != 0 |
| 1463 |
&& XMLString::compareString(Start->getNodeName(), XStrLiteral("FCParameters").unicodeForm()) |
| 1464 |
!= 0) { |
| 1465 |
Base::Console().warning( |
| 1466 |
"FindElement: %s cannot have the element %s of type %s\n", |
| 1467 |
StrX(Start->getNodeName()).c_str(), |
| 1468 |
Name, |
| 1469 |
Type |
| 1470 |
); |
| 1471 |
return nullptr; |
| 1472 |
} |
| 1473 |
const XStr xType(Type); |
| 1474 |
const XStr xName(Name); |
| 1475 |
for (DOMNode* clChild = Start->getFirstChild(); clChild != nullptr; |
| 1476 |
clChild = clChild->getNextSibling()) { |
| 1477 |
if (clChild->getNodeType() == DOMNode::ELEMENT_NODE) { |
| 1478 |
// the right node Type |
| 1479 |
if (!XMLString::compareString(xType.unicodeForm(), clChild->getNodeName())) { |
| 1480 |
auto attrs = clChild->getAttributes(); |
| 1481 |
if (attrs->getLength() > 0) { |
| 1482 |
if (Name) { |
| 1483 |
DOMNode* attr = attrs->getNamedItem(XStrLiteral("Name").unicodeForm()); |
| 1484 |
if (attr |
| 1485 |
&& !XMLString::compareString(xName.unicodeForm(), attr->getNodeValue())) { |
| 1486 |
return dynamic_cast<DOMElement*>(clChild); |
| 1487 |
} |
| 1488 |
} |
| 1489 |
else { |
| 1490 |
return dynamic_cast<DOMElement*>(clChild); |
| 1491 |
} |
| 1492 |
} |
| 1493 |
} |
| 1494 |
} |
| 1495 |
} |
| 1496 |
return nullptr; |
| 1497 |
} |
| 1498 |
|
| 1499 |
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* ParameterGrp::FindNextElement( |
| 1500 |
XERCES_CPP_NAMESPACE_QUALIFIER DOMNode* Prev, |
| 1501 |
const char* Type |
| 1502 |
) const |
| 1503 |
{ |
| 1504 |
DOMNode* clChild = Prev; |
| 1505 |
if (!clChild) { |
| 1506 |
return nullptr; |
| 1507 |
} |
| 1508 |
|
| 1509 |
const XStr xType(Type); |
| 1510 |
while ((clChild = clChild->getNextSibling()) != nullptr) { |
| 1511 |
if (clChild->getNodeType() == DOMNode::ELEMENT_NODE) { |
| 1512 |
// the right node Type |
| 1513 |
if (!XMLString::compareString(xType.unicodeForm(), clChild->getNodeName())) { |
| 1514 |
return dynamic_cast<DOMElement*>(clChild); |
| 1515 |
} |
| 1516 |
} |
| 1517 |
} |
| 1518 |
return nullptr; |
| 1519 |
} |
| 1520 |
|
| 1521 |
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* ParameterGrp::FindOrCreateElement( |
| 1522 |
XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* Start, |
| 1523 |
const char* Type, |
| 1524 |
const char* Name |
| 1525 |
) |
| 1526 |
{ |
| 1527 |
// first try to find it |
| 1528 |
DOMElement* pcElem = FindElement(Start, Type, Name); |
| 1529 |
if (pcElem) { |
| 1530 |
return pcElem; |
| 1531 |
} |
| 1532 |
|
| 1533 |
return CreateElement(Start, Type, Name); |
| 1534 |
} |
| 1535 |
|
| 1536 |
XERCES_CPP_NAMESPACE_QUALIFIER DOMNode* ParameterGrp::FindAttribute( |
| 1537 |
XERCES_CPP_NAMESPACE_QUALIFIER DOMNode* Node, |
| 1538 |
const char* Name |
| 1539 |
) const |
| 1540 |
{ |
| 1541 |
DOMNamedNodeMap* attr = Node->getAttributes(); |
| 1542 |
if (attr) { |
| 1543 |
return attr->getNamedItem(XStr(Name).unicodeForm()); |
| 1544 |
} |
| 1545 |
return nullptr; |
| 1546 |
} |
| 1547 |
|
| 1548 |
std::vector<std::pair<ParameterGrp::ParamType, std::string>> ParameterGrp::GetParameterNames( |
| 1549 |
const char* sFilter |
| 1550 |
) const |
| 1551 |
{ |
| 1552 |
std::vector<std::pair<ParameterGrp::ParamType, std::string>> res; |
| 1553 |
if (!_pGroupNode) { |
| 1554 |
return res; |
| 1555 |
} |
| 1556 |
|
| 1557 |
std::string Name; |
| 1558 |
|
| 1559 |
for (DOMNode* clChild = _pGroupNode->getFirstChild(); clChild != nullptr; |
| 1560 |
clChild = clChild->getNextSibling()) { |
| 1561 |
if (clChild->getNodeType() == DOMNode::ELEMENT_NODE) { |
| 1562 |
StrX type(clChild->getNodeName()); |
| 1563 |
ParamType Type = TypeValue(type.c_str()); |
| 1564 |
if (Type != ParamType::FCInvalid && Type != ParamType::FCGroup) { |
| 1565 |
if (clChild->getAttributes()->getLength() > 0) { |
| 1566 |
StrX name(clChild->getAttributes() |
| 1567 |
->getNamedItem(XStrLiteral("Name").unicodeForm()) |
| 1568 |
->getNodeValue()); |
| 1569 |
if (!sFilter || strstr(name.c_str(), sFilter)) { |
| 1570 |
res.emplace_back(Type, name.c_str()); |
| 1571 |
} |
| 1572 |
} |
| 1573 |
} |
| 1574 |
} |
| 1575 |
} |
| 1576 |
return res; |
| 1577 |
} |
| 1578 |
|
| 1579 |
void ParameterGrp::NotifyAll() |
| 1580 |
{ |
| 1581 |
// get all ints and notify |
| 1582 |
std::vector<std::pair<std::string, long>> IntMap = GetIntMap(); |
| 1583 |
for (const auto& it : IntMap) { |
| 1584 |
Notify(it.first.c_str()); |
| 1585 |
} |
| 1586 |
|
| 1587 |
// get all booleans and notify |
| 1588 |
std::vector<std::pair<std::string, bool>> BoolMap = GetBoolMap(); |
| 1589 |
for (const auto& it : BoolMap) { |
| 1590 |
Notify(it.first.c_str()); |
| 1591 |
} |
| 1592 |
|
| 1593 |
// get all Floats and notify |
| 1594 |
std::vector<std::pair<std::string, double>> FloatMap = GetFloatMap(); |
| 1595 |
for (const auto& it : FloatMap) { |
| 1596 |
Notify(it.first.c_str()); |
| 1597 |
} |
| 1598 |
|
| 1599 |
// get all strings and notify |
| 1600 |
std::vector<std::pair<std::string, std::string>> StringMap = GetASCIIMap(); |
| 1601 |
for (const auto& it : StringMap) { |
| 1602 |
Notify(it.first.c_str()); |
| 1603 |
} |
| 1604 |
|
| 1605 |
// get all uints and notify |
| 1606 |
std::vector<std::pair<std::string, unsigned long>> UIntMap = GetUnsignedMap(); |
| 1607 |
for (const auto& it : UIntMap) { |
| 1608 |
Notify(it.first.c_str()); |
| 1609 |
} |
| 1610 |
} |
| 1611 |
|
| 1612 |
void ParameterGrp::_Reset() |
| 1613 |
{ |
| 1614 |
_pGroupNode = nullptr; |
| 1615 |
for (auto& v : _GroupMap) { |
| 1616 |
v.second->_Reset(); |
| 1617 |
} |
| 1618 |
} |
| 1619 |
|
| 1620 |
//************************************************************************** |
| 1621 |
//************************************************************************** |
| 1622 |
// ParameterSerializer |
| 1623 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 1624 |
ParameterSerializer::ParameterSerializer(std::string fn) |
| 1625 |
: filename(std::move(fn)) |
| 1626 |
{} |
| 1627 |
|
| 1628 |
ParameterSerializer::~ParameterSerializer() = default; |
| 1629 |
|
| 1630 |
void ParameterSerializer::SaveDocument(const ParameterManager& mgr) |
| 1631 |
{ |
| 1632 |
mgr.SaveDocument(filename.c_str()); |
| 1633 |
} |
| 1634 |
|
| 1635 |
int ParameterSerializer::LoadDocument(ParameterManager& mgr) |
| 1636 |
{ |
| 1637 |
return mgr.LoadDocument(filename.c_str()); |
| 1638 |
} |
| 1639 |
|
| 1640 |
bool ParameterSerializer::LoadOrCreateDocument(ParameterManager& mgr) |
| 1641 |
{ |
| 1642 |
return mgr.LoadOrCreateDocument(filename.c_str()); |
| 1643 |
} |
| 1644 |
|
| 1645 |
//************************************************************************** |
| 1646 |
//************************************************************************** |
| 1647 |
// ParameterManager |
| 1648 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 1649 |
|
| 1650 |
static XercesDOMParser::ValSchemes gValScheme = XercesDOMParser::Val_Auto; // NOLINT |
| 1651 |
|
| 1652 |
//************************************************************************** |
| 1653 |
// Construction/Destruction |
| 1654 |
|
| 1655 |
/** Default construction |
| 1656 |
*/ |
| 1657 |
ParameterManager::ParameterManager() |
| 1658 |
{ |
| 1659 |
_Manager = this; |
| 1660 |
|
| 1661 |
// initialize the XML system |
| 1662 |
Init(); |
| 1663 |
|
| 1664 |
// --------------------------------------------------------------------------- |
| 1665 |
// Local data |
| 1666 |
// |
| 1667 |
// gXmlFile |
| 1668 |
// The path to the file to parser. Set via command line. |
| 1669 |
// |
| 1670 |
// gDoNamespaces |
| 1671 |
// Indicates whether namespace processing should be done. |
| 1672 |
// |
| 1673 |
// gDoSchema |
| 1674 |
// Indicates whether schema processing should be done. |
| 1675 |
// |
| 1676 |
// gSchemaFullChecking |
| 1677 |
// Indicates whether full schema constraint checking should be done. |
| 1678 |
// |
| 1679 |
// gDoCreate |
| 1680 |
// Indicates whether entity reference nodes needs to be created or not. |
| 1681 |
// Defaults to false. |
| 1682 |
// |
| 1683 |
// gOutputEncoding |
| 1684 |
// The encoding we are to output in. If not set on the command line, |
| 1685 |
// then it is defaults to the encoding of the input XML file. |
| 1686 |
// |
| 1687 |
// gMyEOLSequence |
| 1688 |
// The end of line sequence we are to output. |
| 1689 |
// |
| 1690 |
// gSplitCdataSections |
| 1691 |
// Indicates whether split-cdata-sections is to be enabled or not. |
| 1692 |
// |
| 1693 |
// gDiscardDefaultContent |
| 1694 |
// Indicates whether default content is discarded or not. |
| 1695 |
// |
| 1696 |
// gUseFilter |
| 1697 |
// Indicates if user wants to plug in the DOMPrintFilter. |
| 1698 |
// |
| 1699 |
// gValScheme |
| 1700 |
// Indicates what validation scheme to use. It defaults to 'auto', but |
| 1701 |
// can be set via the -v= command. |
| 1702 |
// |
| 1703 |
// --------------------------------------------------------------------------- |
| 1704 |
|
| 1705 |
// NOLINTBEGIN |
| 1706 |
gIgnoreSave = false; |
| 1707 |
gDoNamespaces = false; |
| 1708 |
gDoSchema = false; |
| 1709 |
gSchemaFullChecking = false; |
| 1710 |
gDoCreate = true; |
| 1711 |
|
| 1712 |
gOutputEncoding = nullptr; |
| 1713 |
gMyEOLSequence = nullptr; |
| 1714 |
|
| 1715 |
gSplitCdataSections = true; |
| 1716 |
gDiscardDefaultContent = true; |
| 1717 |
gUseFilter = true; |
| 1718 |
gFormatPrettyPrint = true; |
| 1719 |
// NOLINTEND |
| 1720 |
} |
| 1721 |
|
| 1722 |
/** Destruction |
| 1723 |
* complete destruction of the object |
| 1724 |
*/ |
| 1725 |
ParameterManager::~ParameterManager() |
| 1726 |
{ |
| 1727 |
_Reset(); |
| 1728 |
delete _pDocument; |
| 1729 |
delete paramSerializer; |
| 1730 |
} |
| 1731 |
|
| 1732 |
Base::Reference<ParameterManager> ParameterManager::Create() |
| 1733 |
{ |
| 1734 |
return {new ParameterManager()}; |
| 1735 |
} |
| 1736 |
|
| 1737 |
void ParameterManager::Init() |
| 1738 |
{ |
| 1739 |
static bool Init = false; |
| 1740 |
if (!Init) { |
| 1741 |
try { |
| 1742 |
XMLPlatformUtils::Initialize(); |
| 1743 |
} |
| 1744 |
catch (const XMLException& toCatch) { |
| 1745 |
#if defined(FC_OS_LINUX) || defined(FC_OS_CYGWIN) |
| 1746 |
std::ostringstream err; |
| 1747 |
#else |
| 1748 |
std::stringstream err; |
| 1749 |
#endif |
| 1750 |
char* pMsg = XMLString::transcode(toCatch.getMessage()); |
| 1751 |
err << "Error during Xerces-c Initialization.\n" |
| 1752 |
<< " Exception message:" << pMsg; |
| 1753 |
XMLString::release(&pMsg); |
| 1754 |
throw XMLBaseException(err.str().c_str()); |
| 1755 |
} |
| 1756 |
Init = true; |
| 1757 |
} |
| 1758 |
} |
| 1759 |
|
| 1760 |
void ParameterManager::Terminate() |
| 1761 |
{ |
| 1762 |
XMLTools::terminate(); |
| 1763 |
XMLPlatformUtils::Terminate(); |
| 1764 |
} |
| 1765 |
|
| 1766 |
//************************************************************************** |
| 1767 |
// Serializer handling |
| 1768 |
|
| 1769 |
void ParameterManager::SetSerializer(ParameterSerializer* ps) |
| 1770 |
{ |
| 1771 |
if (paramSerializer != ps) { |
| 1772 |
delete paramSerializer; |
| 1773 |
} |
| 1774 |
paramSerializer = ps; |
| 1775 |
} |
| 1776 |
|
| 1777 |
bool ParameterManager::HasSerializer() const |
| 1778 |
{ |
| 1779 |
return (paramSerializer != nullptr); |
| 1780 |
} |
| 1781 |
|
| 1782 |
const std::string& ParameterManager::GetSerializeFileName() const |
| 1783 |
{ |
| 1784 |
static const std::string _dummy; |
| 1785 |
return paramSerializer ? paramSerializer->GetFileName() : _dummy; |
| 1786 |
} |
| 1787 |
|
| 1788 |
int ParameterManager::LoadDocument() |
| 1789 |
{ |
| 1790 |
if (paramSerializer) { |
| 1791 |
return paramSerializer->LoadDocument(*this); |
| 1792 |
} |
| 1793 |
|
| 1794 |
return -1; |
| 1795 |
} |
| 1796 |
|
| 1797 |
bool ParameterManager::LoadOrCreateDocument() |
| 1798 |
{ |
| 1799 |
if (paramSerializer) { |
| 1800 |
return paramSerializer->LoadOrCreateDocument(*this); |
| 1801 |
} |
| 1802 |
|
| 1803 |
return false; |
| 1804 |
} |
| 1805 |
|
| 1806 |
void ParameterManager::SaveDocument() const |
| 1807 |
{ |
| 1808 |
if (paramSerializer) { |
| 1809 |
paramSerializer->SaveDocument(*this); |
| 1810 |
} |
| 1811 |
} |
| 1812 |
|
| 1813 |
void ParameterManager::SetIgnoreSave(bool value) |
| 1814 |
{ |
| 1815 |
gIgnoreSave = value; |
| 1816 |
} |
| 1817 |
|
| 1818 |
bool ParameterManager::IgnoreSave() const |
| 1819 |
{ |
| 1820 |
return gIgnoreSave; |
| 1821 |
} |
| 1822 |
|
| 1823 |
namespace |
| 1824 |
{ |
| 1825 |
QString getLockFile(const Base::FileInfo& file) |
| 1826 |
{ |
| 1827 |
QFileInfo fi(QDir::tempPath(), QString::fromStdString(file.fileName() + ".lock")); |
| 1828 |
return fi.absoluteFilePath(); |
| 1829 |
} |
| 1830 |
|
| 1831 |
int getTimeout() |
| 1832 |
{ |
| 1833 |
const int timeout = 5000; |
| 1834 |
return timeout; |
| 1835 |
} |
| 1836 |
} // namespace |
| 1837 |
|
| 1838 |
//************************************************************************** |
| 1839 |
// Document handling |
| 1840 |
|
| 1841 |
bool ParameterManager::LoadOrCreateDocument(const char* sFileName) |
| 1842 |
{ |
| 1843 |
Base::FileInfo file(sFileName); |
| 1844 |
if (file.exists()) { |
| 1845 |
LoadDocument(sFileName); |
| 1846 |
return false; |
| 1847 |
} |
| 1848 |
|
| 1849 |
CreateDocument(); |
| 1850 |
return true; |
| 1851 |
} |
| 1852 |
|
| 1853 |
int ParameterManager::LoadDocument(const char* sFileName) |
| 1854 |
{ |
| 1855 |
try { |
| 1856 |
Base::FileInfo file(sFileName); |
| 1857 |
QLockFile lock(getLockFile(file)); |
| 1858 |
if (!lock.tryLock(getTimeout())) { |
| 1859 |
// Continue with empty config |
| 1860 |
CreateDocument(); |
| 1861 |
SetIgnoreSave(true); |
| 1862 |
std::cerr << "Failed to access file for reading: " << sFileName << std::endl; |
| 1863 |
return 1; |
| 1864 |
} |
| 1865 |
#if defined(FC_OS_WIN32) |
| 1866 |
std::wstring name = file.toStdWString(); |
| 1867 |
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 1868 |
LocalFileInputSource inputSource(reinterpret_cast<const XMLCh*>(name.c_str())); |
| 1869 |
#else |
| 1870 |
LocalFileInputSource inputSource(XStr(file.filePath().c_str()).unicodeForm()); |
| 1871 |
#endif |
| 1872 |
return LoadDocument(inputSource); |
| 1873 |
} |
| 1874 |
catch (const Base::Exception& e) { |
| 1875 |
std::cerr << e.what() << std::endl; |
| 1876 |
throw; |
| 1877 |
} |
| 1878 |
catch (...) { |
| 1879 |
std::cerr << "An error occurred during parsing\n " << std::endl; |
| 1880 |
throw; |
| 1881 |
} |
| 1882 |
} |
| 1883 |
|
| 1884 |
class NoOpEntityResolver: public XERCES_CPP_NAMESPACE_QUALIFIER EntityResolver |
| 1885 |
{ |
| 1886 |
public: |
| 1887 |
InputSource* resolveEntity(const XMLCh* const publicId, const XMLCh* const systemId) override |
| 1888 |
{ |
| 1889 |
(void)publicId; |
| 1890 |
(void)systemId; |
| 1891 |
return nullptr; // Block all external entity resolution |
| 1892 |
} |
| 1893 |
}; |
| 1894 |
|
| 1895 |
int ParameterManager::LoadDocument(const XERCES_CPP_NAMESPACE_QUALIFIER InputSource& inputSource) |
| 1896 |
{ |
| 1897 |
// |
| 1898 |
// Create our parser, then attach an error handler to the parser. |
| 1899 |
// The parser will call back to methods of the ErrorHandler if it |
| 1900 |
// discovers errors during the course of parsing the XML document. |
| 1901 |
// |
| 1902 |
auto parser = std::make_unique<XercesDOMParser>(); |
| 1903 |
auto entityBlocker = std::make_unique<NoOpEntityResolver>(); |
| 1904 |
parser->setValidationScheme(gValScheme); |
| 1905 |
parser->setDoNamespaces(gDoNamespaces); |
| 1906 |
parser->setDoSchema(gDoSchema); |
| 1907 |
parser->setValidationSchemaFullChecking(gSchemaFullChecking); |
| 1908 |
parser->setCreateEntityReferenceNodes(gDoCreate); |
| 1909 |
parser->setDisableDefaultEntityResolution(true); |
| 1910 |
parser->setEntityResolver(entityBlocker.get()); |
| 1911 |
|
| 1912 |
auto errReporter = std::make_unique<DOMTreeErrorReporter>(); |
| 1913 |
parser->setErrorHandler(errReporter.get()); |
| 1914 |
|
| 1915 |
// |
| 1916 |
// Parse the XML file, catching any XML exceptions that might propagate |
| 1917 |
// out of it. |
| 1918 |
// |
| 1919 |
bool errorsOccurred = false; |
| 1920 |
try { |
| 1921 |
parser->parse(inputSource); |
| 1922 |
} |
| 1923 |
catch (const XMLException& e) { |
| 1924 |
std::cerr << "An error occurred during parsing\n Message: " << StrX(e.getMessage()) << "\n"; |
| 1925 |
errorsOccurred = true; |
| 1926 |
} |
| 1927 |
catch (const DOMException& e) { |
| 1928 |
std::cerr << "A DOM error occurred during parsing\n DOMException code: " << e.code << "\n"; |
| 1929 |
errorsOccurred = true; |
| 1930 |
} |
| 1931 |
catch (...) { |
| 1932 |
std::cerr << "An error occurred during parsing\n " << "\n"; |
| 1933 |
errorsOccurred = true; |
| 1934 |
} |
| 1935 |
|
| 1936 |
if (errorsOccurred) { |
| 1937 |
return 0; |
| 1938 |
} |
| 1939 |
|
| 1940 |
_pDocument = parser->adoptDocument(); |
| 1941 |
|
| 1942 |
if (!_pDocument) { |
| 1943 |
throw XMLBaseException("Malformed Parameter document: Invalid document"); |
| 1944 |
} |
| 1945 |
|
| 1946 |
DOMElement* rootElem = _pDocument->getDocumentElement(); |
| 1947 |
if (!rootElem) { |
| 1948 |
throw XMLBaseException("Malformed Parameter document: Root group not found"); |
| 1949 |
} |
| 1950 |
|
| 1951 |
_pGroupNode = FindElement(rootElem, "FCParamGroup", "Root"); |
| 1952 |
|
| 1953 |
if (!_pGroupNode) { |
| 1954 |
throw XMLBaseException("Malformed Parameter document: Root group not found"); |
| 1955 |
} |
| 1956 |
|
| 1957 |
return 1; |
| 1958 |
} |
| 1959 |
|
| 1960 |
void ParameterManager::SaveDocument(const char* sFileName) const |
| 1961 |
{ |
| 1962 |
try { |
| 1963 |
Base::FileInfo file(sFileName); |
| 1964 |
QLockFile lock(getLockFile(file)); |
| 1965 |
if (!lock.tryLock(getTimeout())) { |
| 1966 |
std::cerr << "Failed to access file for writing: " << sFileName << std::endl; |
| 1967 |
return; |
| 1968 |
} |
| 1969 |
// |
| 1970 |
// Plug in a format target to receive the resultant |
| 1971 |
// XML stream from the serializer. |
| 1972 |
// |
| 1973 |
// LocalFileFormatTarget prints the resultant XML stream |
| 1974 |
// to a file once it receives any thing from the serializer. |
| 1975 |
// |
| 1976 |
XMLFormatTarget* myFormTarget {}; |
| 1977 |
#if defined(FC_OS_WIN32) |
| 1978 |
std::wstring name = file.toStdWString(); |
| 1979 |
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 1980 |
myFormTarget = new LocalFileFormatTarget(reinterpret_cast<const XMLCh*>(name.c_str())); |
| 1981 |
#else |
| 1982 |
myFormTarget = new LocalFileFormatTarget(file.filePath().c_str()); |
| 1983 |
#endif |
| 1984 |
SaveDocument(myFormTarget); |
| 1985 |
delete myFormTarget; |
| 1986 |
} |
| 1987 |
catch (XMLException& e) { |
| 1988 |
std::cerr << "An error occurred during creation of output transcoder. Msg is:" << std::endl |
| 1989 |
<< StrX(e.getMessage()) << std::endl; |
| 1990 |
} |
| 1991 |
} |
| 1992 |
|
| 1993 |
void ParameterManager::SaveDocument(XMLFormatTarget* pFormatTarget) const |
| 1994 |
{ |
| 1995 |
try { |
| 1996 |
std::unique_ptr<DOMPrintFilter> myFilter; |
| 1997 |
std::unique_ptr<DOMErrorHandler> myErrorHandler; |
| 1998 |
|
| 1999 |
// NOLINTBEGIN |
| 2000 |
// get a serializer, an instance of DOMWriter |
| 2001 |
XMLCh tempStr[100]; |
| 2002 |
XMLString::transcode("LS", tempStr, 99); |
| 2003 |
DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(tempStr); |
| 2004 |
DOMLSSerializer* theSerializer = static_cast<DOMImplementationLS*>(impl)->createLSSerializer(); |
| 2005 |
// NOLINTEND |
| 2006 |
|
| 2007 |
// set user specified end of line sequence and output encoding |
| 2008 |
theSerializer->setNewLine(gMyEOLSequence); |
| 2009 |
|
| 2010 |
|
| 2011 |
// |
| 2012 |
// do the serialization through DOMWriter::writeNode(); |
| 2013 |
// |
| 2014 |
if (_pDocument) { |
| 2015 |
DOMLSOutput* theOutput = static_cast<DOMImplementationLS*>(impl)->createLSOutput(); |
| 2016 |
theOutput->setEncoding(gOutputEncoding); |
| 2017 |
|
| 2018 |
if (gUseFilter) { |
| 2019 |
myFilter = std::make_unique<DOMPrintFilter>( |
| 2020 |
DOMNodeFilter::SHOW_ELEMENT | DOMNodeFilter::SHOW_ATTRIBUTE |
| 2021 |
| DOMNodeFilter::SHOW_DOCUMENT_TYPE | DOMNodeFilter::SHOW_TEXT |
| 2022 |
); |
| 2023 |
theSerializer->setFilter(myFilter.get()); |
| 2024 |
} |
| 2025 |
|
| 2026 |
// plug in user's own error handler |
| 2027 |
myErrorHandler = std::make_unique<DOMPrintErrorHandler>(); |
| 2028 |
DOMConfiguration* config = theSerializer->getDomConfig(); |
| 2029 |
|
| 2030 |
// NOLINTBEGIN |
| 2031 |
config->setParameter(XMLUni::fgDOMErrorHandler, myErrorHandler.get()); |
| 2032 |
|
| 2033 |
// set feature if the serializer supports the feature/mode |
| 2034 |
if (config->canSetParameter(XMLUni::fgDOMWRTSplitCdataSections, gSplitCdataSections)) { |
| 2035 |
config->setParameter(XMLUni::fgDOMWRTSplitCdataSections, gSplitCdataSections); |
| 2036 |
} |
| 2037 |
|
| 2038 |
if (config->canSetParameter(XMLUni::fgDOMWRTDiscardDefaultContent, gDiscardDefaultContent)) { |
| 2039 |
config->setParameter(XMLUni::fgDOMWRTDiscardDefaultContent, gDiscardDefaultContent); |
| 2040 |
} |
| 2041 |
|
| 2042 |
if (config->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, gFormatPrettyPrint)) { |
| 2043 |
config->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, gFormatPrettyPrint); |
| 2044 |
} |
| 2045 |
// NOLINTEND |
| 2046 |
|
| 2047 |
theOutput->setByteStream(pFormatTarget); |
| 2048 |
theSerializer->write(_pDocument, theOutput); |
| 2049 |
|
| 2050 |
theOutput->release(); |
| 2051 |
} |
| 2052 |
|
| 2053 |
theSerializer->release(); |
| 2054 |
} |
| 2055 |
catch (XMLException& e) { |
| 2056 |
std::cerr << "An error occurred during creation of output transcoder. Msg is:" << std::endl |
| 2057 |
<< StrX(e.getMessage()) << std::endl; |
| 2058 |
} |
| 2059 |
} |
| 2060 |
|
| 2061 |
void ParameterManager::CreateDocument() |
| 2062 |
{ |
| 2063 |
// creating a document from screatch |
| 2064 |
DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation( |
| 2065 |
XStrLiteral("Core").unicodeForm() |
| 2066 |
); |
| 2067 |
delete _pDocument; |
| 2068 |
_pDocument = impl->createDocument( |
| 2069 |
nullptr, // root element namespace URI. |
| 2070 |
XStrLiteral("FCParameters").unicodeForm(), // root element name |
| 2071 |
nullptr |
| 2072 |
); // document type object (DTD). |
| 2073 |
|
| 2074 |
// creating the node for the root group |
| 2075 |
DOMElement* rootElem = _pDocument->getDocumentElement(); |
| 2076 |
_pGroupNode = _pDocument->createElement(XStrLiteral("FCParamGroup").unicodeForm()); |
| 2077 |
_pGroupNode->setAttribute(XStrLiteral("Name").unicodeForm(), XStrLiteral("Root").unicodeForm()); |
| 2078 |
rootElem->appendChild(_pGroupNode); |
| 2079 |
} |
| 2080 |
|
| 2081 |
void ParameterManager::CheckDocument() const |
| 2082 |
{ |
| 2083 |
if (!_pDocument) { |
| 2084 |
return; |
| 2085 |
} |
| 2086 |
|
| 2087 |
try { |
| 2088 |
// |
| 2089 |
// Plug in a format target to receive the resultant |
| 2090 |
// XML stream from the serializer. |
| 2091 |
// |
| 2092 |
// LocalFileFormatTarget prints the resultant XML stream |
| 2093 |
// to a file once it receives any thing from the serializer. |
| 2094 |
// |
| 2095 |
MemBufFormatTarget myFormTarget; |
| 2096 |
SaveDocument(&myFormTarget); |
| 2097 |
|
| 2098 |
// Either use the file saved on disk or write the current XML into a buffer in memory |
| 2099 |
// const char* xmlFile = "..."; |
| 2100 |
MemBufInputSource xmlFile(myFormTarget.getRawBuffer(), myFormTarget.getLen(), "(memory)"); |
| 2101 |
|
| 2102 |
// Either load the XSD file from disk or use the built-in string |
| 2103 |
// const char* xsdFile = "..."; |
| 2104 |
std::string xsdStr(xmlSchemeString); // NOLINT |
| 2105 |
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 2106 |
MemBufInputSource xsdFile( |
| 2107 |
reinterpret_cast<const XMLByte*>(xsdStr.c_str()), |
| 2108 |
xsdStr.size(), |
| 2109 |
"Parameter.xsd" |
| 2110 |
); |
| 2111 |
|
| 2112 |
// See |
| 2113 |
// http://apache-xml-project.6118.n7.nabble.com/validating-xml-with-xsd-schema-td17515.html |
| 2114 |
// |
| 2115 |
XercesDOMParser parser; |
| 2116 |
Grammar* grammar = parser.loadGrammar(xsdFile, Grammar::SchemaGrammarType, true); |
| 2117 |
if (!grammar) { |
| 2118 |
Base::Console().error("Grammar file cannot be loaded.\n"); |
| 2119 |
return; |
| 2120 |
} |
| 2121 |
|
| 2122 |
parser.setExternalNoNamespaceSchemaLocation("Parameter.xsd"); |
| 2123 |
// parser.setExitOnFirstFatalError(true); |
| 2124 |
// parser.setValidationConstraintFatal(true); |
| 2125 |
parser.cacheGrammarFromParse(true); |
| 2126 |
parser.setValidationScheme(XercesDOMParser::Val_Auto); |
| 2127 |
parser.setDoNamespaces(true); |
| 2128 |
parser.setDoSchema(true); |
| 2129 |
parser.setDisableDefaultEntityResolution(true); |
| 2130 |
|
| 2131 |
DOMTreeErrorReporter errHandler; |
| 2132 |
parser.setErrorHandler(&errHandler); |
| 2133 |
parser.parse(xmlFile); |
| 2134 |
|
| 2135 |
if (parser.getErrorCount() > 0) { |
| 2136 |
Base::Console().error( |
| 2137 |
"Unexpected XML structure detected: %zu errors\n", |
| 2138 |
parser.getErrorCount() |
| 2139 |
); |
| 2140 |
} |
| 2141 |
} |
| 2142 |
catch (XMLException& e) { |
| 2143 |
std::cerr << "An error occurred while checking document. Msg is:" << std::endl |
| 2144 |
<< StrX(e.getMessage()) << std::endl; |
| 2145 |
} |
| 2146 |
} |
| 2147 |
|
| 2148 |
|
| 2149 |
//************************************************************************** |
| 2150 |
//************************************************************************** |
| 2151 |
// DOMTreeErrorReporter |
| 2152 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 2153 |
|
| 2154 |
void DOMTreeErrorReporter::warning(const SAXParseException& /*exc*/) |
| 2155 |
{ |
| 2156 |
// |
| 2157 |
// Ignore all warnings. |
| 2158 |
// |
| 2159 |
} |
| 2160 |
|
| 2161 |
void DOMTreeErrorReporter::error(const SAXParseException& toCatch) |
| 2162 |
{ |
| 2163 |
fSawErrors = true; |
| 2164 |
std::cerr << "Error at file \"" << StrX(toCatch.getSystemId()) << "\", line " |
| 2165 |
<< toCatch.getLineNumber() << ", column " << toCatch.getColumnNumber() |
| 2166 |
<< "\n Message: " << StrX(toCatch.getMessage()) << std::endl; |
| 2167 |
} |
| 2168 |
|
| 2169 |
void DOMTreeErrorReporter::fatalError(const SAXParseException& toCatch) |
| 2170 |
{ |
| 2171 |
fSawErrors = true; |
| 2172 |
std::cerr << "Fatal Error at file \"" << StrX(toCatch.getSystemId()) << "\", line " |
| 2173 |
<< toCatch.getLineNumber() << ", column " << toCatch.getColumnNumber() |
| 2174 |
<< "\n Message: " << StrX(toCatch.getMessage()) << std::endl; |
| 2175 |
} |
| 2176 |
|
| 2177 |
void DOMTreeErrorReporter::resetErrors() |
| 2178 |
{ |
| 2179 |
// No-op in this case |
| 2180 |
} |
| 2181 |
|
| 2182 |
|
| 2183 |
//************************************************************************** |
| 2184 |
//************************************************************************** |
| 2185 |
// DOMPrintFilter |
| 2186 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 2187 |
DOMPrintFilter::DOMPrintFilter(ShowType whatToShow) |
| 2188 |
: fWhatToShow(whatToShow) |
| 2189 |
{} |
| 2190 |
|
| 2191 |
DOMPrintFilter::FilterAction DOMPrintFilter::acceptNode(const DOMNode* node) const |
| 2192 |
{ |
| 2193 |
if (XMLString::compareString(node->getNodeName(), XStrLiteral("FCParameters").unicodeForm()) |
| 2194 |
== 0) { |
| 2195 |
// This node is supposed to have a single FCParamGroup and two text nodes. |
| 2196 |
// Over time it can happen that the text nodes collect extra newlines. |
| 2197 |
const DOMNodeList* children = node->getChildNodes(); |
| 2198 |
for (XMLSize_t i = 0; i < children->getLength(); i++) { |
| 2199 |
DOMNode* child = children->item(i); |
| 2200 |
if (child->getNodeType() == DOMNode::TEXT_NODE) { |
| 2201 |
child->setNodeValue(XStrLiteral("\n").unicodeForm()); |
| 2202 |
} |
| 2203 |
} |
| 2204 |
} |
| 2205 |
|
| 2206 |
// clang-format off |
| 2207 |
switch (node->getNodeType()) { |
| 2208 |
case DOMNode::TEXT_NODE: { |
| 2209 |
// Filter out text element if it is under a group node. Note text xml |
| 2210 |
// element is plain text in between tags, and we do not store any text |
| 2211 |
// there. |
| 2212 |
auto parent = node->getParentNode(); |
| 2213 |
if (parent && XMLString::compareString(parent->getNodeName(), |
| 2214 |
XStrLiteral("FCParamGroup").unicodeForm()) == 0) { |
| 2215 |
return DOMNodeFilter::FILTER_REJECT; |
| 2216 |
} |
| 2217 |
return DOMNodeFilter::FILTER_ACCEPT; |
| 2218 |
} |
| 2219 |
case DOMNode::DOCUMENT_TYPE_NODE: |
| 2220 |
case DOMNode::DOCUMENT_NODE: { |
| 2221 |
return DOMNodeFilter::FILTER_REJECT; // no effect |
| 2222 |
} |
| 2223 |
default: { |
| 2224 |
return DOMNodeFilter::FILTER_ACCEPT; |
| 2225 |
} |
| 2226 |
} |
| 2227 |
// clang-format on |
| 2228 |
} |
| 2229 |
|
| 2230 |
//************************************************************************** |
| 2231 |
//************************************************************************** |
| 2232 |
// DOMPrintErrorHandler |
| 2233 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 2234 |
|
| 2235 |
|
| 2236 |
bool DOMPrintErrorHandler::handleError(const DOMError& domError) |
| 2237 |
{ |
| 2238 |
// Display whatever error message passed from the serializer |
| 2239 |
char* msg = XMLString::transcode(domError.getMessage()); |
| 2240 |
std::cout << msg << std::endl; |
| 2241 |
XMLString::release(&msg); |
| 2242 |
|
| 2243 |
// Instructs the serializer to continue serialization if possible. |
| 2244 |
return true; |
| 2245 |
} |