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 class WXDLLEXPORT wxMBConv
;
25 class WXDLLEXPORT wxHtmlParser
;
26 class WXDLLEXPORT wxHtmlTagHandler
;
27 class WXDLLEXPORT wxHtmlEntitiesParser
;
29 class wxHtmlTextPieces
;
30 class wxHtmlParserState
;
32 // This class handles generic parsing of HTML document : it scans
33 // the document and divide it into blocks of tags (where one block
34 // consists of starting and ending tag and of text between these
36 class WXDLLEXPORT wxHtmlParser
: public wxObject
38 DECLARE_ABSTRACT_CLASS(wxHtmlParser
)
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
);
67 // Returns pointer to the tag at parser's current position
68 wxHtmlTag
*GetCurrentTag() const { return m_CurTag
; }
70 // Returns product of parsing
71 // Returned value is result of parsing of the part. The type of this result
72 // depends on internal representation in derived parser
73 // (see wxHtmlWinParser for details).
74 virtual wxObject
* GetProduct() = 0;
76 // adds handler to the list & hash table of handlers.
77 virtual void AddTagHandler(wxHtmlTagHandler
*handler
);
79 // Forces the handler to handle additional tags (not returned by GetSupportedTags).
80 // The handler should already be in use by this parser.
81 // Example: you want to parse following pseudo-html structure:
83 // <it name="one" value="1">
84 // <it name="two" value="2">
86 // <it> This last it has different meaning, we don't want it to be parsed by myitems handler!
87 // handler can handle only 'myitems' (e.g. it's GetSupportedTags returns "MYITEMS")
88 // you can call PushTagHandler(handler, "IT") when you find <myitems>
89 // and call PopTagHandler() when you find </myitems>
90 void PushTagHandler(wxHtmlTagHandler
*handler
, wxString tags
);
92 // Restores state before last call to PushTagHandler
95 wxString
* GetSource() {return &m_Source
;}
96 void SetSource(const wxString
& src
);
98 // Sets HTML source and remebers current parser's state so that it can
99 // later be restored. This is useful for on-line modifications of
100 // HTML source (for example, <pre> handler replaces spaces with
101 // and newlines with <br>)
102 virtual void SetSourceAndSaveState(const wxString
& src
);
103 // Restores parser's state from stack or returns FALSE if the stack is
105 virtual bool RestoreState();
109 void CreateDOMTree();
110 void DestroyDOMTree();
111 void CreateDOMSubTree(wxHtmlTag
*cur
,
112 int begin_pos
, int end_pos
,
113 wxHtmlTagsCache
*cache
);
115 // Adds text to the output.
116 // This is called from Parse() and must be overriden in derived classes.
117 // txt is not guaranteed to be only one word. It is largest continuous part of text
118 // (= not broken by tags)
119 // NOTE : using char* because of speed improvements
120 virtual void AddText(const wxChar
* txt
) = 0;
122 // Adds tag and proceeds it. Parse() may (and usually is) called from this method.
123 // This is called from Parse() and may be overriden.
124 // Default behavior is that it looks for proper handler in m_Handlers. The tag is
125 // ignored if no hander is found.
126 // Derived class is *responsible* for filling in m_Handlers table.
127 virtual void AddTag(const wxHtmlTag
& tag
);
129 // Returns entity parser object, used to substitute HTML &entities;
130 wxHtmlEntitiesParser
*GetEntitiesParser() const { return m_entitiesParser
; }
136 wxHtmlTextPieces
*m_TextPieces
;
137 size_t m_CurTextPiece
;
141 wxHtmlParserState
*m_SavedStates
;
143 // handlers that handle particular tags. The table is accessed by
145 // This attribute MUST be filled by derived class otherwise it would
146 // be empty and no tags would be recognized
147 // (see wxHtmlWinParser for details about filling it)
148 // m_HandlersHash is for random access based on knowledge of tag name (BR, P, etc.)
149 // it may (and often does) contain more references to one object
150 // m_HandlersList is list of all handlers and it is guaranteed to contain
151 // only one reference to each handler instance.
152 wxList m_HandlersList
;
153 wxHashTable m_HandlersHash
;
155 // class for opening files (file system)
157 // handlers stack used by PushTagHandler and PopTagHandler
158 wxList
*m_HandlersStack
;
161 wxHtmlEntitiesParser
*m_entitiesParser
;
166 // This class (and derived classes) cooperates with wxHtmlParser.
167 // Each recognized tag is passed to handler which is capable
168 // of handling it. Each tag is handled in 3 steps:
169 // 1. Handler will modifies state of parser
170 // (using it's public methods)
171 // 2. Parser parses source between starting and ending tag
172 // 3. Handler restores original state of the parser
173 class WXDLLEXPORT wxHtmlTagHandler
: public wxObject
175 DECLARE_ABSTRACT_CLASS(wxHtmlTagHandler
)
178 wxHtmlTagHandler() : wxObject () { m_Parser
= NULL
; }
181 // NOTE : each _instance_ of handler is guaranteed to be called
182 // only by one parser. This means you don't have to care about
184 virtual void SetParser(wxHtmlParser
*parser
)
185 { m_Parser
= parser
; }
187 // Returns list of supported tags. The list is in uppercase and
188 // tags are delimited by ','.
189 // Example : "I,B,FONT,P"
190 // is capable of handling italic, bold, font and paragraph tags
191 virtual wxString
GetSupportedTags() = 0;
193 // This is hadling core method. It does all the Steps 1-3.
194 // To process step 2, you can call ParseInner()
195 // returned value : TRUE if it called ParseInner(),
197 virtual bool HandleTag(const wxHtmlTag
& tag
) = 0;
200 // parses input between beginning and ending tag.
201 // m_Parser must be set.
202 void ParseInner(const wxHtmlTag
& tag
)
203 { m_Parser
->DoParsing(tag
.GetBeginPos(), tag
.GetEndPos1()); }
205 wxHtmlParser
*m_Parser
;
209 // This class is used to parse HTML entities in strings. It can handle
210 // both named entities and &#xxxx entries where xxxx is Unicode code.
211 class WXDLLEXPORT wxHtmlEntitiesParser
: public wxObject
213 DECLARE_DYNAMIC_CLASS(wxHtmlEntitiesParser
)
216 wxHtmlEntitiesParser();
217 virtual ~wxHtmlEntitiesParser();
219 // Sets encoding of output string.
220 // Has no effect if wxUSE_WCHAR_T==0 or wxUSE_UNICODE==1
221 void SetEncoding(wxFontEncoding encoding
);
223 // Parses entities in input and replaces them with respective characters
224 // (with respect to output encoding)
225 wxString
Parse(const wxString
& input
);
228 wxChar
GetEntityChar(const wxString
& entity
);
229 wxChar
GetCharForCode(unsigned code
);
231 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
233 wxFontEncoding m_encoding
;
240 #endif // _WX_HTMLPARS_H_