00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "PreCompiled.h"
00027
00028 #ifndef _PreComp_
00029 # include <assert.h>
00030 # include <xercesc/util/PlatformUtils.hpp>
00031 # include <xercesc/util/XercesVersion.hpp>
00032 # include <xercesc/dom/DOM.hpp>
00033 # include <xercesc/dom/DOMImplementation.hpp>
00034 # include <xercesc/dom/DOMImplementationLS.hpp>
00035 # if (XERCES_VERSION_MAJOR == 2)
00036 # include <xercesc/dom/DOMWriter.hpp>
00037 # endif
00038 # include <xercesc/framework/StdOutFormatTarget.hpp>
00039 # include <xercesc/framework/LocalFileFormatTarget.hpp>
00040 # include <xercesc/parsers/XercesDOMParser.hpp>
00041 # include <xercesc/util/XMLUni.hpp>
00042 # include <xercesc/util/XMLUniDefs.hpp>
00043 # include <xercesc/util/XMLString.hpp>
00044 # include <xercesc/sax/ErrorHandler.hpp>
00045 # include <xercesc/sax/SAXParseException.hpp>
00046 # include <fcntl.h>
00047 # include <sys/types.h>
00048 # include <sys/stat.h>
00049 # ifdef FC_OS_WIN32
00050 # include <io.h>
00051 # endif
00052 # include <sstream>
00053 # include <stdio.h>
00054 #endif
00055
00056
00057 #include <fcntl.h>
00058 #ifdef FC_OS_LINUX
00059 # include <unistd.h>
00060 #endif
00061
00062 #include "Parameter.h"
00063 #include "Exception.h"
00064 #include "Console.h"
00065
00066
00067
00068
00069
00070
00071 XERCES_CPP_NAMESPACE_USE
00072 using namespace Base;
00073
00074
00075 #include "XMLTools.h"
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 class DOMTreeErrorReporter : public ErrorHandler
00089 {
00090 public:
00091
00092
00093
00094 DOMTreeErrorReporter() :
00095 fSawErrors(false) {
00096 }
00097
00098 ~DOMTreeErrorReporter() {
00099 }
00100
00101
00102
00103
00104
00105 void warning(const SAXParseException& toCatch);
00106 void error(const SAXParseException& toCatch);
00107 void fatalError(const SAXParseException& toCatch);
00108 void resetErrors();
00109
00110
00111
00112
00113 bool getSawErrors() const;
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 bool fSawErrors;
00124 };
00125
00126
00127 #if (XERCES_VERSION_MAJOR == 2)
00128 class DOMPrintFilter : public DOMWriterFilter
00129 {
00130 public:
00131
00133 DOMPrintFilter(unsigned long whatToShow = DOMNodeFilter::SHOW_ALL);
00135
00137 ~DOMPrintFilter() {};
00139
00141 virtual short acceptNode(const XERCES_CPP_NAMESPACE_QUALIFIER DOMNode*) const;
00143
00144 virtual unsigned long getWhatToShow() const {
00145 return fWhatToShow;
00146 };
00147
00148 virtual void setWhatToShow(unsigned long toShow) {
00149 fWhatToShow = toShow;
00150 };
00151
00152 private:
00153
00154 DOMPrintFilter(const DOMPrintFilter&);
00155 DOMPrintFilter & operator = (const DOMPrintFilter&);
00156
00157 unsigned long fWhatToShow;
00158
00159 };
00160 #endif
00161 class DOMPrintErrorHandler : public DOMErrorHandler
00162 {
00163 public:
00164
00165 DOMPrintErrorHandler() {};
00166 ~DOMPrintErrorHandler() {};
00167
00169 bool handleError(const DOMError& domError);
00170 void resetErrors() {};
00171
00172 private :
00173
00174 DOMPrintErrorHandler(const DOMErrorHandler&);
00175 void operator=(const DOMErrorHandler&);
00176
00177 };
00178
00179
00180 inline bool DOMTreeErrorReporter::getSawErrors() const
00181 {
00182 return fSawErrors;
00183 }
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00199 ParameterGrp::ParameterGrp(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *GroupNode,const char* sName)
00200 : Base::Handled(), Subject<const char*>(),_pGroupNode(GroupNode)
00201 {
00202 if (sName) _cName=sName;
00203 }
00204
00205
00209 ParameterGrp::~ParameterGrp()
00210 {
00211 }
00212
00213
00214
00215
00216 void ParameterGrp::copyTo(Base::Reference<ParameterGrp> Grp)
00217 {
00218
00219 Grp->Clear();
00220
00221
00222 insertTo(Grp);
00223 }
00224
00225 void ParameterGrp::insertTo(Base::Reference<ParameterGrp> Grp)
00226 {
00227
00228 std::vector<Base::Reference<ParameterGrp> > Grps = GetGroups();
00229 std::vector<Base::Reference<ParameterGrp> >::iterator It1;
00230 for (It1 = Grps.begin();It1 != Grps.end();++It1)
00231 (*It1)->insertTo(Grp->GetGroup((*It1)->GetGroupName()));
00232
00233
00234 std::vector<std::pair<std::string,std::string> > StringMap = GetASCIIMap();
00235 std::vector<std::pair<std::string,std::string> >::iterator It2;
00236 for (It2 = StringMap.begin();It2 != StringMap.end();++It2)
00237 Grp->SetASCII(It2->first.c_str(),It2->second.c_str());
00238
00239
00240 std::vector<std::pair<std::string,bool> > BoolMap = GetBoolMap();
00241 std::vector<std::pair<std::string,bool> >::iterator It3;
00242 for (It3 = BoolMap.begin();It3 != BoolMap.end();++It3)
00243 Grp->SetBool(It3->first.c_str(),It3->second);
00244
00245
00246 std::vector<std::pair<std::string,long> > IntMap = GetIntMap();
00247 std::vector<std::pair<std::string,long> >::iterator It4;
00248 for (It4 = IntMap.begin();It4 != IntMap.end();++It4)
00249 Grp->SetInt(It4->first.c_str(),It4->second);
00250
00251
00252 std::vector<std::pair<std::string,double> > FloatMap = GetFloatMap();
00253 std::vector<std::pair<std::string,double> >::iterator It5;
00254 for (It5 = FloatMap.begin();It5 != FloatMap.end();++It5)
00255 Grp->SetFloat(It5->first.c_str(),It5->second);
00256
00257
00258 std::vector<std::pair<std::string,unsigned long> > UIntMap = GetUnsignedMap();
00259 std::vector<std::pair<std::string,unsigned long> >::iterator It6;
00260 for (It6 = UIntMap.begin();It6 != UIntMap.end();++It6)
00261 Grp->SetUnsigned(It6->first.c_str(),It6->second);
00262 }
00263
00264 void ParameterGrp::exportTo(const char* FileName)
00265 {
00266 ParameterManager Mngr;
00267
00268 Mngr.CreateDocument();
00269
00270
00271 insertTo(Mngr.GetGroup("BaseApp"));
00272
00273 Mngr.SaveDocument(FileName);
00274 }
00275
00276 void ParameterGrp::importFrom(const char* FileName)
00277 {
00278 ParameterManager Mngr;
00279
00280 if (Mngr.LoadDocument(FileName) != 1)
00281 throw Exception("ParameterGrp::import() cannot load document");
00282
00283 Mngr.GetGroup("BaseApp")->copyTo(Base::Reference<ParameterGrp>(this));
00284 }
00285
00286 void ParameterGrp::insert(const char* FileName)
00287 {
00288 ParameterManager Mngr;
00289
00290 if (Mngr.LoadDocument(FileName) != 1)
00291 throw Exception("ParameterGrp::import() cannot load document");
00292
00293 Mngr.GetGroup("root")->insertTo(Base::Reference<ParameterGrp>(this));
00294 }
00295
00296 Base::Reference<ParameterGrp> ParameterGrp::GetGroup(const char* Name)
00297 {
00298 std::string cName = Name;
00299
00300 std::string::size_type pos = cName.find('/');
00301
00302
00303 if (pos == std::string::npos) {
00304 return _GetGroup(Name);
00305 }
00306 else if (pos == cName.size()) {
00307
00308 cName.erase(pos);
00309 return _GetGroup(cName.c_str());
00310 }
00311 else if (pos == 0) {
00312
00313
00314
00315 cName.erase(0,1);
00316
00317 return GetGroup(cName.c_str());
00318 }
00319 else {
00320
00321 std::string cTemp;
00322
00323 cTemp.assign(cName,0,pos);
00324
00325 cName.erase(0,pos+1);
00326
00327 return _GetGroup(cTemp.c_str())->GetGroup(cName.c_str());
00328 }
00329 }
00330
00331 Base::Reference<ParameterGrp> ParameterGrp::_GetGroup(const char* Name)
00332 {
00333 Base::Reference<ParameterGrp> rParamGrp;
00334 DOMElement *pcTemp;
00335
00336
00337 if ((rParamGrp=_GroupMap[Name]).isValid()) {
00338
00339 return rParamGrp;
00340 }
00341
00342
00343 pcTemp = FindOrCreateElement(_pGroupNode,"FCParamGroup",Name);
00344
00345
00346 rParamGrp = Base::Reference<ParameterGrp> (new ParameterGrp(pcTemp,Name));
00347 _GroupMap[Name] = rParamGrp;
00348
00349 return rParamGrp;
00350 }
00351
00352 std::vector<Base::Reference<ParameterGrp> > ParameterGrp::GetGroups(void)
00353 {
00354 Base::Reference<ParameterGrp> rParamGrp;
00355 std::vector<Base::Reference<ParameterGrp> > vrParamGrp;
00356 DOMElement *pcTemp;
00357 std::string Name;
00358
00359 pcTemp = FindElement(_pGroupNode,"FCParamGroup");
00360
00361 while (pcTemp) {
00362 Name = StrX( ((DOMElement*)pcTemp)->getAttributes()->getNamedItem(XStr("Name").unicodeForm())->getNodeValue()).c_str();
00363
00364 if (!(rParamGrp=_GroupMap[Name]).isValid()) {
00365 rParamGrp = Base::Reference<ParameterGrp> (new ParameterGrp(((DOMElement*)pcTemp),Name.c_str()));
00366 _GroupMap[Name] = rParamGrp;
00367 }
00368 vrParamGrp.push_back( rParamGrp );
00369
00370 pcTemp = FindNextElement(pcTemp,"FCParamGroup");
00371 }
00372
00373 return vrParamGrp;
00374 }
00375
00377 bool ParameterGrp::IsEmpty(void) const
00378 {
00379 if ( _pGroupNode->getFirstChild() )
00380 return false;
00381 else
00382 return true;
00383 }
00384
00386 bool ParameterGrp::HasGroup(const char* Name) const
00387 {
00388 if ( _GroupMap.find(Name) != _GroupMap.end() )
00389 return true;
00390
00391 if ( FindElement(_pGroupNode,"FCParamGroup",Name) != 0 )
00392 return true;
00393
00394 return false;
00395 }
00396
00397 bool ParameterGrp::GetBool(const char* Name, bool bPreset) const
00398 {
00399
00400 DOMElement *pcElem = FindElement(_pGroupNode,"FCBool",Name);
00401
00402 if (!pcElem) return bPreset;
00403
00404 if (strcmp(StrX(pcElem->getAttribute(XStr("Value").unicodeForm())).c_str(),"1"))
00405 return false;
00406 else
00407 return true;
00408 }
00409
00410 void ParameterGrp::SetBool(const char* Name, bool bValue)
00411 {
00412
00413 DOMElement *pcElem = FindOrCreateElement(_pGroupNode,"FCBool",Name);
00414
00415 pcElem->setAttribute(XStr("Value").unicodeForm(), XStr(bValue?"1":"0").unicodeForm());
00416
00417 Notify(Name);
00418 }
00419
00420 std::vector<bool> ParameterGrp::GetBools(const char * sFilter) const
00421 {
00422 std::vector<bool> vrValues;
00423 DOMElement *pcTemp;
00424 std::string Name;
00425
00426 pcTemp = FindElement(_pGroupNode,"FCBool");
00427 while ( pcTemp) {
00428 Name = StrX( ((DOMElement*)pcTemp)->getAttributes()->getNamedItem(XStr("Name").unicodeForm())->getNodeValue()).c_str();
00429
00430 if (sFilter == NULL || Name.find(sFilter)!= std::string::npos) {
00431 if (strcmp(StrX(((DOMElement*)pcTemp)->getAttribute(XStr("Value").unicodeForm())).c_str(),"1"))
00432 vrValues.push_back(false);
00433 else
00434 vrValues.push_back(true);
00435 }
00436 pcTemp = FindNextElement(pcTemp,"FCBool");
00437 }
00438
00439 return vrValues;
00440 }
00441
00442 std::vector<std::pair<std::string,bool> > ParameterGrp::GetBoolMap(const char * sFilter) const
00443 {
00444 std::vector<std::pair<std::string,bool> > vrValues;
00445 DOMElement *pcTemp;
00446 std::string Name;
00447
00448 pcTemp = FindElement(_pGroupNode,"FCBool");
00449 while ( pcTemp) {
00450 Name = StrX( ((DOMElement*)pcTemp)->getAttributes()->getNamedItem(XStr("Name").unicodeForm())->getNodeValue()).c_str();
00451
00452 if (sFilter == NULL || Name.find(sFilter)!= std::string::npos) {
00453 if (strcmp(StrX(((DOMElement*)pcTemp)->getAttribute(XStr("Value").unicodeForm())).c_str(),"1"))
00454 vrValues.push_back(std::make_pair<std::string, bool>(Name, false));
00455 else
00456 vrValues.push_back(std::make_pair<std::string, bool>(Name, true));
00457 }
00458 pcTemp = FindNextElement(pcTemp,"FCBool");
00459 }
00460
00461 return vrValues;
00462 }
00463
00464 long ParameterGrp::GetInt(const char* Name, long lPreset) const
00465 {
00466
00467 DOMElement *pcElem = FindElement(_pGroupNode,"FCInt",Name);
00468
00469 if (!pcElem) return lPreset;
00470
00471 return atol (StrX(pcElem->getAttribute(XStr("Value").unicodeForm())).c_str());
00472 }
00473
00474 void ParameterGrp::SetInt(const char* Name, long lValue)
00475 {
00476 char cBuf[256];
00477
00478 DOMElement *pcElem = FindOrCreateElement(_pGroupNode,"FCInt",Name);
00479
00480 sprintf(cBuf,"%li",lValue);
00481 pcElem->setAttribute(XStr("Value").unicodeForm(), XStr(cBuf).unicodeForm());
00482
00483 Notify(Name);
00484 }
00485
00486 std::vector<long> ParameterGrp::GetInts(const char * sFilter) const
00487 {
00488 std::vector<long> vrValues;
00489 DOMNode *pcTemp;
00490 std::string Name;
00491
00492 pcTemp = FindElement(_pGroupNode,"FCInt") ;
00493 while ( pcTemp ) {
00494 Name = StrX( ((DOMElement*)pcTemp)->getAttributes()->getNamedItem(XStr("Name").unicodeForm())->getNodeValue()).c_str();
00495
00496 if (sFilter == NULL || Name.find(sFilter)!= std::string::npos) {
00497 vrValues.push_back( atol (StrX(((DOMElement*)pcTemp)->getAttribute(XStr("Value").unicodeForm())).c_str()) );
00498 }
00499 pcTemp = FindNextElement(pcTemp,"FCInt") ;
00500 }
00501
00502 return vrValues;
00503 }
00504
00505 std::vector<std::pair<std::string,long> > ParameterGrp::GetIntMap(const char * sFilter) const
00506 {
00507 std::vector<std::pair<std::string,long> > vrValues;
00508 DOMNode *pcTemp;
00509 std::string Name;
00510
00511 pcTemp = FindElement(_pGroupNode,"FCInt") ;
00512 while ( pcTemp ) {
00513 Name = StrX( ((DOMElement*)pcTemp)->getAttributes()->getNamedItem(XStr("Name").unicodeForm())->getNodeValue()).c_str();
00514
00515 if (sFilter == NULL || Name.find(sFilter)!= std::string::npos) {
00516 vrValues.push_back(std::make_pair<std::string, long>(Name,
00517 ( atol (StrX(((DOMElement*)pcTemp)->getAttribute(XStr("Value").unicodeForm())).c_str()))));
00518 }
00519 pcTemp = FindNextElement(pcTemp,"FCInt") ;
00520 }
00521
00522 return vrValues;
00523 }
00524
00525 unsigned long ParameterGrp::GetUnsigned(const char* Name, unsigned long lPreset) const
00526 {
00527
00528 DOMElement *pcElem = FindElement(_pGroupNode,"FCUInt",Name);
00529
00530 if (!pcElem) return lPreset;
00531
00532 return strtoul (StrX(pcElem->getAttribute(XStr("Value").unicodeForm())).c_str(),0,10);
00533 }
00534
00535 void ParameterGrp::SetUnsigned(const char* Name, unsigned long lValue)
00536 {
00537 char cBuf[256];
00538
00539 DOMElement *pcElem = FindOrCreateElement(_pGroupNode,"FCUInt",Name);
00540
00541 sprintf(cBuf,"%lu",lValue);
00542 pcElem->setAttribute(XStr("Value").unicodeForm(), XStr(cBuf).unicodeForm());
00543
00544 Notify(Name);
00545 }
00546
00547 std::vector<unsigned long> ParameterGrp::GetUnsigneds(const char * sFilter) const
00548 {
00549 std::vector<unsigned long> vrValues;
00550 DOMNode *pcTemp;
00551 std::string Name;
00552
00553 pcTemp = FindElement(_pGroupNode,"FCUInt");
00554 while ( pcTemp ) {
00555 Name = StrX( ((DOMElement*)pcTemp)->getAttributes()->getNamedItem(XStr("Name").unicodeForm())->getNodeValue()).c_str();
00556
00557 if (sFilter == NULL || Name.find(sFilter)!= std::string::npos) {
00558 vrValues.push_back( strtoul (StrX(((DOMElement*)pcTemp)->getAttribute(XStr("Value").unicodeForm())).c_str(),0,10) );
00559 }
00560 pcTemp = FindNextElement(pcTemp,"FCUInt") ;
00561 }
00562
00563 return vrValues;
00564 }
00565
00566 std::vector<std::pair<std::string,unsigned long> > ParameterGrp::GetUnsignedMap(const char * sFilter) const
00567 {
00568 std::vector<std::pair<std::string,unsigned long> > vrValues;
00569 DOMNode *pcTemp;
00570 std::string Name;
00571
00572 pcTemp = FindElement(_pGroupNode,"FCUInt");
00573 while ( pcTemp ) {
00574 Name = StrX( ((DOMElement*)pcTemp)->getAttributes()->getNamedItem(XStr("Name").unicodeForm())->getNodeValue()).c_str();
00575
00576 if (sFilter == NULL || Name.find(sFilter)!= std::string::npos) {
00577 vrValues.push_back(std::make_pair<std::string, unsigned long>(Name,
00578 ( strtoul (StrX(((DOMElement*)pcTemp)->getAttribute(XStr("Value").unicodeForm())).c_str(),0,10) )));
00579 }
00580 pcTemp = FindNextElement(pcTemp,"FCUInt");
00581 }
00582
00583 return vrValues;
00584 }
00585
00586 double ParameterGrp::GetFloat(const char* Name, double dPreset) const
00587 {
00588
00589 DOMElement *pcElem = FindElement(_pGroupNode,"FCFloat",Name);
00590
00591 if (!pcElem) return dPreset;
00592
00593 return atof (StrX(pcElem->getAttribute(XStr("Value").unicodeForm())).c_str());
00594 }
00595
00596 void ParameterGrp::SetFloat(const char* Name, double dValue)
00597 {
00598 char cBuf[256];
00599
00600 DOMElement *pcElem = FindOrCreateElement(_pGroupNode,"FCFloat",Name);
00601
00602 sprintf(cBuf,"%.12f",dValue);
00603 pcElem->setAttribute(XStr("Value").unicodeForm(), XStr(cBuf).unicodeForm());
00604
00605 Notify(Name);
00606 }
00607
00608 std::vector<double> ParameterGrp::GetFloats(const char * sFilter) const
00609 {
00610 std::vector<double> vrValues;
00611 DOMElement *pcTemp ;
00612 std::string Name;
00613
00614 pcTemp = FindElement(_pGroupNode,"FCFloat") ;
00615 while ( pcTemp ) {
00616 Name = StrX( ((DOMElement*)pcTemp)->getAttributes()->getNamedItem(XStr("Name").unicodeForm())->getNodeValue()).c_str();
00617
00618 if (sFilter == NULL || Name.find(sFilter)!= std::string::npos) {
00619 vrValues.push_back( atof (StrX(((DOMElement*)pcTemp)->getAttribute(XStr("Value").unicodeForm())).c_str()) );
00620 }
00621 pcTemp = FindNextElement(pcTemp,"FCFloat");
00622 }
00623
00624 return vrValues;
00625 }
00626
00627 std::vector<std::pair<std::string,double> > ParameterGrp::GetFloatMap(const char * sFilter) const
00628 {
00629 std::vector<std::pair<std::string,double> > vrValues;
00630 DOMElement *pcTemp ;
00631 std::string Name;
00632
00633 pcTemp = FindElement(_pGroupNode,"FCFloat") ;
00634 while ( pcTemp ) {
00635 Name = StrX( ((DOMElement*)pcTemp)->getAttributes()->getNamedItem(XStr("Name").unicodeForm())->getNodeValue()).c_str();
00636
00637 if (sFilter == NULL || Name.find(sFilter)!= std::string::npos) {
00638 vrValues.push_back(std::make_pair<std::string, double>(Name,
00639 ( atof (StrX(((DOMElement*)pcTemp)->getAttribute(XStr("Value").unicodeForm())).c_str()))));
00640 }
00641 pcTemp = FindNextElement(pcTemp,"FCFloat");
00642 }
00643
00644 return vrValues;
00645 }
00646
00647
00648
00649 void ParameterGrp::SetBlob(const char* , void* , long )
00650 {
00651
00652 assert(0);
00653 }
00654
00655 void ParameterGrp::GetBlob(const char* , void* , long , void* ) const
00656 {
00657
00658 assert(0);
00659 }
00660
00661 void ParameterGrp::SetASCII(const char* Name, const char *sValue)
00662 {
00663
00664 DOMElement *pcElem = FindOrCreateElement(_pGroupNode,"FCText",Name);
00665
00666 DOMNode *pcElem2 = pcElem->getFirstChild();
00667 if (!pcElem2) {
00668 XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *pDocument = _pGroupNode->getOwnerDocument();
00669 DOMText *pText = pDocument->createTextNode(XUTF8Str(sValue).unicodeForm());
00670 pcElem->appendChild(pText);
00671 }
00672 else {
00673 pcElem2->setNodeValue(XUTF8Str(sValue).unicodeForm());
00674 }
00675
00676 Notify(Name);
00677
00678 }
00679
00680 std::string ParameterGrp::GetASCII(const char* Name, const char * pPreset) const
00681 {
00682
00683 DOMElement *pcElem = FindElement(_pGroupNode,"FCText",Name);
00684
00685 if (!pcElem) {
00686 if (pPreset==0)
00687 return std::string("");
00688 else
00689 return std::string(pPreset);
00690 }
00691
00692 DOMNode *pcElem2 = pcElem->getFirstChild();
00693 if (pcElem2)
00694 return std::string(StrXUTF8(pcElem2->getNodeValue()).c_str());
00695 else if (pPreset==0)
00696 return std::string("");
00697
00698 else
00699 return std::string(pPreset);
00700 }
00701
00702 std::vector<std::string> ParameterGrp::GetASCIIs(const char * sFilter) const
00703 {
00704 std::vector<std::string> vrValues;
00705 DOMElement *pcTemp;
00706 std::string Name;
00707
00708 pcTemp = FindElement(_pGroupNode,"FCText");
00709 while ( pcTemp ) {
00710 Name = StrXUTF8( ((DOMElement*)pcTemp)->getAttributes()->getNamedItem(XStr("Name").unicodeForm())->getNodeValue()).c_str();
00711
00712 if (sFilter == NULL || Name.find(sFilter)!= std::string::npos) {
00713
00714 DOMNode *pcElem2 = pcTemp->getFirstChild();
00715 if (pcElem2)
00716 vrValues.push_back( std::string(StrXUTF8(pcElem2->getNodeValue()).c_str()) );
00717 }
00718 pcTemp = FindNextElement(pcTemp,"FCText");
00719 }
00720
00721 return vrValues;
00722 }
00723
00724 std::vector<std::pair<std::string,std::string> > ParameterGrp::GetASCIIMap(const char * sFilter) const
00725 {
00726 std::vector<std::pair<std::string,std::string> > vrValues;
00727 DOMElement *pcTemp;
00728 std::string Name;
00729
00730 pcTemp = FindElement(_pGroupNode,"FCText");
00731 while ( pcTemp) {
00732 Name = StrXUTF8( ((DOMElement*)pcTemp)->getAttributes()->getNamedItem(XStr("Name").unicodeForm())->getNodeValue()).c_str();
00733
00734 if (sFilter == NULL || Name.find(sFilter)!= std::string::npos) {
00735
00736 DOMNode *pcElem2 = pcTemp->getFirstChild();
00737 if (pcElem2)
00738 vrValues.push_back(std::make_pair<std::string, std::string>(Name, std::string(StrXUTF8(pcElem2->getNodeValue()).c_str())));
00739 }
00740 pcTemp = FindNextElement(pcTemp,"FCText");
00741 }
00742
00743 return vrValues;
00744 }
00745
00746
00747
00748
00749 void ParameterGrp::RemoveGrp(const char* Name)
00750 {
00751
00752 _GroupMap.erase(Name);
00753
00754
00755 DOMElement *pcElem = FindElement(_pGroupNode,"FCParamGroup",Name);
00756
00757 if (!pcElem)
00758 return;
00759 else
00760 _pGroupNode->removeChild(pcElem);
00761
00762 Notify(Name);
00763 }
00764
00765 void ParameterGrp::RemoveASCII(const char* Name)
00766 {
00767
00768 DOMElement *pcElem = FindElement(_pGroupNode,"FCText",Name);
00769
00770 if (!pcElem)
00771 return;
00772 else
00773 _pGroupNode->removeChild(pcElem);
00774
00775 Notify(Name);
00776
00777 }
00778
00779 void ParameterGrp::RemoveBool(const char* Name)
00780 {
00781
00782 DOMElement *pcElem = FindElement(_pGroupNode,"FCBool",Name);
00783
00784 if (!pcElem)
00785 return;
00786 else
00787 _pGroupNode->removeChild(pcElem);
00788
00789
00790 Notify(Name);
00791 }
00792
00793 void ParameterGrp::RemoveBlob(const char* )
00794 {
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804 }
00805
00806 void ParameterGrp::RemoveFloat(const char* Name)
00807 {
00808
00809 DOMElement *pcElem = FindElement(_pGroupNode,"FCFloat",Name);
00810
00811 if (!pcElem)
00812 return;
00813 else
00814 _pGroupNode->removeChild(pcElem);
00815
00816
00817 Notify(Name);
00818 }
00819
00820 void ParameterGrp::RemoveInt(const char* Name)
00821 {
00822
00823 DOMElement *pcElem = FindElement(_pGroupNode,"FCInt",Name);
00824
00825 if (!pcElem)
00826 return;
00827 else
00828 _pGroupNode->removeChild(pcElem);
00829
00830
00831 Notify(Name);
00832 }
00833
00834 void ParameterGrp::RemoveUnsigned(const char* Name)
00835 {
00836
00837 DOMElement *pcElem = FindElement(_pGroupNode,"FCUInt",Name);
00838
00839 if (!pcElem)
00840 return;
00841 else
00842 _pGroupNode->removeChild(pcElem);
00843
00844
00845 Notify(Name);
00846 }
00847
00848 void ParameterGrp::Clear(void)
00849 {
00850 std::vector<DOMNode*> vecNodes;
00851
00852
00853 std::map <std::string ,Base::Reference<ParameterGrp> >::iterator It1;
00854 for (It1 = _GroupMap.begin();It1!=_GroupMap.end();It1++)
00855 if (It1->second.getRefCount() > 1)
00856 Console().Warning("ParameterGrp::Clear(): Group clear with active references");
00857
00858 _GroupMap.clear();
00859
00860
00861 for (DOMNode *clChild = _pGroupNode->getFirstChild(); clChild != 0; clChild = clChild->getNextSibling()) {
00862 vecNodes.push_back(clChild);
00863 }
00864
00865
00866 DOMNode* pcTemp;
00867 for (std::vector<DOMNode*>::iterator It=vecNodes.begin();It!=vecNodes.end();It++) {
00868 pcTemp = _pGroupNode->removeChild(*It);
00869
00870 pcTemp->release();
00871 }
00872
00873 Notify(0);
00874 }
00875
00876
00877
00878
00879 XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *ParameterGrp::FindElement(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *Start, const char* Type, const char* Name) const
00880 {
00881 for (DOMNode *clChild = Start->getFirstChild(); clChild != 0; clChild = clChild->getNextSibling()) {
00882 if (clChild->getNodeType() == DOMNode::ELEMENT_NODE) {
00883
00884 if (!strcmp(Type,StrX(clChild->getNodeName()).c_str())) {
00885 if (clChild->getAttributes()->getLength() > 0) {
00886 if (Name) {
00887 if (!strcmp(Name,StrX(clChild->getAttributes()->getNamedItem(XStr("Name").unicodeForm())->getNodeValue()).c_str()))
00888 return (DOMElement*)clChild;
00889 }
00890 else
00891 return (DOMElement*)clChild;
00892
00893 }
00894 }
00895 }
00896 }
00897 return NULL;
00898 }
00899
00900 XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *ParameterGrp::FindNextElement(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *Prev, const char* Type) const
00901 {
00902 DOMNode *clChild = Prev;
00903 if (!clChild) return 0l;
00904
00905 while ((clChild = clChild->getNextSibling())!=0) {
00906 if (clChild->getNodeType() == DOMNode::ELEMENT_NODE) {
00907
00908 if (!strcmp(Type,StrX(clChild->getNodeName()).c_str())) {
00909 return (DOMElement*)clChild;
00910 }
00911 }
00912 }
00913 return NULL;
00914 }
00915
00916 XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *ParameterGrp::FindOrCreateElement(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *Start, const char* Type, const char* Name) const
00917 {
00918
00919 DOMElement *pcElem = FindElement(Start,Type,Name);
00920
00921 if (!pcElem) {
00922 XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *pDocument = _pGroupNode->getOwnerDocument();
00923
00924 pcElem = pDocument->createElement(XStr(Type).unicodeForm());
00925 pcElem-> setAttribute(XStr("Name").unicodeForm(), XStr(Name).unicodeForm());
00926 Start->appendChild(pcElem);
00927 }
00928
00929 return pcElem;
00930 }
00931
00932 void ParameterGrp::NotifyAll()
00933 {
00934
00935 std::vector<std::pair<std::string,long> > IntMap = GetIntMap();
00936 for (std::vector<std::pair<std::string,long> >::iterator It1= IntMap.begin(); It1 != IntMap.end(); It1++)
00937 Notify(It1->first.c_str());
00938
00939
00940 std::vector<std::pair<std::string,bool> > BoolMap = GetBoolMap();
00941 for (std::vector<std::pair<std::string,bool> >::iterator It2= BoolMap.begin(); It2 != BoolMap.end(); It2++)
00942 Notify(It2->first.c_str());
00943
00944
00945 std::vector<std::pair<std::string,double> > FloatMap = GetFloatMap();
00946 for (std::vector<std::pair<std::string,double> >::iterator It3= FloatMap.begin(); It3 != FloatMap.end(); It3++)
00947 Notify(It3->first.c_str());
00948
00949
00950 std::vector<std::pair<std::string,std::string> > StringMap = GetASCIIMap();
00951 for (std::vector<std::pair<std::string,std::string> >::iterator It4= StringMap.begin(); It4 != StringMap.end(); It4++)
00952 Notify(It4->first.c_str());
00953
00954
00955 std::vector<std::pair<std::string,unsigned long> > UIntMap = GetUnsignedMap();
00956 for (std::vector<std::pair<std::string,unsigned long> >::iterator It5= UIntMap.begin(); It5 != UIntMap.end(); It5++)
00957 Notify(It5->first.c_str());
00958 }
00959
00960
00961
00962
00963
00964
00965 static XercesDOMParser::ValSchemes gValScheme = XercesDOMParser::Val_Auto;
00966
00967
00968
00969
00973 ParameterManager::ParameterManager() : ParameterGrp()
00974 {
00975
00976 Init();
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018
01019 gDoNamespaces = false;
01020 gDoSchema = false;
01021 gSchemaFullChecking = false;
01022 gDoCreate = true;
01023
01024 gOutputEncoding = 0;
01025 gMyEOLSequence = 0;
01026
01027 gSplitCdataSections = true;
01028 gDiscardDefaultContent = true;
01029 gUseFilter = false;
01030 gFormatPrettyPrint = true;
01031 }
01032
01036 ParameterManager::~ParameterManager()
01037 {
01038 }
01039
01040 void ParameterManager::Init(void)
01041 {
01042 static bool Init = false;
01043 if (!Init) {
01044 try {
01045 XMLPlatformUtils::Initialize();
01046 }
01047
01048 catch (const XMLException& toCatch) {
01049 #if defined(FC_OS_LINUX) || defined(FC_OS_CYGWIN)
01050 std::ostringstream err;
01051 #else
01052 std::stringstream err;
01053 #endif
01054 char *pMsg = XMLString::transcode(toCatch.getMessage());
01055 err << "Error during Xerces-c Initialization.\n"
01056 << " Exception message:"
01057 << pMsg;
01058 delete [] pMsg;
01059
01060 throw Exception(err.str().c_str());
01061
01062
01063
01064 }
01065 Init = true;
01066 }
01067 }
01068
01069
01070
01071
01072 bool ParameterManager::LoadOrCreateDocument(const char* sFileName)
01073 {
01074 Base::FileInfo file(sFileName);
01075 if (file.exists()) {
01076 LoadDocument(sFileName);
01077 return false;
01078 }
01079 else {
01080 CreateDocument();
01081 return true;
01082 }
01083 }
01084
01085 int ParameterManager::LoadDocument(const char* sFileName)
01086 {
01087 Base::FileInfo file(sFileName);
01088
01089
01090
01091
01092
01093
01094 XercesDOMParser *parser = new XercesDOMParser;
01095 parser->setValidationScheme(gValScheme);
01096 parser->setDoNamespaces(gDoNamespaces);
01097 parser->setDoSchema(gDoSchema);
01098 parser->setValidationSchemaFullChecking(gSchemaFullChecking);
01099 parser->setCreateEntityReferenceNodes(gDoCreate);
01100
01101 DOMTreeErrorReporter *errReporter = new DOMTreeErrorReporter();
01102 parser->setErrorHandler(errReporter);
01103
01104
01105
01106
01107
01108 bool errorsOccured = false;
01109 try {
01110 #if defined (FC_OS_WIN32)
01111 parser->parse((XMLCh*)file.toStdWString().c_str());
01112 #else
01113 parser->parse(file.filePath().c_str());
01114 #endif
01115 }
01116
01117 catch (const XMLException& e) {
01118 std::cerr << "An error occurred during parsing\n Message: "
01119 << StrX(e.getMessage()) << std::endl;
01120 errorsOccured = true;
01121 }
01122
01123 catch (const DOMException& e) {
01124 std::cerr << "A DOM error occurred during parsing\n DOMException code: "
01125 << e.code << std::endl;
01126 errorsOccured = true;
01127 }
01128
01129 catch (...) {
01130 std::cerr << "An error occurred during parsing\n " << std::endl;
01131 errorsOccured = true;
01132 }
01133
01134 if (errorsOccured)
01135 return 0;
01136
01137 _pDocument = parser->getDocument();
01138 if (!_pDocument)
01139 throw Exception("Malformed Parameter document: Invalid document");
01140
01141 DOMElement* rootElem = _pDocument->getDocumentElement();
01142 if (!rootElem)
01143 throw Exception("Malformed Parameter document: Root group not found");
01144
01145 _pGroupNode = FindElement(rootElem,"FCParamGroup","Root");
01146
01147 if (!_pGroupNode)
01148 throw Exception("Malformed Parameter document: Root group not found");
01149
01150 return 1;
01151 }
01152
01153 void ParameterManager::SaveDocument(const char* sFileName) const
01154 {
01155 #if (XERCES_VERSION_MAJOR == 2)
01156 DOMPrintFilter *myFilter = 0;
01157 Base::FileInfo file(sFileName);
01158
01159 try {
01160
01161 XMLCh tempStr[100];
01162 XMLString::transcode("LS", tempStr, 99);
01163 DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(tempStr);
01164 DOMWriter *theSerializer = ((DOMImplementationLS*)impl)->createDOMWriter();
01165
01166
01167 theSerializer->setNewLine(gMyEOLSequence);
01168 theSerializer->setEncoding(gOutputEncoding);
01169
01170
01171 if (gUseFilter) {
01172
01173
01174
01175
01176
01177
01178
01179
01180 myFilter = new DOMPrintFilter(DOMNodeFilter::SHOW_ELEMENT |
01181 DOMNodeFilter::SHOW_ATTRIBUTE |
01182 DOMNodeFilter::SHOW_DOCUMENT_TYPE
01183 );
01184 theSerializer->setFilter(myFilter);
01185 }
01186
01187
01188 DOMErrorHandler *myErrorHandler = new DOMPrintErrorHandler();
01189 theSerializer->setErrorHandler(myErrorHandler);
01190
01191
01192 if (theSerializer->canSetFeature(XMLUni::fgDOMWRTSplitCdataSections, gSplitCdataSections))
01193 theSerializer->setFeature(XMLUni::fgDOMWRTSplitCdataSections, gSplitCdataSections);
01194
01195 if (theSerializer->canSetFeature(XMLUni::fgDOMWRTDiscardDefaultContent, gDiscardDefaultContent))
01196 theSerializer->setFeature(XMLUni::fgDOMWRTDiscardDefaultContent, gDiscardDefaultContent);
01197
01198 if (theSerializer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, gFormatPrettyPrint))
01199 theSerializer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, gFormatPrettyPrint);
01200
01201
01202
01203
01204
01205
01206
01207
01208 #if defined (FC_OS_WIN32)
01209 XMLFormatTarget *myFormTarget = new LocalFileFormatTarget ((XMLCh*)file.toStdWString().c_str());
01210 #else
01211 XMLFormatTarget *myFormTarget = new LocalFileFormatTarget (file.filePath().c_str());
01212 #endif
01213
01214
01215
01216
01217 theSerializer->writeNode(myFormTarget, *_pDocument);
01218
01219 delete theSerializer;
01220
01221
01222
01223
01224
01225 delete myFormTarget;
01226 delete myErrorHandler;
01227
01228 if (gUseFilter)
01229 delete myFilter;
01230
01231 }
01232 catch (XMLException& e) {
01233 std::cerr << "An error occurred during creation of output transcoder. Msg is:"
01234 << std::endl
01235 << StrX(e.getMessage()) << std::endl;
01236 }
01237 #else
01238 Base::FileInfo file(sFileName);
01239
01240 try {
01241
01242 XMLCh tempStr[100];
01243 XMLString::transcode("LS", tempStr, 99);
01244 DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(tempStr);
01245 DOMLSSerializer *theSerializer = ((DOMImplementationLS*)impl)->createLSSerializer();
01246
01247
01248 theSerializer->setNewLine(gMyEOLSequence);
01249 DOMConfiguration* config = theSerializer->getDomConfig();
01250 config->setParameter(XStr("format-pretty-print").unicodeForm(),true);
01251
01252
01253 if (gUseFilter) {
01254
01255
01256
01257
01258
01259
01260
01261
01262
01263
01264
01265
01266
01267 }
01268
01269
01270 DOMErrorHandler *myErrorHandler = new DOMPrintErrorHandler();
01271
01272
01273
01274
01275
01276
01277
01278
01279
01280
01281
01282
01283
01284
01285
01286
01287
01288
01289
01290 #if defined (FC_OS_WIN32)
01291 XMLFormatTarget *myFormTarget = new LocalFileFormatTarget ((XMLCh*)file.toStdWString().c_str());
01292 #else
01293 XMLFormatTarget *myFormTarget = new LocalFileFormatTarget (file.filePath().c_str());
01294 #endif
01295
01296
01297
01298
01299 DOMLSOutput *theOutput = ((DOMImplementationLS*)impl)->createLSOutput();
01300 theOutput->setEncoding(gOutputEncoding);
01301 theOutput->setByteStream(myFormTarget);
01302 theSerializer->write(_pDocument, theOutput);
01303
01304 delete theSerializer;
01305
01306
01307
01308
01309
01310 delete myFormTarget;
01311 delete myErrorHandler;
01312
01313
01314
01315
01316 }
01317 catch (XMLException& e) {
01318 std::cerr << "An error occurred during creation of output transcoder. Msg is:"
01319 << std::endl
01320 << StrX(e.getMessage()) << std::endl;
01321 }
01322 #endif
01323 }
01324
01325 void ParameterManager::CreateDocument(void)
01326 {
01327
01328 DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(XStr("Core").unicodeForm());
01329 _pDocument = impl->createDocument(
01330 0,
01331 XStr("FCParameters").unicodeForm(),
01332 0);
01333
01334
01335 DOMElement* rootElem = _pDocument->getDocumentElement();
01336 _pGroupNode = _pDocument->createElement(XStr("FCParamGroup").unicodeForm());
01337 ((DOMElement*)_pGroupNode)->setAttribute(XStr("Name").unicodeForm(), XStr("Root").unicodeForm());
01338 rootElem->appendChild(_pGroupNode);
01339
01340
01341 }
01342
01343
01344 void ParameterManager::CheckDocument() const
01345 {
01346
01347 }
01348
01349
01350
01351
01352
01353
01354
01355 void DOMTreeErrorReporter::warning(const SAXParseException&)
01356 {
01357
01358
01359
01360 }
01361
01362 void DOMTreeErrorReporter::error(const SAXParseException& toCatch)
01363 {
01364 fSawErrors = true;
01365 std::cerr << "Error at file \"" << StrX(toCatch.getSystemId())
01366 << "\", line " << toCatch.getLineNumber()
01367 << ", column " << toCatch.getColumnNumber()
01368 << "\n Message: " << StrX(toCatch.getMessage()) << std::endl;
01369 }
01370
01371 void DOMTreeErrorReporter::fatalError(const SAXParseException& toCatch)
01372 {
01373 fSawErrors = true;
01374 std::cerr << "Fatal Error at file \"" << StrX(toCatch.getSystemId())
01375 << "\", line " << toCatch.getLineNumber()
01376 << ", column " << toCatch.getColumnNumber()
01377 << "\n Message: " << StrX(toCatch.getMessage()) << std::endl;
01378 }
01379
01380 void DOMTreeErrorReporter::resetErrors()
01381 {
01382
01383 }
01384
01385
01386
01387
01388
01389
01390 #if (XERCES_VERSION_MAJOR == 2)
01391 DOMPrintFilter::DOMPrintFilter(unsigned long whatToShow)
01392 :fWhatToShow(whatToShow)
01393 {
01394
01395 }
01396
01397 short DOMPrintFilter::acceptNode(const DOMNode* node) const
01398 {
01399
01400
01401
01402
01403
01404
01405
01406
01407
01408
01409
01410
01411
01412
01413 if ((getWhatToShow() & (1 << (node->getNodeType() - 1))) == 0)
01414 return DOMNodeFilter::FILTER_ACCEPT;
01415
01416 switch (node->getNodeType()) {
01417 case DOMNode::ELEMENT_NODE: {
01418
01419
01420
01421
01422
01423
01424
01425 return DOMNodeFilter::FILTER_ACCEPT;
01426
01427 break;
01428 }
01429 case DOMNode::COMMENT_NODE: {
01430
01431
01432 return DOMNodeFilter::FILTER_ACCEPT;
01433 break;
01434 }
01435 case DOMNode::TEXT_NODE: {
01436
01437
01438 return DOMNodeFilter::FILTER_ACCEPT;
01439 break;
01440 }
01441 case DOMNode::DOCUMENT_TYPE_NODE: {
01442
01443
01444
01445
01446
01447
01448 return DOMNodeFilter::FILTER_REJECT;
01449 break;
01450 }
01451 case DOMNode::DOCUMENT_NODE: {
01452
01453 return DOMNodeFilter::FILTER_REJECT;
01454 break;
01455 }
01456 default : {
01457 return DOMNodeFilter::FILTER_ACCEPT;
01458 break;
01459 }
01460 }
01461
01462 return DOMNodeFilter::FILTER_ACCEPT;
01463 }
01464 #endif
01465
01466
01467
01468
01469
01470
01471
01472 bool DOMPrintErrorHandler::handleError(const DOMError &domError)
01473 {
01474
01475 char *msg = XMLString::transcode(domError.getMessage());
01476 std::cout<<msg<<std::endl;
01477 delete[] msg;
01478
01479
01480 return true;
01481 }
01482
01483
01484
01485
01486
01487
01488
01489
01490 static const XMLCh element_person[]= {
01491 chLatin_p, chLatin_e, chLatin_r, chLatin_s, chLatin_o, chLatin_n, chNull
01492 };
01493
01494 static const XMLCh element_link[]= {
01495 chLatin_l, chLatin_i, chLatin_n, chLatin_k, chNull
01496 };