Changeset 44:73e35db677a4 in rrlib_xml


Ignore:
Timestamp:
17.07.2012 11:36:35 (7 years ago)
Author:
Tobias Föhst <foehst@…>
Branch:
default
Phase:
public
Message:

Added ctors that allow explicit specification of input encoding

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • tDocument.cpp

    r42 r44  
    8686{ 
    8787  assert(this->document); 
    88   if (!this->document) 
    89   { 
    90     throw tException("Could not parse XML file `" + file_name + "'!"); 
    91   } 
     88  this->CheckIfDocumentIsValid("Could not parse XML file `" + file_name + "'!"); 
     89  tCleanupHandler::Instance(); 
     90} 
     91 
     92tDocument::tDocument(const std::string &file_name, const std::string &encoding, bool validate) 
     93  : document(xmlReadFile(file_name.c_str(), encoding.c_str(), validate ? XML_PARSE_DTDVALID : 0)), 
     94    root_node(reinterpret_cast<tNode *>(xmlDocGetRootElement(this->document))) 
     95{ 
     96  assert(this->document); 
     97  this->CheckIfDocumentIsValid("Could not parse XML file `" + file_name + "'!"); 
    9298  tCleanupHandler::Instance(); 
    9399} 
     
    97103    root_node(reinterpret_cast<tNode *>(xmlDocGetRootElement(this->document))) 
    98104{ 
    99   if (!this->document) 
    100   { 
    101     throw tException("Could not parse XML from memory buffer `" + std::string(reinterpret_cast<const char *>(buffer)) + "'!"); 
    102   } 
     105  this->CheckIfDocumentIsValid("Could not parse XML from memory buffer `" + std::string(reinterpret_cast<const char *>(buffer)) + "'!"); 
     106  tCleanupHandler::Instance(); 
     107} 
     108 
     109tDocument::tDocument(const void *buffer, size_t size, const std::string &encoding, bool validate) 
     110  : document(xmlReadMemory(reinterpret_cast<const char *>(buffer), size, "noname.xml", encoding.c_str(), validate ? XML_PARSE_DTDVALID : 0)), 
     111    root_node(reinterpret_cast<tNode *>(xmlDocGetRootElement(this->document))) 
     112{ 
     113  this->CheckIfDocumentIsValid("Could not parse XML from memory buffer `" + std::string(reinterpret_cast<const char *>(buffer)) + "'!"); 
    103114  tCleanupHandler::Instance(); 
    104115} 
     
    177188 
    178189//---------------------------------------------------------------------- 
     190// tDocument CheckIfDocumentIsValid 
     191//---------------------------------------------------------------------- 
     192void tDocument::CheckIfDocumentIsValid(const std::string &exception_message) 
     193{ 
     194  if (!this->document) 
     195  { 
     196    throw tException(exception_message); 
     197  } 
     198} 
     199 
     200//---------------------------------------------------------------------- 
    179201// End of namespace declaration 
    180202//---------------------------------------------------------------------- 
  • tDocument.h

    r42 r44  
    110110  explicit tDocument(const std::string &file_name, bool validate = true); 
    111111 
     112  /*! The ctor of tDocument from a given file with explicit encoding 
     113   * 
     114   * This ctor reads and parses a file with given name into a XML DOM 
     115   * representation. 
     116   * If needed, the XML document is also validated using an included 
     117   * DTD specification. 
     118   * 
     119   * \exception tException is thrown if the file was not found or could not be parsed 
     120   * 
     121   * \param file_name   The name of the file to load 
     122   * \param encoding    The encoding of the input file 
     123   * \param validate    Whether the validation should be processed or not 
     124   */ 
     125  explicit tDocument(const std::string &file_name, const std::string &encoding, bool validate = true); 
     126 
    112127  /*! The ctor of tDocument from a memory buffer 
    113128   * 
     
    124139   */ 
    125140  tDocument(const void *buffer, size_t size, bool validate = true); 
     141 
     142  /*! The ctor of tDocument from a memory buffer with explicit encoding 
     143   * 
     144   * This ctor reads and parses XML content given in a memory buffer into a XML DOM 
     145   * representation. 
     146   * If needed, the XML document is also validated using an included 
     147   * DTD specification. 
     148   * 
     149   * \exception tException is thrown if the memory buffer could not be parsed 
     150   * 
     151   * \param buffer      Pointer to the memory buffer with XML content to be parsed 
     152   * \param size        Size of the memory buffer 
     153   * \param encoding    The encoding of the input file 
     154   * \param validate    Whether the validation should be processed or not 
     155   */ 
     156  tDocument(const void *buffer, size_t size, const std::string &encoding, bool validate = true); 
    126157 
    127158  /*! 
     
    185216 
    186217  tDocument(const tDocument&); // generated copy-constructor is not safe 
     218 
     219  void CheckIfDocumentIsValid(const std::string &exception_message); 
     220 
    187221}; 
    188222 
Note: See TracChangeset for help on using the changeset viewer.