Goby v2
queue_xml_callbacks.h
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Goby Underwater Autonomy Project Libraries
8 // ("The Goby Libraries").
9 //
10 // The Goby Libraries are free software: you can redistribute them and/or modify
11 // them under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // The Goby Libraries are distributed in the hope that they will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
22 
23 // 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
24 
25 #ifndef QUEUE_XML_CALLBACKS20091211H
26 #define QUEUE_XML_CALLBACKS20091211H
27 
28 #include <iostream>
29 #include <sstream>
30 #include <stdexcept>
31 #include <vector>
32 
33 #include <boost/algorithm/string.hpp>
34 
35 #include <xercesc/sax2/Attributes.hpp>
36 #include <xercesc/sax2/DefaultHandler.hpp>
37 
38 #include "goby/common/exception.h"
39 #include "goby/moos/protobuf/transitional.pb.h"
40 #include "goby/moos/transitional/xml/tags.h"
41 #include "goby/util/as.h"
42 
43 namespace goby
44 {
45 namespace transitional
46 {
47 // Implements callbacks that receive character data and
48 // notifications about the beginnings and ends of elements
49 class QueueContentHandler : public xercesc::DefaultHandler
50 {
51  public:
52  QueueContentHandler(std::vector<goby::transitional::protobuf::QueueConfig>& q) : q_(q)
53  {
54  xml::initialize_tags(tags_map_);
55  }
56 
57  void startElement(const XMLCh* const uri, // namespace URI
58  const XMLCh* const localname, // tagname w/ out NS prefix
59  const XMLCh* const qname, // tagname + NS pefix
60  const xercesc::Attributes& attrs); // elements's attributes
61 
62  void endElement(const XMLCh* const uri, // namespace URI
63  const XMLCh* const localname, // tagname w/ out NS prefix
64  const XMLCh* const qname); // tagname + NS pefix
65 
66 #if XERCES_VERSION_MAJOR < 3
67  void characters(const XMLCh* const chars, const unsigned int length)
68  {
69  current_text.append(toNative(chars), 0, length);
70  }
71 #else
72  void characters(const XMLCh* const chars, const XMLSize_t length)
73  {
74  current_text.append(toNative(chars), 0, length);
75  }
76 #endif
77 
78  private:
79  bool in_message_var() { return xml::in_message_var(parents_); }
80  bool in_header_var() { return xml::in_header_var(parents_); }
81  bool in_publish() { return xml::in_publish(parents_); }
82 
83  private:
84  std::vector<goby::transitional::protobuf::QueueConfig>& q_;
85  std::string current_text;
86 
87  std::set<xml::Tag> parents_;
88  std::map<std::string, xml::Tag> tags_map_;
89 };
90 
91 // Receives Error notifications.
92 class QueueErrorHandler : public xercesc::DefaultHandler
93 {
94  public:
95  void warning(const xercesc::SAXParseException& e)
96  {
97  std::cout << "warning:" << toNative(e.getMessage());
98  }
99  void error(const xercesc::SAXParseException& e)
100  {
101  XMLSSize_t line = e.getLineNumber();
102  std::stringstream ss;
103  ss << "xml parsing error on line " << line << ": " << std::endl << toNative(e.getMessage());
104  throw goby::Exception(ss.str());
105  }
106  void fatalError(const xercesc::SAXParseException& e) { error(e); }
107 };
108 } // namespace transitional
109 } // namespace goby
110 
111 #endif
The global namespace for the Goby project.
simple exception class for goby applications
Definition: exception.h:32