336ca2dfff33fe86765143f8fe028ff912422397
[SXSI/XMLTree.git] / XMLTreeBuilder.h
1 \r
2 /******************************************************************************\r
3  *   Copyright (C) 2009 by Diego Arroyuelo                                    *\r
4  *   Builder class for the in-memory XQuery/XPath engine                      *\r
5  *                                                                            *\r
6  *   This program is free software; you can redistribute it and/or modify     *\r
7  *   it under the terms of the GNU Lesser General Public License as published *\r
8  *   by the Free Software Foundation; either version 2 of the License, or     *\r
9  *   (at your option) any later version.                                      *\r
10  *                                                                            *\r
11  *   This program is distributed in the hope that it will be useful,          *\r
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of           *\r
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *\r
14  *   GNU Lesser General Public License for more details.                      *\r
15  *                                                                            *\r
16  *   You should have received a copy of the GNU Lesser General Public License *\r
17  *   along with this program; if not, write to the                            *\r
18  *   Free Software Foundation, Inc.,                                          *\r
19  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.                *\r
20  ******************************************************************************/ \r
21 \r
22 #ifndef XMLTREEBUILDER_H_\r
23 #define XMLTREEBUILDER_H_\r
24 #include <unordered_map>\r
25 #include "TextCollection/TextCollectionBuilder.h"\r
26 #include <stdio.h>\r
27 #include <stdlib.h>\r
28 #include <cstring>\r
29 \r
30 \r
31 #undef W\r
32 #undef WW\r
33 #undef Wminusone\r
34 \r
35 \r
36 #include "XMLTree.h"\r
37 #include "bp.h"\r
38 #include <static_bitsequence.h>\r
39 #include <alphabet_mapper.h>\r
40 #include <static_sequence.h>\r
41 using SXSI::TextCollection;\r
42 using SXSI::TextCollectionBuilder;\r
43 \r
44 #define NULLT -1\r
45 \r
46         // sets bit p in e\r
47 #define bitset(e,p) ((e)[(p)/W] |= (1<<((p)%W)))\r
48         // cleans bit p in e\r
49 #define bitclean(e,p) ((e)[(p)/W] &= ~(1<<((p)%W)))\r
50 \r
51 \r
52 \r
53 class XMLTreeBuilder {\r
54   \r
55    /** Array containing the balanced parentheses sequence */\r
56    pb *par_aux;\r
57    int parArraySize;\r
58    int npar;\r
59 \r
60    /** Mapping from tag identifer to tag name */  \r
61    vector<string> *TagName;\r
62    TagIdMap * tIdMap;\r
63    /** Array containing the sequence of tags */\r
64    TagType *tags_aux;\r
65    \r
66    /** The texts in the XML document */\r
67    TextCollectionBuilder *TextBuilder;\r
68    TextCollection *Text;\r
69    \r
70    /** The texts in the XML document (cached for faster display) */\r
71 \r
72    vector<string> *CachedText;\r
73 \r
74    unsigned int *empty_texts_aux;\r
75    int eta_size;\r
76    // Allows to disable the TextCollection for benchmarkin purposes\r
77    bool disable_tc;\r
78 \r
79 public:\r
80 \r
81    XMLTreeBuilder() {;};\r
82 \r
83    ~XMLTreeBuilder();\r
84    \r
85    /** OpenDocument(sample_rate_text,dtc): initilizes the construction\r
86     * of the data structure for the XML document.  Parameter \r
87     * sample_rate_text indicates the sampling rate for the text searching data\r
88     * structures (small values get faster searching but a bigger space \r
89     * requirement). dtc disable the use of the TextCollection\r
90     * (i.e. everything is considered an empty text *)\r
91     * Returns a non-zero value upon success, NULLT in case of \r
92     * error. */\r
93    int OpenDocument(bool empty_texts, int sample_rate_text, bool dtc);\r
94 \r
95    /** CloseDocument(): finishes the construction of the data structure for \r
96     * the XML document. Tree and tags are represented in the final form, \r
97     * dynamic data structures are made static, returning the resulting\r
98     * XMLTree. After that, the XMLTree data structure can be queried. */\r
99    XMLTree *CloseDocument();\r
100 \r
101    /** NewOpenTag(tagname): indicates the event of finding a new opening tag \r
102     * in the document. Tag name is given. Returns a non-zero value upon \r
103     * success, and returns NULLT in case of error. */\r
104    int NewOpenTag(string tagname);\r
105    \r
106    /** NewClosingTag(tagname): indicates the event of finding a new closing tag\r
107     *  in the document. Tag name is given. Returns a non-zero value upon \r
108     *  success, and returns NULLT in case of error. */\r
109    int NewClosingTag(string tagname);\r
110  \r
111    /** NewText(s): indicates the event of finding a new text s in \r
112     * the document. The new text is inserted within the text collection. \r
113     * Returns a non-zero value upon success, NULLT in case of error. \r
114     * If the string is empty, which is legal in attributes, then\r
115     * the string the sequence '\0x01\0x00' is inserted in the TextCollection\r
116     * It is ok to do so since a non printable character cannot occur in an XML document\r
117     */\r
118    int NewText(string text);\r
119 \r
120 \r
121 };\r
122 #endif\r
123 \r