## wxHtmlParser

This class handles the generic parsing of HTML document: it scans the document and divide it into blocks of tags (where one block consists of begining and ending tag and of text between these two tags).

It is independent from wxHtmlWindow and can be used as stand-alone parser (Julian Smart's idea of speech-only HTML viewer or wget-like utility - see InetGet sample for example).

It uses system of tag handlers to parse the HTML document. Tag handlers are not staticaly shared by all instances but are created for each wxHtmlParser 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 Parse method.

Derived from

wxObject

Include files

<wx/html/htmlpars.h>

See also

Cells Overview, Tag Handlers Overview, wxHtmlTag

Members

wxHtmlParser::wxHtmlParser
wxHtmlParser::AddTag
wxHtmlParser::AddTagHandler
wxHtmlParser::AddText
wxHtmlParser::DoParsing
wxHtmlParser::DoneParser
wxHtmlParser::GetFS
wxHtmlParser::GetProduct
wxHtmlParser::GetSource
wxHtmlParser::InitParser
wxHtmlParser::Parse
wxHtmlParser::PushTagHandler
wxHtmlParser::PopTagHandler
wxHtmlParser::SetFS

### wxHtmlParser::wxHtmlParser

wxHtmlParser()

Constructor.

### wxHtmlParser::AddTag

void AddTag(const wxHtmlTag& tag)

This may (and may not) be overwriten in derived class.

This method is called each time new tag is about to be added. tag contains information about the tag. (See wxHtmlTag for details.)

Default (wxHtmlParser) behaviour is this: First it finds a handler capable of handling this tag and then it calls handler's HandleTag method.

### wxHtmlParser::AddTagHandler

virtual void AddTagHandler(wxHtmlTagHandler *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 wxHtmlParser, not to all objects of this class! (Static front-end to AddTagHandler is provided by wxHtmlWinParser).

All handlers are deleted on object deletion.

### wxHtmlParser::AddText

virtual void AddWord(const char* txt)

Must be overwriten in derived class.

This method is called by DoParsing each time a part of text is parsed. txt is NOT only one word, it is substring of input. It is not formatted or preprocessed (so white spaces are unmodified).

### wxHtmlParser::DoParsing

void DoParsing(int begin_pos, int end_pos)

void DoParsing()

Parses the m_Source from begin_pos to end_pos-1. (in noparams version it parses whole m_Source)

### wxHtmlParser::DoneParser

virtual void DoneParser()

This must be called after DoParsing().

### wxHtmlParser::GetFS

wxFileSystem* GetFS() const

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

    wxFSFile *f = m_Parser -> GetFS() -> OpenFile("image.jpg");

### wxHtmlParser::GetProduct

virtual wxObject* GetProduct()

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 wxObject!).

See wxHtmlWinParser for details.

### wxHtmlParser::GetSource

wxString* GetSource()

Returns pointer to the source being parsed.

### wxHtmlParser::InitParser

virtual void InitParser(const wxString& source)

Setups the parser for parsing the source string. (Should be overridden in derived class)

### wxHtmlParser::Parse

wxObject* Parse(const wxString& 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:

1. calls InitParser(source)
2. calls DoParsing
3. calls GetProduct
4. calls DoneParser
5. returns value returned by GetProduct

You shouldn't use InitParser, DoParsing, GetProduct or DoneParser directly.

### wxHtmlParser::PushTagHandler

void PushTagHandler(wxHtmlTagHandler* handler, wxString tags)

Forces the handler to handle additional tags (not returned by GetSupportedTags). The handler should already be added to this parser.

Parameters

handler

the handler

tags

List of tags (in same format as GetSupportedTags's return value). The parser will redirect these tags to handler (until call to PopTagHandler).

Example

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:

    TAG_HANDLER_BEGIN(MYITEM, "MYITEMS")
        TAG_HANDLER_PROC(tag)
        {
            // ...something...

            m_Parser -> PushTagHandler(this, "PARAM");
            ParseInner(tag);
            m_Parser -> PopTagHandler();

            // ...something...
         }
    TAG_HANDLER_END(MYITEM)

### wxHtmlParser::PopTagHandler

void PopTagHandler()

Restores parser's state before last call to PushTagHandler.

### wxHtmlParser::SetFS

void SetFS(wxFileSystem *fs)

Sets the virtual file system that will be used to request additional files. (For example `<IMG>` tag handler requests wxFSFile with the image data.)