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_
15 #pragma interface "htmlpars.h"
21 #include "wx/html/htmltag.h"
22 #include "wx/filesys.h"
24 #include "wx/fontenc.h"
26 class WXDLLEXPORT wxMBConv
;
27 class WXDLLEXPORT wxHtmlParser
;
28 class WXDLLEXPORT wxHtmlTagHandler
;
29 class WXDLLEXPORT wxHtmlEntitiesParser
;
31 class wxHtmlTextPieces
;
32 class wxHtmlParserState
;
34 // This class handles generic parsing of HTML document : it scans
35 // the document and divide it into blocks of tags (where one block
36 // consists of starting and ending tag and of text between these
38 class WXDLLEXPORT wxHtmlParser
: public wxObject
40 DECLARE_ABSTRACT_CLASS(wxHtmlParser
)
44 virtual ~wxHtmlParser();
46 // Sets the class which will be used for opening files
47 void SetFS(wxFileSystem
*fs
) { m_FS
= fs
; }
49 wxFileSystem
* GetFS() const { return m_FS
; }
51 // You can simply call this method when you need parsed output.
52 // This method does these things:
53 // 1. call InitParser(source);
54 // 2. call DoParsing();
55 // 3. call GetProduct(); (it's return value is then returned)
56 // 4. call DoneParser();
57 wxObject
* Parse(const wxString
& source
);
59 // Sets the source. This must be called before running Parse() method.
60 virtual void InitParser(const wxString
& source
);
61 // This must be called after Parse().
62 virtual void DoneParser();
64 // Parses the m_Source from begin_pos to end_pos-1.
65 // (in noparams version it parses whole m_Source)
66 void DoParsing(int begin_pos
, int end_pos
);
69 // Returns pointer to the tag at parser's current position
70 wxHtmlTag
*GetCurrentTag() const { return m_CurTag
; }
72 // Returns product of parsing
73 // Returned value is result of parsing of the part. The type of this result
74 // depends on internal representation in derived parser
75 // (see wxHtmlWinParser for details).
76 virtual wxObject
* GetProduct() = 0;
78 // adds handler to the list & hash table of handlers.
79 virtual void AddTagHandler(wxHtmlTagHandler
*handler
);
81 // Forces the handler to handle additional tags (not returned by GetSupportedTags).
82 // The handler should already be in use by this parser.
83 // Example: you want to parse following pseudo-html structure:
85 // <it name="one" value="1">
86 // <it name="two" value="2">
88 // <it> This last it has different meaning, we don't want it to be parsed by myitems handler!
89 // handler can handle only 'myitems' (e.g. it's GetSupportedTags returns "MYITEMS")
90 // you can call PushTagHandler(handler, "IT") when you find <myitems>
91 // and call PopTagHandler() when you find </myitems>
92 void PushTagHandler(wxHtmlTagHandler
*handler
, wxString tags
);
94 // Restores state before last call to PushTagHandler
97 wxString
* GetSource() {return &m_Source
;}
98 void SetSource(const wxString
& src
);
100 // Sets HTML source and remebers current parser's state so that it can
101 // later be restored. This is useful for on-line modifications of
102 // HTML source (for example, <pre> handler replaces spaces with
103 // and newlines with <br>)
104 virtual void SetSourceAndSaveState(const wxString
& src
);
105 // Restores parser's state from stack or returns FALSE if the stack is
107 virtual bool RestoreState();
111 void CreateDOMTree();
112 void DestroyDOMTree();
113 void CreateDOMSubTree(wxHtmlTag
*cur
,
114 int begin_pos
, int end_pos
,
115 wxHtmlTagsCache
*cache
);
117 // Adds text to the output.
118 // This is called from Parse() and must be overriden in derived classes.
119 // txt is not guaranteed to be only one word. It is largest continuous part of text
120 // (= not broken by tags)
121 // NOTE : using char* because of speed improvements
122 virtual void AddText(const wxChar
* txt
) = 0;
124 // Adds tag and proceeds it. Parse() may (and usually is) called from this method.
125 // This is called from Parse() and may be overriden.
126 // Default behavior is that it looks for proper handler in m_Handlers. The tag is
127 // ignored if no hander is found.
128 // Derived class is *responsible* for filling in m_Handlers table.
129 virtual void AddTag(const wxHtmlTag
& tag
);
131 // Returns entity parser object, used to substitute HTML &entities;
132 wxHtmlEntitiesParser
*GetEntitiesParser() const { return m_entitiesParser
; }
138 wxHtmlTextPieces
*m_TextPieces
;
139 size_t m_CurTextPiece
;
143 wxHtmlParserState
*m_SavedStates
;
145 // handlers that handle particular tags. The table is accessed by
147 // This attribute MUST be filled by derived class otherwise it would
148 // be empty and no tags would be recognized
149 // (see wxHtmlWinParser for details about filling it)
150 // m_HandlersHash is for random access based on knowledge of tag name (BR, P, etc.)
151 // it may (and often does) contain more references to one object
152 // m_HandlersList is list of all handlers and it is guaranteed to contain
153 // only one reference to each handler instance.
154 wxList m_HandlersList
;
155 wxHashTable m_HandlersHash
;
157 // class for opening files (file system)
159 // handlers stack used by PushTagHandler and PopTagHandler
160 wxList
*m_HandlersStack
;
163 wxHtmlEntitiesParser
*m_entitiesParser
;
168 // This class (and derived classes) cooperates with wxHtmlParser.
169 // Each recognized tag is passed to handler which is capable
170 // of handling it. Each tag is handled in 3 steps:
171 // 1. Handler will modifies state of parser
172 // (using it's public methods)
173 // 2. Parser parses source between starting and ending tag
174 // 3. Handler restores original state of the parser
175 class WXDLLEXPORT wxHtmlTagHandler
: public wxObject
177 DECLARE_ABSTRACT_CLASS(wxHtmlTagHandler
)
180 wxHtmlTagHandler() : wxObject () { m_Parser
= NULL
; }
183 // NOTE : each _instance_ of handler is guaranteed to be called
184 // only by one parser. This means you don't have to care about
186 virtual void SetParser(wxHtmlParser
*parser
)
187 { m_Parser
= parser
; }
189 // Returns list of supported tags. The list is in uppercase and
190 // tags are delimited by ','.
191 // Example : "I,B,FONT,P"
192 // is capable of handling italic, bold, font and paragraph tags
193 virtual wxString
GetSupportedTags() = 0;
195 // This is hadling core method. It does all the Steps 1-3.
196 // To process step 2, you can call ParseInner()
197 // returned value : TRUE if it called ParseInner(),
199 virtual bool HandleTag(const wxHtmlTag
& tag
) = 0;
202 // parses input between beginning and ending tag.
203 // m_Parser must be set.
204 void ParseInner(const wxHtmlTag
& tag
)
205 { m_Parser
->DoParsing(tag
.GetBeginPos(), tag
.GetEndPos1()); }
207 wxHtmlParser
*m_Parser
;
211 // This class is used to parse HTML entities in strings. It can handle
212 // both named entities and &#xxxx entries where xxxx is Unicode code.
213 class WXDLLEXPORT wxHtmlEntitiesParser
: public wxObject
215 DECLARE_DYNAMIC_CLASS(wxHtmlEntitiesParser
)
218 wxHtmlEntitiesParser();
219 virtual ~wxHtmlEntitiesParser();
221 // Sets encoding of output string.
222 // Has no effect if wxUSE_WCHAR_T==0 or wxUSE_UNICODE==1
223 void SetEncoding(wxFontEncoding encoding
);
225 // Parses entities in input and replaces them with respective characters
226 // (with respect to output encoding)
227 wxString
Parse(const wxString
& input
);
229 // Returns character for given entity
230 wxChar
GetEntityChar(const wxString
& entity
);
232 // Returns character that represents given Unicode code
233 wxChar
GetCharForCode(unsigned code
);
236 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
238 wxFontEncoding m_encoding
;
245 #endif // _WX_HTMLPARS_H_