]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/html/htmlpars.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: html/htmlpars.h
3 // Purpose: interface of wxHtmlTagHandler
4 // Author: wxWidgets team
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
19 @class wxHtmlTagHandler
26 @see @ref overview_html_handlers, wxHtmlTag
28 class wxHtmlTagHandler
: public wxObject
37 Returns list of supported tags.
38 The list is in uppercase and tags are delimited by ','.
39 Example: @c "I,B,FONT,P"
41 virtual wxString
GetSupportedTags() = 0;
44 This is the core method of each handler. It is called each time
45 one of supported tags is detected. @a tag contains all necessary
46 info (see wxHtmlTag for details).
51 bool MyHandler::HandleTag(const wxHtmlTag& tag)
54 // change state of parser (e.g. set bold face)
57 // restore original state of parser
61 You shouldn't call ParseInner() if the tag is not paired with an ending one.
63 @return @true if ParseInner() was called, @false otherwise.
65 virtual bool HandleTag(const wxHtmlTag
& tag
) = 0;
68 Assigns @a parser to this handler. Each @b instance of handler
69 is guaranteed to be called only from the parser.
71 virtual void SetParser(wxHtmlParser
* parser
);
76 This method calls parser's wxHtmlParser::DoParsing method
77 for the string between this tag and the paired ending tag:
79 ...<A HREF="x.htm">Hello, world!</A>...
82 In this example, a call to ParseInner() (with @a tag pointing to A tag)
83 will parse 'Hello, world!'.
85 void ParseInner(const wxHtmlTag
& tag
);
88 Parses given source as if it was tag's inner code (see
89 wxHtmlParser::GetInnerSource). Unlike ParseInner(), this method lets
90 you specify the source code to parse. This is useful when you need to
91 modify the inner text before parsing.
93 void ParseInnerSource(const wxString
& source
);
96 This attribute is used to access parent parser. It is protected so that
97 it can't be accessed by user but can be accessed from derived classes.
99 wxHtmlParser
* m_Parser
;
107 Classes derived from this handle the @b generic parsing of HTML documents: it
108 scans the document and divide it into blocks of tags (where one block consists
109 of beginning and ending tag and of text between these two tags).
111 It is independent from wxHtmlWindow and can be used as stand-alone parser.
113 It uses system of tag handlers to parse the HTML document. Tag handlers
114 are not statically shared by all instances but are created for each
115 wxHtmlParser instance. The reason is that the handler may contain
116 document-specific temporary data used during parsing (e.g. complicated
117 structures like tables).
119 Typically the user calls only the wxHtmlParser::Parse method.
124 @see @ref overview_html_cells, @ref overview_html_handlers, wxHtmlTag
135 Adds handler to the internal list ( hash table) of handlers.
136 This method should not be called directly by user but rather by derived class'
139 This adds the handler to this @b instance of wxHtmlParser, not to
140 all objects of this class!
141 (Static front-end to AddTagHandler is provided by wxHtmlWinParser).
143 All handlers are deleted on object deletion.
145 virtual void AddTagHandler(wxHtmlTagHandler
* handler
);
148 Must be overwritten in derived class.
150 This method is called by DoParsing() each time a part of text is parsed.
151 @a txt is NOT only one word, it is substring of input.
152 It is not formatted or preprocessed (so white spaces are unmodified).
154 virtual void AddWord(const wxString
& txt
);
157 Parses the m_Source from @a begin_pos to @a end_pos - 1.
159 void DoParsing(const const_iterator
& begin_pos
, const const_iterator
& end_pos
);
162 Parses the whole m_Source.
167 This must be called after DoParsing().
169 virtual void DoneParser();
172 Returns pointer to the file system. Because each tag handler has
173 reference to it is parent parser it can easily request the file by
176 wxFSFile *f = m_Parser -> GetFS() -> OpenFile("image.jpg");
179 wxFileSystem
* GetFS() const;
182 Returns product of parsing.
183 Returned value is result of parsing of the document.
185 The type of this result depends on internal representation in derived
186 parser (but it must be derived from wxObject!).
187 See wxHtmlWinParser for details.
189 virtual wxObject
* GetProduct() = 0;
192 Returns pointer to the source being parsed.
194 const wxString
* GetSource();
197 Setups the parser for parsing the @a source string.
198 (Should be overridden in derived class)
200 virtual void InitParser(const wxString
& source
);
203 Opens given URL and returns @c wxFSFile object that can be used to read data
204 from it. This method may return @NULL in one of two cases: either the URL doesn't
205 point to any valid resource or the URL is blocked by overridden implementation
206 of @e OpenURL in derived class.
209 Indicates type of the resource. Is one of:
210 - wxHTML_URL_PAGE: Opening a HTML page.
211 - wxHTML_URL_IMAGE: Opening an image.
212 - wxHTML_URL_OTHER: Opening a resource that doesn't fall into
218 Always use this method in tag handlers instead of GetFS()->OpenFile()
219 because it can block the URL and is thus more secure.
220 Default behaviour is to call wxHtmlWindow::OnOpeningURL of the associated
221 wxHtmlWindow object (which may decide to block the URL or redirect it to
222 another one),if there's any, and always open the URL if the parser is not
223 used with wxHtmlWindow.
224 Returned wxFSFile object is not guaranteed to point to url, it might have
227 virtual wxFSFile
* OpenURL(wxHtmlURLType type
, const wxString
& url
) const;
230 Proceeds parsing of the document. This is end-user method. You can simply
231 call it when you need to obtain parsed output (which is parser-specific).
233 The method does these things:
234 -# calls InitParser(source)
236 -# calls GetProduct()
237 -# calls DoneParser()
238 -# returns value returned by GetProduct()
240 You shouldn't use InitParser(), DoParsing(), GetProduct() or DoneParser() directly.
242 wxObject
* Parse(const wxString
& source
);
245 Restores parser's state before last call to PushTagHandler().
247 void PopTagHandler();
250 Forces the handler to handle additional tags
251 (not returned by wxHtmlTagHandler::GetSupportedTags).
252 The handler should already be added to this parser.
257 List of tags (in same format as GetSupportedTags()'s return value).
258 The parser will redirect these tags to handler (until call to PopTagHandler()).
262 Imagine you want to parse following pseudo-html structure:
265 <param name="one" value="1">
266 <param name="two" value="2">
270 <param program="text.exe">
274 It is obvious that you cannot use only one tag handler for \<param\> tag.
275 Instead you must use context-sensitive handlers for \<param\> inside \<myitems\>
276 and \<param\> inside \<execute\>.
277 This is the preferred solution:
280 TAG_HANDLER_BEGIN(MYITEM, "MYITEMS")
281 TAG_HANDLER_PROC(tag)
285 m_Parser -> PushTagHandler(this, "PARAM");
287 m_Parser -> PopTagHandler();
291 TAG_HANDLER_END(MYITEM)
294 void PushTagHandler(wxHtmlTagHandler
* handler
,
295 const wxString
& tags
);
298 Sets the virtual file system that will be used to request additional files.
299 (For example @c IMG tag handler requests wxFSFile with the image data.)
301 void SetFS(wxFileSystem
* fs
);
304 Call this function to interrupt parsing from a tag handler.
305 No more tags will be parsed afterward. This function may only be called
306 from Parse() or any function called by it (i.e. from tag handlers).
308 virtual void StopParsing();
313 This may (and may not) be overwritten in derived class.
315 This method is called each time new tag is about to be added.
316 @a tag contains information about the tag. (See wxHtmlTag for details.)
318 Default (wxHtmlParser) behaviour is this: first it finds a handler capable
319 of handling this tag and then it calls handler's HandleTag() method.
321 virtual void AddTag(const wxHtmlTag
& tag
);