31 using namespace pwiz::util;
32 using namespace pwiz::minimxml;
33 using namespace pwiz::minimxml::SAXParser;
40 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
41 "<RootElement param=\"value\">\n"
42 " <FirstElement escaped_attribute=\""<&lt;>"\">\n"
43 " Some Text with Entity References: <&>\n"
45 " <SecondElement param2=\"something\" param3=\"something.else 1234-56\">\n"
46 " Pre-Text <Inline>Inlined text with <![CDATA[<&\">]]></Inline> Post-text. <br/>\n"
48 " <prefix:ThirdElement goober:name=\"value\">\n"
49 " <!--this is a comment-->\n"
50 " <empty_with_space />\n"
51 " </prefix:ThirdElement>\n"
52 " <FifthElement leeloo='>Leeloo > mul-\"tipass'>\n"
53 " You're a monster, Zorg.>I know.\n"
56 "<AnotherRoot>The quick brown fox jumps over the lazy dog.</AnotherRoot>\n";
84 virtual Status processingInstruction(
const string& name,
88 os_ <<
"[0x" << hex << position <<
"] processingInstruction: (" << name <<
"," << value <<
")\n";
92 virtual Status startElement(
const string& name,
96 os_ <<
"[0x" << hex << position <<
"] startElement: " << name;
104 os_ <<
"[0x" << hex << position <<
"] endElement: " << name << endl;
110 os_ <<
"[0x" << hex << position <<
"] text: " << text << endl;
128 *
os_ <<
"first parse events:\n";
132 *
os_ <<
"second parse events:\n";
182 const string& attributeName,
185 Handler::Attributes::attribute_list::const_iterator it = attributes.
find(attributeName);
186 if (it != attributes.
end())
187 result = it->getValue();
198 parseCharacters =
true;
199 this->autoUnescapeAttributes = autoUnescapeAttributes;
200 this->autoUnescapeCharacters = autoUnescapeCharacters;
203 virtual Status startElement(
const string& name,
207 if (name ==
"FirstElement")
208 readAttribute(attributes,
"escaped_attribute", object_.escaped_attribute);
215 object_.text = text.
c_str();
237 parseCharacters =
true;
238 this->autoUnescapeAttributes = autoUnescapeAttributes;
239 this->autoUnescapeCharacters = autoUnescapeCharacters;
242 virtual Status startElement(
const string& name,
246 if (name ==
"SecondElement")
264 object_.text.push_back(text.
c_str());
280 parseCharacters =
true;
281 this->autoUnescapeAttributes = autoUnescapeAttributes;
282 this->autoUnescapeCharacters = autoUnescapeCharacters;
285 virtual Status startElement(
const string& name,
289 if (name ==
"FifthElement")
291 getAttribute(attributes,
"leeloo", object_.leeloo);
299 object_.mr_zorg = text.
c_str();
318 RootHandler(
Root& root,
bool autoUnescapeAttributes =
true,
bool autoUnescapeCharacters =
true)
320 firstHandler_(object_.first, autoUnescapeAttributes, autoUnescapeCharacters),
321 secondHandler_(object_.second, autoUnescapeAttributes, autoUnescapeCharacters),
322 fifthHandler_(object_.fifth, autoUnescapeAttributes, autoUnescapeCharacters)
324 parseCharacters =
true;
325 this->autoUnescapeAttributes = autoUnescapeAttributes;
326 this->autoUnescapeCharacters = autoUnescapeCharacters;
329 virtual Status startElement(
const string& name,
333 if (name ==
"RootElement")
338 else if (name ==
"FirstElement")
342 return Status(Status::Delegate, &firstHandler_);
344 else if (name ==
"SecondElement")
347 return Status(Status::Delegate, &secondHandler_);
349 else if (name ==
"FifthElement")
352 return Status(Status::Delegate, &fifthHandler_);
368 if (
os_) *
os_ <<
"test()\n";
373 parse(is, rootHandler);
377 *
os_ <<
"root.param: " << root.
param << endl
379 <<
"first.text: " << root.
first.
text << endl
384 *os_ <<
"\nfifth.leeloo: " << root.
fifth.
leeloo << endl
406 if (
os_) *
os_ <<
"testNoAutoUnescape()\n";
411 parse(is, rootHandler);
415 *
os_ <<
"root.param: " << root.
param << endl
417 <<
"first.text: " << root.
first.
text << endl
442 virtual Status startElement(
const string& name,
446 if (name ==
"AnotherRoot")
459 if (
os_) *
os_ <<
"testDone()\n";
467 getline(is, buffer,
'<');
469 if (
os_) *
os_ <<
"buffer: " << buffer <<
"\n\n";
476 if (
os_) *
os_ <<
"testBadXML()\n";
478 const char* bad =
"<A><B></A></B>";
479 istringstream is(bad);
488 if (
os_) *
os_ << e.what() <<
"\nOK: Parser caught bad XML.\n\n";
492 throw runtime_error(
"Parser failed to catch bad XML.");
511 if (
os_) *
os_ <<
"testNested()\n";
512 const char* nested =
"<a><a></a></a>";
513 istringstream is(nested);
516 parse(is, nestedHandler);
517 if (
os_) *
os_ <<
"count: " << nestedHandler.
count <<
"\n\n";
524 if (
os_) *
os_ <<
"testRootElement()\n";
526 string RootElement =
"RootElement";
529 istringstream sampleXMLStream(
sampleXML);
532 {ofstream sampleXMLFile(
"testRootElement.xml"); sampleXMLFile <<
sampleXML;}
534 bfs::remove(
"testRootElement.xml");
545 string id1(
"_x0031_invalid_x0020_ID");
550 string id2(
"_invalid-ID__x0023_2__x003c_3_x003e_");
554 string crazyId(
"_x0021__x0021__x0021_");
560 std::string str =
" \t foo \n";
574 std::string str2(xstr.
data());
576 std::string str3(xstr.
c_str());
584 int main(
int argc,
char* argv[])
590 if (argc>1 && !strcmp(argv[1],
"-v"))
os_ = &cout;