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