contrib/brl/bbas/bxml/bxml_document.cxx
Go to the documentation of this file.
00001 // This is brl/bbas/bxml/bxml_document.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author Matt Leotta
00008 // \date   October 5, 2006
00009 
00010 #include "bxml_document.h"
00011 
00012 
00013 //: Return the value of an attribute
00014 vcl_string
00015 bxml_element::attribute(const vcl_string& attr_name) const
00016 {
00017   vcl_map<vcl_string,vcl_string>::const_iterator result = attributes_.find(attr_name);
00018   if (result != attributes_.end())
00019     return result->second;
00020   return "";
00021 }
00022 
00023 
00024 //: Constructor - default
00025 bxml_document::bxml_document()
00026   : root_element_(NULL),
00027     version_("1.0"),
00028     encoding_("UTF-8"),
00029     standalone_(true)
00030 {
00031 }
00032 
00033 
00034 //: Append text in this element
00035 void bxml_element::append_text(const vcl_string& text)
00036 {
00037   bxml_text * last_text = NULL;
00038   if (!data_.empty()){
00039     bxml_data_sptr last_data = data_.back();
00040     if (last_data->type() == bxml_data::TEXT)
00041       last_text = static_cast<bxml_text*>(last_data.ptr());
00042   }
00043 
00044   if (last_text)
00045     last_text->set_data(last_text->data() + text);
00046   else
00047     data_.push_back(new bxml_text(text));
00048 }
00049 
00050 
00051 //: compare two XML data objects
00052 bool operator==(const bxml_data& d1, const bxml_data& d2)
00053 {
00054   if (d1.type() != d2.type())
00055     return false;
00056 
00057   if (d1.type() == bxml_data::TEXT){
00058     const bxml_text& text1 = static_cast<const bxml_text&>(d1);
00059     const bxml_text& text2 = static_cast<const bxml_text&>(d2);
00060     return text1.data() == text2.data();
00061   }
00062 
00063   if (d1.type() == bxml_data::ELEMENT){
00064     const bxml_element& element1 = static_cast<const bxml_element&>(d1);
00065     const bxml_element& element2 = static_cast<const bxml_element&>(d2);
00066     return element1 == element2;
00067   }
00068 
00069   return false;
00070 }
00071 
00072 
00073 //: compare two XML element objects
00074 bool operator==(const bxml_element& e1, const bxml_element& e2)
00075 {
00076   if (e1.name() != e2.name())
00077     return false;
00078 
00079   if (e1.num_attributes() != e2.num_attributes())
00080     return false;
00081 
00082   bxml_element::const_attr_iterator a1 = e1.attr_begin();
00083   bxml_element::const_attr_iterator a2 = e2.attr_begin();
00084   for (; a1 != e1.attr_end();  ++a1, ++a2)
00085   {
00086     if (a1->first != a2->first || a1->second != a2->second)
00087       return false;
00088   }
00089 
00090   if (e1.num_data() != e2.num_data())
00091     return false;
00092 
00093   bxml_element::const_data_iterator d1 = e1.data_begin();
00094   bxml_element::const_data_iterator d2 = e2.data_begin();
00095   for (; d1 != e1.data_end();  ++d1, ++d2)
00096   {
00097     if (!(**d1 == **d2))
00098       return false;
00099   }
00100   return true;
00101 }
00102 
00103 
00104 //: compare two XML documents
00105 bool operator==(const bxml_document& d1, const bxml_document& d2)
00106 {
00107   if (d1.version() != d2.version() ||
00108       d1.encoding() != d2.encoding() ||
00109       d1.standalone() != d2.standalone())
00110     return false;
00111 
00112   return *d1.root_element() == *d2.root_element();
00113 }
00114 
00115 //: Binary io, NOT IMPLEMENTED, signatures defined to use bxml_document as a brdb_value
00116 void vsl_b_write(vsl_b_ostream & /*os*/, bxml_document const & /*ph*/)
00117 {
00118   vcl_cerr << "vsl_b_write() -- Binary io, NOT IMPLEMENTED, signatures defined to use bxml_document as a brdb_value\n";
00119   return;
00120 }
00121 
00122 void vsl_b_read(vsl_b_istream & /*is*/, bxml_document & /*ph*/)
00123 {
00124   vcl_cerr << "vsl_b_read() -- Binary io, NOT IMPLEMENTED, signatures defined to use bxml_document as a brdb_value\n";
00125   return;
00126 }
00127 
00128 void vsl_b_read(vsl_b_istream& is, bxml_document* ph)
00129 {
00130   delete ph;
00131   bool not_null_ptr;
00132   vsl_b_read(is, not_null_ptr);
00133   if (not_null_ptr)
00134   {
00135     ph = new bxml_document();
00136     vsl_b_read(is, *ph);
00137   }
00138   else
00139     ph = 0;
00140 }
00141 
00142 void vsl_b_write(vsl_b_ostream& os, const bxml_document* &ph)
00143 {
00144   if (ph==0)
00145   {
00146     vsl_b_write(os, false); // Indicate null pointer stored
00147   }
00148   else
00149   {
00150     vsl_b_write(os,true); // Indicate non-null pointer stored
00151     vsl_b_write(os,*ph);
00152   }
00153 }
00154 
00155