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();
106 tb->NewOpenTag(name);
108 /* We must be really carefull here. calling process attributes moves
109 the document pointer on the last attribute, hence calling reader_->is_empty
110 afterwards will yield the wrong result. It is better to call it while we are
111 on the element and generate a nodeFinished() call at the end */
112 empty = reader_->is_empty_element();
115 // now, process attributes
116 if (reader_->has_attributes())
121 tb->NewClosingTag(name);
126 void XMLDocShredder::processEndElement()
129 ustring name = reader_->get_name();
130 tb->NewClosingTag(name);
133 void XMLDocShredder::processPCDATA()
135 // send the content of this PCDATA node to the storage interface as a text node
136 if (reader_->has_value())
137 buffer += reader_->get_value();
141 void XMLDocShredder::processAttributes()
143 reader_->move_to_first_attribute();
145 string nspaceStr = "xmlns";
146 tb->NewOpenTag(ATTRIBUTE_OPEN_TAG);
149 ustring name = reader_->get_name();
150 ustring value = reader_->get_value();
152 /* the libxml++ TextReader treats the xmlns attribute like an ordinary attribute,
153 * so we have to extract it and build a namespace uri node out of it before
154 * passing to the storage interface */
156 if ((name.find(nspaceStr.c_str(), 0, 5)) == 0)
161 /* otherwise, this is an ordinary attribute, so we construct a new child node of the
162 * parent element to store the attribute name, possessing a child text node storing the
163 * attribute value. Then, we close off the attribute node with a call to nodeFinished()
168 string attname = "<@>"+name;
169 tb->NewOpenTag(attname);
170 tb->NewOpenTag(ATTRIBUTE_DATA_OPEN_TAG);
172 tb->NewClosingTag(ATTRIBUTE_DATA_OPEN_TAG);
173 tb->NewClosingTag(attname);
176 while (reader_->move_to_next_attribute());
177 tb->NewClosingTag(ATTRIBUTE_OPEN_TAG);
180 void XMLDocShredder::processSignificantWhitespace()
182 if (reader_->has_value())
183 buffer += reader_->get_value();
187 void XMLDocShredder::processStartDocument(const string docName)
189 // tell storage interface to construct the document name
191 tb->NewOpenTag(DOCUMENT_OPEN_TAG);
195 void XMLDocShredder::processEndDocument()
198 /* tell the storage interface that document parsing has finished, and structures
199 * can now be written to disk. */
200 tb->NewClosingTag(DOCUMENT_OPEN_TAG);
201 tree = tb->CloseDocument();
205 void XMLDocShredder::processComment()
207 //storageIfc_->newChild("!" + reader_->get_value());
208 //storageIfc_->nodeFinished();
211 void XMLDocShredder::processProcessingInstruction()
213 ustring name = reader_->get_name();
214 ustring value = reader_->get_value();
216 /* Create a child node to store the target of the PI, append a text node to it to store
217 * the PI data, send to the storage interface. Close off the PI node with a call to
221 // storageIfc_->newChild("?" + name);
222 // storageIfc_->newText(value);
223 // storageIfc_->nodeFinished();
226 void XMLDocShredder::processDocTypeDeclaration()
228 /* We currently ignore the DOCTYPE declaration, but we'll provide this method skeleton
229 * in case we do want to process it in the future.
233 void XMLDocShredder::processCDATASection()
235 /* Currently, we don't preserve CDATA sections since they aren't part of the XPath data
236 * model. Instead, we simply pass the converted text value to the storage interface as
237 * a text node attached to the current context node.
239 if (reader_->has_value())
240 buffer+= reader_->get_value();
243 void XMLDocShredder::processUnknownNodeType()
245 cout << "unknown token encountered during parsing" << endl;
246 throw xmlpp::parse_error("unknown token encountered during parsing");
250 void XMLDocShredder::parse()
252 while (reader_->read() && (reader_->get_read_state() != TextReader::Error))
254 switch (reader_->get_node_type())
256 case TextReader::Element:
257 processStartElement();
260 case TextReader::Text:
264 case TextReader::EndElement:
268 case TextReader::SignificantWhitespace:
269 processSignificantWhitespace();
272 case TextReader::Comment:
276 case TextReader::DocumentType:
277 processDocTypeDeclaration();
280 case TextReader::ProcessingInstruction:
281 processProcessingInstruction();
284 case TextReader::CDATA:
285 processCDATASection();
288 case TextReader::None:
289 processUnknownNodeType();
293 int type = reader_->get_node_type();
294 cout << " Node type: " << type << endl;