]> git.saurik.com Git - wxWidgets.git/blame - include/wx/html/htmlpars.h
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / include / wx / html / htmlpars.h
CommitLineData
5526e819 1/////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: wx/html/htmlpars.h
5526e819
VS
3// Purpose: wxHtmlParser class (generic parser)
4// Author: Vaclav Slavik
5// Copyright: (c) 1999 Vaclav Slavik
65571936 6// Licence: wxWindows licence
5526e819
VS
7/////////////////////////////////////////////////////////////////////////////
8
69941f05
VS
9#ifndef _WX_HTMLPARS_H_
10#define _WX_HTMLPARS_H_
5526e819 11
5526e819
VS
12#include "wx/defs.h"
13#if wxUSE_HTML
14
69941f05
VS
15#include "wx/html/htmltag.h"
16#include "wx/filesys.h"
2826ef0c
VS
17#include "wx/hashmap.h"
18#include "wx/hashset.h"
19#include "wx/vector.h"
fc1f2125 20#include "wx/fontenc.h"
5526e819 21
b5dbe15d
VS
22class WXDLLIMPEXP_FWD_BASE wxMBConv;
23class WXDLLIMPEXP_FWD_HTML wxHtmlParser;
24class WXDLLIMPEXP_FWD_HTML wxHtmlTagHandler;
25class WXDLLIMPEXP_FWD_HTML wxHtmlEntitiesParser;
daa616fc 26
6c62a62b
VS
27class wxHtmlTextPieces;
28class wxHtmlParserState;
29
e4c8592e 30WX_DECLARE_HASH_SET_WITH_DECL_PTR(wxHtmlTagHandler*,
7a7fa93b 31 wxPointerHash, wxPointerEqual,
e4c8592e
VZ
32 wxHtmlTagHandlersSet,
33 class WXDLLIMPEXP_HTML);
2826ef0c
VS
34WX_DECLARE_STRING_HASH_MAP_WITH_DECL(wxHtmlTagHandler*,
35 wxHtmlTagHandlersHash,
36 class WXDLLIMPEXP_HTML);
37
6cc4e6b8
VS
38
39enum wxHtmlURLType
40{
41 wxHTML_URL_PAGE,
42 wxHTML_URL_IMAGE,
43 wxHTML_URL_OTHER
44};
45
daa616fc 46// This class handles generic parsing of HTML document : it scans
6a17b868 47// the document and divides it into blocks of tags (where one block
daa616fc
VS
48// consists of starting and ending tag and of text between these
49// 2 tags.
6acba9a7 50class WXDLLIMPEXP_HTML wxHtmlParser : public wxObject
5526e819
VS
51{
52 DECLARE_ABSTRACT_CLASS(wxHtmlParser)
53
1309ba6c 54public:
daa616fc 55 wxHtmlParser();
1309ba6c
VS
56 virtual ~wxHtmlParser();
57
58 // Sets the class which will be used for opening files
59 void SetFS(wxFileSystem *fs) { m_FS = fs; }
60
61 wxFileSystem* GetFS() const { return m_FS; }
62
6cc4e6b8 63 // Opens file if the parser is allowed to open given URL (may be forbidden
6953da00 64 // for security reasons)
6cc4e6b8 65 virtual wxFSFile *OpenURL(wxHtmlURLType type, const wxString& url) const;
04db5c3f 66
1309ba6c
VS
67 // You can simply call this method when you need parsed output.
68 // This method does these things:
69 // 1. call InitParser(source);
70 // 2. call DoParsing();
6a17b868 71 // 3. call GetProduct(); (its return value is then returned)
1309ba6c
VS
72 // 4. call DoneParser();
73 wxObject* Parse(const wxString& source);
74
75 // Sets the source. This must be called before running Parse() method.
76 virtual void InitParser(const wxString& source);
77 // This must be called after Parse().
78 virtual void DoneParser();
6953da00 79
2b5f62a0 80 // May be called during parsing to immediately return from Parse().
6953da00 81 virtual void StopParsing() { m_stopParsing = true; }
1309ba6c
VS
82
83 // Parses the m_Source from begin_pos to end_pos-1.
84 // (in noparams version it parses whole m_Source)
b1a3a964
VS
85 void DoParsing(const wxString::const_iterator& begin_pos,
86 const wxString::const_iterator& end_pos);
6c62a62b
VS
87 void DoParsing();
88
89 // Returns pointer to the tag at parser's current position
90 wxHtmlTag *GetCurrentTag() const { return m_CurTag; }
1309ba6c
VS
91
92 // Returns product of parsing
93 // Returned value is result of parsing of the part. The type of this result
94 // depends on internal representation in derived parser
95 // (see wxHtmlWinParser for details).
96 virtual wxObject* GetProduct() = 0;
97
98 // adds handler to the list & hash table of handlers.
99 virtual void AddTagHandler(wxHtmlTagHandler *handler);
100
6953da00 101 // Forces the handler to handle additional tags (not returned by GetSupportedTags).
1309ba6c
VS
102 // The handler should already be in use by this parser.
103 // Example: you want to parse following pseudo-html structure:
104 // <myitems>
105 // <it name="one" value="1">
106 // <it name="two" value="2">
107 // </myitems>
108 // <it> This last it has different meaning, we don't want it to be parsed by myitems handler!
6a17b868 109 // handler can handle only 'myitems' (e.g. its GetSupportedTags returns "MYITEMS")
1309ba6c
VS
110 // you can call PushTagHandler(handler, "IT") when you find <myitems>
111 // and call PopTagHandler() when you find </myitems>
fbfb8bcc 112 void PushTagHandler(wxHtmlTagHandler *handler, const wxString& tags);
1309ba6c
VS
113
114 // Restores state before last call to PushTagHandler
115 void PopTagHandler();
116
b1a3a964 117 const wxString* GetSource() {return m_Source;}
1309ba6c 118 void SetSource(const wxString& src);
6953da00 119
6a17b868 120 // Sets HTML source and remembers current parser's state so that it can
6953da00 121 // later be restored. This is useful for on-line modifications of
6c62a62b
VS
122 // HTML source (for example, <pre> handler replaces spaces with &nbsp;
123 // and newlines with <br>)
124 virtual void SetSourceAndSaveState(const wxString& src);
6953da00 125 // Restores parser's state from stack or returns false if the stack is
6c62a62b
VS
126 // empty
127 virtual bool RestoreState();
6953da00 128
e7feeafa
VS
129 // Returns HTML source inside the element (i.e. between the starting
130 // and ending tag)
131 wxString GetInnerSource(const wxHtmlTag& tag);
132
2b5f62a0
VZ
133 // Parses HTML string 'markup' and extracts charset info from <meta> tag
134 // if present. Returns empty string if the tag is missing.
135 // For wxHTML's internal use.
136 static wxString ExtractCharsetInformation(const wxString& markup);
6953da00 137
caea1cb7
VS
138 // Returns entity parser object, used to substitute HTML &entities;
139 wxHtmlEntitiesParser *GetEntitiesParser() const { return m_entitiesParser; }
1309ba6c 140
4609ee2e
VZ
141 // Returns true if the tag starting at the given position is a comment tag
142 //
143 // p should point to '<' character and is modified to point to the closing
144 // '>' of the end comment tag if this is indeed a comment
145 static bool
146 SkipCommentTag(wxString::const_iterator& p, wxString::const_iterator end);
147
1309ba6c 148protected:
6c62a62b
VS
149 // DOM structure
150 void CreateDOMTree();
151 void DestroyDOMTree();
152 void CreateDOMSubTree(wxHtmlTag *cur,
b1a3a964
VS
153 const wxString::const_iterator& begin_pos,
154 const wxString::const_iterator& end_pos,
6c62a62b
VS
155 wxHtmlTagsCache *cache);
156
1309ba6c 157 // Adds text to the output.
4c51a665 158 // This is called from Parse() and must be overridden in derived classes.
5bce3e6f
VS
159 // txt is not guaranteed to be only one word. It is largest continuous part
160 // of text (= not broken by tags)
161 virtual void AddText(const wxString& txt) = 0;
1309ba6c
VS
162
163 // Adds tag and proceeds it. Parse() may (and usually is) called from this method.
4c51a665
DS
164 // This is called from Parse() and may be overridden.
165 // Default behaviour is that it looks for proper handler in m_Handlers. The tag is
1309ba6c
VS
166 // ignored if no hander is found.
167 // Derived class is *responsible* for filling in m_Handlers table.
168 virtual void AddTag(const wxHtmlTag& tag);
169
170protected:
6c62a62b
VS
171 // DOM tree:
172 wxHtmlTag *m_CurTag;
173 wxHtmlTag *m_Tags;
174 wxHtmlTextPieces *m_TextPieces;
175 size_t m_CurTextPiece;
1309ba6c 176
b1a3a964 177 const wxString *m_Source;
6953da00 178
6c62a62b 179 wxHtmlParserState *m_SavedStates;
6953da00 180
1309ba6c
VS
181 // handlers that handle particular tags. The table is accessed by
182 // key = tag's name.
183 // This attribute MUST be filled by derived class otherwise it would
184 // be empty and no tags would be recognized
185 // (see wxHtmlWinParser for details about filling it)
186 // m_HandlersHash is for random access based on knowledge of tag name (BR, P, etc.)
187 // it may (and often does) contain more references to one object
188 // m_HandlersList is list of all handlers and it is guaranteed to contain
189 // only one reference to each handler instance.
2826ef0c
VS
190 wxHtmlTagHandlersSet m_HandlersSet;
191 wxHtmlTagHandlersHash m_HandlersHash;
1309ba6c 192
c0c133e1 193 wxDECLARE_NO_COPY_CLASS(wxHtmlParser);
22f3361e 194
1309ba6c
VS
195 // class for opening files (file system)
196 wxFileSystem *m_FS;
197 // handlers stack used by PushTagHandler and PopTagHandler
2826ef0c 198 wxVector<wxHtmlTagHandlersHash*> m_HandlersStack;
6953da00 199
daa616fc
VS
200 // entity parse
201 wxHtmlEntitiesParser *m_entitiesParser;
6953da00 202
2b5f62a0
VZ
203 // flag indicating that the parser should stop
204 bool m_stopParsing;
5526e819
VS
205};
206
207
208
daa616fc
VS
209// This class (and derived classes) cooperates with wxHtmlParser.
210// Each recognized tag is passed to handler which is capable
211// of handling it. Each tag is handled in 3 steps:
212// 1. Handler will modifies state of parser
6a17b868 213// (using its public methods)
daa616fc
VS
214// 2. Parser parses source between starting and ending tag
215// 3. Handler restores original state of the parser
6acba9a7 216class WXDLLIMPEXP_HTML wxHtmlTagHandler : public wxObject
5526e819
VS
217{
218 DECLARE_ABSTRACT_CLASS(wxHtmlTagHandler)
219
1309ba6c
VS
220public:
221 wxHtmlTagHandler() : wxObject () { m_Parser = NULL; }
222
223 // Sets the parser.
224 // NOTE : each _instance_ of handler is guaranteed to be called
225 // only by one parser. This means you don't have to care about
226 // reentrancy.
6953da00 227 virtual void SetParser(wxHtmlParser *parser)
1309ba6c
VS
228 { m_Parser = parser; }
229
3f7564f2
VZ
230 // Get the parser associated with this tag handler.
231 wxHtmlParser* GetParser() const { return m_Parser; }
232
1309ba6c
VS
233 // Returns list of supported tags. The list is in uppercase and
234 // tags are delimited by ','.
235 // Example : "I,B,FONT,P"
236 // is capable of handling italic, bold, font and paragraph tags
237 virtual wxString GetSupportedTags() = 0;
238
239 // This is hadling core method. It does all the Steps 1-3.
240 // To process step 2, you can call ParseInner()
6953da00
WS
241 // returned value : true if it called ParseInner(),
242 // false etherwise
1309ba6c
VS
243 virtual bool HandleTag(const wxHtmlTag& tag) = 0;
244
245protected:
246 // parses input between beginning and ending tag.
247 // m_Parser must be set.
6953da00 248 void ParseInner(const wxHtmlTag& tag)
b1a3a964 249 { m_Parser->DoParsing(tag.GetBeginIter(), tag.GetEndIter1()); }
1309ba6c 250
e7feeafa
VS
251 // Parses given source as if it was tag's inner code (see
252 // wxHtmlParser::GetInnerSource). Unlike ParseInner(), this method lets
253 // you specify the source code to parse. This is useful when you need to
254 // modify the inner text before parsing.
255 void ParseInnerSource(const wxString& source);
256
1309ba6c 257 wxHtmlParser *m_Parser;
22f3361e 258
c0c133e1 259 wxDECLARE_NO_COPY_CLASS(wxHtmlTagHandler);
5526e819
VS
260};
261
262
daa616fc
VS
263// This class is used to parse HTML entities in strings. It can handle
264// both named entities and &#xxxx entries where xxxx is Unicode code.
6acba9a7 265class WXDLLIMPEXP_HTML wxHtmlEntitiesParser : public wxObject
daa616fc
VS
266{
267 DECLARE_DYNAMIC_CLASS(wxHtmlEntitiesParser)
268
269public:
270 wxHtmlEntitiesParser();
271 virtual ~wxHtmlEntitiesParser();
6953da00 272
daa616fc 273 // Sets encoding of output string.
8d94819c
VS
274 // Has no effect if wxUSE_UNICODE==1
275#if wxUSE_UNICODE
276 void SetEncoding(wxFontEncoding WXUNUSED(encoding)) {}
277#else
daa616fc 278 void SetEncoding(wxFontEncoding encoding);
8d94819c 279#endif
6953da00 280
daa616fc
VS
281 // Parses entities in input and replaces them with respective characters
282 // (with respect to output encoding)
96d665d2 283 wxString Parse(const wxString& input) const;
61b50a43 284
f5e6ed7c 285 // Returns character for given entity or 0 if the enity is unknown
96d665d2 286 wxChar GetEntityChar(const wxString& entity) const;
61b50a43
VS
287
288 // Returns character that represents given Unicode code
2b5f62a0 289#if wxUSE_UNICODE
96d665d2 290 wxChar GetCharForCode(unsigned code) const { return (wxChar)code; }
2b5f62a0 291#else
96d665d2 292 wxChar GetCharForCode(unsigned code) const;
2b5f62a0 293#endif
daa616fc 294
61b50a43 295protected:
8d94819c 296#if !wxUSE_UNICODE
daa616fc
VS
297 wxMBConv *m_conv;
298 wxFontEncoding m_encoding;
299#endif
22f3361e 300
c0c133e1 301 wxDECLARE_NO_COPY_CLASS(wxHtmlEntitiesParser);
daa616fc 302};
5526e819
VS
303
304
5526e819 305#endif
69941f05
VS
306
307#endif // _WX_HTMLPARS_H_