1 /*-----------------------------------------------------------------------------
2 Name: ParserFactory.cpp
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: The abstraction parser for xml literals
6 -----------------------------------------------------------------------------*/
7
8 #ifndef _UTIL_PARSER_PARSERFACTORY_C
9 #define _UTIL_PARSER_PARSERFACTORY_C
10
11 #if defined(_WIN32)
12 #pragma warning(disable:4786)
13 #endif
14
15 #include <util/ErrorCode.h>
16 #include <util/XmlBlasterException.h>
17 #include <util/Global.h>
18
19 #if defined(XMLBLASTER_MSXML_PLUGIN)
20 # error Implement Microsoft XML parser for /DXMLBLASTER_MSXML_PLUGIN
21 #else // XMLBLASTER_XERCES_PLUGIN
22 # include <xercesc/util/PlatformUtils.hpp>
23 # include <xercesc/util/PanicHandler.hpp>
24 #endif // XMLBLASTER_XERCES_PLUGIN
25
26 #include <util/parser/ParserFactory.h>
27
28 #if defined(XMLBLASTER_MSXML_PLUGIN)
29 # error Implement Microsoft XML parser for /DXMLBLASTER_MSXML_PLUGIN
30 #else // XMLBLASTER_XERCES_PLUGIN
31 # include <util/parser/Sax2XercesParser.h>
32 #endif // XMLBLASTER_XERCES_PLUGIN
33
34
35
36 namespace org { namespace xmlBlaster { namespace util { namespace parser {
37
38 using namespace org::xmlBlaster::util;
39 //using namespace std;
40
41 ParserFactory* ParserFactory::factory_ = NULL;
42
43
44 /**
45 * Xerces panic handler
46 */
47 #if defined(XMLBLASTER_MSXML_PLUGIN)
48 # error Implement Microsoft XML parser for /DXMLBLASTER_MSXML_PLUGIN
49 #else // XMLBLASTER_XERCES_PLUGIN
50 class XmlBlasterPanicHandler : public PanicHandler
51 {
52 public:
53
54 XmlBlasterPanicHandler(){};
55
56 virtual ~XmlBlasterPanicHandler(){};
57
58 /*
59 Receive notification of panic.
60 This method is called when an unrecoverable error has occurred in the Xerces library.
61 This method must not return normally, otherwise, the results are undefined.
62 Ways of handling this call could include throwing an exception or exiting the process.
63 Once this method has been called, the results of calling any other Xerces API, or using any existing Xerces objects are undefined.
64 */
65 virtual void panic(const PanicHandler::PanicReasons reason) {
66
67 std::string txt = std::string("Got panic reason '") + lexical_cast<std::string>((int)reason) + "': " +
68 PanicHandler::getPanicReasonString(reason);
69
70 std::cerr << "Xerces::PanicHandler: " << txt << std::endl;
71 throw XmlBlasterException(INTERNAL_UNKNOWN, "Xerces::PanicHandler", txt);
72 }
73
74 private:
75
76 /* Unimplemented Constructors and operators */
77 /* Copy constructor */
78 XmlBlasterPanicHandler(const PanicHandler&);
79
80 XmlBlasterPanicHandler& operator=(const XmlBlasterPanicHandler&);
81
82 };
83
84 static XmlBlasterPanicHandler xmlBlasterPanicHandler;
85 #endif // XMLBLASTER_XERCES_PLUGIN
86
87
88 ParserFactory& ParserFactory::getFactory()
89 {
90 if (factory_ == NULL) {
91 factory_ = new ParserFactory();
92 org::xmlBlaster::util::Object_Lifetime_Manager::instance()->manage_object("XB_ParserFactory", factory_); // if not pre-allocated.
93 }
94 return *factory_;
95 }
96
97 ParserFactory::ParserFactory() :
98 ME("ParserFactory")
99 {
100 isInitialized_ = false;
101 }
102
103 ParserFactory::ParserFactory(const ParserFactory& factory) :
104 ME(factory.ME)
105 {
106 throw util::XmlBlasterException(INTERNAL_NOTIMPLEMENTED, ME, "private copy constructor");
107 }
108
109 ParserFactory& ParserFactory::operator =(const ParserFactory&)
110 {
111 throw util::XmlBlasterException(INTERNAL_NOTIMPLEMENTED, ME, "private assignement operator");
112 }
113
114 ParserFactory::~ParserFactory()
115 {
116 if (isInitialized_) {
117 //std::cerr << "ParserFactory destructor" << std::endl;
118 #if defined(XMLBLASTER_MSXML_PLUGIN)
119 # error Implement Microsoft XML parser for /DXMLBLASTER_MSXML_PLUGIN
120 #else // XMLBLASTER_XERCES_PLUGIN
121 XMLPlatformUtils::Terminate();
122 #endif
123 }
124 }
125
126 std::string ParserFactory::getLocale(org::xmlBlaster::util::Global& global)
127 {
128 locale_ = global.getProperty().getStringProperty("xmlBlaster/locale", "de_DE.iso-8859-1");
129 return locale_;
130 }
131
132
133 void ParserFactory::initialize(org::xmlBlaster::util::Global& global)
134 {
135 #if defined(XMLBLASTER_MSXML_PLUGIN)
136 # error Implement Microsoft XML parser for /DXMLBLASTER_MSXML_PLUGIN
137 #else // XMLBLASTER_XERCES_PLUGIN
138 try {
139 if (!isInitialized_) {
140 //std::cerr << "Initializing xerces with '" << getLocale(global) << "'" << std::endl;
141 // "en_US" is default inside xerces
142 XMLPlatformUtils::Initialize(getLocale(global).c_str(), 0, &xmlBlasterPanicHandler, 0);
143 isInitialized_ = true;
144 }
145 }
146 catch (const XMLException& e) {
147 char* message = XMLString::transcode(e.getMessage());
148 std::string txt = std::string("XMLPlatformUtils::Initialize() - XMLException during initialization. Exception message is: ") + std::string(message);
149 Sax2Parser::releaseXMLCh(&message);
150 throw util::XmlBlasterException(INTERNAL_UNKNOWN, ME, txt);
151 }
152 catch (const std::exception& e) {
153 std::string txt = std::string("XMLPlatformUtils::Initialize() - std::exception during initialization. Exception message is: ") + std::string(e.what());
154 throw util::XmlBlasterException(INTERNAL_UNKNOWN, ME, txt);
155 }
156 #endif // XMLBLASTER_XERCES_PLUGIN
157 }
158
159 I_Parser* ParserFactory::createParser(org::xmlBlaster::util::Global& global, XmlHandlerBase *handler)
160 {
161 initialize(global);
162
163 #if defined(XMLBLASTER_MSXML_PLUGIN)
164 # error Implement Microsoft XML parser for /DXMLBLASTER_MSXML_PLUGIN
165 #else // XMLBLASTER_XERCES_PLUGIN
166 try {
167 return new Sax2Parser(global, handler);
168 }
169 catch (const XmlBlasterException& e) {
170 throw e;
171 }
172 catch (const XMLException& e) {
173 char* message = XMLString::transcode(e.getMessage());
174 std::string txt = std::string("Sax2Parser(): error during SAX parser initialization. Exception message is: ") + std::string(message);
175 Sax2Parser::releaseXMLCh(&message);
176 throw util::XmlBlasterException(INTERNAL_UNKNOWN, ME, txt);
177 }
178 catch (const std::exception& e) {
179 std::string txt = std::string("Sax2Parser() - std::exception during initialization. Exception message is: ") + std::string(e.what());
180 throw util::XmlBlasterException(INTERNAL_UNKNOWN, ME, txt);
181 }
182 #endif // XMLBLASTER_XERCES_PLUGIN
183 }
184
185
186 }}}} // namespace
187
188 #endif
syntax highlighted by Code2HTML, v. 0.9.1