00001 /* 00002 * Copyright (C) 2001-2003 Peter J Jones (pjones@pmade.org) 00003 * All Rights Reserved 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 00009 * 1. Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * 2. Redistributions in binary form must reproduce the above copyright 00012 * notice, this list of conditions and the following disclaimer in 00013 * the documentation and/or other materials provided with the 00014 * distribution. 00015 * 3. Neither the name of the Author nor the names of its contributors 00016 * may be used to endorse or promote products derived from this software 00017 * without specific prior written permission. 00018 * 00019 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' 00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 00021 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00022 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR 00023 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00024 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00025 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 00026 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 00027 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00028 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 00029 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00030 * SUCH DAMAGE. 00031 */ 00032 00033 /** 00034 @file 00035 00036 This file contains the definition of the xml::attributes class. 00037 */ 00038 00039 #ifndef _xmlwrapp_attributes_h_ 00040 #define _xmlwrapp_attributes_h_ 00041 00042 // xmlwrapp includes 00043 #include "xmlwrapp/init.h" 00044 00045 // standard includes 00046 #include <cstddef> 00047 #include <iosfwd> 00048 #include <string> 00049 00050 namespace xml 00051 { 00052 00053 // forward declarations 00054 class node; 00055 00056 namespace impl 00057 { 00058 class ait_impl; 00059 struct node_impl; 00060 } 00061 00062 /** 00063 The xml::attributes class is used to access all the attributes of one 00064 xml::node. You can add, find and erase attributes by name, and for some 00065 member functions, use the provided iterator classes. 00066 00067 The iterator classes allow you to access one XML attribute. This is done 00068 using the xml::attributes::attr class interface. 00069 */ 00070 class attributes 00071 { 00072 public: 00073 /// size type 00074 typedef std::size_t size_type; 00075 00076 /** 00077 Create a new xml::attributes object with no attributes. 00078 00079 @author Peter Jones 00080 */ 00081 attributes(); 00082 00083 /** 00084 Copy construct a xml::attributes object. 00085 00086 @param other The xml::attributes object to copy from. 00087 @author Peter Jones 00088 */ 00089 attributes(const attributes& other); 00090 00091 /** 00092 Copy the given xml::attributes object into this one. 00093 00094 @param other The xml::attributes object to copy from. 00095 @return this. 00096 @author Peter Jones 00097 */ 00098 attributes& operator=(const attributes& other); 00099 00100 /** 00101 Swap this xml::attributes object with another one. 00102 00103 @param other The other xml::attributes object to swap with. 00104 @author Peter Jones 00105 */ 00106 void swap(attributes& other); 00107 00108 ~attributes(); 00109 00110 // forward declarations 00111 class const_iterator; 00112 00113 /** 00114 The xml::attributes::attr class is used to hold information about one 00115 attribute. 00116 */ 00117 class attr 00118 { 00119 public: 00120 /** 00121 Get the name of this attribute. 00122 00123 @return The name for this attribute. 00124 @author Peter Jones 00125 */ 00126 const char *get_name() const; 00127 00128 /** 00129 Get the value of this attribute. 00130 00131 @return The value for this attribute. 00132 @author Peter Jones 00133 */ 00134 const char* get_value() const; 00135 00136 private: 00137 void *node_; 00138 void *prop_; 00139 std::string name_; 00140 mutable std::string value_; 00141 00142 attr(); 00143 attr(const attr& other); 00144 attr& operator=(const attr& other); 00145 void swap(attr& other); 00146 00147 void set_data(void *node, void *prop); 00148 void set_data(const char *name, const char *value, bool); 00149 00150 friend class impl::ait_impl; 00151 }; 00152 00153 /** 00154 Iterator class for accessing attribute pairs. 00155 */ 00156 class iterator 00157 { 00158 public: 00159 typedef attr value_type; 00160 typedef std::ptrdiff_t difference_type; 00161 typedef value_type* pointer; 00162 typedef value_type& reference; 00163 typedef std::forward_iterator_tag iterator_category; 00164 00165 iterator(); 00166 iterator(const iterator& other); 00167 iterator& operator=(const iterator& other); 00168 ~iterator(); 00169 00170 reference operator*() const; 00171 pointer operator->() const; 00172 00173 /// prefix increment 00174 iterator& operator++(); 00175 00176 /// postfix increment (avoid if possible for better performance) 00177 iterator operator++(int); 00178 00179 friend bool operator==(const iterator& lhs, const iterator& rhs); 00180 friend bool operator!=(const iterator& lhs, const iterator& rhs); 00181 00182 private: 00183 impl::ait_impl *pimpl_; 00184 00185 iterator(void *node, void *prop); 00186 iterator(const char *name, const char *value, bool); 00187 void swap(iterator& other); 00188 void* get_raw_attr(); 00189 00190 friend class attributes; 00191 friend class const_iterator; 00192 }; 00193 00194 /** 00195 Const Iterator class for accessing attribute pairs. 00196 */ 00197 class const_iterator 00198 { 00199 public: 00200 typedef const attr value_type; 00201 typedef std::ptrdiff_t difference_type; 00202 typedef value_type* pointer; 00203 typedef value_type& reference; 00204 typedef std::forward_iterator_tag iterator_category; 00205 00206 const_iterator(); 00207 const_iterator(const const_iterator& other); 00208 const_iterator(const iterator& other); 00209 const_iterator& operator=(const const_iterator& other); 00210 ~const_iterator(); 00211 00212 reference operator*() const; 00213 pointer operator->() const; 00214 00215 /// prefix increment 00216 const_iterator& operator++(); 00217 00218 /// postfix increment (avoid if possible better for performance) 00219 const_iterator operator++ (int); 00220 00221 friend bool operator== (const const_iterator &lhs, const const_iterator &rhs); 00222 friend bool operator!= (const const_iterator &lhs, const const_iterator &rhs); 00223 00224 private: 00225 impl::ait_impl *pimpl_; 00226 00227 const_iterator(void *node, void *prop); 00228 const_iterator(const char *name, const char *value, bool); 00229 void swap(const_iterator &other); 00230 void* get_raw_attr(); 00231 00232 friend class attributes; 00233 }; 00234 00235 /** 00236 Get an iterator that points to the first attribute. 00237 00238 @return An iterator that points to the first attribute. 00239 @return An iterator equal to end() if there are no attributes. 00240 @see xml::attributes::iterator 00241 @see xml::attributes::attr 00242 @author Peter Jones 00243 */ 00244 iterator begin(); 00245 00246 /** 00247 Get a const_iterator that points to the first attribute. 00248 00249 @return A const_iterator that points to the first attribute. 00250 @return A const_iterator equal to end() if there are no attributes. 00251 @see xml::attributes::const_iterator 00252 @see xml::attributes::attr 00253 @author Peter Jones 00254 */ 00255 const_iterator begin() const; 00256 00257 /** 00258 Get an iterator that points one past the the last attribute. 00259 00260 @return An "end" iterator. 00261 @author Peter Jones 00262 */ 00263 iterator end(); 00264 00265 /** 00266 Get a const_iterator that points one past the last attribute. 00267 00268 @return An "end" const_iterator. 00269 @author Peter Jones 00270 */ 00271 const_iterator end() const; 00272 00273 /** 00274 Add an attribute to the attributes list. If there is another 00275 attribute with the same name, it will be replaced with this one. 00276 00277 @param name The name of the attribute to add. 00278 @param value The value of the attribute to add. 00279 @author Peter Jones 00280 */ 00281 void insert(const char *name, const char *value); 00282 00283 /** 00284 Find the attribute with the given name. If the attribute is not found 00285 on the current node, the DTD will be searched for a default value. 00286 This is, of course, if there was a DTD parsed with the XML document. 00287 00288 @param name The name of the attribute to find. 00289 @return An iterator that points to the attribute with the given name. 00290 @return If the attribute was not found, find will return end(). 00291 @see xml::attributes::iterator 00292 @see xml::attributes::attr 00293 @author Peter Jones 00294 */ 00295 iterator find(const char *name); 00296 00297 /** 00298 Find the attribute with the given name. If the attribute is not found 00299 on the current node, the DTD will be searched for a default value. 00300 This is, of course, if there was a DTD parsed with the XML document. 00301 00302 @param name The name of the attribute to find. 00303 @return A const_iterator that points to the attribute with the given name. 00304 @return If the attribute was not found, find will return end(). 00305 @see xml::attributes::const_iterator 00306 @see xml::attributes::attr 00307 @author Peter Jones 00308 */ 00309 const_iterator find(const char *name) const; 00310 00311 /** 00312 Erase the attribute that is pointed to by the given iterator. This 00313 will invalidate any iterators for this attribute, as well as any 00314 pointers or references to it. 00315 00316 @param to_erase An iterator that points to the attribute to erased. 00317 @return An iterator that points to the attribute after the one to be erased. 00318 @see xml::attributes::iterator 00319 @see xml::attributes::attr 00320 @author Peter Jones 00321 */ 00322 iterator erase(iterator to_erase); 00323 00324 /** 00325 Erase the attribute with the given name. This will invalidate any 00326 iterators that are pointing to that attribute, as well as any 00327 pointers or references to that attribute. 00328 00329 @param name The name of the attribute to erase. 00330 @author Peter Jones 00331 */ 00332 void erase(const char *name); 00333 00334 /** 00335 Find out if there are any attributes in this xml::attributes object. 00336 00337 @return True if there are no attributes. 00338 @return False if there is at least one attribute. 00339 @author Peter Jones 00340 */ 00341 bool empty() const; 00342 00343 /** 00344 Find out how many attributes there are in this xml::attributes 00345 object. 00346 00347 @return The number of attributes in this xml::attributes object. 00348 @author Peter Jones 00349 */ 00350 size_type size() const; 00351 00352 private: 00353 struct pimpl; pimpl *pimpl_; 00354 00355 // private ctor to create uninitialized instance 00356 explicit attributes (int); 00357 00358 void set_data (void *node); 00359 void* get_data(); 00360 friend struct impl::node_impl; 00361 friend class node; 00362 }; 00363 00364 } // namespace xml 00365 00366 #endif // _xmlwrapp_attributes_h_