00001 /*----------------------------------------------------------------------------- 00002 Name: ParserFactory.cpp 00003 Project: xmlBlaster.org 00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file 00005 Comment: The abstraction parser for xml literals 00006 -----------------------------------------------------------------------------*/ 00007 00008 #ifndef _UTIL_PARSER_PARSERFACTORY_C 00009 #define _UTIL_PARSER_PARSERFACTORY_C 00010 00011 #if defined(_WIN32) 00012 #pragma warning(disable:4786) 00013 #endif 00014 00015 #include <util/ErrorCode.h> 00016 #include <util/XmlBlasterException.h> 00017 #include <util/Global.h> 00018 00019 #if defined(XMLBLASTER_MSXML_PLUGIN) 00020 # error Implement Microsoft XML parser for /DXMLBLASTER_MSXML_PLUGIN 00021 #else // XMLBLASTER_XERCES_PLUGIN 00022 # include <xercesc/util/PlatformUtils.hpp> 00023 # include <xercesc/util/PanicHandler.hpp> 00024 #endif // XMLBLASTER_XERCES_PLUGIN 00025 00026 #include <util/parser/ParserFactory.h> 00027 00028 #if defined(XMLBLASTER_MSXML_PLUGIN) 00029 # error Implement Microsoft XML parser for /DXMLBLASTER_MSXML_PLUGIN 00030 #else // XMLBLASTER_XERCES_PLUGIN 00031 # include <util/parser/Sax2XercesParser.h> 00032 #endif // XMLBLASTER_XERCES_PLUGIN 00033 00034 00035 00036 namespace org { namespace xmlBlaster { namespace util { namespace parser { 00037 00038 using namespace org::xmlBlaster::util; 00039 //using namespace std; 00040 00041 ParserFactory* ParserFactory::factory_ = NULL; 00042 00043 00047 #if defined(XMLBLASTER_MSXML_PLUGIN) 00048 # error Implement Microsoft XML parser for /DXMLBLASTER_MSXML_PLUGIN 00049 #else // XMLBLASTER_XERCES_PLUGIN 00050 class XmlBlasterPanicHandler : public PanicHandler 00051 { 00052 public: 00053 00054 XmlBlasterPanicHandler(){}; 00055 00056 virtual ~XmlBlasterPanicHandler(){}; 00057 00058 /* 00059 Receive notification of panic. 00060 This method is called when an unrecoverable error has occurred in the Xerces library. 00061 This method must not return normally, otherwise, the results are undefined. 00062 Ways of handling this call could include throwing an exception or exiting the process. 00063 Once this method has been called, the results of calling any other Xerces API, or using any existing Xerces objects are undefined. 00064 */ 00065 virtual void panic(const PanicHandler::PanicReasons reason) { 00066 00067 std::string txt = std::string("Got panic reason '") + lexical_cast<std::string>((int)reason) + "': " + 00068 PanicHandler::getPanicReasonString(reason); 00069 00070 std::cerr << "Xerces::PanicHandler: " << txt << std::endl; 00071 throw XmlBlasterException(INTERNAL_UNKNOWN, "Xerces::PanicHandler", txt); 00072 } 00073 00074 private: 00075 00076 /* Unimplemented Constructors and operators */ 00077 /* Copy constructor */ 00078 XmlBlasterPanicHandler(const PanicHandler&); 00079 00080 XmlBlasterPanicHandler& operator=(const XmlBlasterPanicHandler&); 00081 00082 }; 00083 00084 static XmlBlasterPanicHandler xmlBlasterPanicHandler; 00085 #endif // XMLBLASTER_XERCES_PLUGIN 00086 00087 00088 ParserFactory& ParserFactory::getFactory() 00089 { 00090 if (factory_ == NULL) { 00091 factory_ = new ParserFactory(); 00092 org::xmlBlaster::util::Object_Lifetime_Manager::instance()->manage_object("XB_ParserFactory", factory_); // if not pre-allocated. 00093 } 00094 return *factory_; 00095 } 00096 00097 ParserFactory::ParserFactory() : 00098 ME("ParserFactory") 00099 { 00100 isInitialized_ = false; 00101 } 00102 00103 ParserFactory::ParserFactory(const ParserFactory& factory) : 00104 ME(factory.ME) 00105 { 00106 throw util::XmlBlasterException(INTERNAL_NOTIMPLEMENTED, ME, "private copy constructor"); 00107 } 00108 00109 ParserFactory& ParserFactory::operator =(const ParserFactory&) 00110 { 00111 throw util::XmlBlasterException(INTERNAL_NOTIMPLEMENTED, ME, "private assignement operator"); 00112 } 00113 00114 ParserFactory::~ParserFactory() 00115 { 00116 if (isInitialized_) { 00117 //std::cerr << "ParserFactory destructor" << std::endl; 00118 #if defined(XMLBLASTER_MSXML_PLUGIN) 00119 # error Implement Microsoft XML parser for /DXMLBLASTER_MSXML_PLUGIN 00120 #else // XMLBLASTER_XERCES_PLUGIN 00121 XMLPlatformUtils::Terminate(); 00122 #endif 00123 } 00124 } 00125 00126 std::string ParserFactory::getLocale(org::xmlBlaster::util::Global& global) 00127 { 00128 locale_ = global.getProperty().getStringProperty("xmlBlaster/locale", "de_DE.iso-8859-1"); 00129 return locale_; 00130 } 00131 00132 00133 void ParserFactory::initialize(org::xmlBlaster::util::Global& global) 00134 { 00135 #if defined(XMLBLASTER_MSXML_PLUGIN) 00136 # error Implement Microsoft XML parser for /DXMLBLASTER_MSXML_PLUGIN 00137 #else // XMLBLASTER_XERCES_PLUGIN 00138 try { 00139 if (!isInitialized_) { 00140 //std::cerr << "Initializing xerces with '" << getLocale(global) << "'" << std::endl; 00141 // "en_US" is default inside xerces 00142 XMLPlatformUtils::Initialize(getLocale(global).c_str(), 0, &xmlBlasterPanicHandler, 0); 00143 isInitialized_ = true; 00144 } 00145 } 00146 catch (const XMLException& e) { 00147 char* message = XMLString::transcode(e.getMessage()); 00148 std::string txt = std::string("XMLPlatformUtils::Initialize() - XMLException during initialization. Exception message is: ") + std::string(message); 00149 Sax2Parser::releaseXMLCh(&message); 00150 throw util::XmlBlasterException(INTERNAL_UNKNOWN, ME, txt); 00151 } 00152 catch (const std::exception& e) { 00153 std::string txt = std::string("XMLPlatformUtils::Initialize() - std::exception during initialization. Exception message is: ") + std::string(e.what()); 00154 throw util::XmlBlasterException(INTERNAL_UNKNOWN, ME, txt); 00155 } 00156 #endif // XMLBLASTER_XERCES_PLUGIN 00157 } 00158 00159 I_Parser* ParserFactory::createParser(org::xmlBlaster::util::Global& global, XmlHandlerBase *handler) 00160 { 00161 initialize(global); 00162 00163 #if defined(XMLBLASTER_MSXML_PLUGIN) 00164 # error Implement Microsoft XML parser for /DXMLBLASTER_MSXML_PLUGIN 00165 #else // XMLBLASTER_XERCES_PLUGIN 00166 try { 00167 return new Sax2Parser(global, handler); 00168 } 00169 catch (const XmlBlasterException& e) { 00170 throw e; 00171 } 00172 catch (const XMLException& e) { 00173 char* message = XMLString::transcode(e.getMessage()); 00174 std::string txt = std::string("Sax2Parser(): error during SAX parser initialization. Exception message is: ") + std::string(message); 00175 Sax2Parser::releaseXMLCh(&message); 00176 throw util::XmlBlasterException(INTERNAL_UNKNOWN, ME, txt); 00177 } 00178 catch (const std::exception& e) { 00179 std::string txt = std::string("Sax2Parser() - std::exception during initialization. Exception message is: ") + std::string(e.what()); 00180 throw util::XmlBlasterException(INTERNAL_UNKNOWN, ME, txt); 00181 } 00182 #endif // XMLBLASTER_XERCES_PLUGIN 00183 } 00184 00185 00186 }}}} // namespace 00187 00188 #endif