1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtmlParser class (generic parser)
4 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_HTMLPARS_H_
12 #define _WX_HTMLPARS_H_
21 #include "wx/html/htmltag.h"
22 #include "wx/filesys.h"
25 class wxHtmlTagHandler
;
27 //--------------------------------------------------------------------------------
29 // This class handles generic parsing of HTML document : it scans
30 // the document and divide it into blocks of tags (where one block
31 // consists of starting and ending tag and of text between these
33 //--------------------------------------------------------------------------------
35 class WXDLLEXPORT wxHtmlParser
: public wxObject
37 DECLARE_ABSTRACT_CLASS(wxHtmlParser
)
40 wxHtmlParser() : wxObject(), m_HandlersHash(wxKEY_STRING
)
41 { m_FS
= NULL
; m_Cache
= NULL
; m_HandlersStack
= NULL
; }
42 virtual ~wxHtmlParser();
44 // Sets the class which will be used for opening files
45 void SetFS(wxFileSystem
*fs
) { m_FS
= fs
; }
47 wxFileSystem
* GetFS() const { return m_FS
; }
49 // You can simply call this method when you need parsed output.
50 // This method does these things:
51 // 1. call InitParser(source);
52 // 2. call DoParsing();
53 // 3. call GetProduct(); (it's return value is then returned)
54 // 4. call DoneParser();
55 wxObject
* Parse(const wxString
& source
);
57 // Sets the source. This must be called before running Parse() method.
58 virtual void InitParser(const wxString
& source
);
59 // This must be called after Parse().
60 virtual void DoneParser();
62 // Parses the m_Source from begin_pos to end_pos-1.
63 // (in noparams version it parses whole m_Source)
64 void DoParsing(int begin_pos
, int end_pos
);
65 inline void DoParsing() {DoParsing(0, m_Source
.Length());};
67 // Returns product of parsing
68 // Returned value is result of parsing of the part. The type of this result
69 // depends on internal representation in derived parser
70 // (see wxHtmlWinParser for details).
71 virtual wxObject
* GetProduct() = 0;
73 // adds handler to the list & hash table of handlers.
74 virtual void AddTagHandler(wxHtmlTagHandler
*handler
);
76 // Forces the handler to handle additional tags (not returned by GetSupportedTags).
77 // The handler should already be in use by this parser.
78 // Example: you want to parse following pseudo-html structure:
80 // <it name="one" value="1">
81 // <it name="two" value="2">
83 // <it> This last it has different meaning, we don't want it to be parsed by myitems handler!
84 // handler can handle only 'myitems' (e.g. it's GetSupportedTags returns "MYITEMS")
85 // you can call PushTagHandler(handler, "IT") when you find <myitems>
86 // and call PopTagHandler() when you find </myitems>
87 void PushTagHandler(wxHtmlTagHandler
*handler
, wxString tags
);
89 // Restores state before last call to PushTagHandler
92 wxString
* GetSource() {return &m_Source
;}
93 void SetSource(const wxString
& src
);
96 // Adds text to the output.
97 // This is called from Parse() and must be overriden in derived classes.
98 // txt is not guaranteed to be only one word. It is largest continuous part of text
99 // (= not broken by tags)
100 // NOTE : using char* because of speed improvements
101 virtual void AddText(const char* txt
) = 0;
103 // Adds tag and proceeds it. Parse() may (and usually is) called from this method.
104 // This is called from Parse() and may be overriden.
105 // Default behavior is that it looks for proper handler in m_Handlers. The tag is
106 // ignored if no hander is found.
107 // Derived class is *responsible* for filling in m_Handlers table.
108 virtual void AddTag(const wxHtmlTag
& tag
);
111 // source being parsed
114 // tags cache, used during parsing.
115 wxHtmlTagsCache
*m_Cache
;
116 wxHashTable m_HandlersHash
;
118 // handlers that handle particular tags. The table is accessed by
120 // This attribute MUST be filled by derived class otherwise it would
121 // be empty and no tags would be recognized
122 // (see wxHtmlWinParser for details about filling it)
123 // m_HandlersHash is for random access based on knowledge of tag name (BR, P, etc.)
124 // it may (and often does) contain more references to one object
125 // m_HandlersList is list of all handlers and it is guaranteed to contain
126 // only one reference to each handler instance.
127 wxList m_HandlersList
;
129 // class for opening files (file system)
131 // handlers stack used by PushTagHandler and PopTagHandler
132 wxList
*m_HandlersStack
;
140 //--------------------------------------------------------------------------------
142 // This class (and derived classes) cooperates with wxHtmlParser.
143 // Each recognized tag is passed to handler which is capable
144 // of handling it. Each tag is handled in 3 steps:
145 // 1. Handler will modifies state of parser
146 // (using it's public methods)
147 // 2. Parser parses source between starting and ending tag
148 // 3. Handler restores original state of the parser
149 //--------------------------------------------------------------------------------
151 class WXDLLEXPORT wxHtmlTagHandler
: public wxObject
153 DECLARE_ABSTRACT_CLASS(wxHtmlTagHandler
)
156 wxHtmlTagHandler() : wxObject () { m_Parser
= NULL
; }
159 // NOTE : each _instance_ of handler is guaranteed to be called
160 // only by one parser. This means you don't have to care about
162 virtual void SetParser(wxHtmlParser
*parser
)
163 { m_Parser
= parser
; }
165 // Returns list of supported tags. The list is in uppercase and
166 // tags are delimited by ','.
167 // Example : "I,B,FONT,P"
168 // is capable of handling italic, bold, font and paragraph tags
169 virtual wxString
GetSupportedTags() = 0;
171 // This is hadling core method. It does all the Steps 1-3.
172 // To process step 2, you can call ParseInner()
173 // returned value : TRUE if it called ParseInner(),
175 virtual bool HandleTag(const wxHtmlTag
& tag
) = 0;
178 // parses input between beginning and ending tag.
179 // m_Parser must be set.
180 void ParseInner(const wxHtmlTag
& tag
)
181 { m_Parser
->DoParsing(tag
.GetBeginPos(), tag
.GetEndPos1()); }
183 wxHtmlParser
*m_Parser
;
191 #endif // _WX_HTMLPARS_H_