ExpatXMLParser.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 
00018 #include "config.h"
00019 
00020 #ifdef LIBEXPAT_ENABLED
00021 
00022 #include "ExpatXMLParser.h"
00023 #include "XMLDocument.h"
00024 #include "XMLObject.h"
00025 
00026 namespace oasys {
00027 
00028 //----------------------------------------------------------------------
00029 ExpatXMLParser::ExpatXMLParser(const char* logpath)
00030     : Logger("ExpatXMLParser", logpath)
00031 {
00032 }
00033     
00034 //----------------------------------------------------------------------
00035 ExpatXMLParser::~ExpatXMLParser()
00036 {
00037 }
00038 
00039 //----------------------------------------------------------------------
00040 bool
00041 ExpatXMLParser::parse(XMLDocument* doc, const std::string& data)
00042 {
00043     XML_Parser p = XML_ParserCreate(NULL);
00044 
00045     // set up the expat handlers
00046     XML_SetUserData(p, this);
00047     XML_SetElementHandler(p, start_element, end_element);
00048     XML_SetCharacterDataHandler(p, character_data);
00049 
00050     // cache the document and null out the object
00051     doc_ = doc;
00052     cur_ = NULL;
00053 
00054     if (XML_Parse(p, data.c_str(), data.length(), true) != XML_STATUS_OK) {
00055         log_err("parse error at line %u:\n%s",
00056                 static_cast<u_int32_t>(XML_GetCurrentLineNumber(p)),
00057                 XML_ErrorString(XML_GetErrorCode(p)));
00058         return false;
00059     }
00060 
00061     return true;
00062 }
00063 
00064 //----------------------------------------------------------------------
00065 void XMLCALL
00066 ExpatXMLParser::start_element(void* data,
00067                               const char* element,
00068                               const char** attr)
00069 {
00070     ExpatXMLParser* this2 = (ExpatXMLParser*)data;
00071 
00072     XMLObject* new_object = new XMLObject(element);
00073     if (this2->cur_ == NULL) {
00074         this2->doc_->set_root(new_object);
00075     } else {
00076         this2->cur_->add_element(new_object);
00077     }
00078 
00079     this2->cur_ = new_object;
00080     while (attr[0] != NULL) {
00081         ASSERT(attr[1] != NULL);
00082         this2->cur_->add_attr(attr[0], attr[1]);
00083         attr += 2;
00084     }
00085 }
00086 
00087 //----------------------------------------------------------------------
00088 void XMLCALL
00089 ExpatXMLParser::end_element(void* data,
00090                             const char* element)
00091 {
00092     ExpatXMLParser* this2 = (ExpatXMLParser*)data;
00093     ASSERT(this2->cur_->tag() == element);
00094     this2->cur_ = this2->cur_->parent();
00095 }
00096 
00097 //----------------------------------------------------------------------
00098 void XMLCALL
00099 ExpatXMLParser::character_data(void* data,
00100                                const XML_Char* s,
00101                                int len)
00102 {
00103     ExpatXMLParser* this2 = (ExpatXMLParser*)data;
00104     ASSERT(this2->cur_ != NULL);
00105     this2->cur_->add_text(s, len);
00106 }
00107 
00108 } // namespace oasys
00109 
00110 #endif /* LIBEXPAT_ENABLED */

Generated on Sat Sep 8 08:36:16 2007 for DTN Reference Implementation by  doxygen 1.5.3