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