00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <xml_loader.h>
00014 #include <xml_types.h>
00015
00016 void XMLCALL xml_loader::startElement ( void *userData, const char *name, const char **atts ){
00017
00018 xml_loader* th = (xml_loader*)userData;
00019 uint sz = th->text_func.size();
00020 if(th->start_element_func.empty())
00021 if(th->doc)
00022 th->doc->element( th, name, atts );
00023
00024 if(th->start_element_func.empty() == false) {
00025 if(th->start_element_func.top() != NULL)
00026 (th->start_element_func.top())( th, name, atts );
00027 }
00028 if(sz == th->text_func.size())
00029 th->text_func.push(NULL);
00030 }
00031
00032 void XMLCALL xml_loader::endElement ( void *userData, const char *name ) {
00033
00034 xml_loader* th = (xml_loader*)userData;
00035 th->start_element_func.pop();
00036 th->text_func.pop();
00037 if(th != NULL && th->parents_stack.size() == 1) {
00038 th->doc->root = th->parents_stack.top();
00039 th->doc->root_type = name;
00040 }
00041 th->parents_stack.pop();
00042 }
00043
00044 void XMLCALL xml_loader::text_data (void *userData, const XML_Char *s, int len) {
00045 XML_Char* c = (XML_Char*)malloc(sizeof(XML_Char)* len + 1);
00046 memcpy(c, s, len);
00047 c[len] = 0;
00048
00049 free(c);
00050 xml_loader* th = (xml_loader*)userData;
00051 if(th->text_func.empty() == false) {
00052
00053 if(th->text_func.top() != NULL)
00054 (th->text_func.top())( th, s, len );
00055 } else {
00056
00057 }
00058 }
00059
00060 void* xml_loader::load() {
00061 XML_SetUserData ( parser, this );
00062 XML_SetElementHandler ( parser, xml_loader::startElement, xml_loader::endElement );
00063 XML_SetCharacterDataHandler(parser, xml_loader::text_data);
00064 char buf[BUFSIZ];
00065 int done;
00066 do {
00067 size_t len = fread ( buf, 1, sizeof ( buf ), input );
00068 done = len < sizeof ( buf );
00069 if ( XML_Parse ( parser, buf, len, done ) == XML_STATUS_ERROR ) {
00070 fprintf ( stderr, "%s at line %d\n", XML_ErrorString ( XML_GetErrorCode ( parser ) ), XML_GetCurrentLineNumber ( parser ) );
00071 return NULL;
00072 }
00073 } while ( !done );
00074 return doc->root;
00075 }
00076
00077 xml_loader::xml_loader(FILE* inp, Document* Doc): doc(Doc) {
00078 input = inp;
00079 parser = XML_ParserCreate ( NULL );
00080 }
00081
00082 Document* xml_loader::getDocument() {
00083 if(doc->root == NULL)
00084 load();
00085 return doc;
00086 }
00087
00088 xml_loader::~xml_loader() {
00089 XML_ParserFree ( parser );
00090 }
00091
00092 void* xml_loader::get_parent() const {
00093 if(!parents_stack.empty())
00094 return parents_stack.top();
00095 return NULL;
00096 }
00097
00098 void xml_loader::push(void * node) {
00099 parents_stack.push(node);
00100 }
00101
00102 void xml_loader::push(text_handler handler) {
00103 text_func.push(handler);
00104 }
00105
00106 void xml_loader::push(element_handler handler) {
00107 start_element_func.push(handler);
00108 }
00109
00110 void xml_loader::push(void * node, element_handler handler) {
00111 parents_stack.push(node);
00112 start_element_func.push(handler);
00113 }