Classes derived from this handle the generic parsing of HTML documents: it scans the document and divide it into blocks of tags (where one block consists of beginning and ending tag and of text between these two tags).
It is independent from wx.html.HtmlWindow and can be used as stand-alone parser.
It uses system of tag handlers to parse the HTML document. Tag handlers are not statically shared by all instances but are created for each wx.html.HtmlParser instance. The reason is that the handler may contain document-specific temporary data used during parsing (e.g. complicated structures like tables).
Typically the user calls only the wx.html.HtmlParser.Parse
method.
See also
Cells and Containers, Tag Handlers, wx.html.HtmlTag
Constructor. |
|
This may (and may not) be overwritten in derived class. |
|
Adds handler to the internal list ( hash table) of handlers. |
|
Parses the whole m_Source. |
|
This must be called after |
|
Returns pointer to the file system. |
|
Returns product of parsing. |
|
Returns pointer to the source being parsed. |
|
Setups the parser for parsing the source string. |
|
Opens given URL and returns wx.FSFile object that can be used to read data from it. |
|
Proceeds parsing of the document. |
|
Restores parser’s state before last call to |
|
Forces the handler to handle additional tags (not returned by |
|
Sets the virtual file system that will be used to request additional files. |
|
Call this function to interrupt parsing from a tag handler. |
See |
|
See |
wx.html.
HtmlParser
(object)¶Possible constructors:
HtmlParser()
Classes derived from this handle the generic parsing of HTML documents: it scans the document and divide it into blocks of tags (where one block consists of beginning and ending tag and of text between these two tags).
__init__
(self)¶Constructor.
AddTag
(self, tag)¶This may (and may not) be overwritten in derived class.
This method is called each time new tag is about to be added. tag contains information about the tag. (See wx.html.HtmlTag for details.)
Default ( wx.html.HtmlParser) behaviour is this: first it finds a handler capable of handling this tag and then it calls handler’s HandleTag() method.
tag (wx.html.HtmlTag) –
AddTagHandler
(self, handler)¶Adds handler to the internal list ( hash table) of handlers.
This method should not be called directly by user but rather by derived class’ constructor.
This adds the handler to this instance of wx.html.HtmlParser, not to all objects of this class! (Static front-end to AddTagHandler is provided by wx.html.HtmlWinParser).
All handlers are deleted on object deletion.
handler (wx.html.HtmlTagHandler) –
DoParsing
(self)¶Parses the whole m_Source.
GetFS
(self)¶Returns pointer to the file system.
Because each tag handler has reference to it is parent parser it can easily request the file by calling:
f = self.Parser.GetFS().OpenFile("image.jpg")
FileSystem
GetProduct
(self)¶Returns product of parsing.
Returned value is result of parsing of the document.
The type of this result depends on internal representation in derived parser (but it must be derived from Object!). See wx.html.HtmlWinParser for details.
Object
GetSource
(self)¶Returns pointer to the source being parsed.
string
InitParser
(self, source)¶Setups the parser for parsing the source string.
(Should be overridden in derived class)
source (string) –
OpenURL
(self, type, url)¶Opens given URL and returns wx.FSFile object that can be used to read data from it.
This method may return None
in one of two cases: either the URL doesn’t point to any valid resource or the URL is blocked by overridden implementation of OpenURL
in derived class.
type (HtmlURLType) – Indicates type of the resource. Is one of:
wx.html.HTML_URL_PAGE
: Opening a HTML page.
wx.html.HTML_URL_IMAGE
: Opening an image.
wx.html.HTML_URL_OTHER
: Opening a resource that doesn’t fall into any other category.
url (string) – URL being opened.
FSFile
Note
Always use this method in tag handlers instead of GetFS
.OpenFile() because it can block the URL and is thus more secure. Default behaviour is to call wx.html.HtmlWindow.OnOpeningURL
of the associated wx.html.HtmlWindow object (which may decide to block the URL or redirect it to another one),if there’s any, and always open the URL if the parser is not used with wx.html.HtmlWindow. Returned wx.FSFile object is not guaranteed to point to url, it might have been redirected!
Parse
(self, source)¶Proceeds parsing of the document.
This is end-user method. You can simply call it when you need to obtain parsed output (which is parser-specific).
The method does these things:
calls InitParser(source)
calls DoParsing
calls GetProduct
calls DoneParser
returns value returned by GetProduct
You shouldn’t use InitParser
, DoParsing
, GetProduct
or DoneParser
directly.
source (string) –
Object
PopTagHandler
(self)¶Restores parser’s state before last call to PushTagHandler
.
PushTagHandler
(self, handler, tags)¶Forces the handler to handle additional tags (not returned by wx.html.HtmlTagHandler.GetSupportedTags
).
The handler should already be added to this parser.
handler (wx.html.HtmlTagHandler) – the handler
tags (string) – List of tags (in same format as GetSupportedTags()’s return value). The parser will redirect these tags to handler (until call to PopTagHandler
).
Imagine you want to parse following pseudo-html structure:
<myitems>
<param name="one" value="1">
<param name="two" value="2">
</myitems>
<execute>
<param program="text.exe">
</execute>
It is obvious that you cannot use only one tag handler for <param> tag. Instead you must use context-sensitive handlers for <param> inside <myitems> and <param> inside <execute>. This is the preferred solution:
# some code in HandleTag for "MYITEMS"...
self.Parser.PushTagHandler(self, "PARAM")
self.ParseInner(tag)
self.Parser.PopTagHandler()
# back to working on "MYITEMS"...
SetFS
(self, fs)¶Sets the virtual file system that will be used to request additional files.
(For example IMG
tag handler requests wx.FSFile with the image data.)
fs (wx.FileSystem) –
StopParsing
(self)¶Call this function to interrupt parsing from a tag handler.
No more tags will be parsed afterward. This function may only be called from Parse
or any function called by it (i.e. from tag handlers).
Product
¶See GetProduct