Parameter.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   (c) Jürgen Riegel (juergen.riegel@web.de) 2002                        *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This program is free software; you can redistribute it and/or modify  *
00007  *   it under the terms of the GNU Library General Public License (LGPL)   *
00008  *   as published by the Free Software Foundation; either version 2 of     *
00009  *   the License, or (at your option) any later version.                   *
00010  *   for detail see the LICENCE text file.                                 *
00011  *                                                                         *
00012  *   FreeCAD is distributed in the hope that it will be useful,            *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU Library General Public License for more details.                  *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU Library General Public     *
00018  *   License along with FreeCAD; if not, write to the Free Software        *
00019  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
00020  *   USA                                                                   *
00021  *                                                                         *
00022  *   Juergen Riegel 2002                                                   *
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 //#ifdef XERCES_HAS_CPP_NAMESPACE
00068 //  using namespace xercesc;
00069 //#endif
00070 
00071 XERCES_CPP_NAMESPACE_USE
00072 using namespace Base;
00073 
00074 
00075 #include "XMLTools.h"
00076 
00077 //**************************************************************************
00078 //**************************************************************************
00079 // privat classes declaration:
00080 // - DOMTreeErrorReporter
00081 // - StrX
00082 // - DOMPrintFilter
00083 // - DOMPrintErrorHandler
00084 // - XStr
00085 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00086 
00087 
00088 class DOMTreeErrorReporter : public ErrorHandler
00089 {
00090 public:
00091     // -----------------------------------------------------------------------
00092     //  Constructors and Destructor
00093     // -----------------------------------------------------------------------
00094     DOMTreeErrorReporter() :
00095             fSawErrors(false) {
00096     }
00097 
00098     ~DOMTreeErrorReporter() {
00099     }
00100 
00101 
00102     // -----------------------------------------------------------------------
00103     //  Implementation of the error handler interface
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     //  Getter methods
00112     // -----------------------------------------------------------------------
00113     bool getSawErrors() const;
00114 
00115     // -----------------------------------------------------------------------
00116     //  Private data members
00117     //
00118     //  fSawErrors
00119     //      This is set if we get any errors, and is queryable via a getter
00120     //      method. Its used by the main code to suppress output if there are
00121     //      errors.
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     // unimplemented copy ctor and assignement operator
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     /* Unimplemented constructors and operators */
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 // ParameterManager
00189 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00190 
00191 
00192 //**************************************************************************
00193 // Construction/Destruction
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 // Access methods
00215 
00216 void ParameterGrp::copyTo(Base::Reference<ParameterGrp> Grp)
00217 {
00218     // delete previos content
00219     Grp->Clear();
00220 
00221     // copy all
00222     insertTo(Grp);
00223 }
00224 
00225 void ParameterGrp::insertTo(Base::Reference<ParameterGrp> Grp)
00226 {
00227     // copy group
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     // copy strings
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     // copy bool
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     // copy int
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     // copy float
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     // copy uint
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     // copy all into the new document
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     // is there a path seperator ?
00303     if (pos == std::string::npos) {
00304         return _GetGroup(Name);
00305     }
00306     else if (pos == cName.size()) {
00307         // ending slash! cut it away
00308         cName.erase(pos);
00309         return _GetGroup(cName.c_str());
00310     }
00311     else if (pos == 0) {
00312         // a leading slash is not handled (root unknown)
00313         //throw FCException("ParameterGrp::GetGroup() leading slash not allowed");
00314         // remove leading slash
00315         cName.erase(0,1);
00316         // subsequent call
00317         return GetGroup(cName.c_str());
00318     }
00319     else {
00320         // path, split the first path
00321         std::string cTemp;
00322         // geting the first part
00323         cTemp.assign(cName,0,pos);
00324         // removing the first part from the original
00325         cName.erase(0,pos+1);
00326         //sbsequent call
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     // already created?
00337     if ((rParamGrp=_GroupMap[Name]).isValid()) {
00338         // just return the already existing Group handle
00339         return rParamGrp;
00340     }
00341 
00342     // search if Group node already there
00343     pcTemp = FindOrCreateElement(_pGroupNode,"FCParamGroup",Name);
00344 
00345     // create and register handle
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; //= _pGroupNode->getFirstChild();
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         // already created?
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         // go to next
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     // check if Element in group
00400     DOMElement *pcElem = FindElement(_pGroupNode,"FCBool",Name);
00401     // if not return preset
00402     if (!pcElem) return bPreset;
00403     // if yes check the value and return
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     // find or create the Element
00413     DOMElement *pcElem = FindOrCreateElement(_pGroupNode,"FCBool",Name);
00414     // and set the vaue
00415     pcElem->setAttribute(XStr("Value").unicodeForm(), XStr(bValue?"1":"0").unicodeForm());
00416     // trigger observer
00417     Notify(Name);
00418 }
00419 
00420 std::vector<bool> ParameterGrp::GetBools(const char * sFilter) const
00421 {
00422     std::vector<bool>  vrValues;
00423     DOMElement *pcTemp;// = _pGroupNode->getFirstChild();
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         // check on filter condition
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;// = _pGroupNode->getFirstChild();
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         // check on filter condition
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     // check if Element in group
00467     DOMElement *pcElem = FindElement(_pGroupNode,"FCInt",Name);
00468     // if not return preset
00469     if (!pcElem) return lPreset;
00470     // if yes check the value and return
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     // find or create the Element
00478     DOMElement *pcElem = FindOrCreateElement(_pGroupNode,"FCInt",Name);
00479     // and set the vaue
00480     sprintf(cBuf,"%li",lValue);
00481     pcElem->setAttribute(XStr("Value").unicodeForm(), XStr(cBuf).unicodeForm());
00482     // trigger observer
00483     Notify(Name);
00484 }
00485 
00486 std::vector<long> ParameterGrp::GetInts(const char * sFilter) const
00487 {
00488     std::vector<long>  vrValues;
00489     DOMNode *pcTemp;// = _pGroupNode->getFirstChild();
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         // check on filter condition
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;// = _pGroupNode->getFirstChild();
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         // check on filter condition
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     // check if Element in group
00528     DOMElement *pcElem = FindElement(_pGroupNode,"FCUInt",Name);
00529     // if not return preset
00530     if (!pcElem) return lPreset;
00531     // if yes check the value and return
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     // find or create the Element
00539     DOMElement *pcElem = FindOrCreateElement(_pGroupNode,"FCUInt",Name);
00540     // and set the vaue
00541     sprintf(cBuf,"%lu",lValue);
00542     pcElem->setAttribute(XStr("Value").unicodeForm(), XStr(cBuf).unicodeForm());
00543     // trigger observer
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;// = _pGroupNode->getFirstChild();
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         // check on filter condition
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;// = _pGroupNode->getFirstChild();
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         // check on filter condition
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     // check if Element in group
00589     DOMElement *pcElem = FindElement(_pGroupNode,"FCFloat",Name);
00590     // if not return preset
00591     if (!pcElem) return dPreset;
00592     // if yes check the value and return
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     // find or create the Element
00600     DOMElement *pcElem = FindOrCreateElement(_pGroupNode,"FCFloat",Name);
00601     // and set the value
00602     sprintf(cBuf,"%.12f",dValue); // use %.12f instead of %f to handle values < 1.0e-6
00603     pcElem->setAttribute(XStr("Value").unicodeForm(), XStr(cBuf).unicodeForm());
00604     // trigger observer
00605     Notify(Name);
00606 }
00607 
00608 std::vector<double> ParameterGrp::GetFloats(const char * sFilter) const
00609 {
00610     std::vector<double>  vrValues;
00611     DOMElement *pcTemp ;//= _pGroupNode->getFirstChild();
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         // check on filter condition
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 ;//= _pGroupNode->getFirstChild();
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         // check on filter condition
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* /*Name*/, void* /*pValue*/, long /*lLength*/)
00650 {
00651     // not implemented so far
00652     assert(0);
00653 }
00654 
00655 void ParameterGrp::GetBlob(const char* /*Name*/, void* /*pBuf*/, long /*lMaxLength*/, void* /*pPreset*/) const
00656 {
00657     // not implemented so far
00658     assert(0);
00659 }
00660 
00661 void  ParameterGrp::SetASCII(const char* Name, const char *sValue)
00662 {
00663     // find or create the Element
00664     DOMElement *pcElem = FindOrCreateElement(_pGroupNode,"FCText",Name);
00665     // and set the value
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     // trigger observer
00676     Notify(Name);
00677 
00678 }
00679 
00680 std::string ParameterGrp::GetASCII(const char* Name, const char * pPreset) const
00681 {
00682     // check if Element in group
00683     DOMElement *pcElem = FindElement(_pGroupNode,"FCText",Name);
00684     // if not return preset
00685     if (!pcElem) {
00686         if (pPreset==0)
00687             return std::string("");
00688         else
00689             return std::string(pPreset);
00690     }
00691     // if yes check the value and return
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;// = _pGroupNode->getFirstChild();
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         // check on filter condition
00712         if (sFilter == NULL || Name.find(sFilter)!= std::string::npos) {
00713             // retrive the text element
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;// = _pGroupNode->getFirstChild();
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         // check on filter condition
00734         if (sFilter == NULL || Name.find(sFilter)!= std::string::npos) {
00735             // retrive the text element
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 // Access methods
00748 
00749 void ParameterGrp::RemoveGrp(const char* Name)
00750 {
00751     // remove group handle
00752     _GroupMap.erase(Name);
00753 
00754     // check if Element in group
00755     DOMElement *pcElem = FindElement(_pGroupNode,"FCParamGroup",Name);
00756     // if not return
00757     if (!pcElem)
00758         return;
00759     else
00760         _pGroupNode->removeChild(pcElem);
00761     // trigger observer
00762     Notify(Name);
00763 }
00764 
00765 void ParameterGrp::RemoveASCII(const char* Name)
00766 {
00767     // check if Element in group
00768     DOMElement *pcElem = FindElement(_pGroupNode,"FCText",Name);
00769     // if not return
00770     if (!pcElem)
00771         return;
00772     else
00773         _pGroupNode->removeChild(pcElem);
00774     // trigger observer
00775     Notify(Name);
00776 
00777 }
00778 
00779 void ParameterGrp::RemoveBool(const char* Name)
00780 {
00781     // check if Element in group
00782     DOMElement *pcElem = FindElement(_pGroupNode,"FCBool",Name);
00783     // if not return
00784     if (!pcElem)
00785         return;
00786     else
00787         _pGroupNode->removeChild(pcElem);
00788 
00789     // trigger observer
00790     Notify(Name);
00791 }
00792 
00793 void ParameterGrp::RemoveBlob(const char* /*Name*/)
00794 {
00795     /* not implemented yet
00796     // check if Element in group
00797     DOMElement *pcElem = FindElement(_pGroupNode,"FCGrp",Name);
00798     // if not return
00799     if(!pcElem)
00800         return;
00801     else
00802         _pGroupNode->removeChild(pcElem);
00803     */
00804 }
00805 
00806 void ParameterGrp::RemoveFloat(const char* Name)
00807 {
00808     // check if Element in group
00809     DOMElement *pcElem = FindElement(_pGroupNode,"FCFloat",Name);
00810     // if not return
00811     if (!pcElem)
00812         return;
00813     else
00814         _pGroupNode->removeChild(pcElem);
00815 
00816     // trigger observer
00817     Notify(Name);
00818 }
00819 
00820 void ParameterGrp::RemoveInt(const char* Name)
00821 {
00822     // check if Element in group
00823     DOMElement *pcElem = FindElement(_pGroupNode,"FCInt",Name);
00824     // if not return
00825     if (!pcElem)
00826         return;
00827     else
00828         _pGroupNode->removeChild(pcElem);
00829 
00830     // trigger observer
00831     Notify(Name);
00832 }
00833 
00834 void ParameterGrp::RemoveUnsigned(const char* Name)
00835 {
00836     // check if Element in group
00837     DOMElement *pcElem = FindElement(_pGroupNode,"FCUInt",Name);
00838     // if not return
00839     if (!pcElem)
00840         return;
00841     else
00842         _pGroupNode->removeChild(pcElem);
00843 
00844     // trigger observer
00845     Notify(Name);
00846 }
00847 
00848 void ParameterGrp::Clear(void)
00849 {
00850     std::vector<DOMNode*> vecNodes;
00851 
00852     // checking on references
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     // remove group handles
00858     _GroupMap.clear();
00859 
00860     // searching all nodes
00861     for (DOMNode *clChild = _pGroupNode->getFirstChild(); clChild != 0;  clChild = clChild->getNextSibling()) {
00862         vecNodes.push_back(clChild);
00863     }
00864 
00865     // deleting the nodes
00866     DOMNode* pcTemp;
00867     for (std::vector<DOMNode*>::iterator It=vecNodes.begin();It!=vecNodes.end();It++) {
00868         pcTemp = _pGroupNode->removeChild(*It);
00869         //delete pcTemp;
00870         pcTemp->release();
00871     }
00872     // trigger observer
00873     Notify(0);
00874 }
00875 
00876 //**************************************************************************
00877 // Access methods
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             // the right node Type
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             // the right node Type
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     // first try to find it
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     // get all ints and notify
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     // get all booleans and notify
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     // get all Floats and notify
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     // get all strings and notify
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     // get all uints and notify
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 // ParameterManager
00963 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00964 
00965 static XercesDOMParser::ValSchemes    gValScheme       = XercesDOMParser::Val_Auto;
00966 
00967 //**************************************************************************
00968 // Construction/Destruction
00969 
00973 ParameterManager::ParameterManager()    : ParameterGrp()
00974 {
00975     // initialize the XML system
00976     Init();
00977 
00978 // ---------------------------------------------------------------------------
00979 //  Local data
00980 //
00981 //  gXmlFile
00982 //      The path to the file to parser. Set via command line.
00983 //
00984 //  gDoNamespaces
00985 //      Indicates whether namespace processing should be done.
00986 //
00987 //  gDoSchema
00988 //      Indicates whether schema processing should be done.
00989 //
00990 //  gSchemaFullChecking
00991 //      Indicates whether full schema constraint checking should be done.
00992 //
00993 //  gDoCreate
00994 //      Indicates whether entity reference nodes needs to be created or not
00995 //      Defaults to false
00996 //
00997 //  gOutputEncoding
00998 //      The encoding we are to output in. If not set on the command line,
00999 //      then it is defaults to the encoding of the input XML file.
01000 //
01001 //  gMyEOLSequence
01002 //      The end of line sequence we are to output.
01003 //
01004 //  gSplitCdataSections
01005 //      Indicates whether split-cdata-sections is to be enabled or not.
01006 //
01007 //  gDiscardDefaultContent
01008 //      Indicates whether default content is discarded or not.
01009 //
01010 //  gUseFilter
01011 //      Indicates if user wants to plug in the DOMPrintFilter.
01012 //
01013 //  gValScheme
01014 //      Indicates what validation scheme to use. It defaults to 'auto', but
01015 //      can be set via the -v= command.
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 //#ifdef FC_OS_LINUX
01060             throw Exception(err.str().c_str());
01061 //#else
01062 //                      throw FCException(err.str());
01063 //#endif
01064         }
01065         Init = true;
01066     }
01067 }
01068 
01069 //**************************************************************************
01070 // Document handling
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     //  Create our parser, then attach an error handler to the parser.
01091     //  The parser will call back to methods of the ErrorHandler if it
01092     //  discovers errors during the course of parsing the XML document.
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     //  Parse the XML file, catching any XML exceptions that might propogate
01106     //  out of it.
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         // get a serializer, an instance of DOMWriter
01161         XMLCh tempStr[100];
01162         XMLString::transcode("LS", tempStr, 99);
01163         DOMImplementation *impl          = DOMImplementationRegistry::getDOMImplementation(tempStr);
01164         DOMWriter         *theSerializer = ((DOMImplementationLS*)impl)->createDOMWriter();
01165 
01166         // set user specified end of line sequence and output encoding
01167         theSerializer->setNewLine(gMyEOLSequence);
01168         theSerializer->setEncoding(gOutputEncoding);
01169 
01170         // plug in user's own filter
01171         if (gUseFilter) {
01172             // even we say to show attribute, but the DOMWriter
01173             // will not show attribute nodes to the filter as
01174             // the specs explicitly says that DOMWriter shall
01175             // NOT show attributes to DOMWriterFilter.
01176             //
01177             // so DOMNodeFilter::SHOW_ATTRIBUTE has no effect.
01178             // same DOMNodeFilter::SHOW_DOCUMENT_TYPE, no effect.
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         // plug in user's own error handler
01188         DOMErrorHandler *myErrorHandler = new DOMPrintErrorHandler();
01189         theSerializer->setErrorHandler(myErrorHandler);
01190 
01191         // set feature if the serializer supports the feature/mode
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         // Plug in a format target to receive the resultant
01203         // XML stream from the serializer.
01204         //
01205         // LocalFileFormatTarget prints the resultant XML stream
01206         // to a file once it receives any thing from the serializer.
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         // do the serialization through DOMWriter::writeNode();
01216         //
01217         theSerializer->writeNode(myFormTarget, *_pDocument);
01218 
01219         delete theSerializer;
01220 
01221         //
01222         // Filter, formatTarget and error handler
01223         // are NOT owned by the serializer.
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         // get a serializer, an instance of DOMWriter
01242         XMLCh tempStr[100];
01243         XMLString::transcode("LS", tempStr, 99);
01244         DOMImplementation *impl          = DOMImplementationRegistry::getDOMImplementation(tempStr);
01245         DOMLSSerializer   *theSerializer = ((DOMImplementationLS*)impl)->createLSSerializer();
01246 
01247         // set user specified end of line sequence and output encoding
01248         theSerializer->setNewLine(gMyEOLSequence);
01249         DOMConfiguration* config = theSerializer->getDomConfig();
01250         config->setParameter(XStr("format-pretty-print").unicodeForm(),true);
01251 
01252         // plug in user's own filter
01253         if (gUseFilter) {
01254             // even we say to show attribute, but the DOMWriter
01255             // will not show attribute nodes to the filter as
01256             // the specs explicitly says that DOMWriter shall
01257             // NOT show attributes to DOMWriterFilter.
01258             //
01259             // so DOMNodeFilter::SHOW_ATTRIBUTE has no effect.
01260             // same DOMNodeFilter::SHOW_DOCUMENT_TYPE, no effect.
01261             //
01262 //            myFilter = new DOMPrintFilter(DOMNodeFilter::SHOW_ELEMENT   |
01263 //                                          DOMNodeFilter::SHOW_ATTRIBUTE |
01264 //                                          DOMNodeFilter::SHOW_DOCUMENT_TYPE
01265 //                                         );
01266 //            theSerializer->setFilter(myFilter);
01267         }
01268 
01269         // plug in user's own error handler
01270         DOMErrorHandler *myErrorHandler = new DOMPrintErrorHandler();
01271 //        theSerializer->setErrorHandler(myErrorHandler);
01272 
01273         // set feature if the serializer supports the feature/mode
01274 //        if (theSerializer->canSetFeature(XMLUni::fgDOMWRTSplitCdataSections, gSplitCdataSections))
01275 //            theSerializer->setFeature(XMLUni::fgDOMWRTSplitCdataSections, gSplitCdataSections);
01276 
01277 //        if (theSerializer->canSetFeature(XMLUni::fgDOMWRTDiscardDefaultContent, gDiscardDefaultContent))
01278 //            theSerializer->setFeature(XMLUni::fgDOMWRTDiscardDefaultContent, gDiscardDefaultContent);
01279 
01280 //        if (theSerializer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, gFormatPrettyPrint))
01281 //            theSerializer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, gFormatPrettyPrint);
01282 
01283         //
01284         // Plug in a format target to receive the resultant
01285         // XML stream from the serializer.
01286         //
01287         // LocalFileFormatTarget prints the resultant XML stream
01288         // to a file once it receives any thing from the serializer.
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         // do the serialization through DOMWriter::writeNode();
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         // Filter, formatTarget and error handler
01308         // are NOT owned by the serializer.
01309         //
01310         delete myFormTarget;
01311         delete myErrorHandler;
01312 
01313 //        if (gUseFilter)
01314 //            delete myFilter;
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     // creating a document from screatch
01328     DOMImplementation* impl =  DOMImplementationRegistry::getDOMImplementation(XStr("Core").unicodeForm());
01329     _pDocument = impl->createDocument(
01330                      0,                                          // root element namespace URI.
01331                      XStr("FCParameters").unicodeForm(),         // root element name
01332                      0);                                         // document type object (DTD).
01333 
01334     // creating the node for the root group
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 // DOMTreeErrorReporter
01353 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
01354 
01355 void DOMTreeErrorReporter::warning(const SAXParseException&)
01356 {
01357     //
01358     // Ignore all warnings.
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     // No-op in this case
01383 }
01384 
01385 
01386 //**************************************************************************
01387 //**************************************************************************
01388 // DOMPrintFilter
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     // The DOMWriter shall call getWhatToShow() before calling
01401     // acceptNode(), to show nodes which are supposed to be
01402     // shown to this filter.
01403     //
01404     // REVISIT: In case the DOMWriter does not follow the protocol,
01405     //          Shall the filter honour, or NOT, what it claimes
01406     //         (when it is constructed/setWhatToShow())
01407     //          it is interested in ?
01408     //
01409     // The DOMLS specs does not specify that acceptNode() shall do
01410     // this way, or not, so it is up the implementation,
01411     // to skip the code below for the sake of performance ...
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         // for element whose name is "person", skip it
01419         //if (XMLString::compareString(node->getNodeName(), element_person)==0)
01420         //      return DOMNodeFilter::FILTER_SKIP;
01421         // for element whose name is "line", reject it
01422         //if (XMLString::compareString(node->getNodeName(), element_link)==0)
01423         //      return DOMNodeFilter::FILTER_REJECT;
01424         // for rest, accept it
01425         return DOMNodeFilter::FILTER_ACCEPT;
01426 
01427         break;
01428     }
01429     case DOMNode::COMMENT_NODE: {
01430         // the WhatToShow will make this no effect
01431         //return DOMNodeFilter::FILTER_REJECT;
01432         return DOMNodeFilter::FILTER_ACCEPT;
01433         break;
01434     }
01435     case DOMNode::TEXT_NODE: {
01436         // the WhatToShow will make this no effect
01437         //return DOMNodeFilter::FILTER_REJECT;
01438         return DOMNodeFilter::FILTER_ACCEPT;
01439         break;
01440     }
01441     case DOMNode::DOCUMENT_TYPE_NODE: {
01442         // even we say we are going to process document type,
01443         // we are not able be to see this node since
01444         // DOMWriterImpl (a XercesC's default implementation
01445         // of DOMWriter) will not pass DocumentType node to
01446         // this filter.
01447         //
01448         return DOMNodeFilter::FILTER_REJECT;  // no effect
01449         break;
01450     }
01451     case DOMNode::DOCUMENT_NODE: {
01452         // same as DOCUMENT_NODE
01453         return DOMNodeFilter::FILTER_REJECT;  // no effect
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 // DOMPrintErrorHandler
01469 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
01470 
01471 
01472 bool DOMPrintErrorHandler::handleError(const DOMError &domError)
01473 {
01474     // Display whatever error message passed from the serializer
01475     char *msg = XMLString::transcode(domError.getMessage());
01476     std::cout<<msg<<std::endl;
01477     delete[] msg;
01478 
01479     // Instructs the serializer to continue serialization if possible.
01480     return true;
01481 }
01482 
01483 
01484 //**************************************************************************
01485 //**************************************************************************
01486 // XMLCh
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 };

Generated on Wed Nov 23 19:00:25 2011 for FreeCAD by  doxygen 1.6.1