40 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" 42 "<RootElement param=\"value\">\n" 43 " <FirstElement escaped_attribute=\""<&lt;>"\">\n" 44 " Some Text with Entity References: <&>\n" 46 " <SecondElement param2=\"something\" param3=\"something.else 1234-56\">\n" 47 " Pre-Text <Inline>Inlined text with <![CDATA[<&\">]]></Inline> Post-text. <br/>\n" 49 " <prefix:ThirdElement goober:name=\"value\">\n" 50 " <!--this is a comment-->\n" 51 " <empty_with_space />\n" 52 " </prefix:ThirdElement>\n" 53 " <FifthElement leeloo='>Leeloo > mul-\"tipass'>\n" 54 " You're a monster, Zorg.>I know.\n" 57 "<AnotherRoot>The quick brown fox jumps over the lazy dog.</AnotherRoot>\n";
89 os_ <<
"[0x" << hex << position <<
"] processingInstruction: (" << name <<
"," << value <<
")\n";
97 os_ <<
"[0x" << hex << position <<
"] startElement: " << name;
105 os_ <<
"[0x" << hex << position <<
"] endElement: " << name << endl;
111 os_ <<
"[0x" << hex << position <<
"] text: " << text << endl;
129 *
os_ <<
"first parse events:\n";
133 *
os_ <<
"second parse events:\n";
183 const string& attributeName,
186 Handler::Attributes::attribute_list::const_iterator it = attributes.
find(attributeName);
187 if (it != attributes.
end())
188 result = it->getValue();
199 parseCharacters =
true;
200 this->autoUnescapeAttributes = autoUnescapeAttributes;
201 this->autoUnescapeCharacters = autoUnescapeCharacters;
208 if (name ==
"FirstElement")
209 readAttribute(attributes,
"escaped_attribute", object_.escaped_attribute);
216 object_.text = text.
c_str();
238 parseCharacters =
true;
239 this->autoUnescapeAttributes = autoUnescapeAttributes;
240 this->autoUnescapeCharacters = autoUnescapeCharacters;
247 if (name ==
"SecondElement")
265 object_.text.push_back(text.
c_str());
281 parseCharacters =
true;
282 this->autoUnescapeAttributes = autoUnescapeAttributes;
283 this->autoUnescapeCharacters = autoUnescapeCharacters;
290 if (name ==
"FifthElement")
292 getAttribute(attributes,
"leeloo", object_.leeloo);
300 object_.mr_zorg = text.
c_str();
319 RootHandler(
Root& root,
bool autoUnescapeAttributes =
true,
bool autoUnescapeCharacters =
true)
321 firstHandler_(object_.first, autoUnescapeAttributes, autoUnescapeCharacters),
322 secondHandler_(object_.second, autoUnescapeAttributes, autoUnescapeCharacters),
323 fifthHandler_(object_.fifth, autoUnescapeAttributes, autoUnescapeCharacters)
325 parseCharacters =
true;
326 this->autoUnescapeAttributes = autoUnescapeAttributes;
327 this->autoUnescapeCharacters = autoUnescapeCharacters;
334 if (name ==
"RootElement")
339 else if (name ==
"FirstElement")
343 return Status(Status::Delegate, &firstHandler_);
345 else if (name ==
"SecondElement")
348 return Status(Status::Delegate, &secondHandler_);
350 else if (name ==
"FifthElement")
353 return Status(Status::Delegate, &fifthHandler_);
369 if (
os_) *
os_ <<
"test()\n";
374 parse(is, rootHandler);
378 *
os_ <<
"root.param: " << root.
param << endl
380 <<
"first.text: " << root.
first.
text << endl
385 *os_ <<
"\nfifth.leeloo: " << root.
fifth.
leeloo << endl
407 if (
os_) *
os_ <<
"testNoAutoUnescape()\n";
412 parse(is, rootHandler);
416 *
os_ <<
"root.param: " << root.
param << endl
418 <<
"first.text: " << root.
first.
text << endl
447 if (name ==
"AnotherRoot")
460 if (
os_) *
os_ <<
"testDone()\n";
468 getline(is, buffer,
'<');
470 if (
os_) *
os_ <<
"buffer: " << buffer <<
"\n\n";
477 if (
os_) *
os_ <<
"testBadXML()\n";
479 const char* bad =
"<A><B></A></B>";
480 istringstream is(bad);
489 if (
os_) *
os_ << e.what() <<
"\nOK: Parser caught bad XML.\n\n";
493 throw runtime_error(
"Parser failed to catch bad XML.");
512 if (
os_) *
os_ <<
"testNested()\n";
513 const char* nested =
"<a><a></a></a>";
514 istringstream is(nested);
517 parse(is, nestedHandler);
518 if (
os_) *
os_ <<
"count: " << nestedHandler.
count <<
"\n\n";
525 if (
os_) *
os_ <<
"testRootElement()\n";
527 string RootElement =
"RootElement";
530 istringstream sampleXMLStream(
sampleXML);
533 {ofstream sampleXMLFile(
"testRootElement.xml"); sampleXMLFile <<
sampleXML;}
535 bfs::remove(
"testRootElement.xml");
546 string id1(
"_x0031_invalid_x0020_ID");
551 string id2(
"_invalid-ID__x0023_2__x003c_3_x003e_");
555 string crazyId(
"_x0021__x0021__x0021_");
561 std::string str =
" \t foo \n";
575 std::string str2(xstr.
data());
577 std::string str3(xstr.
c_str());
585 int main(
int argc,
char* argv[])
591 if (argc>1 && !strcmp(argv[1],
"-v"))
os_ = &cout;
PWIZ_API_DECL std::string xml_root_element_from_file(const std::string &filepath)
Returns the root element from an XML file; throws runtime_error if no element is found.
virtual Status characters(const SAXParser::saxstring &text, stream_offset position)
const char * c_str() const
#define unit_assert_throws(x, exception)
virtual Status endElement(const string &name, stream_offset position)
SecondHandler secondHandler_
virtual Status characters(const SAXParser::saxstring &text, stream_offset position)
virtual Status endElement(const string &name, stream_offset position)
virtual Status startElement(const string &name, const Handler::Attributes &attributes, stream_offset position)
RootHandler(Root &root, bool autoUnescapeAttributes=true, bool autoUnescapeCharacters=true)
SAX event handler interface.
virtual Status startElement(const string &name, const Attributes &attributes, stream_offset position)
PrintAttribute(ostream &os)
PrintEventHandler(ostream &os)
attribute_list::const_iterator end() const
virtual Status endElement(const string &name, stream_offset position)
An extended SAX interface for custom XML stream parsing.
virtual Status startElement(const string &name, const Handler::Attributes &attributes, stream_offset position)
FirstHandler(First &first, bool autoUnescapeAttributes, bool autoUnescapeCharacters)
int main(int argc, char *argv[])
boost::iostreams::stream_offset stream_offset
PWIZ_API_DECL std::string xml_root_element(const std::string &fileheader)
Returns the root element from an XML buffer; throws runtime_error if no element is found...
void operator()(const Handler::Attributes::attribute &attr)
PWIZ_API_DECL std::map< std::string, std::string > parse(const std::string &id)
parses an id string into a map<string,string>
virtual Status startElement(const string &name, const Handler::Attributes &attributes, stream_offset position)
#define unit_assert_operator_equal(expected, actual)
Handler returns the Status struct as a means of changing the parser's behavior.
std::string getValue(XMLUnescapeBehavior_t Unescape=XMLUnescapeDefault) const
FifthHandler(Fifth &object, bool autoUnescapeAttributes, bool autoUnescapeCharacters)
FirstHandler firstHandler_
attribute_list::const_iterator begin() const
void testSaxParserString()
virtual Status characters(const SAXParser::saxstring &text, stream_offset position)
const char * getName() const
void readAttribute(const Handler::Attributes &attributes, const string &attributeName, string &result)
virtual Status processingInstruction(const string &name, const string &value, stream_offset position)
virtual Status endElement(const string &name, stream_offset position)
PWIZ_API_DECL Position position(CVID cvid=CVID_Unknown)
returns a Position corresponding to one of the following CVIDs: CVID_Unknown: Position::Anywhere MS_m...
virtual Status startElement(const string &name, const Attributes &attributes, stream_offset position)
#define TEST_PROLOG(argc, argv)
void testNoAutoUnescape()
virtual Status startElement(const string &name, const Attributes &attributes, stream_offset position)
PWIZ_API_DECL std::string value(const std::string &id, const std::string &name)
convenience function to extract a named value from an id string
PWIZ_API_DECL std::string & decode_xml_id(std::string &str)
Decodes any characters encoded with their hexadecimal value, e.g.
FifthHandler fifthHandler_
SecondHandler(Second &object, bool autoUnescapeAttributes, bool autoUnescapeCharacters)
attribute_list::const_iterator find(const std::string &name) const
PWIZ_API_DECL std::string decode_xml_id_copy(const std::string &str)
Decodes any characters encoded with their hexadecimal value, e.g.
virtual Status characters(const SAXParser::saxstring &text, stream_offset position)