Note: Goby version 1 (shown here) is now considered obsolete. Please use version 2 for new projects, and consider upgrading old projects.

Goby Underwater Autonomy Project  Series: 1.1, revision: 163, released on 2013-02-06 14:23:27 -0500
acomms/xml/xml_parser.h
00001 // copyright 2008, 2009 t. schneider tes@mit.edu
00002 // 
00003 // this file is part of goby-acomms, a collection of libraries for acoustic underwater networking
00004 //
00005 // This program is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // This software is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this software.  If not, see <http://www.gnu.org/licenses/>.
00017 
00018 // based initially on work in C++ Cookbook by D. Ryan Stephens, Christopher Diggins, Jonathan Turkanis, and Jeff Cogswell. Copyright 2006 O'Reilly Media, INc., 0-596-00761-2.
00019 
00020 #ifndef XML_PARSER20091211H
00021 #define XML_PARSER20091211H
00022 
00023 
00024 #include <exception>
00025 #include <iostream>     // cout
00026 #include <memory>       // auto_ptr
00027 #include <vector>
00028 #include <fstream>
00029 
00030 #include <xercesc/sax2/SAX2XMLReader.hpp>
00031 #include <xercesc/sax2/XMLReaderFactory.hpp>
00032 #include <xercesc/util/PlatformUtils.hpp>
00033 #include <xercesc/sax2/DefaultHandler.hpp>
00034 
00035 #include "xerces_strings.h" 
00036 #include "message_schema.xsd.h"
00037 
00038     
00039 // RAII utility that initializes the parser and frees resources
00040 // when it goes out of scope
00041 class XercesInitializer {
00042   public:
00043     XercesInitializer( ) { xercesc::XMLPlatformUtils::Initialize( ); }
00044     ~XercesInitializer( ) { xercesc::XMLPlatformUtils::Terminate( ); }
00045   private:
00046     // Prohibit copying and assignment
00047     XercesInitializer(const XercesInitializer&);
00048     XercesInitializer& operator=(const XercesInitializer&);
00049 };
00050 
00051 class XMLParser {
00052   public:
00053   XMLParser(xercesc::DefaultHandler& content, xercesc::DefaultHandler& error)
00054       : content_(content),
00055         error_(error) 
00056         { }
00057         
00058     bool parse(const std::string& file)
00059     {
00060         // Initialize Xerces and obtain parser
00061         XercesInitializer init; 
00062         std::auto_ptr<xercesc::SAX2XMLReader> parser(xercesc::XMLReaderFactory::createXMLReader());
00063 
00064         // write XML schema to file
00065         const std::string schema = "/tmp/dccl_message_schema.xsd";
00066         std::ofstream fout;
00067         fout.open(schema.c_str());
00068         for(unsigned int i = 0; i < message_schema_xsd_len; ++i)
00069             fout << message_schema_xsd[i];
00070         fout.close();
00071         
00072         const XMLCh* xs_schema = fromNative(schema.c_str());
00073         const XMLCh* const schema_location = xs_schema;
00074         parser->setFeature(xercesc::XMLUni::fgSAX2CoreValidation, true);
00075         parser->setProperty(xercesc::XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation, (void*)schema_location);
00076         
00077         parser->setContentHandler(&content_);
00078         parser->setErrorHandler(&error_);
00079         
00080         // Parse the XML document
00081         parser->parse(file.c_str());
00082         
00083         return true;
00084     }       
00085         
00086   private:
00087     xercesc::DefaultHandler& content_;
00088     xercesc::DefaultHandler& error_;
00089 };
00090 
00091 
00092 
00093 
00094 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends