|
Rapicorn - Experimental UI Toolkit - Source Code 10.08.1
|
00001 /* RapicornMarkup - simple XML parser 00002 * Copyright (C) 2005 Tim Janik 00003 * 00004 * This library is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU Lesser General Public 00006 * License as published by the Free Software Foundation; either 00007 * version 2.1 of the License, or (at your option) any later version. 00008 * 00009 * This library is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 * Lesser General Public License for more details. 00013 * 00014 * A copy of the GNU Lesser General Public License should ship along 00015 * with this library; if not, see http://www.gnu.org/copyleft/. 00016 */ 00017 #ifndef __RAPICORN_MARKUP_HH__ 00018 #define __RAPICORN_MARKUP_HH__ 00019 00020 #include <rcore/utilities.hh> 00021 00022 namespace Rapicorn { 00023 00024 class MarkupParser { 00025 public: 00026 typedef enum { 00027 NONE = 0, 00028 READ_FAILED, 00029 BAD_UTF8, 00030 DOCUMENT_EMPTY, 00031 PARSE_ERROR, 00032 /* client errors */ 00033 INVALID_ELEMENT, 00034 INVALID_ATTRIBUTE, 00035 INVALID_CONTENT, 00036 MISSING_ELEMENT, 00037 MISSING_ATTRIBUTE, 00038 MISSING_CONTENT 00039 } ErrorType; 00040 struct Error { 00041 ErrorType code; 00042 String message; 00043 uint line_number; 00044 uint char_number; 00045 explicit Error() : code (NONE), line_number (0), char_number (0) {} 00046 void set (ErrorType c, String msg) { code = c; message = msg; } 00047 bool set () { return code != NONE; } 00048 }; 00049 typedef const vector<String> ConstStrings; 00050 static MarkupParser* create_parser (const String &input_name); 00051 virtual ~MarkupParser (); 00052 bool parse (const char *text, 00053 ssize_t text_len, 00054 Error *error); 00055 bool end_parse (Error *error); 00056 String get_element (); 00057 String input_name (); 00058 void get_position (int *line_number, 00059 int *char_number, 00060 const char **input_name_p = NULL); 00061 virtual void error (const Error &error); 00062 /* useful when saving */ 00063 static String escape_text (const String &text); 00064 static String escape_text (const char *text, 00065 ssize_t length); 00066 static String printf_escaped (const char *format, 00067 ...) RAPICORN_PRINTF (1, 2); 00068 static String vprintf_escaped (const char *format, 00069 va_list args); 00070 struct Context; 00071 protected: 00072 explicit MarkupParser (const String &input_name); 00073 virtual void start_element (const String &element_name, 00074 ConstStrings &attribute_names, 00075 ConstStrings &attribute_values, 00076 Error &error); 00077 virtual void end_element (const String &element_name, 00078 Error &error); 00079 virtual void text (const String &text, 00080 Error &error); 00081 virtual void pass_through (const String &pass_through_text, 00082 Error &error); 00083 void recap_element (const String &element_name, 00084 ConstStrings &attribute_names, 00085 ConstStrings &attribute_values, 00086 Error &error, 00087 bool include_outer = true); 00088 const String& recap_string () const; 00089 private: 00090 Context *context; 00091 String m_input_name; 00092 String m_recap; 00093 uint m_recap_depth; 00094 bool m_recap_outer; 00095 void recap_start_element (const String &element_name, 00096 ConstStrings &attribute_names, 00097 ConstStrings &attribute_values, 00098 Error &error); 00099 void recap_end_element (const String &element_name, 00100 Error &error); 00101 void recap_text (const String &text, 00102 Error &error); 00103 void recap_pass_through (const String &pass_through_text, 00104 Error &error); 00105 }; 00106 00107 } // Rapicorn 00108 00109 #endif /* __RAPICORN_MARKUP_HH__ */
1.7.4