ulxr_htmlform_handler.h

Go to the documentation of this file.
00001 /***************************************************************************
00002         ulxr_htmlform_handler.h  -  work with html pages and forms
00003                              -------------------
00004     begin                : Fri Dec 05 2003
00005     copyright            : (C) 2002-2007 by Ewald Arnold
00006     email                : ulxmlrpcpp@ewald-arnold.de
00007 
00008     $Id: ulxr_htmlform_handler.h 942 2006-12-31 13:10:54Z ewald-arnold $
00009 
00010  ***************************************************************************/
00011 
00012 /**************************************************************************
00013  *
00014  * This program is free software; you can redistribute it and/or modify
00015  * it under the terms of the GNU Lesser General Public License as
00016  * published by the Free Software Foundation; either version 2 of the License,
00017  * or (at your option) any later version.
00018  *
00019  * This program is distributed in the hope that it will be useful,
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022  * GNU General Public License for more details.
00023  *
00024  * You should have received a copy of the GNU Lesser General Public License
00025  * along with this program; if not, write to the Free Software
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00027  *
00028  ***************************************************************************/
00029 
00030 #ifndef ULXR_HtmlFormHandler_H
00031 #define ULXR_HtmlFormHandler_H
00032 
00033 
00034 #include <ulxmlrpcpp/ulxmlrpcpp.h>  // always first header
00035 
00036 #include <ulxmlrpcpp/ulxr_http_server.h>
00037 #include <ulxmlrpcpp/ulxr_except.h>
00038 
00039 
00040 namespace ulxr {
00041 
00042 
00046 class ULXR_API_DECL0 HtmlFormData
00047 {
00048   public:
00049    typedef std::map<CppString, std::vector<CppString> > Elements;
00050 
00055    void addElement(const CppString &name, const CppString &value);
00056 
00061    std::vector<CppString> getElement(const CppString &name) const;
00062 
00070    std::vector<CppString> getElement(const CppString &name, unsigned index) const;
00071 
00076    bool hasElement(const CppString &name) const;
00077 
00085    bool hasElement(const CppString &name, unsigned index) const;
00086 
00090    unsigned size() const;
00091 
00092   private:
00093 
00094     Elements  elements;
00095 };
00096 
00097 
00098 namespace hidden
00099 {
00100 
00103 class ULXR_API_DECL0 SubResourceBase
00104 {
00105   public:
00106 
00111     SubResourceBase(const CppString &name, const CppString &descr);
00112 
00115     virtual ~SubResourceBase();
00116 
00122     virtual CppString call(const HtmlFormData &formdata, CppString &mimetype) = 0;
00123 
00127     CppString getName() const;
00128 
00132     CppString getDescription() const;
00133 
00134   private:
00135 
00136     CppString    name;
00137     CppString    descr;
00138 };
00139 
00140 
00143 template<class T>
00144 class SubResource : public SubResourceBase
00145 {
00146   public:
00147 
00153   typedef CppString (T::*PMF)(const HtmlFormData &formdata, CppString &mimetype);
00154 
00161     SubResource(const CppString &in_name, T* in_obj, PMF in_pmf, const CppString &in_descr)
00162         : SubResourceBase(in_name, in_descr)
00163         , obj(in_obj)
00164         , pmf(in_pmf)
00165     {}
00166 
00172     CppString call(const HtmlFormData &formdata, CppString &mimetype)
00173     {
00174       return (obj->*pmf)(formdata, mimetype);
00175     }
00176 
00177   private:
00178     T        *obj;
00179     PMF       pmf;
00180 };
00181 
00182 
00183 } // namespace hidden
00184 
00185 
00193 class ULXR_API_DECL0 HtmlFormHandler
00194 {
00195   public:
00196 
00201     HtmlFormHandler(const CppString &resource);
00202 
00205     virtual ~HtmlFormHandler();
00206 
00213     HtmlFormData extractFormElements(HttpProtocol *prot,
00214                                      const CppString &method,
00215                                      const CppString &resource,
00216                                      const Cpp8BitString &conn_data);
00217 
00228     static bool resourceSplit(const CppString &method,
00229                               const CppString &resource,
00230                               const CppString &rsc_start,
00231                               CppString &target);
00232 
00238     static std::string formDecode(std::string &value);
00239 
00244     virtual std::string encodeForHtml(const CppString &data);
00245 
00250     virtual CppString encodeFromHtml(const std::string &data);
00251 
00257     CppString openForm(const CppString &name, const CppString &method);
00258 
00262     static CppString closeForm();
00263 
00269     CppString makeAnchor(const CppString &url, const CppString &descr);
00270 
00276     static CppString makeIdent(const CppString &name, unsigned index);
00277 
00283     static CppString makeHexNumber(unsigned index);
00284 
00289     static CppString makeNumber(unsigned index);
00290 
00295     static long getLong(const CppString &num);
00296 
00302     static CppString makeText(const CppString &text);
00303 
00310     static CppString makeCheckBox(const CppString &name, const CppString &value,
00311                                   bool checked = false);
00312 
00318     static CppString makeTextField(const CppString &name, const CppString &value);
00319 
00327     static CppString makeTextArea(const CppString &name, const CppString &value,
00328                                   int cols = -1, int rows = -1);
00329 
00337     static CppString openSelect(const CppString &name,
00338                                 int size = 1,
00339                                 bool multi = false);
00340 
00348     static CppString makeOption(const CppString &data,
00349                                 const CppString &value = ULXR_PCHAR(""),
00350                                 bool selected = false);
00351 
00354     static CppString closeSelect();
00355 
00362     static CppString makeRadioButton(const CppString &name, const CppString &value,
00363                                      bool checked = false);
00364 
00370     static CppString makeSubmitButton(const CppString &name,
00371                                       const CppString &value);
00372 
00378     static CppString makeResetButton(const CppString &name,
00379                                      const CppString &value);
00380 
00383     static CppString makeLineBreak();
00384 
00393     static CppString applyTags(const CppString &data,
00394                                const CppString &tag,
00395                                bool newline = false);
00396 
00406     virtual bool handler(HttpProtocol *prot,
00407                          const CppString &method,
00408                          const CppString &resource,
00409                          const Cpp8BitString &conn_data);
00410 
00417     virtual CppString getHeader(const CppString &title) const;
00418 
00423     virtual CppString getTail() const;
00424 
00430     virtual CppString getCssName() const;
00431 
00437     virtual CppString getEncoding() const;
00438 
00444     virtual CppString handle_css_file(const HtmlFormData &formdata, CppString &mimetype);
00445 
00451     CppString handle_subresource_list(const HtmlFormData &formdata, CppString &mimetype);
00452 
00456     CppString getMasterResource() const;
00457 
00464     template <class T>
00465     void addSubResource(const CppString &name,
00466                         T* obj,
00467                         CppString (T::*pmf)(const HtmlFormData &, CppString &),
00468                         const CppString &descr = ULXR_PCHAR(""))
00469     {
00470       for (unsigned i = 0; i < subResources.size(); ++i)
00471       {
00472         if (subResources[i]->getName() == name)
00473           throw RuntimeException(ApplicationError,
00474                                  ULXR_PCHAR("Attempt to register two resources under the same name: ")
00475                                  + name);
00476       }
00477       subResources.push_back(new hidden::SubResource<T>(name, obj, pmf, descr));
00478     }
00479 
00480   private:
00481 
00482     CppString                              masterResource;
00483     std::vector<hidden::SubResourceBase*>  subResources;
00484 
00485  private:
00486 
00487    // forbid them all due to internal pointers
00488    HtmlFormHandler(const HtmlFormHandler&);
00489    HtmlFormHandler& operator=(const HtmlFormHandler&);
00490 };
00491 
00492 
00493 }  // namespace ulxr
00494 
00495 
00496 #endif

Generated on Sun Aug 19 20:08:57 2007 for ulxmlrpcpp by  doxygen 1.5.1