00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 
00043 
00044 
00045 
00046 
00047 
00048 
00049 
00050 
00051 
00052 
00053 
00054 
00055 
00056 
00057 
00058 
00059 
00060 
00061 
00062 
00063 
00064 
00065 
00066 
00067 
00068 
00069 
00070 
00071 
00072 
00073 
00074 
00075 
00076 
00077 
00078 
00079 
00080 
00081 
00082 
00083 
00084 
00085 
00086 
00087 
00088 
00089 
00090 
00091 
00092 
00093 
00094 
00095 
00096 
00097 
00098 
00099 
00100 
00101 #ifndef XIMOL_ATTRIBUTES_HPP_
00102 #define XIMOL_ATTRIBUTES_HPP_
00103 
00104 #include <ximol/stop_warnings.hpp>
00105 #include <ximol/export_defs.hpp>
00106 #include <ximol/str_cast.hpp>
00107 #include <ximol/typedefs.hpp>
00108 
00109 #include <map>
00110 
00111 
00112 
00113 XIMOL_BEGIN_NAMESPACE
00114         class xistream;
00115         class xostream;
00116         class xistringstream;
00117         class xostringstream;
00118 XIMOL_END_NAMESPACE
00119 
00120 
00121 XIMOL_XML_BEGIN_NAMESPACE
00122 
00123 
00124 
00125 
00126 
00127 
00128 
00129 
00130 
00131 
00132  
00133 
00134 class XIMOL_EXPORT attributes {
00135 public: 
00136         typedef xstring attribute_type; 
00137 
00138 private: 
00139         typedef struct XIMOL_EXPORT ::std::pair<xstring, xstring> qname_type;
00140 
00141     typedef class XIMOL_EXPORT ::std::map<qname_type, attribute_type> map_type;
00142 
00143         typedef class XIMOL_EXPORT ::std::map<
00144                 xstring,        
00145                 xstring         
00146         > map_uri_type;
00147 
00148 public: 
00149         typedef map_type::const_iterator const_iterator;
00150     typedef map_type::iterator iterator;
00151 
00152     typedef map_uri_type::const_iterator const_ns_iterator;
00153     typedef map_uri_type::iterator ns_iterator;
00154 
00155 private: 
00156         static xstring const shortcut_default_uri_;
00157 
00158 public: 
00159     attributes();
00160 
00161     attributes(attributes const& rhs);
00162 
00163     ~attributes();
00164 
00165 public: 
00166 
00167 
00168 
00169 
00170 
00171         void swap(attributes& rhs) throw();
00172 
00173 
00174 
00175 
00176         attributes& operator=(attributes const& rhs) {
00177                 attributes temp(rhs);
00178                 swap(temp);
00179                 return *this;
00180         }
00181 
00182 
00183 
00184         void clear();
00185 
00186 
00187 
00188         size_t size() const                                             { return map_.size(); }
00189 
00190 
00191 
00192 
00193         iterator begin()                                                { return map_.begin(); }
00194         const_iterator  begin() const                   { return map_.begin(); }
00195 
00196         iterator end()                                                  { return map_.end(); }
00197         const_iterator end() const                              { return map_.end(); }
00198 
00199 
00200         ns_iterator ns_begin()                                  { return map_uri_.begin(); }
00201         const_ns_iterator ns_begin() const              { return map_uri_.begin(); }
00202 
00203         ns_iterator ns_end()                                    { return map_uri_.end(); }
00204         const_ns_iterator ns_end() const                { return map_uri_.end(); }
00205 
00206 
00207 
00208 
00209 
00210 
00211 
00212         attributes& operator+=(attributes const& rhs);
00213 
00214 
00215 
00216         const_ns_iterator find_namespace(xstring const& short_ns) const;
00217 
00218 
00219         const_ns_iterator find_short_namespace(xstring const& uri) const;
00220 
00221 
00222 
00223         std::pair<ns_iterator, bool> 
00224         insert_namespace(xstring const& short_ns, xstring const& uri);
00225 
00226 
00227 
00228         const_ns_iterator find_namespace() const        { return find_namespace(shortcut_default_uri_); }
00229 
00230 
00231         void set_namespace(xstring const& uri)          { insert_namespace(shortcut_default_uri_, uri); }
00232 
00233         
00234 
00235 
00236 
00237         std::pair<iterator, bool> 
00238         insert(
00239 
00240                 xstring const& namespace_name,
00241 
00242                 xstring const& name,
00243 
00244                 xstring const& value);
00245 
00246 
00247         void 
00248         set(
00249 
00250                 xstring const& namespace_name,
00251 
00252                 xstring const& name,
00253 
00254                 xstring const& value);
00255 
00256 
00257         const_iterator find(
00258 
00259                 xstring const& namespace_name,
00260 
00261                 xstring const& name) const;
00262 
00263 
00264 
00265         xostream& write(xostream& xos) const;
00266 
00267 
00268         xistream& read(xistream& xis);
00269 
00270 private: 
00271 
00272         map_type map_;
00273 
00274 
00275         map_uri_type map_uri_;
00276 }; 
00277 
00278 
00279 
00280 
00281 
00282 inline xostream& operator<<(xostream& xos, attributes const& rhs)
00283 {
00284         return rhs.write(xos);
00285 }
00286 
00287 
00288 
00289 
00290 inline xistream& operator>>(xistream& xis, attributes& rhs)
00291 {
00292         return rhs.read(xis);
00293 }
00294 
00295 
00296 
00297 
00298 XIMOL_EXPORT ::std::ostream& operator<<(::std::ostream& os, attributes const& x);
00299 
00300 XIMOL_XML_END_NAMESPACE
00301 
00302 XIMOL_BEGIN_NAMESPACE
00303 
00304 
00305 
00306 
00307 
00308 
00309 
00310 
00311 
00312 
00313 
00314 
00315 
00316 
00317 
00318 
00319 
00320 template<typename StringT1, typename StringT2, typename T>
00321 std::pair<xml::attributes::iterator, bool> 
00322 insert_attribute(xml::attributes& att,
00323                                  StringT1 const& namespace_name,
00324                                  StringT2 const& name,
00325                                  T const& value)
00326 {
00327         return att.insert(
00328                 str<xstring>::cast(namespace_name),
00329                 str<xstring>::cast(name),
00330         str<xstring>::cast(value));
00331 }
00332 
00333 
00334 
00335 
00336 
00337 
00338 
00339 
00340 template<typename StringT, typename T>
00341 std::pair<xml::attributes::iterator, bool> 
00342 insert_attribute(xml::attributes& att,
00343                                  StringT const& name,
00344                                  T const& value)
00345 {
00346     return insert_attribute(att, L"", name, value);
00347 }
00348 
00349 
00350 
00351 
00352 template<typename StringT1, typename StringT2, typename T>
00353 void 
00354 set_attribute(xml::attributes& att,
00355                                  StringT1 const& namespace_name,
00356                                  StringT2 const& name,
00357                                  T const& value)
00358 {
00359         att.set(
00360                 str<xstring>::cast(namespace_name),
00361                 str<xstring>::cast(name),
00362         str<xstring>::cast(value));
00363 }
00364 
00365 
00366 
00367 
00368 template<typename StringT, typename T>
00369 void
00370 set_attribute(xml::attributes& att,
00371                                  StringT const& name,
00372                                  T const& value)
00373 {
00374     set_attribute(att, L"", name, value);
00375 }
00376 
00377 
00378 
00379 
00380 
00381 
00382 template<typename StringT1, typename StringT2, typename T>
00383 bool get_attribute(xml::attributes const& att,
00384                                    StringT1 const& namespace_name,
00385                                    StringT2 const& name,
00386 
00387 
00388 
00389 
00390                                    T& value)
00391 {
00392         xml::attributes::const_iterator const i_value = att.find(
00393                 str< ::std::wstring>::cast(namespace_name),
00394                 str< ::std::wstring>::cast(name));
00395 
00396         if(i_value != att.end()) {
00397                 ::std::basic_istringstream<xchar_t> is(i_value->second+L" *");
00398 
00399                 T temp(value); 
00400                 is >> temp;
00401 
00402                 if(is.good()) {
00403                         std::swap(temp, value);
00404                         return true;
00405                 }
00406         }
00407 
00408         return false;
00409 }
00410 
00411 
00412 
00413 
00414 template<typename StringT, typename T>
00415 bool get_attribute(xml::attributes const& att, StringT const& name, T& value)
00416 {
00417         return get_attribute(att, L"", name, value);
00418 }
00419 
00420 
00421 
00422 
00423 template<typename StringT1, typename StringT2 >
00424 xstring get_raw_attribute(xml::attributes const& att, StringT1 const& namespace_name, StringT2 const& name)
00425 {
00426         xml::attributes::const_iterator const i_value = att.find(
00427                 str< ::std::wstring>::cast(namespace_name),
00428                 str< ::std::wstring>::cast(name));
00429 
00430         if(i_value != att.end()) return i_value->second;
00431 
00432         return L"";
00433 }
00434 
00435 
00436 
00437 
00438 template<typename StringT>
00439 xstring get_raw_attribute(xml::attributes const& att, StringT const& name)
00440 {
00441         return get_raw_attribute(att, L"", name);
00442 }
00443 
00444 
00445 
00446 
00447 template < typename T>
00448 struct extract_attribute
00449 {
00450     template <typename StringT>
00451     static T get(xml::attributes const& att, StringT const& name)
00452     {
00453         T result;
00454         get_attribute(att, name, result);
00455         return result;
00456     };
00457     template <typename StringT1, typename StringT2>
00458     static T get(xml::attributes const& att, StringT1 const& namespace_name, StringT2 const& name)
00459     {
00460         T result;
00461         get_attribute(att, namespace_name, name, result);
00462         return result;
00463     };
00464 };
00465 
00466 
00467 
00468 
00469 
00470 
00471 template <>
00472 struct extract_attribute<std::string>
00473 {
00474     template <typename StringT>
00475     static std::string get(xml::attributes const& att, StringT const& name)
00476     {
00477         return str<std::string>::cast(get_raw_attribute(att, name));
00478     };
00479     template <typename StringT1, typename StringT2>
00480     static std::string get(xml::attributes const& att, StringT1 const& namespace_name, StringT2 const& name)
00481     {
00482         return str<std::string>::cast(get_raw_attribute(att, namespace_name, name));
00483     };
00484 };
00485 
00486 
00487 
00488 
00489 
00490 
00491 template <>
00492 struct extract_attribute<std::wstring>
00493 {
00494     template <typename StringT>
00495     static std::wstring get(xml::attributes const& att, StringT const& name)
00496     {
00497         return str<std::wstring>::cast(get_raw_attribute(att, name));
00498     };
00499     template <typename StringT1, typename StringT2>
00500     static std::wstring get(xml::attributes const& att, StringT1 const& namespace_name, StringT2 const& name)
00501     {
00502         return str<std::wstring>::cast(get_raw_attribute(att, namespace_name, name));
00503     };
00504 };
00505 
00506 
00507 
00508 
00509 
00510 
00511 
00512 
00513 template < typename String1, typename String2>
00514 bool set_namespace(xml::attributes & att, String1 & short_namespace, const String2 & uri)
00515 {
00516     return att.insert_namespace(str< ::std::wstring>::cast(short_namespace), str< ::std::wstring>::cast(uri)).second;
00517 }
00518 
00519 
00520 
00521 
00522 
00523 
00524 
00525 template < typename String >
00526 void set_namespace(xml::attributes & att, const String & uri)
00527 {
00528     att.set_namespace(str< ::std::wstring>::cast(uri));
00529 }
00530 
00531 
00532 
00533 
00534 
00535 
00536 
00537 
00538 template < typename String >
00539 xstring get_namespace(xml::attributes & att, String & short_namespace)
00540 {
00541     xml::attributes::const_ns_iterator i=att.find_namespace(str< ::std::wstring>::cast(short_namespace));
00542     if (i==att.ns_end())
00543         return str<xstring>::cast(short_namespace);
00544     return i->first;
00545 }
00546 
00547 
00548 
00549 
00550 
00551 
00552 
00553 inline xstring get_namespace(xml::attributes & att)
00554 {
00555     xml::attributes::const_ns_iterator i=att.find_namespace();
00556     if (i==att.ns_end())
00557         return L"";
00558     return i->first;
00559 }
00560 
00561 
00562 
00563 
00564 
00565 
00566 
00567 
00568 
00569 
00570 template < typename String >
00571 xstring get_short_namespace(xml::attributes & att, String & uri)
00572 {
00573     xml::attributes::const_ns_iterator i=att.find_short_namespace(str< ::std::wstring>::cast(uri));
00574     if (i==att.ns_end())
00575         return str<xstring>::cast(uri);
00576     return i->second;
00577 }
00578 
00579 XIMOL_END_NAMESPACE
00580 
00581 #endif // #ifndef XIMOL_ATTRIBUTES_HPP_