parsers.hxx

00001 // parsers.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2005, 2006, 2007 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_PARSERS_HXX
00019 # define VCSN_XML_PARSERS_HXX
00020 
00031 # include <fstream>
00032 # include <xercesc/sax2/XMLReaderFactory.hpp>
00033 
00034 # include <vaucanson/xml/ios.hh>
00035 # include <vaucanson/xml/strings.hh>
00036 
00037 namespace vcsn
00038 {
00039   namespace xml
00040   {
00041     /*
00042      * Parser class.
00043      */
00044     std::string
00045     Parser::get_xsd_path ()
00046     {
00047       const char*       path = getenv("VCSN_DATA_PATH");
00048       const char*       xsd = "vaucanson.xsd";
00049 
00050       if (path == 0)
00051         path = VCSN_DATA_PATH;
00052       std::string file = std::string (path) + "/" + xsd;
00053 
00054       if (std::ifstream (file.c_str()).good())
00055         return file;
00056       FAIL (std::string ("Error: cannot open `") + path + "/" + xsd + "'.\n"
00057             "Please set VCSN_DATA_PATH to the Vaucanson data directory,\n"
00058             "containing `" + xsd + "'.");
00059       return "";
00060     }
00061 
00062     Parser::Parser (bool check)
00063       : parser_(0), eq_()
00064     {
00065       using namespace xercesc;
00066       parser_ = XMLReaderFactory::createXMLReader();
00067 
00068       if (check)
00069       {
00070         parser_->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);
00071         parser_->setFeature(XMLUni::fgSAX2CoreValidation, true);
00072         parser_->setFeature(XMLUni::fgXercesSchema, true);
00073         //parser->setFeature(XMLUni::fgXercesSchemaFullChecking, true);
00074         parser_->setFeature(XMLUni::fgXercesValidationErrorAsFatal, true);
00075         parser_->setFeature(XMLUni::fgXercesUseCachedGrammarInParse, true);
00076         parser_->setFeature(XMLUni::fgXercesCacheGrammarFromParse, true);
00077         XMLCh* xsd = transcode(VCSN_XMLNS " " + get_xsd_path());
00078         parser_->setProperty(XMLUni::fgXercesSchemaExternalSchemaLocation,
00079                              xsd);
00080         XMLString::release(&xsd);
00081       }
00082 
00083       err_handler_ = new ErrHandler();
00084       parser_->setErrorHandler(err_handler_);
00085     }
00086 
00087     Parser::~Parser ()
00088     {
00089       delete parser_;
00090       delete err_handler_;
00091     }
00092 
00093     /*
00094      * AutParser class.
00095      */
00096     template <typename Auto>
00097     AutParser<Auto>::AutParser (Auto& a, bool check)
00098       : Parser(check), a_(a)
00099     {
00100       doc_handler_ = new AutHandler<Auto>(a_, this->parser_, eq_);
00101       this->parser_->setContentHandler(doc_handler_);
00102     }
00103 
00104     template <typename Auto>
00105     AutParser<Auto>::~AutParser ()
00106     {
00107       delete doc_handler_;
00108     }
00109 
00110     template <typename Auto>
00111     void
00112     AutParser<Auto>::parse (std::istream& in)
00113     {
00114       CxxInputSource is(&in);
00115       this->parser_->parse(is);
00116     }
00117 
00118     /*
00119      * SessParser class.
00120      */
00121     SessParser::SessParser (bool check)
00122       : Parser(check), got_more_(true)
00123     {
00124     }
00125 
00126     SessParser::~SessParser()
00127     {
00128       this->parser_->parseReset(token_);
00129     }
00130 
00131     void
00132     SessParser::init (std::istream& in)
00133     {
00134       CxxInputSource is(&in);
00135       this->parser_->parseFirst(is, token_);
00136     }
00137 
00138     template <typename Auto>
00139     bool
00140     SessParser::operator()(Auto& a)
00141     {
00142       AutHandler<Auto> doc_h(a, this->parser_);
00143       this->parser_->setContentHandler(&doc_h);
00144 
00145       while (got_more_ && !this->parser_->getErrorCount() && !doc_h.end())
00146         got_more_ = this->parser_->parseNext(token_);
00147       return got_more_;
00148     }
00149 
00150   } // !xml
00151 } // !vcsn
00152 
00153 #endif // !VCSN_XML_PARSER_HXX

Generated on Sun Jul 29 19:35:28 2007 for Vaucanson by  doxygen 1.5.2