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