1 /**********************************************************
3 * ---------------------
4 * Implementation of the class that receives events from the XML parser and
5 * invokes corresponding construction methods of the storage interface.
7 * Author: Greg Leighton
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
20 #include "XMLDocShredder.h"
21 #include "SXSIStorageInterface.h"
22 #include <libxml++/exceptions/parse_error.h>
27 void XMLDocShredder::setProperties(){
28 /* instruct the parser to expand entity references and report as
31 reader_->set_parser_property(
32 TextReader::SubstEntities, true);
34 /* instruct parser to read external DTD, if present. This is
35 * needed to obtain any entity definitions in the DTD
37 reader_->set_parser_property(
38 TextReader::LoadDtd, true);
43 reader_->set_parser_property(
44 TextReader::DefaultAttrs, true);
47 /* but we don't want to do validation since it would slow us down
51 reader_->set_parser_property(
52 TextReader::Validate, false);
55 XMLDocShredder::XMLDocShredder(const unsigned char * data,
56 TextReader::size_type size)
59 reader_ = new TextReader(data,size,"");
61 storageIfc_ = new SXSIStorageInterface();
65 XMLDocShredder::XMLDocShredder(const string inFileName)
68 reader_ = new TextReader(inFileName);
70 storageIfc_ = new SXSIStorageInterface();
74 XMLDocShredder::~XMLDocShredder()
82 void XMLDocShredder::processStartElement()
84 // fetch element name; this will be the full qualified name
85 ustring name = reader_->get_name();
88 storageIfc_->newText(buffer); //prevText
91 storageIfc_->newChild(name);
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();
100 // now, process attributes
101 if (reader_->has_attributes())
108 storageIfc_->newText(""); //myText
109 storageIfc_->nodeFinished(name);
115 void XMLDocShredder::processEndElement()
117 // tell the storage interface that the current node has been completely processed
118 storageIfc_->newText(buffer); //prevText
120 storageIfc_->nodeFinished(reader_->get_name());
123 void XMLDocShredder::processPCDATA()
125 // send the content of this PCDATA node to the storage interface as a text node
127 if (reader_->has_value())
129 buffer += reader_->get_value();
134 void XMLDocShredder::processAttributes()
136 reader_->move_to_first_attribute();
138 string nspaceStr = "xmlns";
139 storageIfc_->newText(""); //prevText
140 storageIfc_->newChild("<@>");
143 ustring name = reader_->get_name();
144 ustring value = reader_->get_value();
146 /* the libxml++ TextReader treats the xmlns attribute like an ordinary attribute,
147 * so we have to extract it and build a namespace uri node out of it before
148 * passing to the storage interface */
150 if ((name.find(nspaceStr.c_str(), 0, 5)) == 0)
152 storageIfc_->newChild(":" + value);
153 storageIfc_->nodeFinished(":" + value);
156 /* otherwise, this is an ordinary attribute, so we construct a new child node of the
157 * parent element to store the attribute name, possessing a child text node storing the
158 * attribute value. Then, we close off the attribute node with a call to nodeFinished()
163 storageIfc_->newText(""); //prevText
164 storageIfc_->newChild(name);
165 storageIfc_->newText(value);
166 storageIfc_->nodeFinished(name);
169 while (reader_->move_to_next_attribute());
170 storageIfc_->newText(""); //nextText
171 storageIfc_->nodeFinished("<@>");
174 void XMLDocShredder::processSignificantWhitespace()
176 // each significant whitespace sequence constructs a text node
177 buffer += reader_->get_value();
181 void XMLDocShredder::processStartDocument(const string docName)
183 // tell storage interface to construct the document name
184 storageIfc_->newChild("");
188 void XMLDocShredder::processEndDocument()
190 /* tell the storage interface that document parsing has finished, and structures
191 * can now be written to disk. */
192 storageIfc_->newText("");
193 storageIfc_->nodeFinished("");
194 storageIfc_->parsingFinished();
197 void XMLDocShredder::processComment()
199 //storageIfc_->newChild("!" + reader_->get_value());
200 //storageIfc_->nodeFinished();
203 void XMLDocShredder::processProcessingInstruction()
205 ustring name = reader_->get_name();
206 ustring value = reader_->get_value();
208 /* Create a child node to store the target of the PI, append a text node to it to store
209 * the PI data, send to the storage interface. Close off the PI node with a call to
213 // storageIfc_->newChild("?" + name);
214 // storageIfc_->newText(value);
215 // storageIfc_->nodeFinished();
218 void XMLDocShredder::processDocTypeDeclaration()
220 /* We currently ignore the DOCTYPE declaration, but we'll provide this method skeleton
221 * in case we do want to process it in the future.
225 void XMLDocShredder::processCDATASection()
227 /* Currently, we don't preserve CDATA sections since they aren't part of the XPath data
228 * model. Instead, we simply pass the converted text value to the storage interface as
229 * a text node attached to the current context node.
232 ustring value = reader_->get_value();
233 storageIfc_->newText(value);
238 void XMLDocShredder::processUnknownNodeType()
240 cout << "unknown token encountered during parsing" << endl;
241 throw xmlpp::parse_error("unknown token encountered during parsing");
245 void XMLDocShredder::parse()
247 while (reader_->read() && (reader_->get_read_state() != TextReader::Error))
249 switch (reader_->get_node_type())
251 case TextReader::Element:
252 processStartElement();
255 case TextReader::Text:
259 case TextReader::EndElement:
263 case TextReader::SignificantWhitespace:
264 processSignificantWhitespace();
267 case TextReader::Comment:
271 case TextReader::DocumentType:
272 processDocTypeDeclaration();
275 case TextReader::ProcessingInstruction:
276 processProcessingInstruction();
279 case TextReader::CDATA:
280 processCDATASection();
283 case TextReader::None:
284 processUnknownNodeType();
288 int type = reader_->get_node_type();
289 cout << " Node type: " << type << endl;