Note: Goby version 1 (shown here) is now considered obsolete. Please use version 2 for new projects, and consider upgrading old projects.

Goby Underwater Autonomy Project  Series: 1.1, revision: 163, released on 2013-02-06 14:23:27 -0500
acomms/libdccl/message_xml_callbacks.h
00001 // copyright 2008, 2009 t. schneider tes@mit.edu
00002 // 
00003 // this file is part of the Dynamic Compact Control Language (DCCL),
00004 // the goby-acomms codec. goby-acomms is a collection of libraries 
00005 // for acoustic underwater networking
00006 //
00007 // This program is free software: you can redistribute it and/or modify
00008 // it under the terms of the GNU General Public License as published by
00009 // the Free Software Foundation, either version 3 of the License, or
00010 // (at your option) any later version.
00011 //
00012 // This software 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 General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with this software.  If not, see <http://www.gnu.org/licenses/>.
00019 
00020 // xml code based initially on work in C++ Cookbook by D. Ryan Stephens, Christopher Diggins, Jonathan Turkanis, and Jeff Cogswell. Copyright 2006 O'Reilly Media, INc., 0-596-00761-2
00021 
00022 #ifndef MESSAGE_XML_CALLBACKS20091211H
00023 #define MESSAGE_XML_CALLBACKS20091211H
00024 
00025 #include <vector>
00026 #include <sstream>
00027 
00028 #include <xercesc/sax2/Attributes.hpp>
00029 #include <xercesc/sax2/DefaultHandler.hpp> 
00030 #include <boost/algorithm/string.hpp> // for string functions
00031 
00032 #include "goby/acomms/xml/xerces_strings.h"
00033 
00034 #include "message.h"
00035 #include "dccl_exception.h"
00036 #include "goby/acomms/xml/tags.h"
00037 
00038 namespace goby
00039 {
00040     namespace acomms
00041     {
00042     
00043 // Implements callbacks that receive character data and
00044 // notifications about the beginnings and ends of elements 
00045         class DCCLMessageContentHandler : public xercesc::DefaultHandler {
00046           public:
00047           DCCLMessageContentHandler(std::vector<DCCLMessage> & messages) : messages(messages)
00048             { initialize_tags(tags_map_); }
00049 
00050             void startElement( 
00051                 const XMLCh *const uri,        // namespace URI
00052                 const XMLCh *const localname,  // tagname w/ out NS prefix
00053                 const XMLCh *const qname,      // tagname + NS pefix
00054                 const xercesc::Attributes &attrs );      // elements's attributes
00055 
00056             void endElement(          
00057                 const XMLCh *const uri,        // namespace URI
00058                 const XMLCh *const localname,  // tagname w/ out NS prefix
00059                 const XMLCh *const qname );     // tagname + NS pefix
00060 
00061 #if XERCES_VERSION_MAJOR < 3
00062             void characters(const XMLCh* const chars, const unsigned int length)
00063        { current_text.append(toNative(chars), 0, length); }
00064 #else
00065             void characters(const XMLCh* const chars, const XMLSize_t length )
00066        { current_text.append(toNative(chars), 0, length); }
00067 #endif
00068 
00069           private:
00070             bool in_message_var()
00071             { return xml::in_message_var(parents_); }
00072             bool in_header_var()
00073             { return xml::in_header_var(parents_); }
00074             bool in_publish()
00075             { return xml::in_publish(parents_); }
00076         
00077           private:
00078             std::vector<DCCLMessage>& messages;
00079             std::string current_text;
00080 
00081             std::set<xml::Tag> parents_;
00082             std::map<std::string, xml::Tag> tags_map_;
00083 
00084             acomms::DCCLHeaderPart curr_head_piece_;
00085         
00086         };
00087 
00088 // Receives Error notifications.
00089         class DCCLMessageErrorHandler : public xercesc::DefaultHandler 
00090         {
00091 
00092           public:
00093             void warning(const xercesc::SAXParseException& e)
00094             {
00095                 std::cout << "warning:" << toNative(e.getMessage());
00096             }
00097             void error(const xercesc::SAXParseException& e)
00098             {
00099                 XMLSSize_t line = e.getLineNumber();
00100                 std::stringstream ss;
00101                 ss << "message xml parsing error on line " << line << ": " << std::endl << toNative(e.getMessage());
00102             
00103                 throw DCCLException(ss.str());
00104             }
00105             void fatalError(const xercesc::SAXParseException& e) 
00106             {
00107                 error(e);
00108             }
00109         };
00110     }
00111 }
00112 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends