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, 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_PARSERS_HXX
00019 # define VCSN_XML_PARSERS_HXX
00020 
00021 # include <fstream>
00022 # include <xercesc/sax2/XMLReaderFactory.hpp>
00023 
00024 # include <vaucanson/xml/ios.hh>
00025 
00026 namespace vcsn
00027 {
00028   namespace xml
00029   {
00030     /*
00031      * Parser class.
00032      */
00033     std::string
00034     Parser::get_xsd_path ()
00035     {
00036       const char*       path = getenv("VCSN_DATA_PATH");
00037       const char*       xsd = "vaucanson.xsd";
00038 
00039       if (path == 0)
00040         path = VCSN_DATA_PATH;
00041       std::string file = std::string (path) + "/" + xsd;
00042 
00043       if (std::ifstream (file.c_str()).good())
00044         return file;
00045       FAIL (std::string ("Error: cannot open `") + path + "/" + xsd + "'.\n"
00046             "Please set VCSN_DATA_PATH to the Vaucanson data directory,\n"
00047             "containing `" + xsd + "'.");
00048       return "";
00049     }
00050 
00051     Parser::Parser (bool check)
00052       : parser_(0), eq_()
00053     {
00054       using namespace xercesc;
00055       parser_ = XMLReaderFactory::createXMLReader();
00056 
00057       if (check)
00058       {
00059         parser_->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);
00060         parser_->setFeature(XMLUni::fgSAX2CoreValidation, true);
00061         parser_->setFeature(XMLUni::fgXercesSchema, true);
00062          parser_->setFeature(XMLUni::fgXercesSchemaFullChecking, true);
00063         parser_->setFeature(XMLUni::fgXercesValidationErrorAsFatal, true);
00064         parser_->setFeature(XMLUni::fgXercesUseCachedGrammarInParse, true);
00065         parser_->setFeature(XMLUni::fgXercesCacheGrammarFromParse, true);
00066          XMLCh* xsd = transcode(VCSN_XMLNS " " + get_xsd_path());
00067          parser_->setProperty(XMLUni::fgXercesSchemaExternalSchemaLocation, xsd);
00068          XMLString::release(&xsd);
00069       }
00070 
00071       err_handler_ = new ErrHandler();
00072       parser_->setErrorHandler(err_handler_);
00073     }
00074 
00075     Parser::~Parser ()
00076     {
00077       delete parser_;
00078       delete err_handler_;
00079     }
00080 
00081     /*
00082      * AutParser class.
00083      */
00084     template <typename Auto>
00085     AutParser<Auto>::AutParser (Auto& a, bool check)
00086       : Parser(check), a_(a)
00087     {
00088       doc_handler_ = new DocHandler<Auto>(parser_, *err_handler_, a_, eq_);
00089       parser_->setContentHandler(doc_handler_);
00090     }
00091 
00092     template <typename Auto>
00093     AutParser<Auto>::~AutParser ()
00094     {
00095       delete doc_handler_;
00096     }
00097 
00098     template <typename Auto>
00099     void
00100     AutParser<Auto>::parse (std::istream& in)
00101     {
00102       CxxInputSource is(&in);
00103       parser_->parse(is);
00104     }
00105   } // !xml
00106 } // !vcsn
00107 
00108 #endif // !VCSN_XML_PARSERS_HXX

Generated on Thu Oct 9 20:22:40 2008 for Vaucanson by  doxygen 1.5.1