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();
208 if (name ==
"FirstElement")
247 if (name ==
"SecondElement")
290 if (name ==
"FifthElement")
334 if (name ==
"RootElement")
339 else if (name ==
"FirstElement")
345 else if (name ==
"SecondElement")
350 else if (name ==
"FifthElement")
369 if (
os_) *
os_ <<
"test()\n";
374 parse(is, rootHandler);
378 *
os_ <<
"root.param: " << root.
param << endl
380 <<
"first.text: " << root.
first.
text << 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());
585int main(
int argc,
char* argv[])
591 if (argc>1 && !strcmp(argv[1],
"-v"))
os_ = &cout;
int main(int argc, char *argv[])
void testSaxParserString()
void testNoAutoUnescape()
void readAttribute(const Handler::Attributes &attributes, const string &attributeName, string &result)
virtual Status startElement(const string &name, const Attributes &attributes, stream_offset position)
virtual Status startElement(const string &name, const Handler::Attributes &attributes, stream_offset position)
virtual Status endElement(const string &name, stream_offset position)
virtual Status characters(const SAXParser::saxstring &text, stream_offset position)
FifthHandler(Fifth &object, bool autoUnescapeAttributes, bool autoUnescapeCharacters)
virtual Status characters(const SAXParser::saxstring &text, stream_offset position)
virtual Status startElement(const string &name, const Handler::Attributes &attributes, stream_offset position)
FirstHandler(First &first, bool autoUnescapeAttributes, bool autoUnescapeCharacters)
virtual Status endElement(const string &name, stream_offset position)
virtual Status endElement(const string &name, stream_offset position)
PrintEventHandler(ostream &os)
virtual Status startElement(const string &name, const Attributes &attributes, stream_offset position)
virtual Status characters(const SAXParser::saxstring &text, stream_offset position)
virtual Status processingInstruction(const string &name, const string &value, stream_offset position)
FifthHandler fifthHandler_
virtual Status startElement(const string &name, const Attributes &attributes, stream_offset position)
FirstHandler firstHandler_
RootHandler(Root &root, bool autoUnescapeAttributes=true, bool autoUnescapeCharacters=true)
SecondHandler secondHandler_
virtual Status startElement(const string &name, const Handler::Attributes &attributes, stream_offset position)
SecondHandler(Second &object, bool autoUnescapeAttributes, bool autoUnescapeCharacters)
virtual Status characters(const SAXParser::saxstring &text, stream_offset position)
const char * getName() const
std::string getValue(XMLUnescapeBehavior_t Unescape=XMLUnescapeDefault) const
attribute_list::const_iterator begin() const
attribute_list::const_iterator end() const
attribute_list::const_iterator find(const std::string &name) const
SAX event handler interface.
bool parseCharacters
When false, no calls to characters() will be made.
bool autoUnescapeCharacters
bool autoUnescapeAttributes
Setting these to false will disable the auto-unescaping feature of the parser; this is useful for han...
T & getAttribute(const Attributes &attributes, const char *name, T &result, XMLUnescapeBehavior_t Unescape, T defaultValue=T()) const
boost::iostreams::stream_offset stream_offset
const char * c_str() const
An extended SAX interface for custom XML stream parsing.
PWIZ_API_DECL void parse(std::istream &is, Handler &handler)
Extract a single XML element from the istream, sending SAX events to the handler.
PWIZ_API_DECL std::string decode_xml_id_copy(const std::string &str)
Decodes any characters encoded with their hexadecimal value, e.g.
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.
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.
PWIZ_API_DECL std::string & decode_xml_id(std::string &str)
Decodes any characters encoded with their hexadecimal value, e.g.
virtual Status endElement(const string &name, stream_offset position)
void operator()(const Handler::Attributes::attribute &attr)
PrintAttribute(ostream &os)
Handler returns the Status struct as a means of changing the parser's behavior.
#define unit_assert_throws(x, exception)
#define unit_assert_operator_equal(expected, actual)
#define TEST_PROLOG(argc, argv)