core/vsl/vsl_basic_xml_element.cxx
Go to the documentation of this file.
00001 // This is core/vsl/vsl_basic_xml_element.cxx
00002 #include "vsl_basic_xml_element.h"
00003 //:
00004 // \file
00005 
00006 void vsl_basic_xml_element::add_attribute_list(vcl_vector<vcl_pair<vcl_string, vcl_string> > attrs)
00007 {
00008   for (unsigned int i=0; i<attrs.size(); i++) {
00009     attrs_.push_back(attrs[i]);
00010   }
00011 }
00012 
00013 void vsl_basic_xml_element::add_attribute(vcl_string attr_name, vcl_string value)
00014 {
00015   vcl_pair<vcl_string, vcl_string> attr(attr_name, value);
00016   attrs_.push_back(attr);
00017 }
00018 
00019 void vsl_basic_xml_element::add_attribute(vcl_string attr_name, double value)
00020 {
00021   vcl_string value_str = toString(value);
00022   vcl_pair<vcl_string, vcl_string> attr(attr_name, value_str.data());
00023   attrs_.push_back(attr);
00024 }
00025 
00026 void vsl_basic_xml_element::add_attribute(vcl_string attr_name, long value)
00027 {
00028   vcl_string value_str = toString(value); 
00029   vcl_pair<vcl_string, vcl_string> attr(attr_name, value_str);
00030   attrs_.push_back(attr);
00031 }
00032 
00033 void vsl_basic_xml_element::append_cdata(vcl_string cdata)
00034 {
00035   if (cdata_.size() > 0)
00036     cdata_.append(" ");
00037   cdata_.append(cdata);
00038 }
00039 
00040 void vsl_basic_xml_element::append_cdata(double cdata)
00041 {
00042   if (cdata_.size() > 0)
00043     cdata_.append(" ");
00044   cdata_.append(toString(cdata));
00045 }
00046 
00047 void vsl_basic_xml_element::append_cdata(int cdata)
00048 {
00049   if (cdata_.size() > 0)
00050     cdata_.append(" ");
00051   cdata_.append(toString(cdata));
00052 }
00053 
00054 #if 0
00055 bool vsl_basic_xml_element::delete_attribute(vcl_string /*attr_name*/)
00056 {
00057   vcl_cerr << "vsl_basic_xml_element::delete_attribute() not yet implemented\n";
00058   return false;
00059 }
00060 #endif
00061 
00062 void vsl_basic_xml_element::x_write(vcl_ostream& ostr)
00063 {
00064   // put the initial bracket with element name and the attribute-value list
00065   x_write_open(ostr);
00066 
00067   // put the character data between the tags
00068   if (cdata_.size() > 0)
00069     ostr << cdata_ << '\n';
00070 
00071   // close the element
00072   x_write_close(ostr);
00073 }
00074 
00075 void vsl_basic_xml_element::x_write_open(vcl_ostream& ostr)
00076 {
00077   ostr << '<' << tag_;
00078   for (unsigned int i=0; i<attrs_.size(); i++) {
00079     ostr << ' ' << attrs_[i].first << "=\"" << attrs_[i].second << '"';
00080   }
00081   ostr << ">\n";
00082 }
00083 
00084 //: writes the closing tag to the stream
00085 void vsl_basic_xml_element::x_write_close(vcl_ostream& ostr)
00086 {
00087   ostr << "</" << tag_ << ">\n";
00088 }