Vaucanson 1.4
handlers_base.hxx
Go to the documentation of this file.
00001 // handlers_base.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2007, 2008 The Vaucanson Group.
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // The complete GNU General Public Licence Notice can be found as the
00013 // `COPYING' file in the root directory.
00014 //
00015 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
00016 //
00017 
00018 #ifndef VCSN_XML_HANDLERS_BASE_HXX
00019 # define VCSN_XML_HANDLERS_BASE_HXX
00020 
00021 # include <xercesc/util/XMLString.hpp>
00022 
00023 # include <vaucanson/xml/tools.hh>
00024 
00025 namespace vcsn
00026 {
00027   namespace xml
00028   {
00029     /*
00030      * ErrHandler
00031      */
00032     void
00033     ErrHandler::warning (const xercesc::SAXParseException& exc)
00034     {
00035       char* msg = xercesc::XMLString::transcode(exc.getMessage());
00036       std::cerr << "Warning: " << msg << std::endl;
00037       xercesc::XMLString::release(&msg);
00038     }
00039 
00040     void
00041     ErrHandler::error (const xercesc::SAXParseException& exc)
00042     {
00043       char* msg = xercesc::XMLString::transcode(exc.getMessage());
00044       std::cerr << "Error: " << msg << std::endl;
00045       xercesc::XMLString::release(&msg);
00046     }
00047 
00048     void
00049     ErrHandler::fatalError (const xercesc::SAXParseException& exc)
00050     {
00051       char* msg = xercesc::XMLString::transcode(exc.getMessage());
00052       std::cerr << "Fatal error: " << msg << std::endl;
00053       xercesc::XMLString::release(&msg);
00054       throw exc;
00055     }
00056 
00057     namespace error
00058     {
00059       void token(const XMLCh* const localname)
00060       {
00061         std::cerr << "Unexpected token: " << xmlstr(localname) << std::endl;
00062         assertion(false);
00063       }
00064       void attrs(const XMLCh* const localname,
00065                  const std::string& name,
00066                  const std::string& value)
00067       {
00068         std::cerr << "Unexpected value of `" << name << "' (" << value << ") in token: " << xmlstr(localname) << std::endl;
00069         assertion(false);
00070       }
00071 
00072       void missattrs(const XMLCh* const localname,
00073                      const std::string& name)
00074       {
00075         std::cerr << "Missing attribute `" << name << " in token: " << xmlstr(localname) << std::endl;
00076         assertion(false);
00077       }
00078 
00079       void notletter(const std::string& str)
00080       {
00081         std::cerr << str << " is not a letter" << std::endl;
00082         assertion(false);
00083       }
00084 
00085     } // !error
00086     /*
00087      * Handler
00088      */
00089     Handler::Handler (xercesc::SAX2XMLReader* parser,
00090                       xercesc::DefaultHandler& root,
00091                       XMLEq& eq)
00092       : DefaultHandler(),
00093         parser_(parser),
00094         root_(root),
00095         eq_(eq)
00096     {
00097     }
00098 
00099     Handler::Handler (xercesc::SAX2XMLReader* parser,
00100                       Handler& root)
00101       : DefaultHandler(),
00102         parser_(parser),
00103         root_(root),
00104         eq_(root.eq_)
00105     {
00106     }
00107 
00108     void
00109     Handler::startElement (const XMLCh* const uri,
00110                                 const XMLCh* const localname,
00111                                 const XMLCh* const qname,
00112                                 const xercesc::Attributes& attrs)
00113     {
00114 # ifdef DEBUG
00115       indent_++;
00116       std::cout << std::string(2 * indent_, ' ') << "<" << xmlstr(localname) << ">" << std::endl;
00117 # endif
00118       start(uri, localname, qname, attrs);
00119     }
00120 
00121     void
00122     Handler::endElement (const XMLCh* const uri,
00123                               const XMLCh* const localname,
00124                               const XMLCh* const qname)
00125     {
00126       end(uri, localname, qname);
00127 # ifdef DEBUG
00128       std::cout << std::string(2 * indent_, ' ') << "</" << xmlstr(localname) << ">" << std::endl;
00129       indent_--;
00130 # endif
00131     }
00132     /*
00133      * UnsupHandler
00134      */
00135     UnsupHandler::UnsupHandler (xercesc::SAX2XMLReader* parser,
00136                                 Handler& root)
00137       : Handler(parser, root),
00138         depth_(1)
00139     {
00140     }
00141 
00142     void
00143     UnsupHandler::start (const XMLCh* const,
00144                                 const XMLCh* const,
00145                                 const XMLCh* const,
00146                                 const xercesc::Attributes&)
00147     {
00148       depth_++;
00149     }
00150 
00151     void
00152     UnsupHandler::end (const XMLCh* const,
00153 # ifdef DEBUG
00154                               const XMLCh* const localname,
00155 # else
00156                               const XMLCh* const,
00157 # endif
00158                               const XMLCh* const)
00159     {
00160 # ifdef DEBUG
00161       std::cerr << std::string((indent_ + 1) * 2, ' ') << "unsupported token: " << xmlstr(localname) << std::endl;
00162 # endif
00163       depth_--;
00164       if (depth_ <= 0)
00165       {
00166         parser_->setContentHandler(&root_);
00167         depth_ = 1;
00168       }
00169     }
00170   } // !xml
00171 } // !vcsn
00172 
00173 #endif // !VCSN_XML_HANDLERS_BASE_HXX