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 #ifndef ULXR_HtmlFormHandler_H
00031 #define ULXR_HtmlFormHandler_H
00032
00033
00034 #include <ulxmlrpcpp/ulxmlrpcpp.h>
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 }
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
00488 HtmlFormHandler(const HtmlFormHandler&);
00489 HtmlFormHandler& operator=(const HtmlFormHandler&);
00490 };
00491
00492
00493 }
00494
00495
00496 #endif