ximol/xml/content.hpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002  *                                                                           *
00003  * library XiMoL                                                             *
00004  * Copyright (C) 2002, 2003, 2004 Florent Tournois                           *
00005  *                                                                           *
00006  * This library is free software; you can redistribute it and/or             *
00007  * modify it under the terms of the GNU Lesser General Public                *
00008  * License as published by the Free Software Foundation; either              *
00009  * version 2.1 of the License, or (at your option) any later version.        *
00010  *                                                                           *
00011  * This library is distributed in the hope that it will be useful,           *
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00014  * Lesser General Public License for more details.                           *
00015  *                                                                           *
00016  * You should have received a copy of the GNU Lesser General Public          *
00017  * License along with this library; if not, write to the Free Software       *
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA   *
00019  *                                                                           *
00020 \*****************************************************************************/
00021 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00022 /** \file 
00023         \brief Define content Interface.
00024         
00025     \todo make a function boxed_content
00026 
00027         \author Tournois Florent
00028         \version 1.0
00029 
00030     $Id: content.hpp,v 1.7 2004/03/04 18:45:29 tournois Exp $
00031     $Log: content.hpp,v $
00032     Revision 1.7  2004/03/04 18:45:29  tournois
00033     Compatibility with gcc.
00034 
00035     Revision 1.6  2004/02/25 18:59:13  tournois
00036     imporve the gcc compatibility.
00037 
00038     Revision 1.5  2004/02/22 10:27:34  tournois
00039     Add some doc.
00040 
00041     Revision 1.4  2004/02/22 09:54:21  tournois
00042     Change years on the copyright.
00043 
00044     Revision 1.3  2004/02/15 10:07:03  hfp
00045     content-functions (some) needs an ambiguity-buster ("hidden" arg as defaulted param) to compile
00046 
00047     Revision 1.2  2004/01/29 20:52:35  tournois
00048     doc and tutorial.
00049 
00050     Revision 1.1  2004/01/09 18:26:29  tournois
00051     Add box and content manipulator.
00052 
00053 
00054   */
00055 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00056 #ifndef XIMOL_XML_CONTENT_HPP_
00057 #define XIMOL_XML_CONTENT_HPP_
00058 
00059 #include <ximol/stream.hpp>
00060 #include <ximol/assignment.hpp>
00061 #include <ximol/parser/utils.hpp>
00062 #include <ximol/parser/def_signs.hpp>
00063 #include <sstream>
00064 
00065 XIMOL_XML_BEGIN_NAMESPACE
00066 
00067 //=============================================================================
00068 /// Manipulator for the char data content.
00069 //=============================================================================
00070 class XIMOL_EXPORT manipulator_char_data_content
00071 {
00072 public:
00073     manipulator_char_data_content(const xstring& xstr);
00074     manipulator_char_data_content(const manipulator_char_data_content& x);
00075 
00076     manipulator_char_data_content& operator=(const manipulator_char_data_content& x);
00077     manipulator_char_data_content& operator=(const xstring& xstr);
00078 
00079     ::std::basic_istringstream<xchar_t>& istr();
00080     xstring xstr() const;
00081     void xstr(const xstring & xstr);
00082 
00083     ::std::string str() const;
00084     void str(const ::std::string & str);
00085 
00086 private:
00087     ::std::basic_istringstream<xchar_t> content_;
00088 }; // End of class manipulator_char_data_content
00089 
00090 //-----------------------------------------------------------------------------
00091 // content manipulator
00092 /*  @ingroup xml 
00093     content    ::=    CharData? ((element | Reference | cd_sect | PI | comment) CharData?)* 
00094  */
00095 //-----------------------------------------------------------------------------
00096 XIMOL_EXPORT
00097 manipulator_char_data_content char_data_content(xistream& xis);
00098 
00099 //=============================================================================
00100 /// Manipulator for the char data content.
00101 //=============================================================================
00102 template < typename T >
00103 class manipulator_char_content
00104 {
00105 public:
00106     typedef T string_type;
00107 
00108 public:
00109     manipulator_char_content(string_type & x)
00110         :x_(x)
00111     {};
00112 
00113     manipulator_char_content(const manipulator_char_content<T> & x)
00114         :x_(x.x_)
00115     {};
00116 
00117     void set(const xstring & x)
00118     {
00119         assignment_to_string::equality(x_, x);
00120     };
00121 
00122     xstring get() const
00123     {
00124         return str<std::wstring>::cast(x_);
00125     };
00126 
00127 private:
00128     const manipulator_char_content<T> & operator=(const manipulator_char_content<T> & x);
00129 
00130 private:
00131     string_type & x_;
00132 
00133 }; // end of class manipulator_char_content<T>
00134 
00135 //-----------------------------------------------------------------------------
00136 // PutTo operator for the char_content.
00137 //-----------------------------------------------------------------------------
00138 template < typename T >
00139 xostream& operator<<(xostream& xos, const manipulator_char_content<T>& m)
00140 {
00141         XIMOL_PARSER_USING_NAMESPACE;
00142 
00143     xstring buffer(m.get());
00144         xstring::const_iterator i(buffer.begin()), i_end(buffer.end());
00145 
00146         for(;i!=i_end;++i)
00147         {
00148                 switch (*i)
00149                 {
00150                 case XCHAR_AMPERSAND: 
00151                 case XCHAR_LESS_THAN_SIGN:
00152                 case XCHAR_RIGHT_SQUARE_BRACKET:
00153                         write_char_ref(xos,*i);
00154                         break;
00155                 default:
00156                         write_xchar(xos,*i);
00157                 }
00158         }
00159         return xos;
00160 };
00161 
00162 //-----------------------------------------------------------------------------
00163 // PutTo operator for the char_content.
00164 //-----------------------------------------------------------------------------
00165 template < typename T >
00166 xistream& operator>>(xistream& xis, manipulator_char_content<T> m)
00167 {
00168         XIMOL_PARSER_USING_NAMESPACE;
00169     m.set(char_data_content(xis).xstr());
00170         return xis;
00171 };
00172 
00173 //=============================================================================
00174 /// class for the qualified name
00175 //=============================================================================
00176 template < typename T >
00177 class manipulator_data_content
00178 {
00179 public:
00180     typedef T object_type;
00181 
00182 public:
00183     manipulator_data_content(object_type & x)
00184         :x_(x)
00185     {};
00186 
00187     manipulator_data_content(const manipulator_data_content<T> & x)
00188         :x_(x.x_)
00189     {};
00190 
00191     void set(const object_type & x)
00192     {
00193         assignment_std::equality(x_, x);
00194     };
00195 
00196     void set_string(const xstring & xstr)
00197     {
00198         assignment_char_serialization::equality(x_, xstr);
00199     };
00200 
00201     const object_type & get() const
00202     {
00203         return x_;
00204     };
00205 
00206     xstring get_string() const
00207     {
00208         return str<std::wstring>::cast(x_);
00209     };
00210 
00211 private:
00212     const manipulator_data_content<T> & operator=(const manipulator_data_content<T> & x);
00213 
00214 private:
00215     object_type & x_;
00216 
00217 }; // end of class manipulator_data_content<T>
00218 
00219 //-----------------------------------------------------------------------------
00220 // PutTo operator for the cdata.
00221 //-----------------------------------------------------------------------------
00222 template < typename T >
00223 xostream& operator<<(xostream& xos, const manipulator_data_content<T>& m)
00224 {
00225         XIMOL_PARSER_USING_NAMESPACE;
00226     xos << manipulator_char_content<xstring>(m.get_string());
00227         return xos;
00228 };
00229 
00230 //-----------------------------------------------------------------------------
00231 // PutTo operator for the cdata.
00232 //-----------------------------------------------------------------------------
00233 template < typename T >
00234 xistream& operator>>(xistream& xis, manipulator_data_content<T>& m)
00235 {
00236         XIMOL_PARSER_USING_NAMESPACE;
00237     xstring xstr;
00238     manipulator_char_content<xstring> manip(xstr);
00239     xis >> manip;
00240     m.set_string(xstr);
00241         return xis;
00242 };
00243 
00244 
00245 #define XIMOL_CONTENT_CHAR_SPECIALIZATION(type)                                                         \
00246         inline manipulator_char_content<type> content(type& t, bool = false) {  \
00247                 return manipulator_char_content<type>(t);                                                       \
00248         }
00249 
00250         XIMOL_CONTENT_CHAR_SPECIALIZATION(std::string)
00251         XIMOL_CONTENT_CHAR_SPECIALIZATION(const std::string)
00252         XIMOL_CONTENT_CHAR_SPECIALIZATION(std::wstring)
00253         XIMOL_CONTENT_CHAR_SPECIALIZATION(const std::wstring)
00254         XIMOL_CONTENT_CHAR_SPECIALIZATION(char *)
00255         XIMOL_CONTENT_CHAR_SPECIALIZATION(const char *)
00256         XIMOL_CONTENT_CHAR_SPECIALIZATION(wchar_t *)
00257         XIMOL_CONTENT_CHAR_SPECIALIZATION(const wchar_t *)
00258 
00259 #undef XIMOL_CONTENT_CHAR_SPECIALIZATION
00260 
00261 
00262 template<typename T>
00263 manipulator_data_content<T> content(T& t)
00264 { 
00265     return manipulator_data_content<T>(t);
00266 }
00267 
00268 
00269 XIMOL_XML_END_NAMESPACE
00270 
00271 #endif // #ifndef XIMOL_XML_CONTENT_HPP_


Donate to the XiMoL project SourceForge.net Logo If you have any questions about XiMoL, you could write to tournois@users.sourceforge.net.