///////////////////////////////////////////////////////////////////// // Element.cpp - Concrete Element class builds simple XML style // // ver 2.0 elements // // // // Language: Visual Studio.Net, using standard C++ // // Platform: Dell Dimension 8100, Windows 2000, SP 2 // // Application: Demonstration of Programming to Interface for // // CSE687, Spring 2003 // // Author: Jim Fawcett, CST 2-187, Syracuse University // // (315) 443-3948, jfawcett@twcny.rr.com // ///////////////////////////////////////////////////////////////////// #include "stdafx.h" #include #include "Element.h" using namespace std; //----< constructor provides tag >----------------------------------- Element::Element(const string &tag) : _tag(tag) { } ///////////////////////////////////////////////////////////////////// // copy constructor not needed - default works fine //Element::Element(const Element &e) // : _tag(e._tag), _attribs(e._attribs), _body(e._body) {} //----< add attribute name and value to class object >--------------- void Element::addAttribute(const string &name, const string &value) { _attribs[name] = value; } //----< add body string to object >---------------------------------- void Element::addBody(const string &body) { _body = body; } // //----< build an element string >------------------------------------ string Element::makeElement() { string elem = string("<") + _tag; if(_attribs.size() > 0) { map::iterator it; for(it=_attribs.begin(); it!=_attribs.end(); ++it) { elem += ' '; elem += (*it).first + string("=\"") + (*it).second + "\""; } } elem += '>'; elem += _body; elem += string(""; return elem; } //----< create new Element object - note: part of IElement >--------- // // Note: this is a member of IElement, not Element, but this // function has to be here so it sees the Element header. // IElement* IElement::CreateElement(const string &tag) { return new Element(tag); } // //----< test stub >-------------------------------------------------- #ifdef TEST_ELEMENT int _tmain(int argc, _TCHAR* argv[]) { cout << "\n Testing Element Class\n"; Element e1("first"); e1.addAttribute("title","first element"); e1.addAttribute("status","new"); e1.addBody("This is some boring text"); cout << "\n " << e1.makeElement(); Element e2("second"); e2.addBody("second dose of text"); cout << "\n " << e2.makeElement(); Element root("root"); root.addBody(e1.makeElement() + e2.makeElement()); cout << "\n " << root.makeElement(); cout << "\n\n"; return 0; } #endif