Generic hardware access library
/home/cschwick/hal/generic/src/common/PCIAddressTableXMLFileReader.cc
Go to the documentation of this file.
00001 #include "hal/PCIAddressTableXMLFileReader.hh"
00002 #include <sstream>
00003 
00004 #include <xercesc/util/PlatformUtils.hpp>
00005 #include <xercesc/sax/SAXException.hpp>
00006 #include <xercesc/sax/SAXParseException.hpp>
00007 #include <xercesc/parsers/XercesDOMParser.hpp>
00008 #include <xercesc/dom/DOMDocument.hpp>
00009 #include <xercesc/dom/DOMException.hpp>
00010 #include <xercesc/sax/SAXException.hpp>
00011 #include <xercesc/dom/DOMNamedNodeMap.hpp>
00012 #include <xercesc/sax/HandlerBase.hpp>
00013 
00014 using namespace XERCES_CPP_NAMESPACE;
00015 
00016 HAL::PCIAddressTableXMLFileReader::PCIAddressTableXMLFileReader( std::string fileName )
00017   throw ( HAL::XMLProcessingException ) {
00018   // Initialize the XML4C system
00019   try {
00020     XMLPlatformUtils::Initialize();
00021   } catch (const XMLException& toCatch) {
00022     throw( HAL::XMLProcessingException( "Error during Xerces-initialization", __FILE__, __LINE__, __FUNCTION__));
00023   }
00024   // Instantiate the DOM parser.
00025   XercesDOMParser* parser = new XercesDOMParser();
00026   parser->setValidationScheme(XercesDOMParser::Val_Always);
00027   parser->setDoNamespaces(true);
00028   parser->setDoSchema(true);
00029   parser->setValidationSchemaFullChecking(false); // this is default
00030   parser->setCreateEntityReferenceNodes(true);  // this is default
00031   parser->setIncludeIgnorableWhitespace (false);
00032 
00033 
00034   HAL::XMLParserErrorHandler halHandler( fileName );
00035   parser->setErrorHandler( &halHandler );
00036 
00037   try {
00038     parser->parse(fileName.c_str());
00039 
00040   } catch (const XMLException& toCatch) {
00041     const XMLCh* didi(toCatch.getMessage());
00042     char* dada = XMLString::transcode(didi);
00043     std::string errorstr(dada);
00044     XMLString::release( &dada );
00045     std::string text = errorstr;
00046     XMLPlatformUtils::Terminate();
00047     throw(HAL::XMLProcessingException( text, __FILE__, __LINE__, __FUNCTION__ ));
00048 
00049   } catch (const DOMException& toCatch) {
00050     const XMLCh* dada = toCatch.msg;
00051     char* strChar = XMLString::transcode( dada );
00052     std::string errorstr( strChar );
00053     XMLString::release( &strChar );
00054     std::string text = errorstr;
00055     XMLPlatformUtils::Terminate();
00056     throw( HAL::XMLProcessingException( text, __FILE__, __LINE__, __FUNCTION__ ));
00057 
00058   } catch ( const SAXParseException& exception ) {
00059     // This exceptions is thrown in the errorHandler. We treat it a little
00060     // bit more in detail in order to give the user info where to find the
00061     // error in the xml-document.
00062     std::stringstream text;
00063     text << "Error during parsing file " << fileName
00064          << "\n     at line " << exception.getLineNumber()
00065          << " :\n";
00066     const XMLCh* dodo(exception.getMessage());
00067     char* xmlChar = XMLString::transcode(dodo);
00068     std::string errorstr(xmlChar);
00069     text << "     " << errorstr
00070          << std::ends;
00071     XMLString::release( &xmlChar );
00072     throw(HAL::XMLProcessingException( text.str(), __FILE__, __LINE__, __FUNCTION__ ));
00073 
00074   } catch ( const SAXException& toCatch ) {
00075     const XMLCh* dodo = toCatch.getMessage();
00076     char* dada=XMLString::transcode(dodo);
00077     std::string errorstr(dada);
00078     XMLString::release( &dada );
00079     std::string text = errorstr;
00080     XMLPlatformUtils::Terminate();
00081     throw(HAL::XMLProcessingException( text, __FILE__, __LINE__, __FUNCTION__ ));
00082 
00083   } catch ( const HAL::XMLProcessingException& ex ) {
00084    XMLPlatformUtils::Terminate();
00085     throw(ex);
00086 
00087   } catch (...) {
00088     std::string text = "\nUnexpected exception during parsing: " + fileName  + "\n";
00089     XMLPlatformUtils::Terminate();
00090     throw( HAL::XMLProcessingException( text, __FILE__, __LINE__, __FUNCTION__ ));
00091   }
00092   DOMDocument* doc = parser->getDocument();
00093 
00094   HAL::DOMToPCIMapConverter::convert( doc, itemPointerList );
00095 
00096   delete( parser );
00097 
00098   XMLPlatformUtils::Terminate();
00099 }