00001
00002 #ifndef bxml_document_h_
00003 #define bxml_document_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <vcl_string.h>
00020 #include <vcl_sstream.h>
00021 #include <vcl_map.h>
00022 #include <vcl_vector.h>
00023 #include <vbl/vbl_ref_count.h>
00024 #include <vbl/vbl_smart_ptr.h>
00025 #include <vsl/vsl_binary_io.h>
00026
00027
00028
00029
00030 class bxml_data : public vbl_ref_count
00031 {
00032 public:
00033 enum datatype {TEXT, ELEMENT};
00034
00035 virtual ~bxml_data() {}
00036
00037
00038 virtual datatype type() const = 0;
00039 };
00040
00041
00042 bool operator==(const bxml_data& d1, const bxml_data& d2);
00043
00044
00045 typedef vbl_smart_ptr<bxml_data> bxml_data_sptr;
00046
00047
00048
00049 class bxml_text : public bxml_data
00050 {
00051 public:
00052
00053 bxml_text(const vcl_string& data) : data_(data) {}
00054
00055
00056 virtual ~bxml_text() {}
00057
00058
00059 datatype type() const { return TEXT; }
00060
00061
00062 vcl_string data() const { return data_; }
00063
00064
00065 void set_data(const vcl_string& data) { data_ = data; }
00066
00067 private:
00068 vcl_string data_;
00069 };
00070
00071
00072
00073 class bxml_element : public bxml_data
00074 {
00075 public:
00076 typedef vcl_vector<bxml_data_sptr>::const_iterator const_data_iterator;
00077 typedef vcl_map<vcl_string,vcl_string>::const_iterator const_attr_iterator;
00078
00079
00080 bxml_element() {}
00081
00082
00083 bxml_element(const vcl_string& name) : name_(name) {}
00084
00085
00086 virtual ~bxml_element() {}
00087
00088
00089 datatype type() const { return ELEMENT; }
00090
00091
00092 vcl_string name() const { return name_; }
00093
00094
00095 vcl_string attribute(const vcl_string& attr_name) const;
00096
00097
00098 bool get_attribute(const vcl_string& attr_name, vcl_string& value) const
00099 {
00100 value = this->attribute(attr_name);
00101 return true;
00102 }
00103
00104
00105
00106 template <class T>
00107 bool get_attribute(const vcl_string& attr_name, T& value) const
00108 {
00109 vcl_stringstream s(this->attribute(attr_name));
00110 if (s.str() == "")
00111 return false;
00112 s >> value;
00113 return true;
00114 }
00115
00116
00117 unsigned int num_attributes() const { return attributes_.size(); }
00118
00119
00120 const_attr_iterator attr_begin() const { return attributes_.begin(); }
00121
00122
00123 const_attr_iterator attr_end() const { return attributes_.end(); }
00124
00125
00126 unsigned int num_data() const { return data_.size(); }
00127
00128
00129 const_data_iterator data_begin() const { return data_.begin(); }
00130
00131
00132 const_data_iterator data_end() const { return data_.end(); }
00133
00134
00135 void append_text(const vcl_string& text);
00136
00137
00138 void append_data(const bxml_data_sptr& el)
00139 { data_.push_back(el); }
00140
00141 void set_attribute(const vcl_string& attr_name, const vcl_string& attr_value)
00142 { attributes_[attr_name] = attr_value; }
00143
00144
00145 template <class T>
00146 void set_attribute(const vcl_string& attr_name, const T& attr_value)
00147 {
00148 vcl_stringstream s;
00149 s << attr_value;
00150 attributes_[attr_name] = s.str();
00151 }
00152
00153 private:
00154
00155 vcl_string name_;
00156
00157
00158 vcl_map<vcl_string,vcl_string> attributes_;
00159
00160
00161 vcl_vector<bxml_data_sptr> data_;
00162 };
00163
00164
00165
00166 bool operator==(const bxml_element& e1, const bxml_element& e2);
00167
00168
00169
00170 class bxml_document : public vbl_ref_count
00171 {
00172 public:
00173
00174 bxml_document();
00175
00176
00177 ~bxml_document(){}
00178
00179
00180 bxml_data_sptr root_element() const {return root_element_;}
00181
00182
00183 vcl_string version() const { return version_; }
00184
00185
00186 vcl_string encoding() const { return encoding_; }
00187
00188
00189 bool standalone() const { return standalone_; }
00190
00191
00192 void set_root_element(const bxml_data_sptr& root)
00193 { root_element_ = root; }
00194
00195
00196 void set_version(const vcl_string& version) { version_ = version; }
00197
00198
00199 void set_encoding(const vcl_string& encoding) { encoding_ = encoding; }
00200
00201
00202 void set_standalone(bool standalone) { standalone_ = standalone; }
00203
00204 private:
00205
00206 bxml_data_sptr root_element_;
00207
00208 vcl_string version_;
00209 vcl_string encoding_;
00210 bool standalone_;
00211 };
00212
00213
00214
00215 typedef vbl_smart_ptr<bxml_document> bxml_document_sptr;
00216
00217
00218 bool operator==(const bxml_document& d1, const bxml_document& d2);
00219
00220
00221 void vsl_b_write(vsl_b_ostream & os, bxml_document const &ph);
00222 void vsl_b_read(vsl_b_istream & is, bxml_document &ph);
00223 void vsl_b_read(vsl_b_istream& is, bxml_document* ph);
00224 void vsl_b_write(vsl_b_ostream& os, const bxml_document* &ph);
00225
00226
00227 #endif // bxml_document_h_