Vaucanson  1.4.1
handlers_base.hxx
Go to the documentation of this file.
1 // handlers_base.hxx: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2007, 2008 The Vaucanson Group.
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // The complete GNU General Public Licence Notice can be found as the
13 // `COPYING' file in the root directory.
14 //
15 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
16 //
17 
18 #ifndef VCSN_XML_HANDLERS_BASE_HXX
19 # define VCSN_XML_HANDLERS_BASE_HXX
20 
21 # include <xercesc/util/XMLString.hpp>
22 
23 # include <vaucanson/xml/tools.hh>
24 
25 namespace vcsn
26 {
27  namespace xml
28  {
29  /*
30  * ErrHandler
31  */
32  void
33  ErrHandler::warning (const xercesc::SAXParseException& exc)
34  {
35  char* msg = xercesc::XMLString::transcode(exc.getMessage());
36  std::cerr << "Warning: " << msg << std::endl;
37  xercesc::XMLString::release(&msg);
38  }
39 
40  void
41  ErrHandler::error (const xercesc::SAXParseException& exc)
42  {
43  char* msg = xercesc::XMLString::transcode(exc.getMessage());
44  std::cerr << "Error: " << msg << std::endl;
45  xercesc::XMLString::release(&msg);
46  }
47 
48  void
49  ErrHandler::fatalError (const xercesc::SAXParseException& exc)
50  {
51  char* msg = xercesc::XMLString::transcode(exc.getMessage());
52  std::cerr << "Fatal error: " << msg << std::endl;
53  xercesc::XMLString::release(&msg);
54  throw exc;
55  }
56 
57  namespace error
58  {
59  void token(const XMLCh* const localname)
60  {
61  std::cerr << "Unexpected token: " << xmlstr(localname) << std::endl;
62  assertion(false);
63  }
64  void attrs(const XMLCh* const localname,
65  const std::string& name,
66  const std::string& value)
67  {
68  std::cerr << "Unexpected value of `" << name << "' (" << value << ") in token: " << xmlstr(localname) << std::endl;
69  assertion(false);
70  }
71 
72  void missattrs(const XMLCh* const localname,
73  const std::string& name)
74  {
75  std::cerr << "Missing attribute `" << name << " in token: " << xmlstr(localname) << std::endl;
76  assertion(false);
77  }
78 
79  void notletter(const std::string& str)
80  {
81  std::cerr << str << " is not a letter" << std::endl;
82  assertion(false);
83  }
84 
85  } // !error
86  /*
87  * Handler
88  */
89  Handler::Handler (xercesc::SAX2XMLReader* parser,
90  xercesc::DefaultHandler& root,
91  XMLEq& eq)
92  : DefaultHandler(),
93  parser_(parser),
94  root_(root),
95  eq_(eq)
96  {
97  }
98 
99  Handler::Handler (xercesc::SAX2XMLReader* parser,
100  Handler& root)
101  : DefaultHandler(),
102  parser_(parser),
103  root_(root),
104  eq_(root.eq_)
105  {
106  }
107 
108  void
109  Handler::startElement (const XMLCh* const uri,
110  const XMLCh* const localname,
111  const XMLCh* const qname,
112  const xercesc::Attributes& attrs)
113  {
114 # ifdef DEBUG
115  indent_++;
116  std::cout << std::string(2 * indent_, ' ') << "<" << xmlstr(localname) << ">" << std::endl;
117 # endif
118  start(uri, localname, qname, attrs);
119  }
120 
121  void
122  Handler::endElement (const XMLCh* const uri,
123  const XMLCh* const localname,
124  const XMLCh* const qname)
125  {
126  end(uri, localname, qname);
127 # ifdef DEBUG
128  std::cout << std::string(2 * indent_, ' ') << "</" << xmlstr(localname) << ">" << std::endl;
129  indent_--;
130 # endif
131  }
132  /*
133  * UnsupHandler
134  */
135  UnsupHandler::UnsupHandler (xercesc::SAX2XMLReader* parser,
136  Handler& root)
137  : Handler(parser, root),
138  depth_(1)
139  {
140  }
141 
142  void
143  UnsupHandler::start (const XMLCh* const,
144  const XMLCh* const,
145  const XMLCh* const,
146  const xercesc::Attributes&)
147  {
148  depth_++;
149  }
150 
151  void
152  UnsupHandler::end (const XMLCh* const,
153 # ifdef DEBUG
154  const XMLCh* const localname,
155 # else
156  const XMLCh* const,
157 # endif
158  const XMLCh* const)
159  {
160 # ifdef DEBUG
161  std::cerr << std::string((indent_ + 1) * 2, ' ') << "unsupported token: " << xmlstr(localname) << std::endl;
162 # endif
163  depth_--;
164  if (depth_ <= 0)
165  {
166  parser_->setContentHandler(&root_);
167  depth_ = 1;
168  }
169  }
170  } // !xml
171 } // !vcsn
172 
173 #endif // !VCSN_XML_HANDLERS_BASE_HXX