1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wx28HtmlParser class (generic parser)
4 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #ifndef _WX_HTMLPARS_H_
11 #define _WX_HTMLPARS_H_
14 #include "wx/filesys.h"
16 #include "wx/fontenc.h"
22 class wx28HtmlTagHandler
;
23 class wx28HtmlEntitiesParser
;
25 class wx28HtmlTextPieces
;
26 class wx28HtmlParserState
;
36 // This class handles generic parsing of HTML document : it scans
37 // the document and divides it into blocks of tags (where one block
38 // consists of starting and ending tag and of text between these
40 class wx28HtmlParser
: public wxObject
42 DECLARE_ABSTRACT_CLASS(wx28HtmlParser
)
46 virtual ~wx28HtmlParser();
48 // Sets the class which will be used for opening files
49 void SetFS(wxFileSystem
*fs
) { m_FS
= fs
; }
51 wxFileSystem
* GetFS() const { return m_FS
; }
53 // Opens file if the parser is allowed to open given URL (may be forbidden
54 // for security reasons)
55 virtual wxFSFile
*OpenURL(wx28HtmlURLType type
, const wxString
& url
) const;
57 // You can simply call this method when you need parsed output.
58 // This method does these things:
59 // 1. call InitParser(source);
60 // 2. call DoParsing();
61 // 3. call GetProduct(); (its return value is then returned)
62 // 4. call DoneParser();
63 wxObject
* Parse(const wxString
& source
);
65 // Sets the source. This must be called before running Parse() method.
66 virtual void InitParser(const wxString
& source
);
67 // This must be called after Parse().
68 virtual void DoneParser();
70 // May be called during parsing to immediately return from Parse().
71 virtual void StopParsing() { m_stopParsing
= true; }
73 // Parses the m_Source from begin_pos to end_pos-1.
74 // (in noparams version it parses whole m_Source)
75 void DoParsing(int begin_pos
, int end_pos
);
78 // Returns pointer to the tag at parser's current position
79 wx28HtmlTag
*GetCurrentTag() const { return m_CurTag
; }
81 // Returns product of parsing
82 // Returned value is result of parsing of the part. The type of this result
83 // depends on internal representation in derived parser
84 // (see wx28HtmlWinParser for details).
85 virtual wxObject
* GetProduct() = 0;
87 // adds handler to the list & hash table of handlers.
88 virtual void AddTagHandler(wx28HtmlTagHandler
*handler
);
90 // Forces the handler to handle additional tags (not returned by GetSupportedTags).
91 // The handler should already be in use by this parser.
92 // Example: you want to parse following pseudo-html structure:
94 // <it name="one" value="1">
95 // <it name="two" value="2">
97 // <it> This last it has different meaning, we don't want it to be parsed by myitems handler!
98 // handler can handle only 'myitems' (e.g. its GetSupportedTags returns "MYITEMS")
99 // you can call PushTagHandler(handler, "IT") when you find <myitems>
100 // and call PopTagHandler() when you find </myitems>
101 void PushTagHandler(wx28HtmlTagHandler
*handler
, const wxString
& tags
);
103 // Restores state before last call to PushTagHandler
104 void PopTagHandler();
106 wxString
* GetSource() {return &m_Source
;}
107 void SetSource(const wxString
& src
);
109 // Sets HTML source and remembers current parser's state so that it can
110 // later be restored. This is useful for on-line modifications of
111 // HTML source (for example, <pre> handler replaces spaces with
112 // and newlines with <br>)
113 virtual void SetSourceAndSaveState(const wxString
& src
);
114 // Restores parser's state from stack or returns false if the stack is
116 virtual bool RestoreState();
118 // Returns HTML source inside the element (i.e. between the starting
120 wxString
GetInnerSource(const wx28HtmlTag
& tag
);
122 // Parses HTML string 'markup' and extracts charset info from <meta> tag
123 // if present. Returns empty string if the tag is missing.
124 // For wxHTML's internal use.
125 static wxString
ExtractCharsetInformation(const wxString
& markup
);
127 // Returns entity parser object, used to substitute HTML &entities;
128 wx28HtmlEntitiesParser
*GetEntitiesParser() const { return m_entitiesParser
; }
132 void CreateDOMTree();
133 void DestroyDOMTree();
134 void CreateDOMSubTree(wx28HtmlTag
*cur
,
135 int begin_pos
, int end_pos
,
136 wx28HtmlTagsCache
*cache
);
138 // Adds text to the output.
139 // This is called from Parse() and must be overriden in derived classes.
140 // txt is not guaranteed to be only one word. It is largest continuous part of text
141 // (= not broken by tags)
142 // NOTE : using char* because of speed improvements
143 virtual void AddText(const wxChar
* txt
) = 0;
145 // Adds tag and proceeds it. Parse() may (and usually is) called from this method.
146 // This is called from Parse() and may be overriden.
147 // Default behavior is that it looks for proper handler in m_Handlers. The tag is
148 // ignored if no hander is found.
149 // Derived class is *responsible* for filling in m_Handlers table.
150 virtual void AddTag(const wx28HtmlTag
& tag
);
154 wx28HtmlTag
*m_CurTag
;
156 wx28HtmlTextPieces
*m_TextPieces
;
157 size_t m_CurTextPiece
;
161 wx28HtmlParserState
*m_SavedStates
;
163 // handlers that handle particular tags. The table is accessed by
165 // This attribute MUST be filled by derived class otherwise it would
166 // be empty and no tags would be recognized
167 // (see wx28HtmlWinParser for details about filling it)
168 // m_HandlersHash is for random access based on knowledge of tag name (BR, P, etc.)
169 // it may (and often does) contain more references to one object
170 // m_HandlersList is list of all handlers and it is guaranteed to contain
171 // only one reference to each handler instance.
172 wxList m_HandlersList
;
173 wxHashTable m_HandlersHash
;
175 DECLARE_NO_COPY_CLASS(wx28HtmlParser
)
177 // class for opening files (file system)
179 // handlers stack used by PushTagHandler and PopTagHandler
180 wxList
*m_HandlersStack
;
183 wx28HtmlEntitiesParser
*m_entitiesParser
;
185 // flag indicating that the parser should stop
191 // This class (and derived classes) cooperates with wx28HtmlParser.
192 // Each recognized tag is passed to handler which is capable
193 // of handling it. Each tag is handled in 3 steps:
194 // 1. Handler will modifies state of parser
195 // (using its public methods)
196 // 2. Parser parses source between starting and ending tag
197 // 3. Handler restores original state of the parser
198 class wx28HtmlTagHandler
: public wxObject
200 DECLARE_ABSTRACT_CLASS(wx28HtmlTagHandler
)
203 wx28HtmlTagHandler() : wxObject () { m_Parser
= NULL
; }
206 // NOTE : each _instance_ of handler is guaranteed to be called
207 // only by one parser. This means you don't have to care about
209 virtual void SetParser(wx28HtmlParser
*parser
)
210 { m_Parser
= parser
; }
212 // Returns list of supported tags. The list is in uppercase and
213 // tags are delimited by ','.
214 // Example : "I,B,FONT,P"
215 // is capable of handling italic, bold, font and paragraph tags
216 virtual wxString
GetSupportedTags() = 0;
218 // This is hadling core method. It does all the Steps 1-3.
219 // To process step 2, you can call ParseInner()
220 // returned value : true if it called ParseInner(),
222 virtual bool HandleTag(const wx28HtmlTag
& tag
) = 0;
225 // parses input between beginning and ending tag.
226 // m_Parser must be set.
227 void ParseInner(const wx28HtmlTag
& tag
)
228 { m_Parser
->DoParsing(tag
.GetBeginPos(), tag
.GetEndPos1()); }
230 // Parses given source as if it was tag's inner code (see
231 // wx28HtmlParser::GetInnerSource). Unlike ParseInner(), this method lets
232 // you specify the source code to parse. This is useful when you need to
233 // modify the inner text before parsing.
234 void ParseInnerSource(const wxString
& source
);
236 wx28HtmlParser
*m_Parser
;
238 DECLARE_NO_COPY_CLASS(wx28HtmlTagHandler
)
242 // This class is used to parse HTML entities in strings. It can handle
243 // both named entities and &#xxxx entries where xxxx is Unicode code.
244 class wx28HtmlEntitiesParser
: public wxObject
246 DECLARE_DYNAMIC_CLASS(wx28HtmlEntitiesParser
)
249 wx28HtmlEntitiesParser();
250 virtual ~wx28HtmlEntitiesParser();
252 // Sets encoding of output string.
253 // Has no effect if wxUSE_WCHAR_T==0 or wxUSE_UNICODE==1
254 void SetEncoding(wxFontEncoding encoding
);
256 // Parses entities in input and replaces them with respective characters
257 // (with respect to output encoding)
258 wxString
Parse(const wxString
& input
);
260 // Returns character for given entity or 0 if the enity is unknown
261 wxChar
GetEntityChar(const wxString
& entity
);
263 // Returns character that represents given Unicode code
265 wxChar
GetCharForCode(unsigned code
) { return (wxChar
)code
; }
267 wxChar
GetCharForCode(unsigned code
);
271 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
273 wxFontEncoding m_encoding
;
276 DECLARE_NO_COPY_CLASS(wx28HtmlEntitiesParser
)
280 #endif // _WX_HTMLPARS_H_