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

Last change on this file since 12262 was 12262, checked in by mdewsnip, 18 years ago

Now parses the query terms returned from Lucene into the Greenstone TermInfo structures.

  • Property svn:keywords set to Author Date Id Revision
File size: 2.9 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 == "Term") {
46 char* term_value_str = get_attribute(attributes, "value");
47 char* term_frequency_str = get_attribute(attributes, "freq");
48
49 termfreqclass termfreqobj;
50 termfreqobj.termstr = to_uni(term_value_str);
51 termfreqobj.termfreq = atoi(term_frequency_str);
52 queryresult_ptr->orgterms.push_back(termfreqobj);
53 }
54
55 if (element_name=="MatchingDocsInfo") {
56 char* num_match_docs_str = get_attribute(attributes,"num");
57 if (num_match_docs_str != NULL) {
58 int num_match_docs = atoi(num_match_docs_str);
59
60 queryresult_ptr->docs_matched = num_match_docs;
61 free(num_match_docs_str);
62 }
63 else {
64 queryresult_ptr->docs_matched = 0;
65 }
66 }
67
68
69 if (element_name=="Match") {
70
71 char* id = get_attribute(attributes,"id");
72 if (id!=NULL) {
73 int id_num = atoi(id);
74
75 docresultclass doc;
76 doc.clear();
77 doc.docnum = id_num;
78 doc.docweight = qrpack_ptr->match_count;
79 queryresult_ptr->docs.docset[doc.docnum] = doc;
80 queryresult_ptr->docs.docorder.push_back(doc.docnum);
81 ++qrpack_ptr->match_count;
82
83 free(id);
84 }
85
86 }
87}
88
89static void XMLCALL
90endElement(void *userData, const char *name)
91{
92 // no need to do anything
93}
94
95
96
97
98int expat_resultset(text_t& xml_text, queryresultsclass& queryresult)
99{
100 queryresult.clear();
101 queryresultpack qrpack = { &queryresult, 0 };
102
103 char * xml_text_cstr = xml_text.getcstr();
104
105 cerr << "**** xml_text = " << xml_text.getcstr() << endl;
106
107 XML_Parser parser = XML_ParserCreate(NULL);
108
109 XML_SetUserData(parser, &qrpack);
110 XML_SetElementHandler(parser, startElement, endElement);
111
112 int return_status = 0;
113 const int parse_status
114 = XML_Parse(parser, xml_text_cstr, strlen(xml_text_cstr), XML_TRUE);
115
116 if (parse_status == XML_STATUS_ERROR) {
117 fprintf(stderr,
118 "%s at line %d\n",
119 XML_ErrorString(XML_GetErrorCode(parser)),
120 XML_GetCurrentLineNumber(parser));
121 return_status = 1;
122 }
123
124 XML_ParserFree(parser);
125 free(xml_text_cstr);
126
127 return return_status;
128}
Note: See TracBrowser for help on using the repository browser.