00001 #ifdef USE_XALAN
00002
00003 #include "CSXMLHelper.h"
00004
00005 #ifdef WIN32
00006 #pragma warning(disable : 4786 )
00007 #endif
00008
00009 #include <vector>
00010 const char *CSXMLHelper::CLASS = "CSXMLHelper";
00011
00012
00013
00014 #include <Include/PlatformDefinitions.hpp>
00015
00016 #if defined(XALAN_OLD_STREAM_HEADERS)
00017 #include <iostream.h>
00018 #else
00019 #include <iostream>
00020 #endif
00021 #include <util/PlatformUtils.hpp>
00022 #include <framework/LocalFileInputSource.hpp>
00023
00024 #include <XalanDOM/XalanDocument.hpp>
00025 #include <XalanDOM/XalanElement.hpp>
00026
00027 #include <XPath/XObject.hpp>
00028 #include <XPath/XPathEvaluator.hpp>
00029
00030 #include <XalanSourceTree/XalanSourceTreeDOMSupport.hpp>
00031 #include <XalanSourceTree/XalanSourceTreeInit.hpp>
00032 #include <XalanSourceTree/XalanSourceTreeParserLiaison.hpp>
00033
00034 #include <sax/ErrorHandler.hpp>
00035
00036
00037
00038 int CSXMLHelper::mLock = 0;
00039 CSXMLHelper::CSXMLHelper(const std::string &xmlFilename, const std::string &contextNode)
00040 {
00041 static char *functionName="CSXMLHelper";
00042 LOG_ENTER
00043 mError = CSXML_OK;
00044 mInit = 0;
00045 theEvaluator = 0;
00046 theLiaison = 0;
00047 theDOMSupport = 0;
00048 theSourceTreeInit = 0;
00049
00050 if (mLock != 0)
00051 {
00052 std::string errorMessageString;
00053 errorMessageString = std::string("Error - only one single instance of CSXMLHelper allowed!");
00054 mErrorMessageString = mErrorMessageString + "\n"+ errorMessageString;
00055 mError = CSXML_ONLY_SINGLE_XML_HELPER_ALLOWED;
00056 }
00057 mLock = 1;
00058
00059 try
00060 {
00061 theSourceTreeInit = new XalanSourceTreeInit();
00062 theDOMSupport = new XalanSourceTreeDOMSupport();
00063 theLiaison = new XalanSourceTreeParserLiaison(*theDOMSupport);
00064
00065 theLiaison->setUseValidation(true);
00066 theLiaison->setErrorHandler(this);
00067
00068
00069 theDOMSupport->setParserLiaison(theLiaison);
00070
00071 XMLPlatformUtils::Initialize();
00072 XPathEvaluator::initialize();
00073 mInit = 1;
00074
00075
00076 const LocalFileInputSource theInputSource(c_wstr(XalanDOMString(
00077 xmlFilename.c_str()
00078 )));
00079
00080 theEvaluator = new XPathEvaluator();
00081
00082
00083 theDocument = theLiaison->parseXMLStream(theInputSource);
00084 if (theDocument == 0)
00085 {
00086 mError = CSXML_DOCUMENT_LOADING_ERROR;
00087 std::string errorMessageString;
00088 errorMessageString = std::string("Error - while loading document \"" + xmlFilename +"\"!");
00089 mErrorMessageString = mErrorMessageString + "\n"+ errorMessageString;
00090 }
00091
00092
00093
00094 theContextNode =
00095 theEvaluator->selectSingleNode(
00096 *theDOMSupport,
00097 theDocument,
00098 XalanDOMString(contextNode.c_str()).c_str(),
00099 theDocument->getDocumentElement());
00100
00101 if (theContextNode == 0)
00102 {
00103 mError = CSXML_CONTEXT_LOADING_ERROR;
00104 std::string errorMessageString;
00105 errorMessageString = std::string("Error - while loading context \"" + contextNode +"\"!");
00106 mErrorMessageString = mErrorMessageString + "\n"+ errorMessageString;
00107 }
00108 }
00109 catch(const SAXException&)
00110 {
00111 mError = CSXML_INITIALIZE_ERROR;
00112 std::string errorMessageString;
00113 errorMessageString = std::string("SAXException") + mErrorMessageString;
00114 mErrorMessageString = mErrorMessageString + "\n"+ errorMessageString;
00115 }
00116 catch(const XMLException&)
00117 {
00118 mError = CSXML_INITIALIZE_ERROR;
00119 std::string errorMessageString;
00120 errorMessageString = std::string("XMLException") + mErrorMessageString;
00121 mErrorMessageString = mErrorMessageString + "\n"+ errorMessageString;
00122 }
00123 catch(const XalanDOMException&)
00124 {
00125 mError = CSXML_INITIALIZE_ERROR;
00126 std::string errorMessageString;
00127 errorMessageString = std::string("XalanDOMException") + mErrorMessageString;
00128 mErrorMessageString = mErrorMessageString + "\n"+ errorMessageString;
00129 }
00130 LOG_EXIT
00131 }
00132
00133 CSXMLHelper::~CSXMLHelper()
00134 {
00135 static char *functionName="~CSXMLHelper";
00136 LOG_ENTER
00137
00138 delete (theEvaluator);
00139 delete (theLiaison);
00140 delete (theDOMSupport);
00141 delete (theSourceTreeInit);
00142
00143 if (mInit != 1)
00144 {
00145 LOG_EXIT
00146 return;
00147 }
00148 XPathEvaluator::terminate();
00149 XMLPlatformUtils::Terminate();
00150 mLock = 0;
00151 LOG_EXIT
00152 }
00153
00154 int CSXMLHelper::getInt(const std::string &xpath)
00155 {
00156 static char *functionName="getInt";
00157 int ret = 0;
00158 if (mError != 0)
00159 {
00160 LOG_WARN_MESSAGE("Error")
00161 throw "error";
00162 }
00163 try
00164 {
00165 XObjectPtr xml = getXPathResult(xpath);
00166 ret = DOMStringToInt(xml->str());
00167 }
00168 catch(const SAXException&)
00169 {
00170 mError = CSXML_XPATH_ERROR;
00171 std::string errorMessageString;
00172 errorMessageString = std::string("SAXException") + mErrorMessageString;
00173 mErrorMessageString = mErrorMessageString + "\n"+ errorMessageString;
00174 LOG_WARN_MESSAGE(mErrorMessageString)
00175 throw "error";
00176 }
00177 catch(const XMLException&)
00178 {
00179 mError = CSXML_XPATH_ERROR;
00180 std::string errorMessageString;
00181 errorMessageString = std::string("XMLException") + mErrorMessageString;
00182 mErrorMessageString = mErrorMessageString + "\n"+ errorMessageString;
00183 LOG_WARN_MESSAGE(mErrorMessageString)
00184 throw "error";
00185 }
00186 catch(const XalanDOMException&)
00187 {
00188 mError = CSXML_XPATH_ERROR;
00189 std::string errorMessageString;
00190 errorMessageString = std::string("XalanDOMException") + mErrorMessageString;
00191 mErrorMessageString = mErrorMessageString + "\n"+ errorMessageString;
00192 LOG_WARN_MESSAGE(mErrorMessageString)
00193 throw "error";
00194 }
00195 return ret;
00196 }
00197
00198 std::string CSXMLHelper::getString(const std::string &xpath)
00199 {
00200 static char *functionName="getString";
00201 std::string cstr;
00202 if (mError != 0)
00203 {
00204 LOG_WARN_MESSAGE("XML! Not properly initialized!")
00205 throw std::string("XML! Not properly initialized!");
00206 }
00207 try
00208 {
00209 XObjectPtr xml = getXPathResult(xpath);
00210 cstr = toAscii(xml);
00211 }
00212 catch(const SAXException&)
00213 {
00214 mError = CSXML_XPATH_ERROR;
00215 std::string errorMessageString;
00216 errorMessageString = std::string("SAXException") + mErrorMessageString;
00217 mErrorMessageString = mErrorMessageString + "\n"+ errorMessageString;
00218 LOG_WARN_MESSAGE(mErrorMessageString)
00219 throw "error";
00220 }
00221 catch(const XMLException&)
00222 {
00223 mError = CSXML_XPATH_ERROR;
00224 std::string errorMessageString;
00225 errorMessageString = std::string("XMLException") + mErrorMessageString;
00226 mErrorMessageString = mErrorMessageString + "\n"+ errorMessageString;
00227 LOG_WARN_MESSAGE(mErrorMessageString)
00228 throw "error";
00229 }
00230 catch(const XalanDOMException&)
00231 {
00232 mError = CSXML_XPATH_ERROR;
00233 std::string errorMessageString;
00234 errorMessageString = std::string("XalanDOMException") + mErrorMessageString;
00235 mErrorMessageString = mErrorMessageString + "\n"+ errorMessageString;
00236 LOG_WARN_MESSAGE(mErrorMessageString)
00237 throw "error";
00238 }
00239 return cstr;
00240 }
00241
00242
00243 XObjectPtr CSXMLHelper::getXPathResult(const std::string &xpath)
00244 {
00245 static char *functionName="getXPathResult";
00246 static char h[1025];
00247 LOG_ENTER
00248 h[0] = 0;
00249 strcat(h, "normalize-space(");
00250 strcat(h, xpath.c_str());
00251 strcat(h, ")");
00252
00253 const XObjectPtr theResult(
00254 theEvaluator->evaluate(
00255 *theDOMSupport,
00256 theContextNode,
00257 XalanDOMString(h).c_str(),
00258 theDocument->getDocumentElement())
00259 );
00260 if (theResult.null() != false)
00261 {
00262 mError = CSXML_XPATH_ERROR;
00263 std::string errorMessageString;
00264 errorMessageString = std::string("General XPATH-Result = 0") + mErrorMessageString;
00265 mErrorMessageString = mErrorMessageString + "\n"+ errorMessageString;
00266 LOG_EXIT
00267 throw "error";
00268 }
00269 LOG_DEBUG_MESSAGE(xpath + "->" + toAscii(theResult) )
00270 LOG_EXIT
00271 return theResult;
00272 }
00273
00274 std::string CSXMLHelper::toAscii(const XObjectPtr &xObjectPtr)
00275 {
00276 static char *functionName="toAscii";
00277 std::string cstr;
00278 const XalanDOMString xmlString = xObjectPtr->str();
00279 return toAscii(xmlString);
00280 }
00281
00282 std::string CSXMLHelper::toAscii(const XalanDOMString &xmlString)
00283 {
00284 static char *functionName="toAscii";
00285 const CharVectorType charVec = TranscodeToLocalCodePage (xmlString);
00286 return toAscii(charVec);
00287 }
00288
00289
00290 std::string CSXMLHelper::toAscii(const CharVectorType &charVec)
00291 {
00292 static char *functionName="toAscii";
00293 std::string cstr;
00294 CharVectorType::const_iterator iter = charVec.begin();
00295 char helper[2];
00296 helper[1] = 0;
00297 while (iter != charVec.end())
00298 {
00299 helper[0] = *iter;
00300 cstr = cstr+helper;
00301 iter++;
00302 }
00303 return std::string(cstr);
00304 }
00305
00306 std::string CSXMLHelper::toAscii(const DOMString &domString)
00307 {
00308 static char *functionName="toAscii";
00309 char* charArray = domString.transcode();
00310 std::string ret(charArray);
00311 free(charArray);
00312 return ret;
00313 }
00314
00315 std::string CSXMLHelper::toAscii(const XMLCh* xmlChars)
00316 {
00317 return toAscii(DOMString(xmlChars));
00318 }
00319
00320 int CSXMLHelper::getError(void)
00321 {
00322 return mError;
00323 }
00324
00325 std::string CSXMLHelper::getErrorMessage(void)
00326 {
00327 return mErrorMessageString;
00328 }
00329
00330 void CSXMLHelper::warning(const SAXParseException &toCatch)
00331 {
00332 static char *functionName="warning";
00333 LOG_ENTER
00334 char line[10];
00335 char column[10];
00336 itoa(toCatch.getLineNumber(),line,10);
00337 itoa(toCatch.getColumnNumber(),column,10);
00338 std::string errorMessageString;
00339 errorMessageString = errorMessageString
00340 + std::string("Warning at File \"")
00341 + toAscii(toCatch.getSystemId()) + "\", "
00342 + std::string(line) + ", "
00343 + std::string(column) + " : "
00344 + toAscii(toCatch.getMessage()) + "\n";
00345 mErrorMessageString = errorMessageString;
00346 mError = CSXML_DOCUMENT_LOADING_ERROR;
00347 LOG_EXIT
00348 }
00349
00350 void CSXMLHelper::error(const SAXParseException& toCatch)
00351 {
00352 static char *functionName="error";
00353 LOG_ENTER
00354 char line[10];
00355 char column[10];
00356 itoa(toCatch.getLineNumber(),line,10);
00357 itoa(toCatch.getColumnNumber(),column,10);
00358 std::string errorMessageString;
00359 errorMessageString = errorMessageString
00360 + std::string("Error at File \"")
00361 + toAscii(toCatch.getSystemId()) + "\", "
00362 + std::string(line) + ", "
00363 + std::string(column) + " : "
00364 + toAscii(toCatch.getMessage()) + "\n";
00365 mErrorMessageString = errorMessageString;
00366 mError = CSXML_DOCUMENT_LOADING_ERROR;
00367 LOG_EXIT
00368 }
00369
00370 void CSXMLHelper::fatalError(const SAXParseException& toCatch)
00371 {
00372 static char *functionName="fatal";
00373 LOG_ENTER
00374 char line[10];
00375 char column[10];
00376 itoa(toCatch.getLineNumber(),line,10);
00377 itoa(toCatch.getColumnNumber(),column,10);
00378 std::string errorMessageString;
00379 errorMessageString = errorMessageString
00380 + std::string("Fatal Error at File \"")
00381 + toAscii(toCatch.getSystemId()) + "\", "
00382 + std::string(line) + ", "
00383 + std::string(column) + " : "
00384 + toAscii(toCatch.getMessage()) + "\n";
00385 mErrorMessageString = errorMessageString;
00386 mError = CSXML_DOCUMENT_LOADING_ERROR;
00387 LOG_EXIT
00388 }
00389
00390 void CSXMLHelper::resetErrors()
00391 {
00392 static char *functionName="resetErrors";
00393 LOG_ENTER
00394 mError = CSXML_OK;
00395 mErrorMessageString = std::string();
00396 LOG_EXIT
00397 }
00398
00399 #endif // USE_XALAN