// XmlElement_t.cxx #include "dataset_util/XmlElement.h" #include #include using std::string; using std::ostream; using std::cout; using std::endl; using std::vector; namespace { void msg(const char* msg) { cout << "----- "; cout << msg; cout << " -----" << endl; } string txt1 = "Once upon a time, there were three little pigs."; class Chapter { private: int _num; string _title; public: Chapter(int num, string title) : _num(num), _title(title) { } const XmlElement* xml() const { XmlElement* pchap = new XmlElement("Chapter"); pchap->add_attribute_as_int("number", _num); pchap->add_attribute("title", _title); return pchap; } }; class Book { private: string _title; int _edition; string _pub; bool _pb; vector _refs; public: Book(string title, int edition, string pub, bool pb) : _title(title), _edition(edition), _pub(pub), _pb(pb) { } void add_reference(string ref) { _refs.push_back(ref); } const XmlElement* xml() const { XmlElement* pbook = new XmlElement("Book"); return pbook; } }; int XmlElement_t() { msg("Create blank"); string blank = ""; XmlElement emp_ele(blank); cout << emp_ele << endl; assert( ! emp_ele.is_valid() ); msg("Create invalid"); XmlElement bad_ele; cout << bad_ele << endl; assert( ! bad_ele.is_valid() ); msg("Create"); XmlElement ele("Book"); size_t oldsize = 0; size_t newsize = 0; { cout << ele << endl; assert( ele.is_valid() ); assert( ele.name() == "Book" ); assert( ele.id() == "" ); assert( ele.reference_names().size() == 0 ); newsize = ele.memsize(); cout << "memsize = " << newsize << endl; assert( newsize > oldsize ); oldsize = newsize; } msg("Add ID"); { int stat = ele.set_id("ISBN_0-201-88954-4"); assert( stat == 0 ); cout << ele << endl; assert( ele.id() == "ISBN_0-201-88954-4" ); newsize = ele.memsize(); cout << "memsize = " << newsize << endl; assert( newsize > oldsize ); oldsize = newsize; } msg("Add references"); { int stat = ele.add_reference("Waite", "ISBN_0-672-30319-1"); assert( stat == 0 ); stat = ele.add_reference("Alhir", "ISBN_1-56592-448-7"); assert( stat == 0 ); cout << ele << endl; assert( ele.reference_names().size() == 2 ); assert( ele.has_reference("Waite") ); assert( ele.has_reference("Alhir") ); assert( ! ele.has_reference("whooo") ); assert( ele.reference_names().front() == "Waite" ); assert( ele.reference_names().back() == "Alhir" ); assert( ele.reference("Waite") == "ISBN_0-672-30319-1" ); assert( ele.reference("Alhir") == "ISBN_1-56592-448-7" ); newsize = ele.memsize(); cout << "memsize = " << newsize << endl; assert( newsize > oldsize ); oldsize = newsize; } msg("Add attributes"); { ele.add_attribute("title", "The \"C++\" and \"C&\" Programming Languages"); ele.add_attribute_as_int("edition", 3); ele.add_attribute("publisher", "Addison-Wesley"); ele.add_attribute_as_bool("paperback", true); cout << ele << endl; assert( ele.attribute_names().size() == 4 ); assert( ele.has_attribute("title") ); assert( ele.has_attribute("edition") ); assert( ele.has_attribute("publisher") ); assert( ele.has_attribute("paperback") ); assert( ! ele.has_attribute("crap") ); string text = "The \"C++\" and \"C&\" Programming Languages"; cout << text << endl; cout << ele.attribute("title") << endl; assert( ele.attribute("title") == text); cout << ele.attribute_as_int("edition") << endl; assert( ele.attribute_as_int("edition") == 3 ); assert( ele.attribute("publisher") == "Addison-Wesley"); cout << ele.attribute_as_bool("paperback") << endl; assert( ele.attribute_as_bool("paperback") ); newsize = ele.memsize(); cout << "memsize = " << newsize << endl; assert( newsize > oldsize ); oldsize = newsize; } msg("Add PC data"); ele.set_pcdata(txt1); msg("Add elements"); { XmlElement* ptoc = new XmlElement("TableOfContents"); ele.add_child(ptoc); ele.add_child(Chapter(1, "Notes to the Reader").xml()); ele.add_child(Chapter(2, "A Tour of C++").xml()); ele.add_child(Chapter(3, "A Tour of the Standard Library").xml()); cout << ele << endl; const XmlElement::ElementList& eles = ele.children(); assert( eles.size() == 4 ); assert( ele.has_children("TableOfContents") == 1 ); assert( ele.has_children("Chapter") == 3 ); assert( ele.single_child("TableOfContents") == ptoc ); assert( ele.single_child("Junk") == 0 ); assert( ele.single_child("Chapter") == 0 ); XmlElement::ElementList chaps = ele.children("Chapter"); assert( chaps.size() == 3 ); int ic = 0; for ( XmlElement::ElementList::const_iterator iele = chaps.begin(); iele!=chaps.end(); ++iele ) { const XmlElement& chap = **iele; assert( chap.name() == "Chapter" ); ++ic; assert( chap.attribute_as_int("number") == ic ); } assert( ic == 3 ); newsize = ele.memsize(); cout << "memsize = " << newsize << endl; assert( newsize > oldsize ); oldsize = newsize; } msg("Copy"); { XmlElement ele2 = ele; cout << ele2 << endl; assert( ele2.name() == ele.name() ); const XmlElement::ElementList& eles = ele2.children(); assert( eles.size() == 4 ); assert( ele.has_children("TableOfContents") == 1 ); assert( ele.has_children("Chapter") == 3 ); assert( ele.single_child("Junk") == 0 ); } msg("Assign"); { XmlElement ele2; ele2 = ele; cout << ele2 << endl; assert( ele2.name() == ele.name() ); const XmlElement::ElementList& eles = ele2.children(); assert( eles.size() == 4 ); assert( ele.has_children("TableOfContents") == 1 ); assert( ele.has_children("Chapter") == 3 ); assert( ele.single_child("Junk") == 0 ); } // Write xml. string sxml = ele.to_xml_text(); cout << sxml; assert( sxml.size() > 100 ); assert( sxml.find('\n') != string::npos ); // Write w/o CR's. string lxml = ele.to_xml_text("NOCR"); cout << lxml; assert( lxml.size() > 100 ); assert( lxml.find('\n') == string::npos ); return 0; } } // end unnamed namespace #ifdef CTEST_MAIN int main() { return XmlElement_t(); } #endif