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