00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
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
00080
00081
00082 Handler::Handler (xercesc::SAX2XMLReader* parser,
00083 xercesc::DefaultHandler& root,
00084 XMLEq& eq)
00085 : DefaultHandler(),
00086 parser_(parser),
00087 root_(root),
00088 eq_(eq)
00089 {
00090 }
00091
00092 Handler::Handler (xercesc::SAX2XMLReader* parser,
00093 Handler& root)
00094 : DefaultHandler(),
00095 parser_(parser),
00096 root_(root),
00097 eq_(root.eq_)
00098 {
00099 }
00100
00101 void
00102 Handler::startElement (const XMLCh* const uri,
00103 const XMLCh* const localname,
00104 const XMLCh* const qname,
00105 const xercesc::Attributes& attrs)
00106 {
00107 # ifdef DEBUG
00108 indent_++;
00109 std::cout << std::string(2 * indent_, ' ') << "<" << xmlstr(localname) << ">" << std::endl;
00110 # endif
00111 start(uri, localname, qname, attrs);
00112 }
00113
00114 void
00115 Handler::endElement (const XMLCh* const uri,
00116 const XMLCh* const localname,
00117 const XMLCh* const qname)
00118 {
00119 end(uri, localname, qname);
00120 # ifdef DEBUG
00121 std::cout << std::string(2 * indent_, ' ') << "</" << xmlstr(localname) << ">" << std::endl;
00122 indent_--;
00123 # endif
00124 }
00125
00126
00127
00128 UnsupHandler::UnsupHandler (xercesc::SAX2XMLReader* parser,
00129 Handler& root)
00130 : Handler(parser, root),
00131 depth_(1)
00132 {
00133 }
00134
00135 void
00136 UnsupHandler::start (const XMLCh* const,
00137 const XMLCh* const,
00138 const XMLCh* const,
00139 const xercesc::Attributes&)
00140 {
00141 depth_++;
00142 }
00143
00144 void
00145 UnsupHandler::end (const XMLCh* const,
00146 # ifdef DEBUG
00147 const XMLCh* const localname,
00148 # else
00149 const XMLCh* const,
00150 # endif
00151 const XMLCh* const)
00152 {
00153 # ifdef DEBUG
00154 std::cerr << std::string((indent_ + 1) * 2, ' ') << "unsupported token: " << xmlstr(localname) << std::endl;
00155 # endif
00156 depth_--;
00157 if (depth_ <= 0)
00158 {
00159 parser_->setContentHandler(&root_);
00160 depth_ = 1;
00161 }
00162 }
00163 }
00164 }
00165
00166 #endif // !VCSN_XML_HANDLERS_BASE_HXX