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/libqueue/queue_xml_callbacks.h
00001 // copyright 2009 t. schneider tes@mit.edu 
00002 //
00003 // this file is part of the Queue Library (libqueue),
00004 // the goby-acomms message queue manager. goby-acomms is a collection of 
00005 // libraries 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 QUEUE_XML_CALLBACKS20091211H
00023 #define QUEUE_XML_CALLBACKS20091211H
00024 
00025 #include <stdexcept>
00026 #include <vector>
00027 #include <sstream>
00028 
00029 #include <xercesc/sax2/Attributes.hpp>
00030 #include <xercesc/sax2/DefaultHandler.hpp>
00031 
00032 #include "goby/protobuf/queue.pb.h"
00033 #include "goby/acomms/xml/xerces_strings.h"
00034 #include "goby/acomms/xml/tags.h"
00035 #include "queue_manager.h"
00036 
00037 namespace goby
00038 {
00039     namespace acomms
00040     {
00041     
00042 // Implements callbacks that receive character data and
00043 // notifications about the beginnings and ends of elements 
00044         class QueueContentHandler : public xercesc::DefaultHandler {
00045           public:
00046           QueueContentHandler(std::vector<protobuf::QueueConfig>& q)
00047               : q_(q)
00048             { xml::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<protobuf::QueueConfig>& q_;
00079             std::string current_text;
00080         
00081             std::set<xml::Tag> parents_;
00082             std::map<std::string, xml::Tag> tags_map_;
00083         };
00084 
00085 // Receives Error notifications.
00086         class QueueErrorHandler : public xercesc::DefaultHandler 
00087         {
00088 
00089           public:
00090             void warning(const xercesc::SAXParseException& e)
00091             {
00092                 std::cout << "warning:" << toNative(e.getMessage());
00093             }
00094             void error(const xercesc::SAXParseException& e)
00095             {
00096                 XMLSSize_t line = e.getLineNumber();
00097                 std::stringstream ss;
00098                 ss << "xml parsing error on line " << line << ": " << std::endl << toNative(e.getMessage());
00099                 throw queue_exception(ss.str());
00100             }
00101             void fatalError(const xercesc::SAXParseException& e) 
00102             {
00103                 error(e);
00104             }
00105         };
00106     }
00107 }
00108 
00109 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends