00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
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 
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 
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   } 
00106 } 
00107 
00108 #endif // !VCSN_XML_PARSERS_HXX