📄 cpfdemarshaller.cpp
字号:
/*************************************************************************** tag: Peter Soetens Tue Apr 5 16:53:25 CEST 2005 CPFDemarshaller.cxx CPFDemarshaller.cxx - description ------------------- begin : Tue April 05 2005 copyright : (C) 2005 Peter Soetens email : peter.soetens@mech.kuleuven.ac.be *************************************************************************** * This library is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public * * License as published by the Free Software Foundation; * * version 2 of the License. * * * * As a special exception, you may use this file as part of a free * * software library without restriction. Specifically, if other files * * instantiate templates or use macros or inline functions from this * * file, or you compile this file and link it with other files to * * produce an executable, this file does not by itself cause the * * resulting executable to be covered by the GNU General Public * * License. This exception does not however invalidate any other * * reasons why the executable file might be covered by the GNU General * * Public License. * * * * This library is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * * Lesser General Public License for more details. * * * * You should have received a copy of the GNU General Public * * License along with this library; if not, write to the Free Software * * Foundation, Inc., 59 Temple Place, * * Suite 330, Boston, MA 02111-1307 USA * * * ***************************************************************************/ #include "marsh/CPFDemarshaller.hpp"#include "marsh/CPFDTD.hpp"#ifdef OROPKG_SUPPORT_XERCES_C#include <xercesc/util/PlatformUtils.hpp>#include <xercesc/util/TransService.hpp>#include <xercesc/sax2/SAX2XMLReader.hpp>#include <xercesc/sax2/XMLReaderFactory.hpp>#include <xercesc/sax2/DefaultHandler.hpp>#include <xercesc/sax2/Attributes.hpp>#include <xercesc/util/XMLUniDefs.hpp>#include <xercesc/util/BinMemInputStream.hpp>#include <xercesc/framework/LocalFileInputSource.hpp>#include <xercesc/framework/MemBufInputSource.hpp>#include <xercesc/validators/common/Grammar.hpp>#include <vector>#include <stack>#include <map>#include <string>#include <iostream>#include <Property.hpp>#include "PropertyIntrospection.hpp"#include <Logger.hpp>namespace RTT{#ifdef XERCES_CPP_NAMESPACE using namespace XERCES_CPP_NAMESPACE;#endif inline bool XMLChToStdString(const XMLCh* const c, std::string& res) { char* chholder; chholder = XMLString::transcode( c ); if ( chholder ) { res = chholder; delete[] chholder; return true; } return false; } inline std::string XMLgetString(const XMLCh* const c) { std::string res; char* chholder; chholder = XMLString::transcode( c ); if ( chholder ) { res = chholder; delete[] chholder; return res; } return res; } class SAX2CPFHandler : public DefaultHandler { /** * Stores the results of the parsing. */ PropertyBag &bag; std::stack< std::pair<PropertyBag*, Property<PropertyBag>*> > bag_stack; enum Tag { TAG_STRUCT, TAG_SIMPLE, TAG_SEQUENCE, TAG_PROPERTIES, TAG_DESCRIPTION, TAG_VALUE, TAG_UNKNOWN}; std::stack<Tag> tag_stack; /** * The name of the property. */ std::string name; std::string description; std::string type; std::string value_string; public: SAX2CPFHandler( PropertyBag &b ) : bag( b ) { Property<PropertyBag>* dummy = 0; bag_stack.push(std::make_pair(&bag, dummy)); } void endElement( const XMLCh* const uri, const XMLCh* const localname, const XMLCh* const qname ) { //char *ln = XMLString::transcode( localname );// if ( value_string.empty() ) {// Logger::log()<<Logger::Debug << "SAX2CPFHandler : Empty value for property."// <<Logger::endl;// } switch ( tag_stack.top() ) { case TAG_SIMPLE: if ( type == "boolean" ) { if ( value_string == "1" || value_string == "true") bag_stack.top().first->add ( new Property<bool>( name, description, true ) ); else if ( value_string == "0" || value_string == "false" ) bag_stack.top().first->add ( new Property<bool>( name, description, false ) ); else throw SAXException(std::string("Wrong value for property '"+type+"'." \ " Value should contain '0' or '1', got '"+ value_string +"'.").c_str()); } else if ( type == "char" ) { if ( value_string.length() != 1 ) throw SAXException(std::string("Wrong value for property '"+type+"'." \ " Value should contain a single character, got '"+ value_string +"'.").c_str()); else bag_stack.top().first->add ( new Property<char>( name, description, value_string[0] ) ); } else if ( type == "uchar" ) { if ( value_string.length() != 1 ) throw SAXException(std::string("Wrong value for property '"+type+"'." \ " Value should contain a single unsigned character, got '"+ value_string +"'.").c_str()); else bag_stack.top().first->add ( new Property<unsigned char>( name, description, value_string[0] ) ); } else if ( type == "long" || type == "short") { int v; if ( sscanf(value_string.c_str(), "%d", &v) == 1) bag_stack.top().first->add( new Property<int>( name, description, v ) ); else throw SAXException(std::string("Wrong value for property '"+type+"'." \ " Value should contain an integer value, got '"+ value_string +"'.").c_str()); } else if ( type == "ulong" || type == "ushort") { unsigned int v; if ( sscanf(value_string.c_str(), "%u", &v) == 1) bag_stack.top().first->add( new Property<unsigned int>( name, description, v ) ); else throw SAXException(std::string("Wrong value for property '"+type+"'." \ " Value should contain an integer value, got '"+ value_string +"'.").c_str()); } else if ( type == "double") { double v; if ( sscanf(value_string.c_str(), "%lf", &v) == 1 ) bag_stack.top().first->add ( new Property<double>( name, description, v ) ); else throw SAXException(std::string("Wrong value for property '"+type+"'." \ " Value should contain a double value, got '"+ value_string +"'.").c_str()); } else if ( type == "float") { float v; if ( sscanf(value_string.c_str(), "%f", &v) == 1 ) bag_stack.top().first->add ( new Property<float>( name, description, v ) ); else throw SAXException(std::string("Wrong value for property '"+type+"'." \ " Value should contain a float value, got '"+ value_string +"'.").c_str()); } else if ( type == "string") bag_stack.top().first->add ( new Property<std::string>( name, description, value_string ) ); tag_stack.pop(); value_string.clear(); // cleanup description.clear(); name.clear(); break; case TAG_SEQUENCE: case TAG_STRUCT: { Property<PropertyBag>* prop = bag_stack.top().second; bag_stack.pop(); bag_stack.top().first->add( prop ); //( new Property<PropertyBag>( pn, description, *pb ) ); //delete pb; tag_stack.pop(); description.clear(); name.clear(); type.clear(); } break; case TAG_DESCRIPTION: tag_stack.pop(); if ( tag_stack.top() == TAG_STRUCT ) { // it is a description of a struct that ended bag_stack.top().second->setDescription(description); description.clear(); } break; case TAG_VALUE: case TAG_PROPERTIES: case TAG_UNKNOWN: tag_stack.pop(); break; } } void startElement( const XMLCh* const uri, const XMLCh* const localname, const XMLCh* const qname, const Attributes& attributes ) { std::string ln; XMLChToStdString( localname, ln );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -