1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * expat_resultset.cpp --
|
---|
4 | *
|
---|
5 | * Copyright (C) 2005-2010 The New Zealand Digital Library Project
|
---|
6 | *
|
---|
7 | * A component of the Greenstone digital library software
|
---|
8 | * from the New Zealand Digital Library Project at the
|
---|
9 | * University of Waikato, New Zealand.
|
---|
10 | *
|
---|
11 | * This program is free software; you can redistribute it and/or modify
|
---|
12 | * it under the terms of the GNU General Public License as published by
|
---|
13 | * the Free Software Foundation; either version 2 of the License, or
|
---|
14 | * (at your option) any later version.
|
---|
15 | *
|
---|
16 | * This program is distributed in the hope that it will be useful,
|
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | * GNU General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with this program; if not, write to the Free Software
|
---|
23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
24 | *
|
---|
25 | *********************************************************************/
|
---|
26 |
|
---|
27 | #include <stdio.h>
|
---|
28 | #include <string.h>
|
---|
29 | #include <expat.h>
|
---|
30 |
|
---|
31 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
32 | # include <ospace\std\iostream>
|
---|
33 | #elif defined(GSDL_USE_IOS_H)
|
---|
34 | # include <iostream.h>
|
---|
35 | #else
|
---|
36 | # include <iostream>
|
---|
37 | using namespace std;
|
---|
38 | #endif
|
---|
39 |
|
---|
40 | #include "text_t.h"
|
---|
41 | #include "queryinfo.h"
|
---|
42 | #include "gsdlunicode.h"
|
---|
43 |
|
---|
44 | struct queryresultpack {
|
---|
45 | queryresultsclass* queryresult_ptr;
|
---|
46 | int match_count;
|
---|
47 | };
|
---|
48 |
|
---|
49 | char* get_attribute(const char** attr, char* att_name)
|
---|
50 | {
|
---|
51 | char* att_val = NULL;
|
---|
52 |
|
---|
53 | for (int i = 0; attr[i]; i += 2) {
|
---|
54 | if (strcmp(attr[i],att_name)==0) {
|
---|
55 | att_val = strdup(attr[i+1]);
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | return att_val;
|
---|
60 | }
|
---|
61 |
|
---|
62 | static void XMLCALL
|
---|
63 | startElement(void *userData, const char *name, const char **attributes)
|
---|
64 | {
|
---|
65 | queryresultpack* qrpack_ptr = (queryresultpack*)userData;
|
---|
66 | queryresultsclass* queryresult_ptr = qrpack_ptr->queryresult_ptr;
|
---|
67 |
|
---|
68 | text_t element_name = (char*)name;
|
---|
69 |
|
---|
70 | if (element_name == "ResultSet") {
|
---|
71 | char* cached_attribute_str = get_attribute(attributes, "cached");
|
---|
72 | if (cached_attribute_str != NULL) {
|
---|
73 | cerr << "Cached: " << cached_attribute_str << endl;
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | if (element_name == "Error") {
|
---|
78 | char* error_type_str = get_attribute(attributes, "type");
|
---|
79 | queryresult_ptr->error_message = error_type_str;
|
---|
80 | if ((text_t) error_type_str == "PARSE_EXCEPTION") {
|
---|
81 | queryresult_ptr->syntax_error = true;
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | if (element_name == "Term") {
|
---|
86 | char* term_value_str = get_attribute(attributes, "value");
|
---|
87 | char* term_frequency_str = get_attribute(attributes, "freq");
|
---|
88 |
|
---|
89 | termfreqclass termfreqobj;
|
---|
90 | termfreqobj.termstr = to_uni(term_value_str);
|
---|
91 |
|
---|
92 | termfreqobj.termfreq = atoi(term_frequency_str);
|
---|
93 | queryresult_ptr->orgterms.push_back(termfreqobj);
|
---|
94 | }
|
---|
95 |
|
---|
96 | if (element_name == "StopWord") {
|
---|
97 | char* stop_word_value_str = get_attribute(attributes, "value");
|
---|
98 | queryresult_ptr->stopwords.insert((text_t) stop_word_value_str);
|
---|
99 | }
|
---|
100 |
|
---|
101 | if (element_name=="MatchingDocsInfo") {
|
---|
102 | char* num_match_docs_str = get_attribute(attributes,"num");
|
---|
103 | if (num_match_docs_str != NULL) {
|
---|
104 | int num_match_docs = atoi(num_match_docs_str);
|
---|
105 |
|
---|
106 | queryresult_ptr->docs_matched = num_match_docs;
|
---|
107 | free(num_match_docs_str);
|
---|
108 | }
|
---|
109 | else {
|
---|
110 | queryresult_ptr->docs_matched = 0;
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | if (element_name=="Match") {
|
---|
116 |
|
---|
117 | char* id = get_attribute(attributes,"id");
|
---|
118 | if (id != NULL)
|
---|
119 | {
|
---|
120 | docresultclass doc;
|
---|
121 | doc.clear();
|
---|
122 | doc.docid = id;
|
---|
123 |
|
---|
124 | char* docrank = get_attribute(attributes, "rank");
|
---|
125 | if (docrank != NULL)
|
---|
126 | {
|
---|
127 | doc.docweight = atof(docrank);
|
---|
128 | }
|
---|
129 |
|
---|
130 | char* termfreq = get_attribute(attributes, "termfreq");
|
---|
131 | if (termfreq != NULL)
|
---|
132 | {
|
---|
133 | doc.num_query_terms_matched = atoi(termfreq);
|
---|
134 | }
|
---|
135 |
|
---|
136 | queryresult_ptr->docs.docset[doc.docid] = doc;
|
---|
137 | queryresult_ptr->docs.docorder.push_back(doc.docid);
|
---|
138 | ++qrpack_ptr->match_count;
|
---|
139 |
|
---|
140 | free(id);
|
---|
141 | }
|
---|
142 |
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | static void XMLCALL
|
---|
147 | endElement(void *userData, const char *name)
|
---|
148 | {
|
---|
149 | // no need to do anything
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 |
|
---|
154 |
|
---|
155 | int expat_resultset(text_t& xml_text, queryresultsclass& queryresult)
|
---|
156 | {
|
---|
157 | queryresult.clear();
|
---|
158 | queryresultpack qrpack = { &queryresult, 0 };
|
---|
159 |
|
---|
160 | char * xml_text_cstr = xml_text.getcstr();
|
---|
161 |
|
---|
162 | XML_Parser parser = XML_ParserCreate(NULL);
|
---|
163 |
|
---|
164 | XML_SetUserData(parser, &qrpack);
|
---|
165 | XML_SetElementHandler(parser, startElement, endElement);
|
---|
166 |
|
---|
167 | int return_status = 0;
|
---|
168 | const int parse_status
|
---|
169 | = XML_Parse(parser, xml_text_cstr, strlen(xml_text_cstr), XML_TRUE);
|
---|
170 |
|
---|
171 | if (parse_status == XML_STATUS_ERROR) {
|
---|
172 | fprintf(stderr,
|
---|
173 | "%s at line %d\n",
|
---|
174 | XML_ErrorString(XML_GetErrorCode(parser)),
|
---|
175 | XML_GetCurrentLineNumber(parser));
|
---|
176 | return_status = 1;
|
---|
177 | }
|
---|
178 |
|
---|
179 | XML_ParserFree(parser);
|
---|
180 | delete []xml_text_cstr;
|
---|
181 |
|
---|
182 | return return_status;
|
---|
183 | }
|
---|