source: gsdl/trunk/src/colservr/expat_resultset.cpp@ 16445

Last change on this file since 16445 was 16311, checked in by davidb, 16 years ago

Removed debugging statement

  • Property svn:keywords set to Author Date Id Revision
File size: 3.4 KB
Line 
1
2#include <stdio.h>
3#include <string.h>
4#include <expat.h>
5
6#if defined(GSDL_USE_OBJECTSPACE)
7# include <ospace\std\iostream>
8#elif defined(GSDL_USE_IOS_H)
9# include <iostream.h>
10#else
11# include <iostream>
12using namespace std;
13#endif
14
15#include "text_t.h"
16#include "queryinfo.h"
17#include "gsdlunicode.h"
18
19struct queryresultpack {
20 queryresultsclass* queryresult_ptr;
21 int match_count;
22};
23
24char* get_attribute(const char** attr, char* att_name)
25{
26 char* att_val = NULL;
27
28 for (int i = 0; attr[i]; i += 2) {
29 if (strcmp(attr[i],att_name)==0) {
30 att_val = strdup(attr[i+1]);
31 }
32 }
33
34 return att_val;
35}
36
37static void XMLCALL
38startElement(void *userData, const char *name, const char **attributes)
39{
40 queryresultpack* qrpack_ptr = (queryresultpack*)userData;
41 queryresultsclass* queryresult_ptr = qrpack_ptr->queryresult_ptr;
42
43 text_t element_name = (char*)name;
44
45 if (element_name == "ResultSet") {
46 char* cached_attribute_str = get_attribute(attributes, "cached");
47 if (cached_attribute_str != NULL) {
48 cerr << "Cached: " << cached_attribute_str << endl;
49 }
50 }
51
52 if (element_name == "Error") {
53 char* error_type_str = get_attribute(attributes, "type");
54 queryresult_ptr->error_message = error_type_str;
55 if ((text_t) error_type_str == "PARSE_EXCEPTION") {
56 queryresult_ptr->syntax_error = true;
57 }
58 }
59
60 if (element_name == "Term") {
61 char* term_value_str = get_attribute(attributes, "value");
62 char* term_frequency_str = get_attribute(attributes, "freq");
63
64 termfreqclass termfreqobj;
65 termfreqobj.termstr = to_uni(term_value_str);
66
67 termfreqobj.termfreq = atoi(term_frequency_str);
68 queryresult_ptr->orgterms.push_back(termfreqobj);
69 }
70
71 if (element_name == "StopWord") {
72 char* stop_word_value_str = get_attribute(attributes, "value");
73 queryresult_ptr->stopwords.insert((text_t) stop_word_value_str);
74 }
75
76 if (element_name=="MatchingDocsInfo") {
77 char* num_match_docs_str = get_attribute(attributes,"num");
78 if (num_match_docs_str != NULL) {
79 int num_match_docs = atoi(num_match_docs_str);
80
81 queryresult_ptr->docs_matched = num_match_docs;
82 free(num_match_docs_str);
83 }
84 else {
85 queryresult_ptr->docs_matched = 0;
86 }
87 }
88
89
90 if (element_name=="Match") {
91
92 char* id = get_attribute(attributes,"id");
93 if (id!=NULL) {
94 int id_num = atoi(id);
95
96 docresultclass doc;
97 doc.clear();
98 doc.docnum = id_num;
99 doc.docweight = qrpack_ptr->match_count;
100 queryresult_ptr->docs.docset[doc.docnum] = doc;
101 queryresult_ptr->docs.docorder.push_back(doc.docnum);
102 ++qrpack_ptr->match_count;
103
104 free(id);
105 }
106
107 }
108}
109
110static void XMLCALL
111endElement(void *userData, const char *name)
112{
113 // no need to do anything
114}
115
116
117
118
119int expat_resultset(text_t& xml_text, queryresultsclass& queryresult)
120{
121 queryresult.clear();
122 queryresultpack qrpack = { &queryresult, 0 };
123
124 char * xml_text_cstr = xml_text.getcstr();
125
126 XML_Parser parser = XML_ParserCreate(NULL);
127
128 XML_SetUserData(parser, &qrpack);
129 XML_SetElementHandler(parser, startElement, endElement);
130
131 int return_status = 0;
132 const int parse_status
133 = XML_Parse(parser, xml_text_cstr, strlen(xml_text_cstr), XML_TRUE);
134
135 if (parse_status == XML_STATUS_ERROR) {
136 fprintf(stderr,
137 "%s at line %d\n",
138 XML_ErrorString(XML_GetErrorCode(parser)),
139 XML_GetCurrentLineNumber(parser));
140 return_status = 1;
141 }
142
143 XML_ParserFree(parser);
144 free(xml_text_cstr);
145
146 return return_status;
147}
Note: See TracBrowser for help on using the repository browser.