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 <libxml++/exceptions/parse_error.h>
26 void XMLDocShredder::doText(){
29 tb->NewOpenTag(PCDATA_OPEN_TAG);
31 tb->NewClosingTag(PCDATA_OPEN_TAG);
37 void XMLDocShredder::setProperties(){
38 /* instruct the parser to expand entity references and report as
41 reader_->set_parser_property(
42 TextReader::SubstEntities, true);
44 /* instruct parser to read external DTD, if present. This is
45 * needed to obtain any entity definitions in the DTD
47 reader_->set_parser_property(
48 TextReader::LoadDtd, true);
53 reader_->set_parser_property(
54 TextReader::DefaultAttrs, true);
57 /* but we don't want to do validation since it would slow us down
61 reader_->set_parser_property(
62 TextReader::Validate, false);
65 XMLDocShredder::XMLDocShredder(const unsigned char * data,
66 TextReader::size_type size,
72 reader_ = new TextReader(data,size,"");
74 tb = new XMLTreeBuilder();
76 tb->OpenDocument(iet,sf,dtc);
79 XMLDocShredder::XMLDocShredder(const string inFileName,int sf, bool iet, bool dtc)
82 reader_ = new TextReader(inFileName);
84 tb = new XMLTreeBuilder();
86 tb->OpenDocument(iet,sf,dtc);
89 XMLDocShredder::~XMLDocShredder()
99 void XMLDocShredder::processStartElement()
102 // fetch element name; this will be the full qualified name
103 ustring name = reader_->get_name();
105 size_t found = name.find_first_of(':');
106 if (found == ustring::npos)
107 tb->NewOpenTag(name);
109 tb->NewOpenTag(name.substr(found+1,name.length() - found - 1));
111 /* We must be really carefull here. calling process attributes moves
112 the document pointer on the last attribute, hence calling reader_->is_empty
113 afterwards will yield the wrong result. It is better to call it while we are
114 on the element and generate a nodeFinished() call at the end */
115 empty = reader_->is_empty_element();
118 // now, process attributes
119 if (reader_->has_attributes())
124 tb->NewClosingTag(name);
129 void XMLDocShredder::processEndElement()
132 ustring name = reader_->get_name();
133 tb->NewClosingTag(name);
136 void XMLDocShredder::processPCDATA()
138 // send the content of this PCDATA node to the storage interface as a text node
139 if (reader_->has_value())
140 buffer += reader_->get_value();
144 void XMLDocShredder::processAttributes()
146 reader_->move_to_first_attribute();
148 string nspaceStr = "xmlns";
149 tb->NewOpenTag(ATTRIBUTE_OPEN_TAG);
152 ustring name = reader_->get_name();
153 ustring value = reader_->get_value();
155 /* the libxml++ TextReader treats the xmlns attribute like an ordinary attribute,
156 * so we have to extract it and build a namespace uri node out of it before
157 * passing to the storage interface */
159 if ((name.find(nspaceStr.c_str(), 0, 5)) == 0)
164 /* otherwise, this is an ordinary attribute, so we construct a new child node of the
165 * parent element to store the attribute name, possessing a child text node storing the
166 * attribute value. Then, we close off the attribute node with a call to nodeFinished()
171 string attname = "<@>"+name;
172 tb->NewOpenTag(attname);
173 tb->NewOpenTag(ATTRIBUTE_DATA_OPEN_TAG);
175 tb->NewClosingTag(ATTRIBUTE_DATA_OPEN_TAG);
176 tb->NewClosingTag(attname);
179 while (reader_->move_to_next_attribute());
180 tb->NewClosingTag(ATTRIBUTE_OPEN_TAG);
183 void XMLDocShredder::processSignificantWhitespace()
185 if (reader_->has_value())
186 buffer += reader_->get_value();
190 void XMLDocShredder::processStartDocument(const string docName)
192 // tell storage interface to construct the document name
194 tb->NewOpenTag(DOCUMENT_OPEN_TAG);
198 void XMLDocShredder::processEndDocument()
201 /* tell the storage interface that document parsing has finished, and structures
202 * can now be written to disk. */
203 tb->NewClosingTag(DOCUMENT_OPEN_TAG);
204 tree = tb->CloseDocument();
208 void XMLDocShredder::processComment()
210 //storageIfc_->newChild("!" + reader_->get_value());
211 //storageIfc_->nodeFinished();
214 void XMLDocShredder::processProcessingInstruction()
216 ustring name = reader_->get_name();
217 ustring value = reader_->get_value();
219 /* Create a child node to store the target of the PI, append a text node to it to store
220 * the PI data, send to the storage interface. Close off the PI node with a call to
224 // storageIfc_->newChild("?" + name);
225 // storageIfc_->newText(value);
226 // storageIfc_->nodeFinished();
229 void XMLDocShredder::processDocTypeDeclaration()
231 /* We currently ignore the DOCTYPE declaration, but we'll provide this method skeleton
232 * in case we do want to process it in the future.
236 void XMLDocShredder::processCDATASection()
238 /* Currently, we don't preserve CDATA sections since they aren't part of the XPath data
239 * model. Instead, we simply pass the converted text value to the storage interface as
240 * a text node attached to the current context node.
242 if (reader_->has_value())
243 buffer+= reader_->get_value();
246 void XMLDocShredder::processUnknownNodeType()
248 cout << "unknown token encountered during parsing" << endl;
249 throw xmlpp::parse_error("unknown token encountered during parsing");
253 void XMLDocShredder::parse()
255 while (reader_->read() && (reader_->get_read_state() != TextReader::Error))
257 switch (reader_->get_node_type())
259 case TextReader::Element:
260 processStartElement();
263 case TextReader::Text:
267 case TextReader::EndElement:
271 case TextReader::SignificantWhitespace:
272 processSignificantWhitespace();
275 case TextReader::Comment:
279 case TextReader::DocumentType:
280 processDocTypeDeclaration();
283 case TextReader::ProcessingInstruction:
284 processProcessingInstruction();
287 case TextReader::CDATA:
288 processCDATASection();
291 case TextReader::None:
292 processUnknownNodeType();
296 int type = reader_->get_node_type();
297 cout << " Node type: " << type << endl;