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>
|
---|
12 | using namespace std;
|
---|
13 | #endif
|
---|
14 |
|
---|
15 | #include "text_t.h"
|
---|
16 | #include "queryinfo.h"
|
---|
17 | #include "gsdlunicode.h"
|
---|
18 |
|
---|
19 | struct queryresultpack {
|
---|
20 | queryresultsclass* queryresult_ptr;
|
---|
21 | int match_count;
|
---|
22 | };
|
---|
23 |
|
---|
24 | char* 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 |
|
---|
37 | static void XMLCALL
|
---|
38 | startElement(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 | termfreqobj.termfreq = atoi(term_frequency_str);
|
---|
67 | queryresult_ptr->orgterms.push_back(termfreqobj);
|
---|
68 | }
|
---|
69 |
|
---|
70 | if (element_name == "StopWord") {
|
---|
71 | char* stop_word_value_str = get_attribute(attributes, "value");
|
---|
72 | queryresult_ptr->stopwords.insert((text_t) stop_word_value_str);
|
---|
73 | }
|
---|
74 |
|
---|
75 | if (element_name=="MatchingDocsInfo") {
|
---|
76 | char* num_match_docs_str = get_attribute(attributes,"num");
|
---|
77 | if (num_match_docs_str != NULL) {
|
---|
78 | int num_match_docs = atoi(num_match_docs_str);
|
---|
79 |
|
---|
80 | queryresult_ptr->docs_matched = num_match_docs;
|
---|
81 | free(num_match_docs_str);
|
---|
82 | }
|
---|
83 | else {
|
---|
84 | queryresult_ptr->docs_matched = 0;
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 |
|
---|
89 | if (element_name=="Match") {
|
---|
90 |
|
---|
91 | char* id = get_attribute(attributes,"id");
|
---|
92 | if (id!=NULL) {
|
---|
93 | int id_num = atoi(id);
|
---|
94 |
|
---|
95 | docresultclass doc;
|
---|
96 | doc.clear();
|
---|
97 | doc.docnum = id_num;
|
---|
98 | doc.docweight = qrpack_ptr->match_count;
|
---|
99 | queryresult_ptr->docs.docset[doc.docnum] = doc;
|
---|
100 | queryresult_ptr->docs.docorder.push_back(doc.docnum);
|
---|
101 | ++qrpack_ptr->match_count;
|
---|
102 |
|
---|
103 | free(id);
|
---|
104 | }
|
---|
105 |
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | static void XMLCALL
|
---|
110 | endElement(void *userData, const char *name)
|
---|
111 | {
|
---|
112 | // no need to do anything
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 |
|
---|
117 |
|
---|
118 | int expat_resultset(text_t& xml_text, queryresultsclass& queryresult)
|
---|
119 | {
|
---|
120 | queryresult.clear();
|
---|
121 | queryresultpack qrpack = { &queryresult, 0 };
|
---|
122 |
|
---|
123 | char * xml_text_cstr = xml_text.getcstr();
|
---|
124 |
|
---|
125 | // cerr << "**** xml_text = " << xml_text.getcstr() << endl;
|
---|
126 |
|
---|
127 | XML_Parser parser = XML_ParserCreate(NULL);
|
---|
128 |
|
---|
129 | XML_SetUserData(parser, &qrpack);
|
---|
130 | XML_SetElementHandler(parser, startElement, endElement);
|
---|
131 |
|
---|
132 | int return_status = 0;
|
---|
133 | const int parse_status
|
---|
134 | = XML_Parse(parser, xml_text_cstr, strlen(xml_text_cstr), XML_TRUE);
|
---|
135 |
|
---|
136 | if (parse_status == XML_STATUS_ERROR) {
|
---|
137 | fprintf(stderr,
|
---|
138 | "%s at line %d\n",
|
---|
139 | XML_ErrorString(XML_GetErrorCode(parser)),
|
---|
140 | XML_GetCurrentLineNumber(parser));
|
---|
141 | return_status = 1;
|
---|
142 | }
|
---|
143 |
|
---|
144 | XML_ParserFree(parser);
|
---|
145 | free(xml_text_cstr);
|
---|
146 |
|
---|
147 | return return_status;
|
---|
148 | }
|
---|