1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wx28HtmlParser class (generic parser)
4 // Author: Vaclav Slavik
5 // Copyright: (c) 1999 Vaclav Slavik
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 #ifndef _WX_HTMLPARS_H_
10 #define _WX_HTMLPARS_H_
13 #include "wx/filesys.h"
15 #include "wx/fontenc.h"
21 class wx28HtmlTagHandler
;
22 class wx28HtmlEntitiesParser
;
24 class wx28HtmlTextPieces
;
25 class wx28HtmlParserState
;
35 // This class handles generic parsing of HTML document : it scans
36 // the document and divides it into blocks of tags (where one block
37 // consists of starting and ending tag and of text between these
39 class wx28HtmlParser
: public wxObject
41 DECLARE_ABSTRACT_CLASS(wx28HtmlParser
)
45 virtual ~wx28HtmlParser();
47 // Sets the class which will be used for opening files
48 void SetFS(wxFileSystem
*fs
) { m_FS
= fs
; }
50 wxFileSystem
* GetFS() const { return m_FS
; }
52 // Opens file if the parser is allowed to open given URL (may be forbidden
53 // for security reasons)
54 virtual wxFSFile
*OpenURL(wx28HtmlURLType type
, const wxString
& url
) const;
56 // You can simply call this method when you need parsed output.
57 // This method does these things:
58 // 1. call InitParser(source);
59 // 2. call DoParsing();
60 // 3. call GetProduct(); (its return value is then returned)
61 // 4. call DoneParser();
62 wxObject
* Parse(const wxString
& source
);
64 // Sets the source. This must be called before running Parse() method.
65 virtual void InitParser(const wxString
& source
);
66 // This must be called after Parse().
67 virtual void DoneParser();
69 // May be called during parsing to immediately return from Parse().
70 virtual void StopParsing() { m_stopParsing
= true; }
72 // Parses the m_Source from begin_pos to end_pos-1.
73 // (in noparams version it parses whole m_Source)
74 void DoParsing(int begin_pos
, int end_pos
);
77 // Returns pointer to the tag at parser's current position
78 wx28HtmlTag
*GetCurrentTag() const { return m_CurTag
; }
80 // Returns product of parsing
81 // Returned value is result of parsing of the part. The type of this result
82 // depends on internal representation in derived parser
83 // (see wx28HtmlWinParser for details).
84 virtual wxObject
* GetProduct() = 0;
86 // adds handler to the list & hash table of handlers.
87 virtual void AddTagHandler(wx28HtmlTagHandler
*handler
);
89 // Forces the handler to handle additional tags (not returned by GetSupportedTags).
90 // The handler should already be in use by this parser.
91 // Example: you want to parse following pseudo-html structure:
93 // <it name="one" value="1">
94 // <it name="two" value="2">
96 // <it> This last it has different meaning, we don't want it to be parsed by myitems handler!
97 // handler can handle only 'myitems' (e.g. its GetSupportedTags returns "MYITEMS")
98 // you can call PushTagHandler(handler, "IT") when you find <myitems>
99 // and call PopTagHandler() when you find </myitems>
100 void PushTagHandler(wx28HtmlTagHandler
*handler
, const wxString
& tags
);
102 // Restores state before last call to PushTagHandler
103 void PopTagHandler();
105 wxString
* GetSource() {return &m_Source
;}
106 void SetSource(const wxString
& src
);
108 // Sets HTML source and remembers current parser's state so that it can
109 // later be restored. This is useful for on-line modifications of
110 // HTML source (for example, <pre> handler replaces spaces with
111 // and newlines with <br>)
112 virtual void SetSourceAndSaveState(const wxString
& src
);
113 // Restores parser's state from stack or returns false if the stack is
115 virtual bool RestoreState();
117 // Returns HTML source inside the element (i.e. between the starting
119 wxString
GetInnerSource(const wx28HtmlTag
& tag
);
121 // Parses HTML string 'markup' and extracts charset info from <meta> tag
122 // if present. Returns empty string if the tag is missing.
123 // For wxHTML's internal use.
124 static wxString
ExtractCharsetInformation(const wxString
& markup
);
126 // Returns entity parser object, used to substitute HTML &entities;
127 wx28HtmlEntitiesParser
*GetEntitiesParser() const { return m_entitiesParser
; }
131 void CreateDOMTree();
132 void DestroyDOMTree();
133 void CreateDOMSubTree(wx28HtmlTag
*cur
,
134 int begin_pos
, int end_pos
,
135 wx28HtmlTagsCache
*cache
);
137 // Adds text to the output.
138 // This is called from Parse() and must be overridden in derived classes.
139 // txt is not guaranteed to be only one word. It is largest continuous part of text
140 // (= not broken by tags)
141 // NOTE : using char* because of speed improvements
142 virtual void AddText(const wxChar
* txt
) = 0;
144 // Adds tag and proceeds it. Parse() may (and usually is) called from this method.
145 // This is called from Parse() and may be overridden.
146 // Default behaviour is that it looks for proper handler in m_Handlers. The tag is
147 // ignored if no hander is found.
148 // Derived class is *responsible* for filling in m_Handlers table.
149 virtual void AddTag(const wx28HtmlTag
& tag
);
153 wx28HtmlTag
*m_CurTag
;
155 wx28HtmlTextPieces
*m_TextPieces
;
156 size_t m_CurTextPiece
;
160 wx28HtmlParserState
*m_SavedStates
;
162 // handlers that handle particular tags. The table is accessed by
164 // This attribute MUST be filled by derived class otherwise it would
165 // be empty and no tags would be recognized
166 // (see wx28HtmlWinParser for details about filling it)
167 // m_HandlersHash is for random access based on knowledge of tag name (BR, P, etc.)
168 // it may (and often does) contain more references to one object
169 // m_HandlersList is list of all handlers and it is guaranteed to contain
170 // only one reference to each handler instance.
171 wxList m_HandlersList
;
172 wxHashTable m_HandlersHash
;
174 DECLARE_NO_COPY_CLASS(wx28HtmlParser
)
176 // class for opening files (file system)
178 // handlers stack used by PushTagHandler and PopTagHandler
179 wxList
*m_HandlersStack
;
182 wx28HtmlEntitiesParser
*m_entitiesParser
;
184 // flag indicating that the parser should stop
190 // This class (and derived classes) cooperates with wx28HtmlParser.
191 // Each recognized tag is passed to handler which is capable
192 // of handling it. Each tag is handled in 3 steps:
193 // 1. Handler will modifies state of parser
194 // (using its public methods)
195 // 2. Parser parses source between starting and ending tag
196 // 3. Handler restores original state of the parser
197 class wx28HtmlTagHandler
: public wxObject
199 DECLARE_ABSTRACT_CLASS(wx28HtmlTagHandler
)
202 wx28HtmlTagHandler() : wxObject () { m_Parser
= NULL
; }
205 // NOTE : each _instance_ of handler is guaranteed to be called
206 // only by one parser. This means you don't have to care about
208 virtual void SetParser(wx28HtmlParser
*parser
)
209 { m_Parser
= parser
; }
211 // Returns list of supported tags. The list is in uppercase and
212 // tags are delimited by ','.
213 // Example : "I,B,FONT,P"
214 // is capable of handling italic, bold, font and paragraph tags
215 virtual wxString
GetSupportedTags() = 0;
217 // This is hadling core method. It does all the Steps 1-3.
218 // To process step 2, you can call ParseInner()
219 // returned value : true if it called ParseInner(),
221 virtual bool HandleTag(const wx28HtmlTag
& tag
) = 0;
224 // parses input between beginning and ending tag.
225 // m_Parser must be set.
226 void ParseInner(const wx28HtmlTag
& tag
)
227 { m_Parser
->DoParsing(tag
.GetBeginPos(), tag
.GetEndPos1()); }
229 // Parses given source as if it was tag's inner code (see
230 // wx28HtmlParser::GetInnerSource). Unlike ParseInner(), this method lets
231 // you specify the source code to parse. This is useful when you need to
232 // modify the inner text before parsing.
233 void ParseInnerSource(const wxString
& source
);
235 wx28HtmlParser
*m_Parser
;
237 DECLARE_NO_COPY_CLASS(wx28HtmlTagHandler
)
241 // This class is used to parse HTML entities in strings. It can handle
242 // both named entities and &#xxxx entries where xxxx is Unicode code.
243 class wx28HtmlEntitiesParser
: public wxObject
245 DECLARE_DYNAMIC_CLASS(wx28HtmlEntitiesParser
)
248 wx28HtmlEntitiesParser();
249 virtual ~wx28HtmlEntitiesParser();
251 // Sets encoding of output string.
252 // Has no effect if wxUSE_UNICODE==1
253 void SetEncoding(wxFontEncoding encoding
);
255 // Parses entities in input and replaces them with respective characters
256 // (with respect to output encoding)
257 wxString
Parse(const wxString
& input
);
259 // Returns character for given entity or 0 if the enity is unknown
260 wxChar
GetEntityChar(const wxString
& entity
);
262 // Returns character that represents given Unicode code
264 wxChar
GetCharForCode(unsigned code
) { return (wxChar
)code
; }
266 wxChar
GetCharForCode(unsigned code
);
272 wxFontEncoding m_encoding
;
275 DECLARE_NO_COPY_CLASS(wx28HtmlEntitiesParser
)
279 #endif // _WX_HTMLPARS_H_