core/vsl/vsl_basic_xml_element.h
Go to the documentation of this file.
00001 // This is core/vsl/vsl_basic_xml_element.h
00002 #ifndef vsl_basic_xml_element_h_
00003 #define vsl_basic_xml_element_h_
00004 //:
00005 // \file
00006 // \brief  creates basic xml nodes and writes them out to the stream
00007 // A basic node contains only text content, and optionally has attribute(s).
00008 // \author Gamze Tunali (gamze@lems.brown.edu)
00009 // \date   Dec 21, 2005
00010 
00011 #include <vcl_vector.h>
00012 #include <vcl_utility.h>
00013 #include <vcl_string.h>
00014 #include <vcl_sstream.h>
00015 #include <vcl_iostream.h>
00016 #include <vcl_iomanip.h> // for vcl_fixed
00017 
00018 template<typename T> vcl_string toString(const T& t)
00019 {
00020   vcl_stringstream strm;
00021 
00022   strm << vcl_fixed << t;
00023   return strm.str();
00024 }
00025 
00026 class vsl_basic_xml_element
00027 {
00028  public:
00029   //: constructs with a name
00030   vsl_basic_xml_element(vcl_string tag)
00031     : tag_(tag) {}
00032 
00033   //: constructs with a name and a list of (attribute,value) pair
00034   vsl_basic_xml_element(vcl_string tag, vcl_vector<vcl_pair<vcl_string, vcl_string> > attrs)
00035     : tag_(tag), attrs_(attrs) {}
00036 
00037   //destructor
00038   ~vsl_basic_xml_element() {}
00039 
00040   //: overloaded methods to add attribute values
00041   void add_attribute(vcl_string attr_name, vcl_string value);
00042   void add_attribute(vcl_string attr_name, double value);
00043   void add_attribute(vcl_string attr_name, float value) { add_attribute(attr_name, (double)value); }
00044   void add_attribute(vcl_string attr_name, long value);
00045   void add_attribute(vcl_string attr_name, int value) { add_attribute(attr_name, (long)value); }
00046   void add_attribute(vcl_string attr_name, unsigned long value) { add_attribute(attr_name, (long)value); }
00047   void add_attribute(vcl_string attr_name, unsigned int value) { add_attribute(attr_name, (long)value); }
00048 
00049   void add_attribute_list(vcl_vector<vcl_pair<vcl_string, vcl_string> > attrs);
00050 #if 0
00051   bool delete_attribute(vcl_string attr_name);
00052 #endif
00053   void append_cdata(vcl_string cdata);
00054   void append_cdata(double cdata);
00055   void append_cdata(int cdata);
00056 
00057   void x_write(vcl_ostream& ostr);
00058   //: writes the opening tag for this node to the stream
00059   void x_write_open(vcl_ostream& ostr);
00060 
00061   //: writes the closing tag for this node to the stream
00062   void x_write_close(vcl_ostream& ostr);
00063 
00064  protected:
00065   vcl_string tag_; //!< the node name
00066   vcl_vector<vcl_pair<vcl_string, vcl_string> > attrs_; //!< the node attributes
00067   vcl_string cdata_; //!< the text() content of this node
00068 };
00069 
00070 #endif // vsl_basic_xml_element_h_