Commit before changing Tree.ml interface
[SXSI/xpathcomp.git] / XMLDocShredder.cpp
1 /**********************************************************
2  * XMLDocShredder.cpp
3  * ---------------------
4  * Implementation of the class that receives events from the XML parser and 
5  * invokes corresponding construction methods of the storage interface.
6  * 
7  * Author: Greg Leighton
8  * Date: 02/11/08
9  * Changes:
10  *              05/11/08 -- Fixed bug related to parsing empty elements
11  *                               -- Set parser properties to automatically resolve
12  *                                      entity references and load external DTD if present
13  *                               -- Modified processEndDocument() by adding a nodeFinished()
14  *                                      call to the storage interface to close off the 
15  *                                      document node
16  *
17  */
18
19 #include <iostream>
20 #include "XMLDocShredder.h"
21 #include "SXSIStorageInterface.h"
22 #include <libxml++/exceptions/parse_error.h>
23 #include "Utils.h"
24
25 using namespace Glib;
26
27 void XMLDocShredder::setProperties(){
28   /* instruct the parser to expand entity references and report as 
29    * regular PCDATA
30    */ 
31   reader_->set_parser_property(
32                                TextReader::SubstEntities, true);
33                 
34   /* instruct parser to read external DTD, if present.  This is 
35          * needed to obtain any entity definitions in the DTD
36          */
37   reader_->set_parser_property(
38                                TextReader::LoadDtd, true);
39   
40   
41   /* 
42    */
43   reader_->set_parser_property(
44                                TextReader::DefaultAttrs, true);
45   
46
47   /* but we don't want to do validation since it would slow us down
48    */
49
50
51   reader_->set_parser_property(
52                                TextReader::Validate, false);
53   
54 }
55 XMLDocShredder::XMLDocShredder(const unsigned char * data,
56                                TextReader::size_type size,
57                                int sf, 
58                                bool iet, 
59                                bool dtc)                        
60 {
61   last_text = false;
62   reader_ = new TextReader(data,size,"");
63   setProperties();
64   storageIfc_ = new SXSIStorageInterface(sf,iet,dtc);
65   buffer = "";
66 }
67
68 XMLDocShredder::XMLDocShredder(const string inFileName,int sf, bool iet, bool dtc)
69 {
70   last_text = false;
71   reader_ = new TextReader(inFileName);
72   setProperties();
73   storageIfc_ = new SXSIStorageInterface(sf,iet,dtc);
74   buffer = "";
75 }
76
77 XMLDocShredder::~XMLDocShredder()
78 {
79         delete reader_;
80         delete storageIfc_;
81
82 }
83
84
85 void XMLDocShredder::processStartElement()
86 {
87         // fetch element name; this will be the full qualified name
88         ustring name = reader_->get_name();
89         bool empty = false;
90         
91         storageIfc_->newChild(name);
92
93         /* We must be really carefull here. calling process attributes moves
94            the document pointer on the last attribute, hence calling reader_->is_empty
95            afterwards will yield the wrong result. It is better to call it while we are
96            on the element and generate a nodeFinished() call at the end */
97         empty = reader_->is_empty_element();
98
99
100         // now, process attributes
101         if (reader_->has_attributes())
102           {
103             processAttributes();
104           };
105
106         
107         if (empty){
108           storageIfc_->nodeFinished(name);         
109         };
110
111
112 }
113
114 void XMLDocShredder::processEndElement()
115 {
116   // tell the storage interface that the current node has been completely processed  
117   storageIfc_->nodeFinished(reader_->get_name());
118 }
119
120 void XMLDocShredder::processPCDATA()
121 {
122   // send the content of this PCDATA node to the storage interface as a text node
123   
124   if (reader_->has_value()){
125     storageIfc_->newChild("<$>");
126     storageIfc_->newText(reader_->get_value());
127     storageIfc_->nodeFinished("<$>");
128   };
129 }
130
131 void XMLDocShredder::processAttributes()
132 {
133         reader_->move_to_first_attribute();
134                 
135         string nspaceStr = "xmlns";
136         storageIfc_->newChild("<@>");
137         do
138         {
139                 ustring name = reader_->get_name();
140                 ustring value = reader_->get_value();
141                 
142                 /* the libxml++ TextReader treats the xmlns attribute like an ordinary attribute,
143                 * so we have to extract it and build a namespace uri node out of it before
144                 * passing to the storage interface */
145                 
146                 if ((name.find(nspaceStr.c_str(), 0, 5)) == 0)
147                 {
148                         storageIfc_->newChild(":" + value);
149                         storageIfc_->nodeFinished(":" + value); 
150                 }
151                 
152                 /* otherwise, this is an ordinary attribute, so we construct a new child node of the 
153                  * parent element to store the attribute name, possessing a child text node storing the 
154                  * attribute value.  Then, we close off the attribute node with a call to nodeFinished()
155                  */
156                  
157                 else
158                 {
159                   string attname = "<@>"+name;
160                   storageIfc_->newChild(attname);
161                   storageIfc_->newChild("<@$>");
162                   storageIfc_->newText(value);
163                   storageIfc_->nodeFinished("<@$>");
164                   storageIfc_->nodeFinished(attname);
165                 }
166         }
167         while (reader_->move_to_next_attribute());
168         storageIfc_->nodeFinished("<@>");
169 }
170
171 void XMLDocShredder::processSignificantWhitespace()
172 {
173   
174   if (reader_->has_value()){
175     storageIfc_->newChild("<$>");
176     storageIfc_->newText(reader_->get_value());
177     storageIfc_->nodeFinished("<$>");
178   };  
179 }
180
181 void XMLDocShredder::processStartDocument(const string docName)
182 {
183   // tell storage interface to construct the document name
184   storageIfc_->newChild("");  
185   
186 }
187
188 void XMLDocShredder::processEndDocument()
189 {
190         /* tell the storage interface that document parsing has finished, and structures
191          * can now be written to disk. */
192   storageIfc_->nodeFinished("");
193   storageIfc_->parsingFinished();       
194 }
195
196 void XMLDocShredder::processComment()
197 {
198   //storageIfc_->newChild("!" + reader_->get_value());
199   //storageIfc_->nodeFinished();
200 }
201
202 void XMLDocShredder::processProcessingInstruction()
203 {
204         ustring name = reader_->get_name();
205         ustring value = reader_->get_value();   
206         
207         /* Create a child node to store the target of the PI, append a text node to it to store 
208          * the PI data, send to the storage interface.  Close off the PI node with a call to
209          * nodeFinished
210          */
211         
212         // storageIfc_->newChild("?" + name);
213         // storageIfc_->newText(value);
214         // storageIfc_->nodeFinished();
215 }
216
217 void XMLDocShredder::processDocTypeDeclaration()
218 {
219         /* We currently ignore the DOCTYPE declaration, but we'll provide this method skeleton 
220          * in case we do want to process it in the future.
221         */
222 }
223
224 void XMLDocShredder::processCDATASection()
225 {
226         /* Currently, we don't preserve CDATA sections since they aren't part of the XPath data
227          * model.  Instead, we simply pass the converted text value to the storage interface as 
228          * a text node attached to the current context node.
229          */
230   if (reader_->has_value()){
231     storageIfc_->newChild("<$>");
232     storageIfc_->newText(reader_->get_value());
233     storageIfc_->nodeFinished("<$>");
234   };
235
236 }
237
238 void XMLDocShredder::processUnknownNodeType()
239 {
240         cout << "unknown token encountered during parsing" << endl;
241         throw xmlpp::parse_error("unknown token encountered during parsing");
242                 
243 }
244
245 void XMLDocShredder::parse()
246 {       
247         while (reader_->read() && (reader_->get_read_state() != TextReader::Error))
248         {
249                 switch (reader_->get_node_type())
250                 {
251                         case TextReader::Element:
252                                 processStartElement();
253                                 break;
254                                 
255                         case TextReader::Text:
256                                 processPCDATA();
257                                 break;
258                                 
259                         case TextReader::EndElement:
260                                 processEndElement();
261                                 break;
262                                 
263                         case TextReader::SignificantWhitespace:
264                                 processSignificantWhitespace();
265                                 break;
266                                 
267                         case TextReader::Comment:
268                                 processComment();
269                                 break;
270                         
271                         case TextReader::DocumentType:
272                                 processDocTypeDeclaration();
273                                 break;
274                                 
275                         case TextReader::ProcessingInstruction:
276                                 processProcessingInstruction();
277                                 break;
278                         
279                         case TextReader::CDATA:
280                                 processCDATASection();
281                                 break;
282                         
283                         case TextReader::None:
284                                 processUnknownNodeType();
285                                 break;
286                                 
287                         default:
288                                 int type = reader_->get_node_type();
289                                 cout << "  Node type: " << type << endl;
290                                 break;  
291                         
292                 }
293         }                       
294 }