ubit
uxmlparser.hpp
1 /* ==================================================== ======== ======= *
2  *
3  * uxmlparser.hpp
4  * Ubit GUI Toolkit - Version 6
5  * (C) 2009 / Eric Lecolinet / Telecom ParisTech / http://www.enst.fr/~elc/ubit
6  *
7  * ***********************************************************************
8  * COPYRIGHT NOTICE :
9  * THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY AND WITHOUT EVEN THE
10  * IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
11  * YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU
12  * GENERAL PUBLIC LICENSE AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION;
13  * EITHER VERSION 2 OF THE LICENSE, OR (AT YOUR OPTION) ANY LATER VERSION.
14  * SEE FILES 'COPYRIGHT' AND 'COPYING' FOR MORE DETAILS.
15  * ***********************************************************************/
16 
17 #ifndef _uxmlparser_hpp_
18 #define _uxmlparser_hpp_ 1
19 #include <ubit/udom.hpp>
20 #include <fstream>
21 namespace ubit {
22 
23  class UXmlGrammar;
24 
30  class UXmlParser {
31  public:
32  struct ParseError {};
33 
34  UXmlParser();
36 
37  virtual ~UXmlParser();
38 
39  void addGrammar(const UXmlGrammar&);
45  UXmlDocument* read(const UStr& path);
50  UXmlDocument* readBuffer(const UStr& docname, const UStr& buffer)
51  {return parse(docname,buffer);}
53 
54  UXmlDocument* parse(const UStr& docname, const UStr& buffer);
59  int getStatus() {return status;}
61 
62  UErrorHandler* getErrorHandler() {return perrhandler;}
64 
65  void setErrorHandler(UErrorHandler* eh) {perrhandler = eh;}
67 
68  void setPermissive(bool b) {permissive = b;}
78  void setCollapseSpaces(bool b) {collapse_spaces = b;}
84  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
85  protected:
86  void readElement(UElem* parent);
87  void readText(UElem* parent);
88  bool readXMLDeclaration();
89  void readXMLInstruction(UElem* parent);
90  void readSGMLData(UElem* parent);
91 
92  void skipSpaces();
93  UChar readCharEntityReference();
94  bool readName(UStr&);
95  bool readQuotedValue(UStr&, UChar quoting_char);
96  bool readUnquotedValue(UStr&);
97  bool readNameValuePair(UStr& name, UStr& value);
98  UElem* readElementStartTag(UStr& elem_name, int& stat);
99  int readElementEndTag(const UStr& elem_name);
100 
101  void error(const char* msg, const UChar* line);
102  void error(const char* msg_start, const UStr& name,
103  const char* msg_end, const UChar* line);
104  void unexpected(const char* msg, const UChar* line);
105 
106  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
107 
108  private:
109  static const int INVALID_TAG = 0, END_TAG = 1, END_TAG_AND_ELEM = 2;
110  int status;
111  bool permissive, collapse_spaces;
112  const UChar *text_buffer, *p;
113  UXmlDocument* doc;
114  UXmlGrammars* parser_grammars;
115  uptr<UErrorHandler> perrhandler;
116  };
117 }
118 #endif
119 
120 
121 
UXmlDocument * readBuffer(const UStr &docname, const UStr &buffer)
synonym for parse();
Definition: uxmlparser.hpp:50
Error management.
Definition: uappli.hpp:410
XML parser.
Definition: uxmlparser.hpp:30
UXmlParser()
creates a new XML parser.
Definition: uxmlparser.cpp:667
void addGrammar(const UXmlGrammar &)
adds a grammar to the parser.
Definition: uxmlparser.cpp:682
void setPermissive(bool b)
parses documents in permissive mode (default is false).
Definition: uxmlparser.hpp:68
UXmlDocument * parse(const UStr &docname, const UStr &buffer)
reads and parses a XML buffer and returns the corresponding XML tree.
Definition: uxmlparser.cpp:701
UXmlDocument * read(const UStr &path)
reads and parses a XML file and returns the corresponding XML tree.
Definition: uxmlparser.cpp:688
lightweight general purpose container.
Definition: uelem.hpp:44
void setCollapseSpaces(bool b)
collapses whitespaces (and tabs and newlines) in elements (default is false).
Definition: uxmlparser.hpp:78
UErrorHandler * getErrorHandler()
returns the current error handler.
Definition: uxmlparser.hpp:62
Smart Pointer for UObject instances (.
Definition: uobject.hpp:365
XML Grammar.
Definition: uxmlgrammar.hpp:27
Definition: uxmlparser.hpp:32
Definition: uhardfont.hpp:31
XML Doc.
Definition: udom.hpp:106
List of XML Grammars.
Definition: uxmlgrammar.hpp:59
int getStatus()
returns the reading/parsing status.
Definition: uxmlparser.hpp:59
void setErrorHandler(UErrorHandler *eh)
changes the error handler (UAppli default handler used if argument is null).
Definition: uxmlparser.hpp:65
Ubit String.
Definition: ustr.hpp:72