contrib/brl/bbas/bxml/bxml_document.h

Go to the documentation of this file.
00001 // This is brl/bbas/bxml/bxml_document.h
00002 #ifndef bxml_document_h_
00003 #define bxml_document_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief An XML document representation
00010 // \author Matt Leotta (Brown)
00011 // \date   October 5, 2006
00012 //
00013 // \verbatim
00014 //  Modifications
00015 //   <none yet>
00016 // \endverbatim
00017 
00018 #include <vcl_string.h>
00019 #include <vcl_sstream.h>
00020 #include <vcl_map.h>
00021 #include <vcl_vector.h>
00022 #include <vbl/vbl_ref_count.h>
00023 #include <vbl/vbl_smart_ptr.h>
00024 
00025 
00026 //: A block of character data found within XML tags
00027 // This is a base class and can be either plain text or
00028 // an XML element
00029 class bxml_data : public vbl_ref_count
00030 {
00031  public:
00032   enum datatype {TEXT, ELEMENT};
00033 
00034   virtual ~bxml_data() {}
00035 
00036   //: Return the type of XML data
00037   virtual datatype type() const = 0;
00038 };
00039 
00040 //: compare two XML data objects
00041 bool operator==(const bxml_data& d1, const bxml_data& d2);
00042 
00043 
00044 typedef vbl_smart_ptr<bxml_data> bxml_data_sptr;
00045 
00046 
00047 //: text data within XML
00048 class bxml_text : public bxml_data
00049 {
00050  public:
00051   //: Constructor
00052   bxml_text(const vcl_string& data) : data_(data) {}
00053 
00054   //: Destructor
00055   virtual ~bxml_text() {}
00056 
00057   //: Return the type of XML data
00058   datatype type() const { return TEXT; }
00059 
00060   //: Access the text data
00061   vcl_string data() const { return data_; }
00062 
00063   //: Set the text data
00064   void set_data(const vcl_string& data) { data_ = data; }
00065 
00066  private:
00067   vcl_string data_;
00068 };
00069 
00070 
00071 //: An XML element
00072 class bxml_element : public bxml_data
00073 {
00074  public:
00075   typedef vcl_vector<bxml_data_sptr>::const_iterator const_data_iterator;
00076   typedef vcl_map<vcl_string,vcl_string>::const_iterator const_attr_iterator;
00077 
00078   //: Constructor - default
00079   bxml_element() {}
00080 
00081   //: Constructor
00082   bxml_element(const vcl_string& name) : name_(name) {}
00083 
00084   //: Destructor
00085   virtual ~bxml_element() {}
00086 
00087   //: Return the type of XML data
00088   datatype type() const { return ELEMENT; }
00089 
00090   //: Return the name of the element
00091   vcl_string name() const { return name_; }
00092 
00093   //: Return the value of an attribute
00094   vcl_string attribute(const vcl_string& attr_name) const;
00095 
00096   //: Return the value of an attribute
00097   template <class T>
00098   bool get_attribute(const vcl_string& attr_name, T& value) const
00099   {
00100     vcl_stringstream s(this->attribute(attr_name));
00101     if (s.str() == "")
00102       return false;
00103     s >> value;
00104     return true;
00105   }
00106 
00107   //: Return the number of attributes
00108   unsigned int num_attributes() const { return attributes_.size(); }
00109 
00110   //: An iterator to the beginning of the attributes
00111   const_attr_iterator attr_begin() const { return attributes_.begin(); }
00112 
00113   //: An iterator to the end of the attributes
00114   const_attr_iterator attr_end() const { return attributes_.end(); }
00115 
00116   //: Return the number of data nodes
00117   unsigned int num_data() const { return data_.size(); }
00118 
00119   //: An iterator to the beginning of the data
00120   const_data_iterator data_begin() const { return data_.begin(); }
00121 
00122   //: An iterator to the end of the data
00123   const_data_iterator data_end() const { return data_.end(); }
00124 
00125   //: Append text in this element
00126   void append_text(const vcl_string& text);
00127 
00128   //: Append data (typically another element) in this element
00129   void append_data(const bxml_data_sptr& el)
00130   { data_.push_back(el); }
00131 
00132   void set_attribute(const vcl_string& attr_name, const vcl_string& attr_value)
00133   { attributes_[attr_name] = attr_value; }
00134 
00135   template <class T>
00136   void set_attribute(const vcl_string& attr_name, const T& attr_value)
00137   {
00138     vcl_stringstream s;
00139     s << attr_value;
00140     attributes_[attr_name] = s.str();
00141   }
00142 
00143  private:
00144   //: The name of the element
00145   vcl_string name_;
00146 
00147   //: The map of attributes to values
00148   vcl_map<vcl_string,vcl_string> attributes_;
00149 
00150   //: The character data.
00151   vcl_vector<bxml_data_sptr> data_;
00152 };
00153 
00154 
00155 //: compare two XML element objects
00156 bool operator==(const bxml_element& e1, const bxml_element& e2);
00157 
00158 
00159 //: Represents a full XML document stored as a tree
00160 class bxml_document
00161 {
00162  public:
00163   //: Constructor - default
00164   bxml_document();
00165 
00166   //: Destructor
00167   ~bxml_document(){}
00168 
00169   //: Return the root element
00170   bxml_data_sptr root_element() const {return root_element_;}
00171 
00172   //: Return the version string
00173   vcl_string version() const { return version_; }
00174 
00175   //: Return the encoding string
00176   vcl_string encoding() const { return encoding_; }
00177 
00178   //: Return the standalone bit
00179   bool standalone() const { return standalone_; }
00180 
00181   //: set the root element
00182   void set_root_element(const bxml_data_sptr& root)
00183   { root_element_ = root; }
00184 
00185   //: Set the version string
00186   void set_version(const vcl_string& version) { version_ = version; }
00187 
00188   //: Set the encoding string
00189   void set_encoding(const vcl_string& encoding) { encoding_ = encoding; }
00190 
00191   //: Set the standalone bit
00192   void set_standalone(bool standalone) { standalone_ = standalone; }
00193 
00194  private:
00195   //: The root element
00196   bxml_data_sptr root_element_;
00197 
00198   vcl_string version_;
00199   vcl_string encoding_;
00200   bool standalone_;
00201 };
00202 
00203 
00204 //: compare two XML documents
00205 bool operator==(const bxml_document& d1, const bxml_document& d2);
00206 
00207 
00208 #endif // bxml_document_h_

Generated on Sun Sep 7 05:21:10 2008 for contrib/brl/bbas/bxml by  doxygen 1.5.1