new HTML tags parser and entities substitution code
[wxWidgets.git] / include / wx / html / htmlpars.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: htmlpars.h
3 // Purpose: wxHtmlParser class (generic parser)
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 #ifndef _WX_HTMLPARS_H_
12 #define _WX_HTMLPARS_H_
13
14 #ifdef __GNUG__
15 #pragma interface "htmlpars.h"
16 #endif
17
18 #include "wx/defs.h"
19 #if wxUSE_HTML
20
21 #include "wx/html/htmltag.h"
22 #include "wx/filesys.h"
23
24 class WXDLLEXPORT wxMBConv;
25 class WXDLLEXPORT wxHtmlParser;
26 class WXDLLEXPORT wxHtmlTagHandler;
27 class WXDLLEXPORT wxHtmlEntitiesParser;
28
29 // This class handles generic parsing of HTML document : it scans
30 // the document and divide it into blocks of tags (where one block
31 // consists of starting and ending tag and of text between these
32 // 2 tags.
33 class WXDLLEXPORT wxHtmlParser : public wxObject
34 {
35 DECLARE_ABSTRACT_CLASS(wxHtmlParser)
36
37 public:
38 wxHtmlParser();
39 virtual ~wxHtmlParser();
40
41 // Sets the class which will be used for opening files
42 void SetFS(wxFileSystem *fs) { m_FS = fs; }
43
44 wxFileSystem* GetFS() const { return m_FS; }
45
46 // You can simply call this method when you need parsed output.
47 // This method does these things:
48 // 1. call InitParser(source);
49 // 2. call DoParsing();
50 // 3. call GetProduct(); (it's return value is then returned)
51 // 4. call DoneParser();
52 wxObject* Parse(const wxString& source);
53
54 // Sets the source. This must be called before running Parse() method.
55 virtual void InitParser(const wxString& source);
56 // This must be called after Parse().
57 virtual void DoneParser();
58
59 // Parses the m_Source from begin_pos to end_pos-1.
60 // (in noparams version it parses whole m_Source)
61 void DoParsing(int begin_pos, int end_pos);
62 inline void DoParsing() {DoParsing(0, m_Source.Length());};
63
64 // Returns product of parsing
65 // Returned value is result of parsing of the part. The type of this result
66 // depends on internal representation in derived parser
67 // (see wxHtmlWinParser for details).
68 virtual wxObject* GetProduct() = 0;
69
70 // adds handler to the list & hash table of handlers.
71 virtual void AddTagHandler(wxHtmlTagHandler *handler);
72
73 // Forces the handler to handle additional tags (not returned by GetSupportedTags).
74 // The handler should already be in use by this parser.
75 // Example: you want to parse following pseudo-html structure:
76 // <myitems>
77 // <it name="one" value="1">
78 // <it name="two" value="2">
79 // </myitems>
80 // <it> This last it has different meaning, we don't want it to be parsed by myitems handler!
81 // handler can handle only 'myitems' (e.g. it's GetSupportedTags returns "MYITEMS")
82 // you can call PushTagHandler(handler, "IT") when you find <myitems>
83 // and call PopTagHandler() when you find </myitems>
84 void PushTagHandler(wxHtmlTagHandler *handler, wxString tags);
85
86 // Restores state before last call to PushTagHandler
87 void PopTagHandler();
88
89 wxString* GetSource() {return &m_Source;}
90 void SetSource(const wxString& src);
91
92 protected:
93 // Adds text to the output.
94 // This is called from Parse() and must be overriden in derived classes.
95 // txt is not guaranteed to be only one word. It is largest continuous part of text
96 // (= not broken by tags)
97 // NOTE : using char* because of speed improvements
98 virtual void AddText(const char* txt) = 0;
99
100 // Adds tag and proceeds it. Parse() may (and usually is) called from this method.
101 // This is called from Parse() and may be overriden.
102 // Default behavior is that it looks for proper handler in m_Handlers. The tag is
103 // ignored if no hander is found.
104 // Derived class is *responsible* for filling in m_Handlers table.
105 virtual void AddTag(const wxHtmlTag& tag);
106
107 // Returns entity parser object, used to substitute HTML &entities;
108 wxHtmlEntitiesParser *GetEntitiesParser() const { return m_entitiesParser; }
109
110 protected:
111 // source being parsed
112 wxString m_Source;
113
114 // tags cache, used during parsing.
115 wxHtmlTagsCache *m_Cache;
116 wxHashTable m_HandlersHash;
117
118 // handlers that handle particular tags. The table is accessed by
119 // key = tag's name.
120 // This attribute MUST be filled by derived class otherwise it would
121 // be empty and no tags would be recognized
122 // (see wxHtmlWinParser for details about filling it)
123 // m_HandlersHash is for random access based on knowledge of tag name (BR, P, etc.)
124 // it may (and often does) contain more references to one object
125 // m_HandlersList is list of all handlers and it is guaranteed to contain
126 // only one reference to each handler instance.
127 wxList m_HandlersList;
128
129 // class for opening files (file system)
130 wxFileSystem *m_FS;
131 // handlers stack used by PushTagHandler and PopTagHandler
132 wxList *m_HandlersStack;
133
134 // entity parse
135 wxHtmlEntitiesParser *m_entitiesParser;
136 };
137
138
139
140 // This class (and derived classes) cooperates with wxHtmlParser.
141 // Each recognized tag is passed to handler which is capable
142 // of handling it. Each tag is handled in 3 steps:
143 // 1. Handler will modifies state of parser
144 // (using it's public methods)
145 // 2. Parser parses source between starting and ending tag
146 // 3. Handler restores original state of the parser
147 class WXDLLEXPORT wxHtmlTagHandler : public wxObject
148 {
149 DECLARE_ABSTRACT_CLASS(wxHtmlTagHandler)
150
151 public:
152 wxHtmlTagHandler() : wxObject () { m_Parser = NULL; }
153
154 // Sets the parser.
155 // NOTE : each _instance_ of handler is guaranteed to be called
156 // only by one parser. This means you don't have to care about
157 // reentrancy.
158 virtual void SetParser(wxHtmlParser *parser)
159 { m_Parser = parser; }
160
161 // Returns list of supported tags. The list is in uppercase and
162 // tags are delimited by ','.
163 // Example : "I,B,FONT,P"
164 // is capable of handling italic, bold, font and paragraph tags
165 virtual wxString GetSupportedTags() = 0;
166
167 // This is hadling core method. It does all the Steps 1-3.
168 // To process step 2, you can call ParseInner()
169 // returned value : TRUE if it called ParseInner(),
170 // FALSE etherwise
171 virtual bool HandleTag(const wxHtmlTag& tag) = 0;
172
173 protected:
174 // parses input between beginning and ending tag.
175 // m_Parser must be set.
176 void ParseInner(const wxHtmlTag& tag)
177 { m_Parser->DoParsing(tag.GetBeginPos(), tag.GetEndPos1()); }
178
179 wxHtmlParser *m_Parser;
180 };
181
182
183 // This class is used to parse HTML entities in strings. It can handle
184 // both named entities and &#xxxx entries where xxxx is Unicode code.
185 class WXDLLEXPORT wxHtmlEntitiesParser : public wxObject
186 {
187 DECLARE_DYNAMIC_CLASS(wxHtmlEntitiesParser)
188
189 public:
190 wxHtmlEntitiesParser();
191 virtual ~wxHtmlEntitiesParser();
192
193 // Sets encoding of output string.
194 // Has no effect if wxUSE_WCHAR_T==0 or wxUSE_UNICODE==1
195 void SetEncoding(wxFontEncoding encoding);
196
197 // Parses entities in input and replaces them with respective characters
198 // (with respect to output encoding)
199 wxString Parse(const wxString& input);
200
201 protected:
202 wxChar GetEntityChar(const wxString& entity);
203 wxChar GetCharForCode(unsigned code);
204
205 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
206 wxMBConv *m_conv;
207 wxFontEncoding m_encoding;
208 #endif
209 };
210
211
212 #endif
213
214 #endif // _WX_HTMLPARS_H_