Goby v2
message_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 MESSAGE_XML_CALLBACKS20091211H
26 #define MESSAGE_XML_CALLBACKS20091211H
27 
28 #include <sstream>
29 #include <vector>
30 
31 #include <boost/algorithm/string.hpp> // for string functions
32 #include <xercesc/sax2/Attributes.hpp>
33 #include <xercesc/sax2/DefaultHandler.hpp>
34 
35 #include "goby/moos/transitional/xml/xerces_strings.h"
36 
37 #include "goby/acomms/dccl.h"
38 #include "goby/moos/transitional/xml/tags.h"
39 #include "message.h"
40 
41 namespace goby
42 {
43 namespace transitional
44 {
45 class DCCLMessage;
46 
47 // Implements callbacks that receive character data and
48 // notifications about the beginnings and ends of elements
49 class DCCLMessageContentHandler : public xercesc::DefaultHandler
50 {
51  public:
52  DCCLMessageContentHandler(std::vector<DCCLMessage>& messages) : messages(messages)
53  {
54  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<DCCLMessage>& messages;
85  std::string current_text;
86 
87  std::set<xml::Tag> parents_;
88  std::map<std::string, xml::Tag> tags_map_;
89 
90  transitional::DCCLHeaderPart curr_head_piece_;
91 };
92 
93 // Receives Error notifications.
94 class DCCLMessageErrorHandler : public xercesc::DefaultHandler
95 {
96  public:
97  void warning(const xercesc::SAXParseException& e)
98  {
99  std::cout << "warning:" << toNative(e.getMessage());
100  }
101  void error(const xercesc::SAXParseException& e)
102  {
103  XMLSSize_t line = e.getLineNumber();
104  std::stringstream ss;
105  ss << "message xml parsing error on line " << line << ": " << std::endl
106  << toNative(e.getMessage());
107 
108  throw goby::acomms::DCCLException(ss.str());
109  }
110  void fatalError(const xercesc::SAXParseException& e) { error(e); }
111 };
112 } // namespace transitional
113 } // namespace goby
114 #endif
The global namespace for the Goby project.