00001 /*----------------------------------------------------------------------------- 00002 Name: XmlHandlerBase.cpp 00003 Project: xmlBlaster.org 00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file 00005 Comment: Default handling of Sax callbacks 00006 -----------------------------------------------------------------------------*/ 00007 00008 #ifndef _UTIL_XMLHANDLERBASE_C 00009 #define _UTIL_XMLHANDLERBASE_C 00010 00011 #if defined(_WIN32) 00012 #pragma warning(disable:4786) 00013 #endif 00014 00015 #include <util/parser/XmlHandlerBase.h> 00016 #include <util/StopParseException.h> 00017 #include <util/XmlBlasterException.h> 00018 #include <util/Global.h> 00019 #include <util/StopWatch.h> 00020 #include <util/lexical_cast.h> 00021 #include <iostream> 00022 #include <util/parser/ParserFactory.h> 00023 00024 namespace org { namespace xmlBlaster { namespace util { namespace parser { 00025 00026 using namespace std; 00027 using namespace org::xmlBlaster::util; 00028 using namespace org::xmlBlaster::util::thread; 00029 00030 XmlHandlerBase::XmlHandlerBase(Global& global) : 00031 ME("XmlHandlerBase"), 00032 inAttribute_(false), 00033 global_(global), 00034 log_(global.getLog("org.xmlBlaster.util.xml")), 00035 invocationMutex_() 00036 { 00037 doTrimStrings_ = true; 00038 //if (log_.call()) log_.trace(ME, "Creating new XmlHandlerBase"); 00039 } 00040 00044 string XmlHandlerBase::getLocale() 00045 { 00046 // xerces defaults to "en_US"; 00047 locale_ = global_.getProperty().getStringProperty("xmlBlaster/locale", "de_DE.iso-8859-1"); 00048 return locale_; 00049 } 00050 00051 void XmlHandlerBase::init(const string &xmlLiteral) 00052 { 00053 xmlLiteral_ = xmlLiteral; 00054 if (xmlLiteral_.size() > 0) { 00055 parse(xmlLiteral_); 00056 } 00057 } 00058 00063 void XmlHandlerBase::parse(const string &xmlData) 00064 { 00065 log_.call(ME, "parse"); 00066 //if (log_.trace()) log_.trace(ME, string("parse content:'") + xmlData + string("'")); 00067 00068 StopWatch stopWatch; 00069 I_Parser *parser = NULL; 00070 try { 00071 parser = ParserFactory::getFactory().createParser(global_, this); 00072 } 00073 catch (XmlBlasterException& ex) { 00074 throw ex; 00075 } 00076 catch (std::exception e) { 00077 throw XmlBlasterException(INTERNAL_UNKNOWN, ME + "::parse", string("ParserFactory: ") + e.what()); 00078 } 00079 catch (...) { 00080 throw XmlBlasterException(INTERNAL_UNKNOWN, ME + "::parse", string("ParserFactory: unknown exception")); 00081 } 00082 00083 Lock lock(invocationMutex_); 00084 00085 try { 00086 parser->parse(xmlData); 00087 delete parser; 00088 } 00089 catch (StopParseException&) { 00090 // If it does not work, it could be wrapped into SAXParseException 00091 log_.error(ME, string("StopParseException: ") + "Parsing execution stopped half the way "); 00092 if (log_.trace()) { 00093 string help = XmlBlasterException::getStackTrace(); 00094 log_.plain(ME, help); 00095 } 00096 delete parser; 00097 return; 00098 } 00099 catch (XmlBlasterException& ex) { 00100 if (log_.trace()) log_.trace(ME, ex.getMessage() + ": " + xmlData); // Remove logging here 00101 delete parser; 00102 throw ex; 00103 } 00104 catch (const exception& err) { 00105 delete parser; 00106 throw XmlBlasterException(INTERNAL_UNKNOWN, ME + "::parse", string("parse: std::exception. message:") + err.what() + ": " + xmlData); 00107 } 00108 catch (const string& err) { 00109 delete parser; 00110 throw XmlBlasterException(INTERNAL_UNKNOWN, ME + "::parse", string("parse: exception-string. message:") + err + ": " + xmlData); 00111 } 00112 catch (const char* err) { 00113 delete parser; 00114 throw XmlBlasterException(INTERNAL_UNKNOWN, ME + "::parse", string("parse: exception-char*. message:") + err + ": " + xmlData); 00115 } 00116 catch (...) { 00117 delete parser; 00118 throw XmlBlasterException(INTERNAL_UNKNOWN, ME + "::parse", string("parse: unknown exception ...: ") + xmlData); 00119 } 00120 if (log_.trace()) log_.trace(ME, "Time used for parsing: " + stopWatch.nice()); 00121 } 00122 00127 void XmlHandlerBase::characters(const string &ch) 00128 { 00129 if (doTrimStrings_) { 00130 if (inAttribute_) 00131 attributeCharacter_ += trimmer_.trim(ch); 00132 else 00133 character_ += trimmer_.trim(ch); 00134 } 00135 else { 00136 if (inAttribute_) 00137 attributeCharacter_ += ch; 00138 else 00139 character_ += ch; 00140 } 00141 //if (log_.trace()) log_.trace(ME, string("characters, character:'") + character_ + string("'")); 00142 } 00143 00144 void XmlHandlerBase::endCDATA() 00145 { 00146 if (inAttribute_) 00147 attributeCharacter_ += "]]>"; 00148 else 00149 character_ += "]]>"; 00150 doTrimStrings_ = true; 00151 if (log_.trace()) log_.trace(ME, "end of cdata"); 00152 } 00153 00154 void XmlHandlerBase::startCDATA() 00155 { 00156 if (inAttribute_) 00157 attributeCharacter_ += "<![CDATA["; 00158 else 00159 character_ += "<![CDATA["; 00160 doTrimStrings_ = false; 00161 if (log_.trace()) log_.trace(ME, "start of cdata"); 00162 } 00163 00164 void XmlHandlerBase::startDocument() 00165 { 00166 if (log_.trace()) log_.trace(ME, "startDocument"); 00167 } 00168 00169 void XmlHandlerBase::endDocument() 00170 { 00171 if (log_.trace()) log_.trace(ME, "endDocument"); 00172 } 00173 00174 void XmlHandlerBase::startElement(const string &name, const AttributeMap& attrs) 00175 { 00176 log_.warn(ME,"Please provide your startElement() impl. for: " + getStartElementAsString(name, attrs)); 00177 } 00178 00180 void XmlHandlerBase::endElement(const string &/*name*/) 00181 { 00182 log_.warn(ME,"Please provide your endElement() impl."); 00183 } 00184 00185 // 00186 // ErrorHandler methods 00187 // 00188 00190 void XmlHandlerBase::warning(const string &exTxt) 00191 { 00192 string txt = exTxt + xmlLiteral_; 00193 log_.warn(ME+".warning()", txt); 00194 } 00195 00196 00198 void XmlHandlerBase::error(const string &exTxt) 00199 { 00200 string txt = exTxt + xmlLiteral_; 00201 log_.warn(ME+".error()", txt); 00202 } 00203 00204 00206 void XmlHandlerBase::fatalError(const string &exTxt) 00207 { 00208 string txt = exTxt + xmlLiteral_; 00209 log_.warn(ME+".fatalError()", txt); 00210 throw XmlBlasterException(INTERNAL_UNKNOWN, ME + "::parse", string("parse: fatalError exception. message:") + exTxt); 00211 } 00212 00219 bool XmlHandlerBase::getStringAttr(const AttributeMap& attrs, const string &name, string& value, bool doTrim) const 00220 { 00221 AttributeMap::const_iterator iter = attrs.find(name); 00222 if (iter == attrs.end()) return false; 00223 if (doTrim) { 00224 value.assign(StringTrim::trim((*iter).second)); 00225 } 00226 else value.assign((*iter).second); 00227 return true; 00228 } 00229 00236 bool XmlHandlerBase::getIntAttr(const AttributeMap &attrs, const string &name, int& value) const 00237 { 00238 string buf; 00239 bool ret = getStringAttr(attrs, name, buf); 00240 if (ret) { 00241 value = atoi(buf.c_str()); 00242 return true; 00243 } 00244 return false; 00245 } 00246 00253 bool XmlHandlerBase::getLongAttr(const AttributeMap &attrs, const string &name, long& value) const 00254 { 00255 string buf; 00256 bool ret = getStringAttr(attrs, name, buf); 00257 if (ret) { 00258 value = atol(buf.c_str()); 00259 return true; 00260 } 00261 return false; 00262 } 00263 00270 bool XmlHandlerBase::getTimestampAttr(const AttributeMap& attrs, const string &name, Timestamp& value) const 00271 { 00272 string buf; 00273 bool ret = getStringAttr(attrs, name, buf); 00274 if (ret) { 00275 // value = STRING_TO_TIMESTAMP(buf.c_str()); 00276 value = lexical_cast<Timestamp>(buf); 00277 return true; 00278 } 00279 return false; 00280 } 00281 00288 bool XmlHandlerBase::getBoolAttr(const AttributeMap &attrs, const string &name, bool& value) const 00289 { 00290 string buf; 00291 bool ret = getStringAttr(attrs, name, buf); 00292 if (ret) { 00293 value = lexical_cast<bool>(buf); 00294 return true; 00295 } 00296 return false; 00297 } 00298 00299 00303 int XmlHandlerBase::getIntValue(const string &value) const 00304 { 00305 if (value.length() < 1) return 0; 00306 try { 00307 return lexical_cast<int>(value); 00308 } 00309 catch (...) { 00310 cerr << "XmlHandlerBase:: Conversion from " << value << " to int failed" << endl; 00311 } 00312 return 0; 00313 } 00314 00318 long XmlHandlerBase::getLongValue(const string &value) const 00319 { 00320 if (value.length() < 1) return 0l; 00321 try { 00322 return lexical_cast<long>(value); 00323 } 00324 catch (...) { 00325 cerr << "XmlHandlerBase:: Conversion from " << value << " to long failed" << endl; 00326 } 00327 return 0l; 00328 } 00329 00333 Timestamp XmlHandlerBase::getTimestampValue(const string &value) const 00334 { 00335 Timestamp ret = 0l; 00336 try { 00337 ret = lexical_cast<Timestamp>(value); 00338 } 00339 catch (...) { 00340 cerr << "XmlHandlerBase:: Conversion from " << value << " to Timestamp failed" << endl; 00341 } 00342 return ret; 00343 } 00344 00348 bool XmlHandlerBase::getBoolValue(const string &value) const 00349 { 00350 try { 00351 return StringTrim::isTrue(value); 00352 } 00353 catch (...) { 00354 cerr << "XmlHandlerBase:: Conversion from " << value << " to bool failed" << endl; 00355 } 00356 return false; 00357 } 00358 00359 std::string XmlHandlerBase::getStartElementAsString(const std::string &name, const AttributeMap &attrMap) 00360 { 00361 string ret = string("<") + name + string(" "); 00362 AttributeMap::const_iterator iter = attrMap.begin(); 00363 while (iter != attrMap.end()) { 00364 ret += (*iter).first + string("='") + (*iter).second + string("' "); 00365 iter++; 00366 } 00367 ret += string(">"); 00368 return ret; 00369 } 00370 00371 #endif 00372 00373 }}}} // namespace 00374