00001 /* 00002 * Copyright (c) 2009, Czech Technical University in Prague 00003 * All rights reserved. 00004 * 00005 * Authors/Contact: 00006 * 00007 * Petr Vlacil 00008 * (vlacipet@fel.cvut.cz) 00009 * R&D Centre (RDC) for Mobile Applications 00010 * Dept of Telecommunications Engineering 00011 * Faculty of Electrical Engineering 00012 * Czech Technical University in Prague 00013 * Technicka 2, 166 27 Prague 6, Czech Republic 00014 * 00015 * Redistribution and use in source and binary forms, with or 00016 * without modification, are permitted provided that the following 00017 * conditions are met: 00018 * 00019 * 1. Redistributions of source code must retain the above copyright 00020 * notice, this list of conditions and the following disclaimer. 00021 * 2. Redistributions in binary form must reproduce the above copyright 00022 * notice, this list of conditions and the following disclaimer in 00023 * the documentation and/or other materials provided with the 00024 * distribution. 00025 * 3. Neither the name of the Czech Technical University in Prague nor 00026 * the names of its contributors may be used to endorse or promote 00027 * products derived from this software without specific prior 00028 * written permission. 00029 * 00030 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00031 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00032 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00033 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00034 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00035 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00036 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00037 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00038 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00039 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00040 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00041 * 00042 */ 00043 00044 00045 #ifndef MLP_LIB_CPP_H 00046 #define MLP_LIB_CPP_H 00047 00048 00049 /** 00050 * \file 00051 * \brief Main header file 00052 * 00053 * This file contains MlpParser class declaration. 00054 * 00055 * \author Petr Vláčil 00056 */ 00057 00058 00059 /** \mainpage MLP Library API reference 00060 * 00061 * Welcome to API documentation for MLP Library (Mobile Location Protocol 00062 * Library). This document was generated by Doxygen from source code and 00063 * its comments. 00064 * 00065 * <b>Please let us know that you use our library and tell us what you 00066 * use it for. You can use our forum or email. 00067 * 00068 * Forum : http://sourceforge.net/apps/phpbb/libmlp-cpp/index.php \n 00069 * e-mail: http://libmlp-cpp.sourceforge.net/contacts.php 00070 * </b> 00071 */ 00072 00073 00074 //-------------------------------------------------------------------------- 00075 // Standard libraries 00076 //-------------------------------------------------------------------------- 00077 #include <iostream> 00078 #include <sstream> 00079 #include <string> 00080 00081 //-------------------------------------------------------------------------- 00082 // Xercesc libraries 00083 //-------------------------------------------------------------------------- 00084 #include <xercesc/sax2/SAX2XMLReader.hpp> 00085 #include <xercesc/sax2/XMLReaderFactory.hpp> 00086 #include <xercesc/sax2/DefaultHandler.hpp> 00087 00088 #include <xercesc/framework/MemBufInputSource.hpp> 00089 00090 #include <xercesc/util/OutOfMemoryException.hpp> 00091 00092 //-------------------------------------------------------------------------- 00093 // MLP-Lib-Cpp library files 00094 //-------------------------------------------------------------------------- 00095 #include <libmlp-cpp/MlpDtdResolver.hpp> 00096 #include <libmlp-cpp/MlpXmlString.hpp> 00097 #include <libmlp-cpp/MlpParseHandlers.hpp> 00098 #include <libmlp-cpp/MlpErrorHandler.hpp> 00099 #include <libmlp-cpp/MlpLog.hpp> 00100 #include <libmlp-cpp/MlpMessage.hpp> 00101 #include <libmlp-cpp/MlpConfig.hpp> 00102 #include <libmlp-cpp/MlpXmlException.hpp> 00103 00104 00105 /** 00106 * \brief Macro to initialize the environment. 00107 * 00108 * This macro should be used at the beginning of program before creating 00109 * any MLP parser. It initiates Xerces environment. It is better not 00110 * to use this macro in constructor as it throws the MlpXmlException. 00111 * @throw MlpXmlException 00112 */ 00113 #define MLP_LIB_CPP_INIT do \ 00114 { \ 00115 try { \ 00116 xercesc::XMLPlatformUtils::Initialize(); \ 00117 } \ 00118 catch (const xercesc::XMLException& e){ \ 00119 std::ostringstream error; \ 00120 error << "MlpXmlException: Error during Xerces-c initialization! :\n" \ 00121 << "MlpXmlException: Exception message is: \n" \ 00122 << "MlpXmlException: " << MlpXmlString( e.getMessage() ) ; \ 00123 MLP_LOG_ERROR( error.str() ); \ 00124 throw MlpXmlException( error.str() ); \ 00125 } \ 00126 } \ 00127 while ( false ) 00128 00129 00130 00131 /** 00132 * \brief Macro to clean the environment. 00133 * 00134 * This macro should be used at the end of program to clean things up. 00135 * It cleans Xerces environment. It is better not 00136 * to use this macro in destructors as it throws the MlpXmlException. 00137 * Destructors should never throw the exceptions. 00138 * @throw MlpXmlException 00139 */ 00140 #define MLP_LIB_CPP_TERMINATE do \ 00141 { \ 00142 try { \ 00143 xercesc::XMLPlatformUtils::Terminate(); \ 00144 } \ 00145 catch ( const xercesc::XMLException& e ) { \ 00146 std::ostringstream error; \ 00147 error << "MlpXmlException: Error during Xerces-c termination! :\n" \ 00148 << "MlpXmlException: Exception message is: \n" \ 00149 << "MlpXmlException: " << MlpXmlString( e.getMessage() ) ; \ 00150 MLP_LOG_ERROR( error.str() ); \ 00151 throw MlpXmlException( error.str() ); \ 00152 } \ 00153 } \ 00154 while ( false ) \ 00155 00156 00157 /** 00158 * \brief Main parser class 00159 * 00160 * This is main class that implements MLP parser. It wraps around the Xerces 00161 * parser an MlpHandler and provides simple interface. Use this class 00162 * to process 00163 * MLP messages. MlpParser is not synchronized but it does not use any 00164 * global variables. That means you can use more MlpParsers simultaneously. 00165 * Use one or more parsers in one thread or more parsers in more threads. 00166 * Because it is not synchronized do not access one parser from more threads 00167 * simultaneously. 00168 */ 00169 class MlpParser { 00170 00171 /** Memory buffer name for Xerces. */ 00172 const char* gMemBufId; 00173 00174 /** Pointer to handler that processes the XML message. */ 00175 xercesc::ContentHandler* contHandler; 00176 00177 /** Pointer to object that handles errors during parsing. */ 00178 xercesc::ErrorHandler* errHandler; 00179 00180 /** Pointer to the object that handles DTD loading. */ 00181 MlpDtdResolver* resolver; 00182 00183 /** Pointer to SAX parser created by Xerces-c. */ 00184 xercesc::SAX2XMLReader* xercescParser; 00185 00186 /** Path to DTD files. */ 00187 std::string _dtdPath; 00188 00189 public: 00190 00191 /** Constructor */ 00192 MlpParser (); 00193 00194 /** Destructor */ 00195 ~MlpParser (); 00196 00197 /** Configuration */ 00198 void configure ( const MlpConfig* config ); 00199 00200 /** Initialization */ 00201 void init (); 00202 00203 /** Main parsing method */ 00204 void parse ( MlpMessage* inMsg, const std::string& msg ) const; 00205 }; 00206 00207 00208 00209 #endif // MLP_LIB_CPP_H