Rapicorn - Experimental UI Toolkit - Source Code  13.07.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
markup.hh
Go to the documentation of this file.
00001  // Licensed GNU LGPL v3 or later: http://www.gnu.org/licenses/lgpl.html
00002 #ifndef __RAPICORN_MARKUP_HH__
00003 #define __RAPICORN_MARKUP_HH__
00004 
00005 #include <rcore/utilities.hh>
00006 #include <rcore/strings.hh>
00007 
00008 namespace Rapicorn {
00009 
00010 class MarkupParser {
00011 public:
00012   typedef enum {
00013     NONE        = 0,
00014     READ_FAILED,
00015     BAD_UTF8,
00016     DOCUMENT_EMPTY,
00017     PARSE_ERROR,
00018     /* client errors */
00019     INVALID_ELEMENT,
00020     INVALID_ATTRIBUTE,
00021     INVALID_CONTENT,
00022     MISSING_ELEMENT,
00023     MISSING_ATTRIBUTE,
00024     MISSING_CONTENT
00025   } ErrorType;
00026   struct Error {
00027     ErrorType   code;
00028     String      message;
00029     uint        line_number;
00030     uint        char_number;
00031     explicit    Error() : code (NONE), line_number (0), char_number (0) {}
00032     void        set  (ErrorType c, String msg)  { code = c; message = msg; }
00033     bool        set  ()                         { return code != NONE; }
00034   };
00035   typedef const vector<String> ConstStrings;
00036   static MarkupParser*  create_parser   (const String   &input_name);
00037   virtual               ~MarkupParser   ();
00038   bool                  parse           (const char     *text,
00039                                          ssize_t         text_len,  
00040                                          Error          *error);
00041   bool                  end_parse       (Error          *error);
00042   String                get_element     ();
00043   String                input_name      ();
00044   void                  get_position    (int            *line_number,
00045                                          int            *char_number,
00046                                          const char    **input_name_p = NULL);
00047   virtual void          error           (const Error    &error);
00048   /* useful when saving */
00049   static String         escape_text     (const String   &text);
00050   static String         escape_text     (const char     *text,
00051                                          ssize_t         length);
00052   template<class... Args> RAPICORN_PRINTF (1, 0) static String
00053   escape_format_args (const char *format, const Args &...args)
00054   {
00055     auto arg_transform = [] (const String &s) { return escape_text (s); };
00056     return Lib::StringFormatter::format (arg_transform, format, args...);
00057   }
00058   struct Context;
00059 protected:
00060   explicit              MarkupParser    (const String   &input_name);
00061   virtual void          start_element   (const String   &element_name,
00062                                          ConstStrings   &attribute_names,
00063                                          ConstStrings   &attribute_values,
00064                                          Error          &error);
00065   virtual void          end_element     (const String   &element_name,
00066                                          Error          &error);
00067   virtual void          text            (const String   &text,
00068                                          Error          &error);
00069   virtual void          pass_through    (const String   &pass_through_text,
00070                                          Error          &error);
00071   void                  recap_element   (const String   &element_name,
00072                                          ConstStrings   &attribute_names,
00073                                          ConstStrings   &attribute_values,
00074                                          Error          &error,
00075                                          bool            include_outer = true);
00076   const String&         recap_string    () const;
00077 private:
00078   Context *context;
00079   String   input_name_;
00080   String   recap_;
00081   uint     recap_depth_;
00082   bool     recap_outer_;
00083   void     recap_start_element   (const String   &element_name,
00084                                   ConstStrings   &attribute_names,
00085                                   ConstStrings   &attribute_values,
00086                                   Error          &error);
00087   void     recap_end_element     (const String   &element_name,
00088                                   Error          &error);
00089   void     recap_text            (const String   &text,
00090                                   Error          &error);
00091   void     recap_pass_through    (const String   &pass_through_text,
00092                                   Error          &error);
00093 };
00094 
00095 } // Rapicorn
00096 
00097 #endif  /* __RAPICORN_MARKUP_HH__ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines