Update code according to libbp renaming.
[SXSI/XMLTree.git] / XMLTreeBuilder.cpp
1 #include "common.h"\r
2 #include "XMLTreeBuilder.h"\r
3 #include "timings.h"\r
4 using std::string;\r
5 \r
6 XMLTreeBuilder::~XMLTreeBuilder(){\r
7   //free(par_aux);\r
8   free(tags_aux);\r
9   //delete other stuff.\r
10 \r
11 }\r
12 \r
13 // OpenDocument(empty_texts): it starts the construction of the data structure for\r
14 // the XML document. Parameter empty_texts indicates whether we index empty texts\r
15 // in document or not. Returns a non-zero value upon success, NULLT in case of error.\r
16 int XMLTreeBuilder::OpenDocument(bool empty_texts,\r
17                                  int sample_rate_text,\r
18                                  bool dtc,\r
19                                  TextCollectionBuilder::index_type_t index_type)\r
20  {\r
21     npar = 0;\r
22     parArraySize = 1;\r
23     disable_tc = dtc;\r
24     text_index_type = index_type;\r
25     STARTTIMER();\r
26 \r
27     par_aux = (pb *)umalloc(sizeof(pb)*parArraySize);\r
28 \r
29     tags_aux = (TagType *) umalloc(sizeof(TagType));\r
30 \r
31     TagName = new vector<string>();\r
32     tIdMap = new std::unordered_map<string,int>();\r
33 \r
34     REGISTER_TAG(TagName,tIdMap,DOCUMENT_OPEN_TAG);\r
35     REGISTER_TAG(TagName,tIdMap,ATTRIBUTE_OPEN_TAG);\r
36     REGISTER_TAG(TagName,tIdMap,PCDATA_OPEN_TAG);\r
37     REGISTER_TAG(TagName,tIdMap,ATTRIBUTE_DATA_OPEN_TAG);\r
38     REGISTER_TAG(TagName,tIdMap,CLOSING_TAG);\r
39     REGISTER_TAG(TagName,tIdMap,DOCUMENT_CLOSE_TAG);\r
40     REGISTER_TAG(TagName,tIdMap,ATTRIBUTE_CLOSE_TAG);\r
41     REGISTER_TAG(TagName,tIdMap,PCDATA_CLOSE_TAG);\r
42     REGISTER_TAG(TagName,tIdMap,ATTRIBUTE_DATA_CLOSE_TAG);\r
43 \r
44 \r
45     if (disable_tc)\r
46         TextBuilder = 0;\r
47     else\r
48         TextBuilder = TextCollectionBuilder::create((unsigned)sample_rate_text, index_type);\r
49 \r
50     Text = 0;\r
51     empty_texts_aux = (unsigned int *)ucalloc(sizeof(unsigned int),1);\r
52     eta_size = sizeof(unsigned int);\r
53     return 1;  // indicates success in the initialization of the data structure\r
54  }\r
55 \r
56 // CloseDocument(): it finishes the construction of the data structure for the XML\r
57 // document. Tree and tags are represented in the final form, dynamic data\r
58 // structures are made static, and the flag "finished" is set to true. After that,\r
59 // the data structure can be queried.\r
60 XMLTree *XMLTreeBuilder::CloseDocument()\r
61  {\r
62     //closing parenthesis for the tree root\r
63     //par_aux = (pb *)urealloc(par_aux, sizeof(pb)*(1+npar/(8*sizeof(pb))));\r
64     //setbit(par_aux, npar, CP);\r
65     //npar++;\r
66 \r
67     // makes the text collection static\r
68    STOPTIMER(Parsing);\r
69    PRINTTIME("Parsing XML Document", Parsing);\r
70 \r
71     if (!disable_tc) {\r
72        assert(Text == 0);\r
73        assert(TextBuilder != 0);\r
74        STARTTIMER();\r
75        Text = TextBuilder->InitTextCollection();\r
76        delete TextBuilder;\r
77        TextBuilder = 0;\r
78        STOPTIMER(Building);\r
79        PRINTTIME("Building TextCollection", Building);\r
80 \r
81     }\r
82 \r
83     XMLTree *T = new XMLTree(par_aux,\r
84                              npar,\r
85                              TagName,\r
86                              tIdMap,\r
87                              empty_texts_aux, // freed by the constructor\r
88                              tags_aux,        // freed by the constructor\r
89                              Text,\r
90                              disable_tc,\r
91                              text_index_type);\r
92     tags_aux = 0;\r
93     empty_texts_aux = 0;\r
94     return T;\r
95  }\r
96 \r
97 \r
98 // NewOpenTag(tagname): indicates the event of finding a new opening tag in the document.\r
99 // Tag name is given. Returns a non-zero value upon success, and returns NULLT\r
100 // in case of failing when trying to insert the new tag.\r
101 int XMLTreeBuilder::NewOpenTag(string tagname)\r
102  {\r
103     int i;\r
104 \r
105     // inserts a new opening parentheses in the bit sequence\r
106     if (sizeof(pb)*8*parArraySize == npar) { // no space left for the new parenthesis\r
107        par_aux = (pb *)urealloc(par_aux, sizeof(pb)*2*parArraySize);\r
108        parArraySize *= 2;\r
109     }\r
110 \r
111     bp_setbit(par_aux,npar,OP);  // marks a new opening parenthesis\r
112 \r
113     TagIdMapIT tag_id = tIdMap->find(tagname);\r
114 \r
115     if (tag_id == tIdMap->end()){\r
116       REGISTER_TAG(TagName,tIdMap,tagname);\r
117       i = TagName->size() - 1;\r
118     }\r
119     else\r
120       i = tag_id->second;\r
121 \r
122     if (tagname.compare(PCDATA_OPEN_TAG) == 0 ||\r
123         tagname.compare(ATTRIBUTE_DATA_OPEN_TAG) == 0){\r
124     };\r
125 \r
126     tags_aux = (TagType *) urealloc(tags_aux, sizeof(TagType)*(npar + 1));\r
127 \r
128     tags_aux[npar] = i; // inserts the new tag id within the preorder sequence of tags\r
129 \r
130     npar++;\r
131 \r
132     return 1; // success\r
133  }\r
134 \r
135 \r
136 // NewClosingTag(tagname): indicates the event of finding a new closing tag in the document.\r
137 // Tag name is given. Returns a non-zero value upon success, and returns NULLT\r
138 // in case of failing when trying to insert the new tag.\r
139 int XMLTreeBuilder::NewClosingTag(string tagname)\r
140  {\r
141     int i;\r
142 \r
143     // inserts a new closing parentheses in the bit sequence\r
144     if (sizeof(pb)*8*parArraySize == npar) { // no space left for the new parenthesis\r
145        par_aux = (pb *)urealloc(par_aux, sizeof(pb)*2*parArraySize);\r
146        parArraySize *= 2;\r
147     }\r
148 \r
149     bp_setbit(par_aux,npar,CP);  // marks a new closing parenthesis\r
150 \r
151     //tagname.insert(0,"/");\r
152 \r
153     //TagIdMapIT tag_id = tIdMap->find(tagname);\r
154 \r
155     // if (tag_id == tIdMap->end()){\r
156     //   REGISTER_TAG(TagName,tIdMap,tagname);\r
157     //   i = TagName->size() - 1;\r
158     // }\r
159     // else\r
160     //   i = tag_id->second;\r
161 \r
162     tags_aux = (TagType *)urealloc(tags_aux, sizeof(TagType)*(npar + 1));\r
163 \r
164     tags_aux[npar] = CLOSING_TAG_ID; // inserts the new tag id within the preorder sequence of tags\r
165 \r
166     npar++;\r
167 \r
168     return 1; // success\r
169  }\r
170 \r
171 \r
172 // NewText(s): indicates the event of finding a new (non-empty) text s in the document.\r
173 // The new text is inserted within the text collection. Returns a non-zero value upon\r
174 // success, NULLT in case of error.\r
175 int XMLTreeBuilder::NewText(string text)\r
176  {\r
177    if (!disable_tc) {\r
178      if  (text.empty())\r
179        TextBuilder->InsertText((uchar *)"\001");\r
180      else\r
181        TextBuilder->InsertText((uchar *) text.c_str());\r
182    };\r
183 \r
184    int n_eta_size = sizeof(uint)*(1+(npar-1)/(8*sizeof(uint)));\r
185    //see basics.h, recalloc resizes and sets the new area to 0.\r
186 \r
187    empty_texts_aux = (uint *)urecalloc(empty_texts_aux,eta_size,n_eta_size);\r
188    eta_size = n_eta_size;\r
189    bitset(empty_texts_aux, npar-1);  // marks the non-empty text with a 1 in the bit vector\r
190 \r
191    return 1; // success\r
192  }\r
193 \r
194 \r