00001 #include "CSXMLHelper.h" 00002 00003 #ifdef USE_TINYXPATH 00004 00005 #ifdef WIN32 00006 #pragma warning(disable : 4786 ) 00007 #endif 00008 00009 #include <vector> 00010 00011 const char *CSXMLHelper::CLASS = "CSXMLHelper"; 00012 00013 CSXMLHelper::CSXMLHelper(const std::string &xmlFilename, const std::string &contextNode) 00014 { 00015 static char *functionName="CSXMLHelper"; 00016 mError = CSXML_OK; 00017 mXMLdoc = 0; 00018 00019 mXMLdoc = new TiXmlDocument(xmlFilename); 00020 if (!mXMLdoc->LoadFile() ) 00021 { 00022 mError = CSXML_DOCUMENT_LOADING_ERROR; 00023 std::string errorMessageString; 00024 errorMessageString = std::string("Error - while loading document \"" + xmlFilename +"\"!"); 00025 mErrorMessageString = mErrorMessageString + "\n"+ errorMessageString; 00026 return; 00027 } 00028 mXMLsource = mXMLdoc->FirstChildElement(contextNode); 00029 if (!mXMLsource) 00030 { 00031 mError = CSXML_CONTEXT_LOADING_ERROR; 00032 std::string errorMessageString; 00033 errorMessageString = std::string("Error - while loading context \"" + contextNode +"\"!"); 00034 mErrorMessageString = mErrorMessageString + "\n"+ errorMessageString; 00035 } 00036 } 00037 00038 CSXMLHelper::~CSXMLHelper() 00039 { 00040 static char *functionName="~CSXMLHelper"; 00041 LOG_ENTER 00042 delete (mXMLdoc); 00043 mXMLdoc = 0; 00044 mXMLsource = 0; 00045 LOG_EXIT 00046 } 00047 00048 int CSXMLHelper::getInt(const std::string &xpath) 00049 { 00050 static char *functionName="getInt"; 00051 int ret = 0; 00052 if (mError != 0) 00053 { 00054 LOG_WARN_MESSAGE("Error") 00055 throw "error"; 00056 } 00057 try 00058 { 00059 XObjectPtr xml = getXPathResult(xpath); 00060 ret = DOMStringToInt(xml->str()); 00061 } 00062 catch(const SAXException&) 00063 { 00064 mError = CSXML_XPATH_ERROR; 00065 std::string errorMessageString; 00066 errorMessageString = std::string("SAXException") + mErrorMessageString; 00067 mErrorMessageString = mErrorMessageString + "\n"+ errorMessageString; 00068 LOG_WARN_MESSAGE(mErrorMessageString) 00069 throw "error"; 00070 } 00071 catch(const XMLException&) 00072 { 00073 mError = CSXML_XPATH_ERROR; 00074 std::string errorMessageString; 00075 errorMessageString = std::string("XMLException") + mErrorMessageString; 00076 mErrorMessageString = mErrorMessageString + "\n"+ errorMessageString; 00077 LOG_WARN_MESSAGE(mErrorMessageString) 00078 throw "error"; 00079 } 00080 catch(const XalanDOMException&) 00081 { 00082 mError = CSXML_XPATH_ERROR; 00083 std::string errorMessageString; 00084 errorMessageString = std::string("XalanDOMException") + mErrorMessageString; 00085 mErrorMessageString = mErrorMessageString + "\n"+ errorMessageString; 00086 LOG_WARN_MESSAGE(mErrorMessageString) 00087 throw "error"; 00088 } 00089 return ret; 00090 } 00091 00092 std::string CSXMLHelper::getString(const std::string &xpath) 00093 { 00094 static char *functionName="getString"; 00095 std::string cstr; 00096 if (mError != 0) 00097 { 00098 LOG_WARN_MESSAGE("XML! Not properly initialized!") 00099 throw std::string("XML! Not properly initialized!"); 00100 } 00101 try 00102 { 00103 XObjectPtr xml = getXPathResult(xpath); 00104 cstr = toAscii(xml); 00105 } 00106 catch(const SAXException&) 00107 { 00108 mError = CSXML_XPATH_ERROR; 00109 std::string errorMessageString; 00110 errorMessageString = std::string("SAXException") + mErrorMessageString; 00111 mErrorMessageString = mErrorMessageString + "\n"+ errorMessageString; 00112 LOG_WARN_MESSAGE(mErrorMessageString) 00113 throw "error"; 00114 } 00115 catch(const XMLException&) 00116 { 00117 mError = CSXML_XPATH_ERROR; 00118 std::string errorMessageString; 00119 errorMessageString = std::string("XMLException") + mErrorMessageString; 00120 mErrorMessageString = mErrorMessageString + "\n"+ errorMessageString; 00121 LOG_WARN_MESSAGE(mErrorMessageString) 00122 throw "error"; 00123 } 00124 catch(const XalanDOMException&) 00125 { 00126 mError = CSXML_XPATH_ERROR; 00127 std::string errorMessageString; 00128 errorMessageString = std::string("XalanDOMException") + mErrorMessageString; 00129 mErrorMessageString = mErrorMessageString + "\n"+ errorMessageString; 00130 LOG_WARN_MESSAGE(mErrorMessageString) 00131 throw "error"; 00132 } 00133 return cstr; 00134 } 00135 00136 00137 XObjectPtr CSXMLHelper::getXPathResult(const std::string &xpath) 00138 { 00139 static char *functionName="getXPathResult"; 00140 static char h[1025]; 00141 h[0] = 0; 00142 strcat(h, "normalize-space("); 00143 strcat(h, xpath.c_str()); 00144 strcat(h, ")"); 00145 00146 xfsp_engine = new xpath_from_source(mXMLsource, xpath); 00147 xfsp_engine -> v_apply_xpath (cp_test_name, Fp_html); 00148 delete xfsp_engine; 00149 00150 const XObjectPtr theResult( 00151 theEvaluator->evaluate( 00152 *theDOMSupport, 00153 theContextNode, 00154 XalanDOMString(h).c_str(), 00155 theDocument->getDocumentElement()) 00156 ); 00157 if (theResult.null() != false) 00158 { 00159 mError = CSXML_XPATH_ERROR; 00160 std::string errorMessageString; 00161 errorMessageString = std::string("General XPATH-Result = 0") + mErrorMessageString; 00162 mErrorMessageString = mErrorMessageString + "\n"+ errorMessageString; 00163 throw "error"; 00164 } 00165 return theResult; 00166 } 00167 00168 std::string CSXMLHelper::toAscii(const XObjectPtr &xObjectPtr) 00169 { 00170 static char *functionName="toAscii"; 00171 std::string cstr; 00172 const XalanDOMString xmlString = xObjectPtr->str(); 00173 return toAscii(xmlString); 00174 } 00175 00176 #endif // USE_TINYXPATH