1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtmlParser class (generic parser)
4 // Author: Vaclav Slavik
5 // Copyright: (c) 1999 Vaclav Slavik
6 // Licence: wxWindows Licence
7 /////////////////////////////////////////////////////////////////////////////
10 #ifndef __HTMLPARSER_H__
11 #define __HTMLPARSER_H__
20 #include <wx/html/htmltag.h>
21 #include <wx/filesys.h>
24 class wxHtmlTagHandler
;
26 //--------------------------------------------------------------------------------
28 // This class handles generic parsing of HTML document : it scans
29 // the document and divide it into blocks of tags (where one block
30 // consists of starting and ending tag and of text between these
32 //--------------------------------------------------------------------------------
34 class WXDLLEXPORT wxHtmlParser
: public wxObject
36 DECLARE_ABSTRACT_CLASS(wxHtmlParser
)
40 // source being parsed
41 wxHtmlTagsCache
*m_Cache
;
42 // tags cache, used during parsing.
43 wxHashTable m_HandlersHash
;
44 wxList m_HandlersList
;
45 // handlers that handle particular tags. The table is accessed by
47 // This attribute MUST be filled by derived class otherwise it would
48 // be empty and no tags would be recognized
49 // (see wxHtmlWinParser for details about filling it)
50 // m_HandlersHash is for random access based on knowledge of tag name (BR, P, etc.)
51 // it may (and often does) contain more references to one object
52 // m_HandlersList is list of all handlers and it is guaranteed to contain
53 // only one reference to each handler instance.
55 // class for opening files (file system)
58 wxHtmlParser() : wxObject(), m_HandlersHash(wxKEY_STRING
) {m_FS
= NULL
; m_Cache
= NULL
;}
59 virtual ~wxHtmlParser();
61 void SetFS(wxFileSystem
*fs
) {m_FS
= fs
;}
62 // Sets the class which will be used for opening files
63 wxFileSystem
* GetFS() const {return m_FS
;}
65 wxObject
* Parse(const wxString
& source
);
66 // You can simply call this method when you need parsed output.
67 // This method does these things:
68 // 1. call InitParser(source);
69 // 2. call DoParsing();
70 // 3. call GetProduct(); (it's return value is then returned)
71 // 4. call DoneParser();
73 virtual void InitParser(const wxString
& source
);
74 // Sets the source. This must be called before running Parse() method.
75 virtual void DoneParser();
76 // This must be called after Parse().
78 void DoParsing(int begin_pos
, int end_pos
);
79 inline void DoParsing() {DoParsing(0, m_Source
.Length());};
80 // Parses the m_Source from begin_pos to end_pos-1.
81 // (in noparams version it parses whole m_Source)
83 virtual wxObject
* GetProduct() = 0;
84 // Returns product of parsing
85 // Returned value is result of parsing of the part. The type of this result
86 // depends on internal representation in derived parser
87 // (see wxHtmlWinParser for details).
89 virtual void AddTagHandler(wxHtmlTagHandler
*handler
);
90 // adds handler to the list & hash table of handlers.
92 wxString
* GetSource() {return &m_Source
;}
94 virtual wxList
* GetTempData() {return NULL
;}
95 // this method returns list of wxObjects that represents
96 // all data allocated by the parser. These can't be freeded
97 // by destructor because they must be valid as long as
98 // GetProduct's return value is valid - the caller must
99 // explicitly call delete MyParser -> GetTempData() to free
101 // (this method always sets the list to delete its contents)
105 virtual void AddText(const char* txt
) = 0;
106 // Adds text to the output.
107 // This is called from Parse() and must be overriden in derived classes.
108 // txt is not guaranteed to be only one word. It is largest continuous part of text
109 // (= not broken by tags)
110 // NOTE : using char* because of speed improvements
112 virtual void AddTag(const wxHtmlTag
& tag
);
113 // Adds tag and proceeds it. Parse() may (and usually is) called from this method.
114 // This is called from Parse() and may be overriden.
115 // Default behavior is that it looks for proper handler in m_Handlers. The tag is
116 // ignored if no hander is found.
117 // Derived class is *responsible* for filling in m_Handlers table.
125 //--------------------------------------------------------------------------------
127 // This class (and derived classes) cooperates with wxHtmlParser.
128 // Each recognized tag is passed to handler which is capable
129 // of handling it. Each tag is handled in 3 steps:
130 // 1. Handler will modifies state of parser
131 // (using it's public methods)
132 // 2. Parser parses source between starting and ending tag
133 // 3. Handler restores original state of the parser
134 //--------------------------------------------------------------------------------
136 class WXDLLEXPORT wxHtmlTagHandler
: public wxObject
138 DECLARE_ABSTRACT_CLASS(wxHtmlTagHandler
)
141 wxHtmlParser
*m_Parser
;
144 wxHtmlTagHandler() : wxObject () {m_Parser
= NULL
;};
146 virtual void SetParser(wxHtmlParser
*parser
) {m_Parser
= parser
;}
148 // NOTE : each _instance_ of handler is guaranteed to be called
149 // only by one parser. This means you don't have to care about
152 virtual wxString
GetSupportedTags() = 0;
153 // Returns list of supported tags. The list is in uppercase and
154 // tags are delimited by ','.
155 // Example : "I,B,FONT,P"
156 // is capable of handling italic, bold, font and paragraph tags
158 virtual bool HandleTag(const wxHtmlTag
& tag
) = 0;
159 // This is hadling core method. It does all the Steps 1-3.
160 // To process step 2, you can call ParseInner()
161 // returned value : TRUE if it called ParseInner(),
165 void ParseInner(const wxHtmlTag
& tag
) {m_Parser
-> DoParsing(tag
.GetBeginPos(), tag
.GetEndPos1());}
166 // parses input between beginning and ending tag.
167 // m_Parser must be set.
174 #endif // __HTMLPARSER_H__