00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __PEEKABOT_SCOPED_HANDLER_HH
00019 #define __PEEKABOT_SCOPED_HANDLER_HH
00020
00021 #include <map>
00022 #include <vector>
00023 #include <string>
00024 #include <stdexcept>
00025 #include <boost/function.hpp>
00026
00027 #include "XMLHandler.hh"
00028 #include "ScopedMap.hh"
00029
00030 namespace peekabot
00031 {
00054 class ScopedHandler : public XMLHandler
00055 {
00056 public:
00061 typedef boost::function<
00062 void (const std::string&, AttributeMap&,
00063 ScopedHandler* handler)> ElementStartFunctor;
00068 typedef boost::function<
00069 void (const std::string&, ScopedHandler* handler)> CharacterFunctor;
00070
00074 typedef boost::function<
00075 void (const std::string&, ScopedHandler* handler)> ElementEndFunctor;
00076
00080 typedef boost::function<void (ScopedHandler* handler)> HandlerFunctor;
00081
00082 typedef ElementStartFunctor UnhandledElementFunctor;
00083
00086 typedef std::pair<ElementStartFunctor, ElementEndFunctor> StartEndPair;
00087
00090 typedef std::map<std::string, ElementStartFunctor> StartFunctorMap;
00091
00106 struct TagScope
00107 {
00108 TagScope()
00109 : unhandled_functor(&default_unhandled) {}
00110
00114 CharacterFunctor cdata_functor;
00115
00119 StartFunctorMap start_functors;
00120
00125 std::vector<ElementEndFunctor> end_functors;
00126
00132 UnhandledElementFunctor unhandled_functor;
00133
00134 private:
00137 static void default_unhandled(
00138 const std::string &tag_name,
00139 AttributeMap &,
00140 ScopedHandler *handler) throw(std::runtime_error)
00141 {
00142 throw std::runtime_error(
00143 "Unhandled start tag encountered: " + tag_name);
00144 }
00145 };
00146
00150 ScopedHandler(HandlerFunctor start_handler = do_nothing,
00151 HandlerFunctor end_handler = do_nothing,
00152 HandlerFunctor failure_handler = do_nothing);
00153
00162 virtual void on_start_element(const std::string & tag_name,
00163 AttributeMap & attributes)
00164 throw(std::exception);
00165
00174 virtual void on_end_element(const std::string & tag_name)
00175 throw(std::exception);
00176
00184 virtual void on_cdata(const std::string & cdata)
00185 throw(std::exception);
00186
00192 virtual void on_start_document() throw(std::exception);
00193
00194
00200 virtual void on_end_document() throw(std::exception);
00201
00204 virtual void on_failure() throw(std::exception);
00205
00209 ScopedMap & get_variables();
00210
00213 void enter_scope(const TagScope & tag_handlers);
00214
00217 void enter_scope();
00218
00222 void duplicate_scope();
00223
00226 bool add_start_handler(const std::string &name,
00227 ElementStartFunctor start_handler);
00228
00232 void set_start_document_handler(HandlerFunctor start_handler);
00233
00237 void set_end_document_handler(HandlerFunctor end_handler);
00238
00242 void set_failure_handler(HandlerFunctor failure_handler);
00243
00244 TagScope &get_current_scope() throw(std::runtime_error);
00245
00246
00247 private:
00250 static void do_nothing(ScopedHandler* handler) {}
00251
00252 std::stack<TagScope> m_scoped_tag_handlers;
00253 ScopedMap m_scoped_variables;
00254
00255 HandlerFunctor m_start_handler;
00256 HandlerFunctor m_end_handler;
00257 HandlerFunctor m_failure_handler;
00258
00259 bool m_new_scope;
00260 };
00261
00262 }
00263 #endif //__PEEKABOT_SCOPED_HANDLER_HH